-
Notifications
You must be signed in to change notification settings - Fork 3
/
SmartHealth
98 lines (85 loc) · 2.51 KB
/
SmartHealth
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
#!/bin/bash
# Install pre-reqs if needed.install the smartctl package first! (apt-get install smartct
Installed=`apt list smartmontools 2>/dev/null | grep smartmontools | grep installed | wc | tr -s " " | cut -d ' ' -f 2`
if [[ "$Installed" == "0" ]]; then
apt install -y smartmontools &>/dev/null
fi
Installed=`apt list mosquitto-clients 2>/dev/null | grep mosquitto-clients | grep installed | wc | tr -s " " | cut -d ' ' -f 2`
if [[ "$Installed" == "0" ]]; then
apt install -y mosquitto-clients &>/dev/null
fi
HostName=`hostname`
if [[ "$@" == "-j" ]]; then
Header='{"msg": {"payload": {"host": "'
Header+=$HostName
Header+='", "type": "smartct", "drives": ['
PreMessage='{"path": "'
MidMessage='","status": "'
PostMessage='"}'
InterDrive=','
Mid='],"status": "'
Closing='"}}}\n'
else
Header="######## CHECKING ########\n"
PreMessage=""
MidMessage=" "
PostMessage="\n"
InterDrive=''
Mid="#####################\nOverall Health: "
Closing="\n#####################\n"
fi
Status="OK"
if sudo true
then
true
else
echo 'Root privileges required'
exit 1
fi
printf "$Header"
FirstDrive=1
for drive in $(smartctl --scan | cut -d ' ' -f 1); do
printf "$Line"
if [[ ! -e $drive ]]; then continue ; fi
Line="$PreMessage$drive$MidMessage"
smart=$(
sudo smartctl -H $drive 2>/dev/null |
grep '^SMART overall\|^SMART Health Status' |
rev | cut -d ' ' -f1 | rev
)
[[ "$smart" == "" ]] && smart='unavailable'
if [[ "$smart" != "OK" ]] && [[ "$smart" != "PASSED" ]]; then
Status="ERROR"
Line+="ERROR$PostMessage$InterDrive"
else
#Count DOWN endurence tests
Life=`sudo smartctl -a $drive | grep -i 'remain\|left' | grep -i 'percent\|life' | grep -vi hours | rev | cut -d ' ' -f 1 | rev`
if [ "$Life" != "" ]; then
if [ "$Life" -le "10" ]; then
Status="ERROR"
smart="WARN - Life: $Life%%"
else
smart="OK - Life: $Life%%"
fi
else
#Count UP endurence tests
Life=`sudo smartctl -a $drive | grep -i 'Percentage' | grep -i 'endurance\|Used' | grep -vi hours | rev | cut -d ' ' -f 1 | rev | cut -d '%' -f 1`
if [ "$Life" != "" ]; then
let Life=100-$Life
if [ "$Life" -le "10" ]; then
Status="ERROR"
smart="WARN - Life: $Life%%"
else
smart="OK - Life: $Life%%"
fi
else
smart="OK"
fi
fi
Line+="$smart$PostMessage$InterDrive"
fi
done
if [[ "$InterDrive" != "" ]]; then
Line=${Line::-${#InterDrive}}
fi
printf "$Line$Mid$Status$Closing"