xref: /freebsd/sys/contrib/openzfs/cmd/zpool/zpool.d/test_progress (revision dd41de95a84d979615a2ef11df6850622bf6184e)
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=$(basename "$0")
66
67if [ "$1" = "-h" ] ; then
68        echo "$helpstr" | grep "$script:" | tr -s '\t' | cut -f 2-
69        exit
70fi
71
72smartctl_path=$(command -v smartctl)
73
74# shellcheck disable=SC2015
75if [ -b "$VDEV_UPATH" ] && [ -x "$smartctl_path" ] || [ -n "$samples" ] ; then
76	if [ -n "$samples" ] ; then
77		# cat a smartctl output text file instead of running smartctl
78		# on a vdev (only used for developer testing).
79		file=$(get_filename_from_dir "$samples")
80		echo "file=$file"
81		raw_out=$(cat "$samples/$file")
82	else
83		raw_out=$(eval "sudo $smartctl_path -a $VDEV_UPATH")
84	fi
85
86	# What kind of drive are we?  Look for the right line in smartctl:
87	#
88	# SAS:
89	#	Transport protocol:   SAS
90	#
91	# SATA:
92	#	ATA Version is:   8
93	#
94	# NVMe:
95	#       SMART/Health Information (NVMe Log 0xnn, NSID 0xnn)
96	#
97	out=$(echo "$raw_out" | awk '
98# SAS specific
99/read:/{print "rrd="$4"\nr_cor="$5"\nr_proc="$7"\nr_ucor="$8}
100/write:/{print "rwr="$4"\nw_cor="$5"\nw_proc="$7"\nw_ucor="$8}
101/Non-medium error count/{print "nonmed="$4}
102/Elements in grown defect list/{print "defect="$6}
103
104# SAS common
105/SAS/{type="sas"}
106/Drive Temperature:/{print "temp="$4}
107# Status can be a long string, substitute spaces for '_'
108/SMART Health Status:/{printf "health="; for(i=4;i<=NF-1;i++){printf "%s_", $i}; printf "%s\n", $i}
109/number of hours powered up/{print "hours_on="$7; hours_on=int($7)}
110/Serial number:/{print "serial="$3}
111
112# SATA specific
113/Reallocated_Sector_Ct/{print "realloc="$10}
114/Reported_Uncorrect/{print "rep_ucor="$10}
115/Command_Timeout/{print "cmd_to="$10}
116/Current_Pending_Sector/{print "pend_sec="$10}
117/Offline_Uncorrectable/{print "off_ucor="$10}
118/ATA Error Count:/{print "ata_err="$4}
119/Power_Cycle_Count/{print "pwr_cyc="$10}
120
121# SATA common
122/SATA/{type="sata"}
123/Temperature_Celsius/{print "temp="$10}
124/Airflow_Temperature_Cel/{print "temp="$10}
125/Current Temperature:/{print "temp="$3}
126/SMART overall-health self-assessment test result:/{print "health="$6}
127/Power_On_Hours/{print "hours_on="$10; hours_on=int($10)}
128/Serial Number:/{print "serial="$3}
129
130# NVMe common
131/NVMe/{type="nvme"}
132/Temperature:/{print "temp="$2}
133/SMART overall-health self-assessment test result:/{print "health="$6}
134/Power On Hours:/{gsub("[^0-9]","",$4); print "hours_on="$4}
135/Serial Number:/{print "serial="$3}
136/Power Cycles:/{print "pwr_cyc="$3}
137
138# NVMe specific
139/Media and Data Integrity Errors:/{print "nvme_err="$6}
140
141# SMART self-test info
142/Self-test execution status:/{progress=tolower($4)} # SAS
143/SMART Self-test log/{test_seen=1} # SAS
144/SMART Extended Self-test Log/{test_seen=1} # SATA
145/# 1/{
146	test_type=tolower($3"_"$4);
147	# Status could be one word ("Completed") or multiple ("Completed: read
148	# failure").  Look for the ":" to see if we need to grab more words.
149
150	if ($5 ~ ":")
151		status=tolower($5""$6"_"$7)
152	else
153		status=tolower($5)
154	if (status=="self")
155		status="running";
156
157	if (type == "sas") {
158		hours=int($(NF-4))
159	} else {
160		hours=int($(NF-1))
161		# SATA reports percent remaining, rather than percent done
162		# Convert it to percent done.
163		progress=(100-int($(NF-2)))"%"
164	}
165	# When we int()-ify "hours", it converts stuff like "NOW" and "-" into
166	# 0.  In those cases, set it to hours_on, so they will cancel out in
167	# the "hours_ago" calculation later on.
168	if (hours == 0)
169		hours=hours_on
170
171	if (test_seen) {
172		print "test="hours_on
173		print "test_type="test_type
174		print "test_status="status
175		print "test_progress="progress
176	}
177	# Not all drives report hours_on
178	if (hours_on && hours) {
179		total_hours_ago=(hours_on-hours)
180		days_ago=int(total_hours_ago/24)
181		hours_ago=(total_hours_ago % 24)
182		if (days_ago != 0)
183			ago_str=days_ago"d"
184		if (hours_ago !=0)
185			ago_str=ago_str""hours_ago"h"
186		print "test_ended="ago_str
187	}
188}
189
190END {print "type="type; ORS="\n"; print ""}
191');
192fi
193type=$(echo "$out" | grep '^type=' | cut -d '=' -f 2)
194
195# If type is not set by now, either we don't have a block device
196# or smartctl failed. Either way, default to ATA and set $out to
197# nothing.
198if [ -z "$type" ]; then
199	type="sata"
200	out=
201fi
202
203case $script in
204smart)
205	# Print temperature plus common predictors of drive failure
206	if [ "$type" = "sas" ] ; then
207		scripts="temp|health|r_ucor|w_ucor"
208	elif [ "$type" = "sata" ] ; then
209		scripts="temp|health|ata_err|realloc|rep_ucor|cmd_to|pend_sec|off_ucor"
210	elif [ "$type" = "nvme" ] ; then
211		scripts="temp|health|nvme_err"
212	fi
213	;;
214smartx)
215	# Print some other interesting stats
216	if [ "$type" = "sas" ] ; then
217		scripts="hours_on|defect|nonmed|r_proc|w_proc"
218	elif [ "$type" = "sata" ] ; then
219		scripts="hours_on|pwr_cyc"
220	elif [ "$type" = "nvme" ] ; then
221		scripts="hours_on|pwr_cyc"
222	fi
223	;;
224smart_test)
225	scripts="test_type|test_status|test_progress|test_ended"
226	;;
227*)
228	scripts="$script"
229esac
230
231with_vals=$(echo "$out" | grep -E "$scripts")
232if [ -n "$with_vals" ]; then
233	echo "$with_vals"
234	without_vals=$(echo "$scripts" | tr "|" "\n" |
235		grep -v -E "$(echo "$with_vals" |
236		awk -F "=" '{print $1}')" | awk '{print $0"="}')
237else
238	without_vals=$(echo "$scripts" | tr "|" "\n" | awk '{print $0"="}')
239fi
240
241if [ -n "$without_vals" ]; then
242	echo "$without_vals"
243fi
244