#!/bin/sh # Managed by Ansible. Export SMART health to Node Exporter's textfile collector. set -eu METRICS_DIR="{{ monitoring_exporter_textfile_dir }}" METRICS_FILE="$METRICS_DIR/homelab_smartctl.prom" METRICS_TMP=$(mktemp "$METRICS_FILE.XXXXXX") trap 'rm -f "$METRICS_TMP"' EXIT printf '# HELP homelab_smart_device_healthy SMART overall health (1 healthy, 0 failed)\n' >> "$METRICS_TMP" printf '# TYPE homelab_smart_device_healthy gauge\n' >> "$METRICS_TMP" lsblk -dn -o NAME,TYPE | while read -r name type; do [ "$type" = "disk" ] || continue device="/dev/$name" json=$(smartctl -a -j "$device" 2>/dev/null || true) [ -n "$json" ] || continue healthy=$(printf '%s' "$json" | jq -r 'if .smart_status.passed == true then 1 else 0 end') temperature=$(printf '%s' "$json" | jq -r '.temperature.current // .nvme_smart_health_information_log.temperature // empty') nvme_warning=$(printf '%s' "$json" | jq -r '.nvme_smart_health_information_log.critical_warning // empty') printf 'homelab_smart_device_healthy{device="%s"} %s\n' "$name" "$healthy" >> "$METRICS_TMP" [ -z "$temperature" ] || printf 'homelab_smart_temperature_celsius{device="%s"} %s\n' "$name" "$temperature" >> "$METRICS_TMP" [ -z "$nvme_warning" ] || printf 'homelab_smart_nvme_critical_warning{device="%s"} %s\n' "$name" "$nvme_warning" >> "$METRICS_TMP" done chmod 0644 "$METRICS_TMP" mv "$METRICS_TMP" "$METRICS_FILE"