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