1#!/bin/sh 2# 3# Show SMART stats 4# 5 6helpstr=" 7smart: Show SMART temperature and error stats (specific to drive type) 8smartx: Show SMART extended drive stats (specific to drive type). 9temp: Show SMART drive temperature in celsius (all drives). 10health: Show reported SMART status (all drives). 11r_proc: Show SMART read GBytes processed over drive lifetime (SAS). 12w_proc: Show SMART write GBytes processed over drive lifetime (SAS). 13r_ucor: Show SMART read uncorrectable errors (SAS). 14w_ucor: Show SMART write uncorrectable errors (SAS). 15nonmed: Show SMART non-medium errors (SAS). 16defect: Show SMART grown defect list (SAS). 17hours_on: Show number of hours drive powered on (all drives). 18realloc: Show SMART reallocated sectors count (ATA). 19rep_ucor: Show SMART reported uncorrectable count (ATA). 20cmd_to: Show SMART command timeout count (ATA). 21pend_sec: Show SMART current pending sector count (ATA). 22off_ucor: Show SMART offline uncorrectable errors (ATA). 23ata_err: Show SMART ATA errors (ATA). 24pwr_cyc: Show SMART power cycle count (ATA). 25serial: Show disk serial number. 26nvme_err: Show SMART NVMe errors (NVMe). 27smart_test: Show SMART self-test results summary. 28test_type: Show SMART self-test type (short, long... ). 29test_status: Show SMART self-test status. 30test_progress: Show SMART self-test percentage done. 31test_ended: Show when the last SMART self-test ended (if supported). 32" 33 34# Hack for developer testing 35# 36# If you set $samples to a directory containing smartctl output text files, 37# we will use them instead of running smartctl on the vdevs. This can be 38# useful if you want to test a bunch of different smartctl outputs. Also, if 39# $samples is set, and additional 'file' column is added to the zpool output 40# showing the filename. 41samples= 42 43# get_filename_from_dir DIR 44# 45# Look in directory DIR and return a filename from it. The filename returned 46# is chosen quasi-sequentially (based off our PID). This allows us to return 47# a different filename every time this script is invoked (which we do for each 48# vdev), without having to maintain state. 49get_filename_from_dir() 50{ 51 dir=$1 52 pid="$$" 53 num_files=$(find "$dir" -maxdepth 1 -type f | wc -l) 54 mod=$((pid % num_files)) 55 i=0 56 find "$dir" -type f -printf '%f\n' | while read -r file ; do 57 if [ "$mod" = "$i" ] ; then 58 echo "$file" 59 break 60 fi 61 i=$((i+1)) 62 done 63} 64 65script="${0##*/}" 66 67if [ "$1" = "-h" ] ; then 68 echo "$helpstr" | grep "$script:" | tr -s '\t' | cut -f 2- 69 exit 70fi 71 72# shellcheck disable=SC2154 73if [ -b "$VDEV_UPATH" ] && PATH="/usr/sbin:$PATH" command -v smartctl > /dev/null || [ -n "$samples" ] ; then 74 if [ -n "$samples" ] ; then 75 # cat a smartctl output text file instead of running smartctl 76 # on a vdev (only used for developer testing). 77 file=$(get_filename_from_dir "$samples") 78 echo "file=$file" 79 raw_out=$(cat "$samples/$file") 80 else 81 raw_out=$(sudo smartctl -a "$VDEV_UPATH") 82 fi 83 84 # What kind of drive are we? Look for the right line in smartctl: 85 # 86 # SAS: 87 # Transport protocol: SAS 88 # 89 # SATA: 90 # ATA Version is: 8 91 # 92 # NVMe: 93 # SMART/Health Information (NVMe Log 0xnn, NSID 0xnn) 94 # 95 out=$(echo "$raw_out" | awk ' 96# SAS specific 97/read:/{print "rrd="$4"\nr_cor="$5"\nr_proc="$7"\nr_ucor="$8} 98/write:/{print "rwr="$4"\nw_cor="$5"\nw_proc="$7"\nw_ucor="$8} 99/Non-medium error count/{print "nonmed="$4} 100/Elements in grown defect list/{print "defect="$6} 101 102# SAS common 103/SAS/{type="sas"} 104/Drive Temperature:/{print "temp="$4} 105# Status can be a long string, substitute spaces for '_' 106/SMART Health Status:/{printf "health="; for(i=4;i<=NF-1;i++){printf "%s_", $i}; printf "%s\n", $i} 107/number of hours powered up/{print "hours_on="$7; hours_on=int($7)} 108/Serial number:/{print "serial="$3} 109 110# SATA specific 111/Reallocated_Sector_Ct/{print "realloc="$10} 112/Reported_Uncorrect/{print "rep_ucor="$10} 113/Command_Timeout/{print "cmd_to="$10} 114/Current_Pending_Sector/{print "pend_sec="$10} 115/Offline_Uncorrectable/{print "off_ucor="$10} 116/ATA Error Count:/{print "ata_err="$4} 117/Power_Cycle_Count/{print "pwr_cyc="$10} 118 119# SATA common 120/SATA/{type="sata"} 121/Temperature_Celsius/{print "temp="$10} 122/Airflow_Temperature_Cel/{print "temp="$10} 123/Current Temperature:/{print "temp="$3} 124/SMART overall-health self-assessment test result:/{print "health="$6} 125/Power_On_Hours/{print "hours_on="$10; hours_on=int($10)} 126/Serial Number:/{print "serial="$3} 127 128# NVMe common 129/NVMe/{type="nvme"} 130/Temperature:/{print "temp="$2} 131/SMART overall-health self-assessment test result:/{print "health="$6} 132/Power On Hours:/{gsub("[^0-9]","",$4); print "hours_on="$4} 133/Serial Number:/{print "serial="$3} 134/Power Cycles:/{print "pwr_cyc="$3} 135 136# NVMe specific 137/Media and Data Integrity Errors:/{print "nvme_err="$6} 138 139# SMART self-test info 140/Self-test execution status:/{progress=tolower($4)} # SAS 141/SMART Self-test log/{test_seen=1} # SAS 142/SMART Extended Self-test Log/{test_seen=1} # SATA 143/# 1/{ 144 test_type=tolower($3"_"$4); 145 # Status could be one word ("Completed") or multiple ("Completed: read 146 # failure"). Look for the ":" to see if we need to grab more words. 147 148 if ($5 ~ ":") 149 status=tolower($5""$6"_"$7) 150 else 151 status=tolower($5) 152 if (status=="self") 153 status="running"; 154 155 if (type == "sas") { 156 hours=int($(NF-4)) 157 } else { 158 hours=int($(NF-1)) 159 # SATA reports percent remaining, rather than percent done 160 # Convert it to percent done. 161 progress=(100-int($(NF-2)))"%" 162 } 163 # When we int()-ify "hours", it converts stuff like "NOW" and "-" into 164 # 0. In those cases, set it to hours_on, so they will cancel out in 165 # the "hours_ago" calculation later on. 166 if (hours == 0) 167 hours=hours_on 168 169 if (test_seen) { 170 print "test="hours_on 171 print "test_type="test_type 172 print "test_status="status 173 print "test_progress="progress 174 } 175 # Not all drives report hours_on 176 if (hours_on && hours) { 177 total_hours_ago=(hours_on-hours) 178 days_ago=int(total_hours_ago/24) 179 hours_ago=(total_hours_ago % 24) 180 if (days_ago != 0) 181 ago_str=days_ago"d" 182 if (hours_ago !=0) 183 ago_str=ago_str""hours_ago"h" 184 print "test_ended="ago_str 185 } 186} 187 188END {print "type="type; ORS="\n"; print ""} 189'); 190fi 191type=$(echo "$out" | grep '^type=' | cut -d '=' -f 2) 192 193# If type is not set by now, either we don't have a block device 194# or smartctl failed. Either way, default to ATA and set $out to 195# nothing. 196if [ -z "$type" ]; then 197 type="sata" 198 out= 199fi 200 201case $script in 202smart) 203 # Print temperature plus common predictors of drive failure 204 if [ "$type" = "sas" ] ; then 205 scripts="temp|health|r_ucor|w_ucor" 206 elif [ "$type" = "sata" ] ; then 207 scripts="temp|health|ata_err|realloc|rep_ucor|cmd_to|pend_sec|off_ucor" 208 elif [ "$type" = "nvme" ] ; then 209 scripts="temp|health|nvme_err" 210 fi 211 ;; 212smartx) 213 # Print some other interesting stats 214 if [ "$type" = "sas" ] ; then 215 scripts="hours_on|defect|nonmed|r_proc|w_proc" 216 elif [ "$type" = "sata" ] ; then 217 scripts="hours_on|pwr_cyc" 218 elif [ "$type" = "nvme" ] ; then 219 scripts="hours_on|pwr_cyc" 220 fi 221 ;; 222smart_test) 223 scripts="test_type|test_status|test_progress|test_ended" 224 ;; 225*) 226 scripts="$script" 227esac 228 229with_vals=$(echo "$out" | grep -E "$scripts") 230if [ -n "$with_vals" ]; then 231 echo "$with_vals" 232 without_vals=$(echo "$scripts" | tr '|' '\n' | 233 grep -v -E "$(echo "$with_vals" | 234 awk -F "=" '{print $1}')" | awk '{print $0"="}') 235else 236 without_vals=$(echo "$scripts" | tr '|' '\n' | awk '{print $0"="}') 237fi 238 239if [ -n "$without_vals" ]; then 240 echo "$without_vals" 241fi 242