X7ROOT File Manager
Current Path:
/opt/disk-mem-monitor
opt
/
disk-mem-monitor
/
📁
..
📄
disk-mem-health.sh
(7.26 KB)
Editing: disk-mem-health.sh
#!/bin/bash # ============================================================ # Disk (NVMe + SATA/SAS SMART) and Memory ECC health collector # Writes Prometheus textfile-collector metrics. # # Deploy per-server (Enhance + KVM nodes for now). # Requires: nvme-cli (NVMe), smartmontools (SATA/SAS), rasdaemon (ECC) # # Install missing tools first if needed: # dnf install -y nvme-cli smartmontools rasdaemon (RHEL/AlmaLinux) # apt install -y nvme-cli smartmontools rasdaemon (Debian/Ubuntu) # # Usage: ./disk-mem-health.sh # Output: /var/lib/node_exporter/textfile/disk_mem_health.prom # Cron: */5 * * * * /opt/disk-mem-monitor/disk-mem-health.sh # ============================================================ OUTDIR=/var/lib/node_exporter/textfile OUTFILE="$OUTDIR/disk_mem_health.prom" TMPFILE="${OUTFILE}.tmp" HOSTNAME=$(hostname) mkdir -p "$OUTDIR" > "$TMPFILE" # ---------- Header ---------- cat >> "$TMPFILE" <<EOF # HELP disk_nvme_critical_warning NVMe critical_warning field (0 = healthy, non-zero = firmware-flagged problem) # TYPE disk_nvme_critical_warning gauge # HELP disk_nvme_media_errors Cumulative NVMe media/data integrity errors (should stay 0) # TYPE disk_nvme_media_errors gauge # HELP disk_nvme_available_spare_percent NVMe available spare capacity percent # TYPE disk_nvme_available_spare_percent gauge # HELP disk_nvme_available_spare_threshold_percent NVMe vendor-set spare threshold percent # TYPE disk_nvme_available_spare_threshold_percent gauge # HELP disk_nvme_percentage_used NVMe endurance/wear-out percentage (100 = end of rated life) # TYPE disk_nvme_percentage_used gauge # HELP disk_nvme_temperature_celsius NVMe primary temperature sensor reading # TYPE disk_nvme_temperature_celsius gauge # HELP disk_nvme_unsafe_shutdowns Cumulative count of unsafe shutdowns detected by the drive # TYPE disk_nvme_unsafe_shutdowns gauge # HELP disk_sata_smart_healthy SATA/SAS SMART overall-health self-assessment (1 = PASSED, 0 = FAILED) # TYPE disk_sata_smart_healthy gauge # HELP disk_sata_reallocated_sectors SATA/SAS reallocated sector count (should stay 0) # TYPE disk_sata_reallocated_sectors gauge # HELP disk_sata_pending_sectors SATA/SAS current pending sector count (should stay 0) # TYPE disk_sata_pending_sectors gauge # HELP disk_sata_uncorrectable_sectors SATA/SAS offline uncorrectable sector count (should stay 0) # TYPE disk_sata_uncorrectable_sectors gauge # HELP memory_ecc_corrected_errors_total Cumulative corrected (CECC) memory errors reported by rasdaemon # TYPE memory_ecc_corrected_errors_total gauge # HELP memory_ecc_uncorrected_errors_total Cumulative uncorrected (UECC) memory errors reported by rasdaemon (should stay 0 - indicates failing DIMM) # TYPE memory_ecc_uncorrected_errors_total gauge # HELP disk_health_check_failed Whether this collector could not complete cleanly for a given check # TYPE disk_health_check_failed gauge EOF # ---------- NVMe drives ---------- if command -v nvme >/dev/null 2>&1; then for dev in $(ls /dev/nvme[0-9]n[0-9] 2>/dev/null); do devname=$(basename "$dev") LOG=$(nvme smart-log "$dev" 2>/dev/null) if [ -z "$LOG" ]; then echo "disk_health_check_failed{host=\"$HOSTNAME\",device=\"$devname\",check=\"nvme_smart_log\"} 1" >> "$TMPFILE" continue fi crit_warn=$(echo "$LOG" | awk -F: '/^critical_warning/ {gsub(/ /,"",$2); print $2}') media_err=$(echo "$LOG" | awk -F: '/^media_errors/ {gsub(/[ ,]/,"",$2); print $2}') spare=$(echo "$LOG" | awk -F: '/^available_spare[^_]/ {gsub(/[ %]/,"",$2); print $2}') spare_thresh=$(echo "$LOG" | awk -F: '/available_spare_threshold/ {gsub(/[ %]/,"",$2); print $2}') pct_used=$(echo "$LOG" | awk -F: '/^percentage_used/ {gsub(/[ %]/,"",$2); print $2}') temp=$(echo "$LOG" | awk -F: '/^temperature[ \t]*:/{print $2}' | head -1 | grep -oE '[0-9]+' | head -1) unsafe_shut=$(echo "$LOG" | awk -F: '/^unsafe_shutdowns/ {gsub(/[ ,]/,"",$2); print $2}') [ -n "$crit_warn" ] && echo "disk_nvme_critical_warning{host=\"$HOSTNAME\",device=\"$devname\"} $crit_warn" >> "$TMPFILE" [ -n "$media_err" ] && echo "disk_nvme_media_errors{host=\"$HOSTNAME\",device=\"$devname\"} $media_err" >> "$TMPFILE" [ -n "$spare" ] && echo "disk_nvme_available_spare_percent{host=\"$HOSTNAME\",device=\"$devname\"} $spare" >> "$TMPFILE" [ -n "$spare_thresh" ] && echo "disk_nvme_available_spare_threshold_percent{host=\"$HOSTNAME\",device=\"$devname\"} $spare_thresh" >> "$TMPFILE" [ -n "$pct_used" ] && echo "disk_nvme_percentage_used{host=\"$HOSTNAME\",device=\"$devname\"} $pct_used" >> "$TMPFILE" [ -n "$temp" ] && echo "disk_nvme_temperature_celsius{host=\"$HOSTNAME\",device=\"$devname\"} $temp" >> "$TMPFILE" [ -n "$unsafe_shut" ] && echo "disk_nvme_unsafe_shutdowns{host=\"$HOSTNAME\",device=\"$devname\"} $unsafe_shut" >> "$TMPFILE" done else echo "disk_health_check_failed{host=\"$HOSTNAME\",device=\"none\",check=\"nvme_cli_missing\"} 1" >> "$TMPFILE" fi # ---------- SATA/SAS drives ---------- if command -v smartctl >/dev/null 2>&1; then for dev in $(lsblk -ndo NAME,TYPE 2>/dev/null | awk '$2=="disk" && $1 !~ /^nvme/ {print $1}'); do devpath="/dev/$dev" HEALTH=$(smartctl -H "$devpath" 2>/dev/null) if [ -z "$HEALTH" ]; then echo "disk_health_check_failed{host=\"$HOSTNAME\",device=\"$dev\",check=\"smartctl\"} 1" >> "$TMPFILE" continue fi if echo "$HEALTH" | grep -qi "PASSED"; then echo "disk_sata_smart_healthy{host=\"$HOSTNAME\",device=\"$dev\"} 1" >> "$TMPFILE" elif echo "$HEALTH" | grep -qi "FAILED"; then echo "disk_sata_smart_healthy{host=\"$HOSTNAME\",device=\"$dev\"} 0" >> "$TMPFILE" fi ATTRS=$(smartctl -A "$devpath" 2>/dev/null) realloc=$(echo "$ATTRS" | awk '/Reallocated_Sector_Ct/ {print $NF}') pending=$(echo "$ATTRS" | awk '/Current_Pending_Sector/ {print $NF}') uncorrect=$(echo "$ATTRS" | awk '/Offline_Uncorrectable/ {print $NF}') [ -n "$realloc" ] && echo "disk_sata_reallocated_sectors{host=\"$HOSTNAME\",device=\"$dev\"} $realloc" >> "$TMPFILE" [ -n "$pending" ] && echo "disk_sata_pending_sectors{host=\"$HOSTNAME\",device=\"$dev\"} $pending" >> "$TMPFILE" [ -n "$uncorrect" ] && echo "disk_sata_uncorrectable_sectors{host=\"$HOSTNAME\",device=\"$dev\"} $uncorrect" >> "$TMPFILE" done fi # ---------- Memory ECC errors (rasdaemon) ---------- if command -v ras-mc-ctl >/dev/null 2>&1; then RAS_OUT=$(ras-mc-ctl --errors 2>/dev/null) if [ -z "$RAS_OUT" ]; then echo "disk_health_check_failed{host=\"$HOSTNAME\",device=\"memory\",check=\"ras_mc_ctl\"} 1" >> "$TMPFILE" else # Count corrected errors (lines under "Memory controller events:" before the MCE events section, # each numbered line is one corrected-error event) corrected=$(echo "$RAS_OUT" | awk '/^Memory controller events:/,/^$/' | grep -cE '^[0-9]+ [0-9]{4}-[0-9]{2}-[0-9]{2}') uncorrected=$(echo "$RAS_OUT" | grep -iE '^[0-9]+ [0-9]{4}-[0-9]{2}-[0-9]{2}' | grep -ci 'uncorrected\|fatal\|Memory failure') echo "memory_ecc_corrected_errors_total{host=\"$HOSTNAME\"} ${corrected:-0}" >> "$TMPFILE" echo "memory_ecc_uncorrected_errors_total{host=\"$HOSTNAME\"} ${uncorrected:-0}" >> "$TMPFILE" fi else echo "disk_health_check_failed{host=\"$HOSTNAME\",device=\"memory\",check=\"rasdaemon_missing\"} 1" >> "$TMPFILE" fi # ---------- Atomic write ---------- mv "$TMPFILE" "$OUTFILE"
Upload File
Create Folder