-
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathsyno_smart_info.sh
More file actions
1592 lines (1412 loc) · 56 KB
/
Copy pathsyno_smart_info.sh
File metadata and controls
1592 lines (1412 loc) · 56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env bash
# shellcheck disable=SC2317,SC2091,SC2076,SC2207
#------------------------------------------------------------------------------
# Show Synology smart test progress or smart health and attributes
#
# GitHub: https://github.com/007revad/Synology_SMART_info
# Script verified at https://www.shellcheck.net/
#
# To run in a shell (replace /volume1/scripts/ with path to script):
# sudo /volume1/scripts/syno_smart_info.sh
#------------------------------------------------------------------------------
# References:
# https://www.backblaze.com/blog/hard-drive-smart-stats/
# https://www.backblaze.com/blog/what-smart-stats-indicate-hard-drive-failures/
# https://www.backblaze.com/blog/making-sense-of-ssd-smart-stats/
#------------------------------------------------------------------------------
# References for converting Seagate raw values:
# https://codeberg.org/SWEETGOOD/shell-scripts#parse-raw-smart-values-seagate-sh
# https://codeberg.org/SWEETGOOD/shell-scripts/raw/branch/main/parse-raw-smart-values-seagate.sh
#
# online Seagate SMART value convertor
# https://www.disktuna.com/seagate-raw-smart-attributes-to-error-convertertest/#102465319
#
# https://github.com/Seagate/openSeaChest/wiki/Drive-Health-and-SMART
#
# S.M.A.R.T. attribute list (ATA ans SCSI)
# https://www.hdsentinel.com/smart/smartattr.php
#
# https://linux.die.net/man/8/smartctl
#------------------------------------------------------------------------------
# Attribute ID: 191
# Attribute Name: G-sense error rate
# Description: This value tracks errors resulting from external shock or vibration.
# Other names: G-Sense Error Rate, Shock Sense
scriptver="v1.4.38"
script=Synology_SMART_info
repo="007revad/Synology_SMART_info"
# Get NAS model
nas_model=$(cat /proc/sys/kernel/syno_hw_version)
# Get NAS hostname
host_name=$(hostname)
# Get DSM full version
productversion=$(/usr/syno/bin/synogetkeyvalue /etc.defaults/VERSION productversion)
buildphase=$(/usr/syno/bin/synogetkeyvalue /etc.defaults/VERSION buildphase)
buildnumber=$(/usr/syno/bin/synogetkeyvalue /etc.defaults/VERSION buildnumber)
smallfixnumber=$(/usr/syno/bin/synogetkeyvalue /etc.defaults/VERSION smallfixnumber)
# Get DSM major version
dsm=$(get_key_value /etc.defaults/VERSION majorversion)
# Get location of script
scriptpath="$(realpath "$0")"
logpath="${scriptpath%/*}"
# Get smartctl location and check if version 7
if which smartctl7 >/dev/null; then
# smartmontools 7 from SynoCli Disk Tools is installed
smartctl=$(which smartctl7)
smartversion=7
else
smartctl=$(which smartctl)
fi
ding(){
printf \\a
}
debug() {
[[ $debug == "yes" ]] && echo "DEBUG: $*"
}
usage(){
cat <<EOF
$script $scriptver - by 007revad
Usage: $(basename "$0") [options]
Options:
-a, --all Show all SMART attributes
-e, --email Disable colored text in output scheduler emails
-i, --increased Only show important attributes that have increased
-u, --update Update the script to the latest version
-h, --help Show this help message
-v, --version Show the script version
EOF
exit 1
}
scriptversion(){
cat <<EOF
$script $scriptver - by 007revad
See https://github.com/$repo
EOF
exit 0
}
# Save options used
args=("$@")
# Check for flags with getopt
if options="$(getopt -o abcdefghijklmnopqrstuvwxyz0123456789 -l \
all,email,increased,update,help,version,debug \
-- "$@")"; then
eval set -- "$options"
while true; do
case "${1,,}" in
-a|--all) # Show all SMART attributes
all=yes
;;
-e|--email) # Disable colour text in task scheduler emails
color=no
;;
-i|--increased) # Only display increased attributes
increased=yes
;;
-u|--update) # Update the script to the latest version
update=yes
;;
-h|--help) # Show usage options
usage
;;
-v|--version) # Show script version
scriptversion
;;
-d|--debug) # Show and log debug info
debug=yes
;;
--)
shift
break
;;
*) # Show usage options
echo -e "Invalid option '$1'\n"
usage "$1"
;;
esac
shift
done
else
echo
usage
fi
# Shell Colors
if [[ $color != "no" ]]; then
#Black='\e[0;30m' # ${Black}
#Red='\e[0;31m' # ${Red}
LiteRed='\e[1;31m' # ${LiteRed}
#Green='\e[0;32m' # ${Green}
LiteGreen='\e[1;32m' # ${LiteGreen}
Yellow='\e[0;33m' # ${Yellow}
#Blue='\e[0;34m' # ${Blue}
#Purple='\e[0;35m' # ${Purple}
Cyan='\e[0;36m' # ${Cyan}
#White='\e[0;37m' # ${White}
Error='\e[41m' # ${Error}
Off='\e[0m' # ${Off}
# For Synomartinfo package's white background
if [[ $scriptpath =~ "/@appstore/Synosmartinfo/bin/syno_smart_info.sh" ]]; then
#Yellow='\e[0;93m' # ${Yellow}
#Yellow='\e[1;33m' # ${Yellow}
Yellow='\e[0;34m' # ${Yellow} # Purple
YellowPy='\033[0;34m' # ${Yellow} # Purple
#Cyan='\e[0;96m' # ${Cyan}
Cyan='\e[1;36m' # ${Cyan}
fi
else
echo "" # For task scheduler email readability
fi
# Check script is running as root
if [[ $( whoami ) != "root" ]]; then
ding
echo -e "\n${Error}ERROR${Off} This script must be run as sudo or root!\n"
exit 1 # Not running as root
fi
# Show script version
#echo -e "$script $scriptver\ngithub.com/$repo\n"
echo "$script $scriptver - by 007revad"
# Show hostname, model and DSM full version
if [[ $buildphase == GM ]]; then buildphase=""; fi
if [[ $smallfixnumber -gt "0" ]]; then smallfix="-$smallfixnumber"; fi
echo -e "$host_name $nas_model DSM $productversion-$buildnumber$smallfix $buildphase"
echo "Using smartctl $("$smartctl" --version | head -1 | awk '{print $2}')"
# Show options used
if [[ ${#args[@]} -gt "0" ]]; then
echo "Using options: ${args[*]}"
fi
# Reset shell's SECONDS var to later show how long the script took
SECONDS=0
#------------------------------------------------------------------------------
# Check latest release with GitHub API
# Get latest release info
# Curl timeout options:
# https://unix.stackexchange.com/questions/94604/does-curl-have-a-timeout
#release=$(curl --silent -m 10 --connect-timeout 5 \
# "https://api.github.com/repos/$repo/releases/latest")
# Use wget to avoid installing curl in Ubuntu
release=$(wget -qO- -q --connect-timeout=5 \
"https://api.github.com/repos/$repo/releases/latest")
# Release version
tag=$(echo "$release" | grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/')
#shorttag="${tag:1}"
if ! printf "%s\n%s\n" "$tag" "$scriptver" |
sort --check=quiet --version-sort >/dev/null ; then
echo -e "\n${Cyan}There is a newer version of this script available.${Off}"
echo -e "Current version: ${scriptver}\nLatest version: $tag"
if [[ $update == "yes" ]]; then
if [[ -f "${logpath}/updateLocalScript.sh" ]]; then
if bash "${logpath}/updateLocalScript.sh" "${logpath}"; then
echo -e "syno_smart_info.sh updated"
exit
else
echo -e "Failed to update syno_smart_info.sh!"
exit 1
fi
else
echo -e "Missing file: ${logpath}/updateLocalScript.sh"
exit 1
fi
elif [[ -f "${logpath}/updateLocalScript.sh" ]]; then
# Skip if running from Synomartinfo package folder
if [[ ! $scriptpath =~ "/@appstore/Synosmartinfo/bin/syno_smart_info.sh" ]]; then
echo -e "Run ${Cyan}syno_smart_info.sh -u${Off} to update."
fi
fi
fi
#------------------------------------------------------------------------------
detect_dtype(){
# Default to SAT
local dtype="sat"
# If SAS appears at least once, treat as SCSI
if [ "$("$smartctl" -i /dev/"$drive" 2>/dev/null | grep -c SAS)" -gt 0 ]; then
dtype="scsi"
# Else if SATA appears at least once, treat as SAT
elif [ "$("$smartctl" -i /dev/"$drive" 2>/dev/null | grep -c SATA)" -gt 0 ]; then
dtype="sat"
fi
echo "$dtype"
}
get_drive_num(){
drive_num=""
disk_id=""
disk_cnr=""
#disk_cnridx=""
#eunit_num=""
#eunit_model=""
eunit=""
# Get Drive number
disk_id=$(synodisk --get_location_form "/dev/$drive" | grep 'Disk id:' | awk '{print $NF}')
disk_cnr=$(synodisk --get_location_form "/dev/$drive" | grep 'Disk cnr:' | awk '{print $NF}')
#disk_cnridx=$(synodisk --get_location_form "/dev/$drive" | grep 'Disk cnridx:' | awk '{print $NF}')
# Get eunit model and port number
# Only device tree models have syno_slot_mapping so we use different method
# /tmp/eunitinfo_2 example contents:
# EUnitModel=DX213-2
# EUnitDisks=/dev/sdja,/dev/sdjb
for f in /tmp/eunitinfo_*; do
if [[ -f "$f" ]]; then
if grep -q "/dev/$drive" "$f"; then
eunit="$(get_key_value "$f" EUnitModel)"
fi
fi
done
if [[ $disk_cnr -eq "4" ]]; then
drive_num="USB Drive "
elif [[ $eunit ]]; then
drive_num="Drive $disk_id ($eunit) "
elif synodisk --enum -t sys | grep -q "/dev/$drive"; then
# HD6500
drive_num="System Drive $disk_id "
else
drive_num="Drive $disk_id "
fi
}
get_nvme_num(){
# Get M.2 Drive number
drive_num=""
pcislot=""
cardslot=""
if nvme=$(synonvme --get-location "/dev/$drive"); then
if [[ ! $nvme =~ "PCI Slot: 0" ]]; then
pcislot="$(echo "$nvme" | cut -d"," -f2 | awk '{print $NF}')-"
fi
cardslot="$(echo "$nvme" | awk '{print $NF}')"
else
pcislot="$(basename -- "$drive")"
cardslot=""
fi
drive_num="M.2 Drive $pcislot$cardslot "
}
log_drive(){
# $1 is "$serial"
# $2 is "$drive_num"
# $3 is "$model"
# $4 is "/dev/$drive"
# set_section_key_value has bugs
# It cannot create a new section
# It cannot write keys if no key/value pair exist below section
# So we echo the section with a key/value pair
first_run=""
if ! grep '\['"$1"'\]' "$smart_log" >/dev/null; then
echo -e "[${serial}]\ndrive_num=" >> "$smart_log"
first_run="yes"
fi
set_section_key_value "$smart_log" "$1" drive_num "$2"
set_section_key_value "$smart_log" "$1" model "$3"
set_section_key_value "$smart_log" "$1" device "$4"
}
show_drive_model(){
# Get drive model
# $drive is sata1 or sda or usb1 etc
#vendor=$(cat "/sys/block/$drive/device/vendor")
#vendor=$(printf "%s" "$vendor" | xargs) # trim leading and trailing white space
model=$(cat "/sys/block/$drive/device/model")
model=$(printf "%s" "$model" | xargs) # trim leading and trailing white space
# Get drive serial number
if echo "$drive" | grep nvme >/dev/null ; then
serial=$(cat "/sys/block/$drive/device/serial")
else
serial=$(cat "/sys/block/$drive/device/syno_disk_serial")
fi
serial=$(printf "%s" "$serial" | xargs) # trim leading and trailing white space
# Get drive serial number with smartctl for USB drives
# if [[ -z "$serial" && "${drive:0:4}" != "nvme" ]]; then
if [[ -z "$serial" ]]; then
serial=$("$smartctl" -i -d "$drive_type" /dev/"$drive" | grep Serial | cut -d":" -f2 | xargs)
fi
# Show drive model and serial
if [[ $increased != "yes" ]]; then
#echo -e "\n${Cyan}${drive_num}${Off}$model ${Yellow}$serial${Off}"
echo -e "\n${Cyan}${drive_num}${Off}$model $serial /dev/$drive"
#echo -e "\n${Cyan}${drive_num}${Off}$vendor $model $serial"
else
show_drive_info="\n${Cyan}${drive_num}${Off}$model $serial /dev/$drive"
fi
# Log drive num, vendor and model to smart.log
log_drive "$serial" "$drive_num" "$model" "/dev/$drive"
}
# Python-based SMART attribute formatting function using EOF method
print_colored_smart_attribute(){
local line="$1"
[[ -z "$line" ]] && return
# Pass color disable option to Python
local color_opt="1"
if [[ $color == "no" ]]; then
color_opt="0"
fi
# Execute Python script using HERE document
echo "$line" | python3 -c "
import sys
import re
def format_smart_line():
line = sys.stdin.read().strip()
color_enabled = bool(int('$color_opt'))
if not line:
return
script_path = '$scriptpath'
if re.search(r'/@appstore/Synosmartinfo/bin/syno_smart_info\.sh', script_path):
# For Synomartinfo package's white background
YELLOW = '$YellowPy'
else:
YELLOW = '\033[0;33m'
OFF = '\033[0m'
COLOR_IDS = {5, 10, 187, 188, 196, 197, 198}
fields = line.split()
if len(fields) < 6:
print(line)
return
try:
id_field = fields[0]
id_num = int(id_field)
except ValueError:
print(line)
return
flags_pattern = re.compile(r'^[POSRCK-]{6}$')
flags_idx = -1
for i, field in enumerate(fields[1:], 1):
if flags_pattern.match(field):
flags_idx = i
break
if flags_idx == -1 or len(fields) < flags_idx + 5:
print(line)
return
attr_name = ' '.join(fields[1:flags_idx])
flags = fields[flags_idx]
value = fields[flags_idx + 1]
worst = fields[flags_idx + 2]
thresh = fields[flags_idx + 3]
fail = fields[flags_idx + 4]
raw_value = ' '.join(fields[flags_idx + 5:]) if len(fields) > flags_idx + 5 else ''
formatted_line = f'{id_field:<4} {attr_name:<32} {flags:<8} {value:>6} {worst:>6} {thresh:>7} {fail:>6} {raw_value}'
if color_enabled and id_num in COLOR_IDS:
print(f'{YELLOW}{formatted_line}{OFF}')
else:
print(formatted_line)
format_smart_line()
"
}
# SMART header output function
print_smart_header(){
printf "%-4s %-32s %-8s %6s %6s %7s %6s %s\n" \
"ID#" "ATTRIBUTE_NAME" "FLAGS" "VALUE" "WORST" "THRESH" "FAIL" "RAW_VALUE"
}
# SCSI SMART attribute formatting function (uses SCSI-only parsing)
format_scsi_smart(){
local drive="$1"
local output
debug "format_scsi_smart called for drive: $drive"
# Retrieve SCSI SMART output via wrapper; strip the leading header block
output=$("$smartctl" -a "/dev/$drive" | tail -n +19)
debug "SCSI output (first 20 lines):"
debug "$(echo "$output" | head -20)"
# Arrays to hold parsed items
declare -a scsi_ids=()
declare -a scsi_names=()
declare -a scsi_values=()
# Parse 5 patterns and map to standard IDs
while IFS= read -r line; do
debug "Processing line: '$line'"
if [[ "$line" == *"Current Drive Temperature:"* ]]; then
# Extract temperature number only
temp_value=$(echo "$line" | grep -o '[0-9]\+' | head -1)
debug "Found temperature: $temp_value"
scsi_ids+=(194); scsi_names+=("Current Drive Temperature"); scsi_values+=("$temp_value")
elif [[ "$line" == *"Accumulated power on time, hours:minutes"* ]]; then
time_value=$(echo "$line" | awk -F'Accumulated power on time, hours:minutes ' '{print $2}' | xargs)
debug "Found power on time: '$time_value'"
scsi_ids+=(9); scsi_names+=("Accumulated power on time"); scsi_values+=("$time_value")
elif [[ "$line" == *"Accumulated start-stop cycles:"* ]]; then
cycle_value=$(echo "$line" | awk '{print $NF}')
debug "Found start-stop cycles: $cycle_value"
scsi_ids+=(4); scsi_names+=("Accumulated start-stop cycles"); scsi_values+=("$cycle_value")
elif [[ "$line" == *"Accumulated load-unload cycles:"* ]]; then
load_value=$(echo "$line" | awk '{print $NF}')
debug "Found load-unload cycles: $load_value"
scsi_ids+=(193); scsi_names+=("Accumulated load-unload cycles"); scsi_values+=("$load_value")
elif [[ "$line" == *"Elements in grown defect list:"* ]]; then
defect_value=$(echo "$line" | awk '{print $NF}')
debug "Found defect list elements: $defect_value"
scsi_ids+=(5); scsi_names+=("Elements in grown defect list"); scsi_values+=("$defect_value")
fi
done <<< "$output"
debug "Found ${#scsi_ids[@]} attributes"
for ((i=0; i<${#scsi_ids[@]}; i++)); do
debug "ID=${scsi_ids[i]}, Name='${scsi_names[i]}', Value='${scsi_values[i]}'"
done
if [[ ${#scsi_ids[@]} -eq 0 ]]; then
debug "No SCSI attributes found, showing raw output"
echo "No SCSI attributes found in expected format"
echo "$output"
return
fi
# Sort by ID (bubble sort for simplicity)
local n=${#scsi_ids[@]}
for ((i=0; i<n-1; i++)); do
for ((j=0; j<n-i-1; j++)); do
if [[ ${scsi_ids[j]} -gt ${scsi_ids[j+1]} ]]; then
# swap id
tmp=${scsi_ids[j]}; scsi_ids[j]=${scsi_ids[j+1]}; scsi_ids[j+1]=$tmp
# swap name
tmp=${scsi_names[j]}; scsi_names[j]=${scsi_names[j+1]}; scsi_names[j+1]=$tmp
# swap value
tmp=${scsi_values[j]}; scsi_values[j]=${scsi_values[j+1]}; scsi_values[j+1]=$tmp
fi
done
done
# Output (SCSI-only summary header and rows)
printf "%-4s %-40s %s\n" "ID#" "ATTRIBUTE_NAME" "RAW_VALUE"
for ((i=0; i<${#scsi_ids[@]}; i++)); do
local id=${scsi_ids[i]} name=${scsi_names[i]} val=${scsi_values[i]}
if [[ $color != "no" && ( $id -eq 5 ) ]]; then
printf "${Yellow}%-4s %-40s %s${Off}\n" "$id" "$name" "$val"
else
printf "%-4s %-40s %s\n" "$id" "$name" "$val"
fi
done
}
smart_all(){
echo ""
# Decide device type (sat/scsi) via detect_dtype()
local drive_type
drive_type=$(detect_dtype)
if [[ "$drive_type" == "scsi" ]]; then
# SCSI path: do not print SAT header or use SAT python formatter.
# Let the dedicated SCSI formatter read and parse directly.
format_scsi_smart "$drive"
return
fi
# SAT / non-SCSI path
print_smart_header
if [[ $seagate == "yes" ]] && [[ $smartversion -gt "6" ]]; then
# Get all attributes, skip built-in header (first 6 lines), then drop “ID#” header
readarray -t att_array < <(
"$smartctl" -A -f brief -d "$drive_type" -T permissive \
-v 1,raw48:54 -v 7,raw48:54 -v 188,raw48:54 -v 195,raw48:54 \
-v 240,msec24hour32 "/dev/$drive" \
| tail -n +7 \
| grep -v '^ID#'
)
elif [[ $seagate == "yes" ]] && [[ $smartversion -lt "7" ]]; then
# Get all attributes, skip built-in header (first 6 lines), then drop “ID#” header
readarray -t att_array < <(
"$smartctl" -A -f brief -d "$drive_type" -T permissive \
-v 1,raw48:54,Raw_Read_Error_Rate -v 7,raw48:54,Seek_Error_Rate \
-v 188,raw48:54,Command_Timeout_Count \
-v 195,raw48:54,Hardware_ECC_Recovered \
-v 240,msec24hour32,Head_Flying_Hours "/dev/$drive" \
| tail -n +7 \
| grep -v '^ID#'
)
else
# Same for non-Seagate drives
readarray -t att_array < <(
"$smartctl" -A -f brief -d "$drive_type" -T permissive "/dev/$drive" \
| tail -n +7 \
| grep -v '^ID#'
)
fi
for strIn in "${att_array[@]}"; do
# Remove lines containing ||||||_ to |______
if ! echo "$strIn" | grep '|_' >/dev/null ; then
# Use Python-based formatting instead of original string cutting
print_colored_smart_attribute "$strIn"
fi
done
}
log_bad(){
# $1 is "197 Current_Pending_Sector 0x0032 200 200 000 Old_age Always - 52"
# $1 for SAS is "Elements in grown defect list: 0"
local var1
local var2
if [[ $drive_type == "scsi" ]]; then # SAS drive
# Get attribute name
#var1=$(echo "$strIn" | awk -F':' '{printf $1}')
var1=$(echo "$1" | awk -F':' '{printf $1}')
# Get attribute value
#var2=$(echo "$strIn" | awk -F':' '{printf $2}' | xargs)
var2=$(echo "$1" | awk -F':' '{printf $2}' | xargs)
else
# Get attribute name
#var1=$(echo "$1" | awk '{printf "%-28s", $2}')
var1=$(echo "$1" | awk '{printf $2}')
# Get attribute value
var2=$(echo "$1" | awk '{printf $10}' | awk -F"+" '{print $1}' | cut -d"h" -f1)
fi
var1_trimmed="$(echo "$var1" | xargs)"
show_increased=""
previous_att="$(get_section_key_value "$smart_log" "$serial" "$var1_trimmed")"
if [[ -z $previous_att ]]; then
# Create ini section if missing
if ! grep "$serial" "$smart_log" > /dev/null; then
echo -e "[${serial}]\ndrive_num=" >> "$smart_log"
fi
set_section_key_value "$smart_log" "$serial" "$var1_trimmed" "$var2"
elif [[ $var2 -gt "$previous_att" ]]; then
set_section_key_value "$smart_log" "$serial" "$var1_trimmed" "$var2"
show_increased=$'\\t'"Increased by $((var2 - previous_att))"
elif [[ $var2 -lt "$previous_att" ]]; then
set_section_key_value "$smart_log" "$serial" "$var1_trimmed" "$var2"
show_increased=$'\\t'"Decreased by $((previous_att - var2))"
fi
# Don't show "Increased by #" or " Decreased by #" if first time adding the drive to smart.log
if [[ $first_run == "yes" ]]; then
show_increased=""
fi
}
log_bad_nvme(){
# $var2 is smart attribute value
# $var3 is smart attribute name
var3_trimmed="$(echo "$var3" | xargs)"
show_increased=""
previous_att="$(get_section_key_value "$smart_log" "$serial" "$var3_trimmed")"
if [[ -z $previous_att ]]; then
# Create ini section if missing
if ! grep "$serial" "$smart_log" > /dev/null; then
echo -e "[${serial}]\ndrive_num=" >> "$smart_log"
fi
set_section_key_value "$smart_log" "$serial" "$var3_trimmed" "$var2"
elif [[ $var2 -gt "$previous_att" ]]; then
set_section_key_value "$smart_log" "$serial" "$var3_trimmed" "$var2"
show_increased=$'\\t'"Increased by $((var2 - previous_att))"
elif [[ $var2 -lt "$previous_att" ]]; then
set_section_key_value "$smart_log" "$serial" "$var3_trimmed" "$var2"
show_increased=$'\\t'"Decreased by $((previous_att - var2))"
fi
# Don't show "Increased by #" or " Decreased by #" if first time adding the drive to smart.log
if [[ $first_run == "yes" ]]; then
show_increased=""
fi
}
short_attibutes(){
# $1 is space padded attribute number like " 5" or "199"
# $2 is "zero" or "none"
# $3 is SAS attribute name with padding like "Elements in grown defect list: "
# $strIn is like "199 UDMA_CRC_Error_Count 0x003e 200 200 000 Old_age Always - 0"
local num
if [[ $1 == "_" ]]; then
num=""
else
num="$1 "
fi
if [[ "$strIn" ]]; then
if [[ $drive_type == "scsi" ]]; then # SAS drive
var1="$3"
# Get attribute value
var2=$(echo "$strIn" | awk -F':' '{printf $2}' | xargs)
else
# Get attribute name with padding like "UDMA_CRC_Error_Count "
var1=$(echo "$strIn" | awk '{printf "%-28s", $2}')
# Get attribute value
var2=$(echo "$strIn" | awk '{printf $10}' | awk -F"+" '{print $1}' | cut -d"h" -f1)
fi
# Remove % from Percentage_Used
#var2="${var2//%/}" # Currently not used
if [[ $2 == "zero" && "${var2:0:1}" -gt "0" ]]; then
# Log important attributes with raw value greater than 0
if [[ $var2 -gt "0" ]]; then
warn=$((warn +1))
# Log important attributes that should be zero but have raw value greater than 0
log_bad "$strIn"
fi
if [[ $increased != "yes" ]]; then
echo -e "$num${Yellow}$var1${Off} ${LiteRed}$var2${Off}$show_increased"
elif [[ $first_run == "yes" ]]; then
new_atts+=("$num${Yellow}$var1${Off} ${LiteRed}$var2${Off}")
else
changed_atts+=("$num${Yellow}$var1${Off} ${LiteRed}$var2${Off}$show_increased")
fi
elif [[ $2 == "none" && "${var2:0:1}" -gt "0" ]]; then
if [[ $increased != "yes" ]]; then
echo -e "$num$var1 $var2"
fi
else
if [[ $increased != "yes" ]]; then
echo -e "$num${Yellow}$var1${Off} $var2"
fi
fi
# Log important attributes with 0 raw value
if [[ $2 == "zero" && $var2 -eq "0" ]]; then
log_bad "$strIn"
fi
fi
}
show_health(){
# $drive is sata1 or sda or usb1 etc
local att194
# Decide device type (sat/scsi) via detect_dtype()
local drive_type
drive_type=$(detect_dtype)
# Show drive overall health
readarray -t health_array < <("$smartctl" -H -d "$drive_type" -T permissive /dev/"$drive" | tail -n +5)
for strIn in "${health_array[@]}"; do
if echo "$strIn" | awk '{print $1}' | grep -E '[0-9]' >/dev/null ||\
echo "$strIn" | awk '{print $1}' | grep 'ID#' >/dev/null ; then
# Remove columns 36 to 78
strOut="${strIn:0:36}${strIn:78}"
# Remove columns 65 to 74
strOut="${strOut:0:65}${strOut:74}"
# Remove columns after 77
strOut="${strOut:0:77}"
if [[ $increased != "yes" ]]; then
echo "$strOut"
fi
else
if [[ -n "$strIn" ]]; then # Don't echo blank line
if $(echo "$strIn" | grep -qi PASSED); then
if [[ $increased != "yes" ]]; then
echo -e "SMART overall-health self-assessment test result: ${LiteGreen}PASSED${Off}"
fi
elif $(echo "$strIn" | grep -qi 'Health Status: OK'); then
if [[ $increased != "yes" ]]; then
echo -e "SMART Health Status: ${LiteGreen}OK${Off}"
fi
else
if [[ $increased != "yes" ]]; then
echo "$strIn"
fi
fi
fi
fi
done
# Show error counter
#"$smartctl" -l error /dev/"$drive" | grep -iE 'error.*logg'
# Retrieve Error Log and show error count
if [[ $drive_type != "scsi" ]]; then # Not SAS drive
errlog="$("$smartctl" -l error -d "$drive_type" /dev/"$drive" | grep -iE 'error.*logg')"
errcount="$(echo "$errlog" | awk '{print $3}')"
if [[ -z $errlog ]]; then
errlog="$("$smartctl" -l error -d "$drive_type" /dev/"$drive" | grep -iE 'error count')"
errcount="$(echo "$errlog" | awk '{print $4}')"
fi
if [[ -z $errcount ]]; then
if [[ $increased != "yes" ]]; then
"$smartctl" -l error -d "$drive_type" /dev/"$drive" | grep -iE 'not supported'
fi
elif [[ $errcount -gt "0" ]]; then
errtotal=$((errtotal +errcount))
if [[ $increased != "yes" ]]; then
echo -e "SMART Error Counter Log: ${LiteRed}$errcount${Off}"
fi
else
if [[ $increased != "yes" ]]; then
echo -e "SMART Error Counter Log: ${LiteGreen}No Errors Logged${Off}"
fi
fi
else
# SAS drive
readarray -t sas_errlog_array < <("$smartctl" -d "$drive_type" --all /dev/"$drive")
for e in "${sas_errlog_array[@]}"; do
nameA=""
nameB=""
valA=""
valB=""
if echo "$e" | grep -E '^read:' >/dev/null; then
nameA="${Yellow}Total uncorrected read errors: ${Off}"
valA=$(echo "$e" | awk '{printf $8}')
nameB="Total corrected read errors: "
valB=$(echo "$e" | awk '{printf $5}')
elif echo "$e" | grep -E '^write:' >/dev/null; then
nameA="${Yellow}Total uncorrected write errors: ${Off}"
valA=$(echo "$e" | awk '{printf $8}')
nameB="Total corrected write errors: "
valB=$(echo "$e" | awk '{printf $5}')
elif echo "$e" | grep -E '^verify:' >/dev/null; then
nameA="${Yellow}Total uncorrected verify errors: ${Off}"
valA=$(echo "$e" | awk '{printf $8}')
nameB="Total corrected verify errors: "
valB=$(echo "$e" | awk '{printf $5}')
fi
# Total uncorrected read/write/verify errors
if [[ -n $nameA ]] && [[ -n $valA ]]; then # Don't echo blank line
if [[ $valA -gt "0" ]]; then
if [[ $increased != "yes" ]]; then
echo -e "${nameA}${LiteRed}$valA${Off}"
fi
else
if [[ $increased != "yes" ]]; then
echo -e "${nameA}${valA}"
fi
fi
fi
# Total corrected read/write/verify errors
if [[ -n $nameB ]] && [[ -n $valB ]]; then # Don't echo blank line
if [[ $valB -gt "0" ]]; then
if [[ $increased != "yes" ]]; then
echo -e "${nameB}${LiteRed}$valB${Off}"
fi
else
if [[ $increased != "yes" ]]; then
echo -e "${nameB}${valB}"
fi
fi
fi
done
fi
# Show SMART attributes
health_bad=""
if [[ $drive_type == "scsi" ]]; then # SAS drive
health=$("$smartctl" -H -d "$drive_type" -T permissive /dev/"$drive" | tail -n +5)
if ! echo "$health" | grep -E 'SMART Health Status.*OK' >/dev/null; then
if [[ $increased != "yes" ]]; then
# Show all SMART attributes
health_bad="yes"
fi
fi
else # SATA drive
health=$("$smartctl" -H -d "$drive_type" -T permissive /dev/"$drive" | tail -n +5)
if ! echo "$health" | grep PASSED >/dev/null; then
if [[ $increased != "yes" ]]; then
# Show all SMART attributes
health_bad="yes"
fi
fi
fi
if [[ $health_bad == "yes" ]] || [[ $all == "yes" ]]; then
# Show all SMART attributes if health-bad == yes, or -a/--all option used
smart_all
else
# Show only important SMART attributes
if [[ $seagate == "yes" ]] && [[ $smartversion -gt "6" ]]; then
readarray -t smart_atts < <("$smartctl" -A -d "$drive_type" \
-v 1,raw48:54 -v 7,raw48:54 -v 188,raw48:54 -v 195,raw48:54 /dev/"$drive")
elif [[ $seagate == "yes" ]] && [[ $smartversion -lt "7" ]]; then
readarray -t smart_atts < <("$smartctl" -A -d "$drive_type" \
-v 1,raw48:54,Raw_Read_Error_Rate -v 7,raw48:54,Seek_Error_Rate \
-v 188,raw48:54,Command_Timeout_Count \
-v 195,raw48:54,Hardware_ECC_Recovered /dev/"$drive")
else
readarray -t smart_atts < <("$smartctl" -A -d "$drive_type" /dev/"$drive")
fi
# Decide if show airflow temperature
if echo "${smart_atts[*]}" | grep -c -E '194.*Temp' >/dev/null; then
att194=yes
elif echo "${smart_atts[*]}" | grep -c 'Current Drive Temp' >/dev/null; then
att194=yes
fi
sas_attibutes=()
for strIn in "${smart_atts[@]}"; do
if [[ $drive_type == "scsi" ]]; then # SAS drive
if [[ $strIn =~ "Elements in grown defect list" ]]; then
# 5 Elements in grown defect list
#short_attibutes " 5" zero "Elements in grown defect list: "
sas_attibutes+=(" 5,zero,Elements in grown defect list: ,$strIn")
elif [[ $strIn =~ "Current Drive Temperature" ]]; then
# 194 Current Drive Temperature
#short_attibutes "194" none "Current Drive Temperature: "
sas_attibutes+=("194,none,Current Drive Temperature: ,$strIn")
fi
else # SATA drive
if [[ ${strIn:0:3} == " 1" ]]; then
# 1 Raw read error rate
#if [[ $seagate == "yes" ]]; then
# if [[ $smartversion -gt "6" ]]; then
# short_attibutes " 1" zero
# else
# short_attibutes " 1" none
# fi
#else
short_attibutes " 1" zero
#fi
elif [[ ${strIn:0:3} == " 5" ]]; then
# 5 Reallocated sectors - scrutiny and BackBlaze
short_attibutes " 5" zero
elif [[ ${strIn:0:3} == " 7" ]]; then
# 7 Seek_Error_Rate
#if [[ $seagate == "yes" ]]; then
# if [[ $smartversion -gt "6" ]]; then
# short_attibutes " 7" zero
# else
# short_attibutes " 7" none
# fi
#else
short_attibutes " 7" zero
#fi
elif [[ ${strIn:0:3} == " 9" ]]; then
# 9 Power on hours
short_attibutes " 9" none
elif [[ ${strIn:0:3} == " 10" ]]; then
# 10 Spin_Retry_Count - scrutiny
short_attibutes " 10" zero
elif [[ ${strIn:0:3} == "187" ]]; then
# 187 Current pending sectors - BackBlaze
short_attibutes "187" zero
elif [[ ${strIn:0:3} == "188" ]]; then
# 188 Current pending sectors - BackBlaze
short_attibutes "188" zero
elif [[ ${strIn:0:3} == "190" && -z $att194 ]]; then
# 190 Airflow_Temperature
short_attibutes "190" none
elif [[ ${strIn:0:3} == "194" ]]; then
# 194 Temperature - scrutiny
short_attibutes "194" none
elif [[ ${strIn:0:3} == "195" ]]; then
# 195 Hardware_ECC_Recovered aka ECC_On_the_Fly_Count
#if [[ $seagate == "yes" ]]; then
# if [[ $smartversion -gt "6" ]]; then
# short_attibutes "195" zero
# else
# short_attibutes "195" none
# fi
#else
short_attibutes "195" zero
#fi
elif [[ ${strIn:0:3} == "197" ]]; then
# 197 Current pending sectors - scrutiny and BackBlaze
short_attibutes "197" zero
elif [[ ${strIn:0:3} == "198" ]]; then
# 198 Offline uncorrectable - scrutiny and BackBlaze
short_attibutes "198" zero
elif [[ ${strIn:0:3} == "199" ]]; then
# 199 UDMA_CRC_Error_Count
short_attibutes "199" zero
elif [[ ${strIn:0:3} == "200" ]]; then
# 200 Multi_Zone_Error_Rate - WD
short_attibutes "200" zero
elif [[ ${strIn:0:3} == "252" ]]; then
# 252 Added_Bad_Flash_Blk_Ct - Samsung SSD
short_attibutes "252" zero
fi
fi
done
# Sort SAS smart attributes by ID number
sas_attibutes_sorted=()
IFS=$'\n'
sas_attibutes_sorted=($(sort -g <<<"${sas_attibutes[*]}")) # Sort array
unset IFS
# Show SAS smart attributes sorted by ID number
for att in "${sas_attibutes_sorted[@]}"; do
arg1="$(echo "$att" | cut -d',' -f1)"
arg2="$(echo "$att" | cut -d',' -f2)"
arg3="$(echo "$att" | cut -d',' -f3)"
strIn="$(echo "$att" | cut -d',' -f4)"
short_attibutes "$arg1" "$arg2" "$arg3"
done
fi
}