-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsmartctl.sh
More file actions
161 lines (154 loc) · 4.09 KB
/
smartctl.sh
File metadata and controls
161 lines (154 loc) · 4.09 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
#!/bin/bash -u
# Wrapper for smartctl. Usage:
#
# smartctl.sh <smartctl flags(s)>
#
# or:
#
# smartctl.sh routine
#
# or:
#
# smartctl.sh <device>
#
# In the first case, the smartctl program is run with the <smartctl flags(s)>
# on all known devices, as determined by the following command:
#
# sudo parted -l | grep Disk | awk '{print $2}' | sed 's/://g'
#
# The second case makes successive calls to 'smartctl.sh <smartctl flags(s)>',
# with the following flags (in this order): --info, --capabilities, --health,
# --log=error, --log=selftest, --test=short (every 10 days),
# --test=long (every 105 days). This is suitable for the root crontab, with (e.g.)
# the following entry:
#
# export PATH=/usr/bin:/bin:/usr/sbin:/sbin; /some/path/smartctl.sh routine
#
# The third case runs 'smartctl <flag>' sequentially, on the specified deviced,
# with the following flags (in this order): --info, --capabilities, --health,
# --log=error, --log=selftest. It is useful to (e.g.) get SMART information about
# a new hard drive.
function machine_is
{
OS=`uname -v`
[[ ! "${OS//$1/}" == "$OS" ]] && return 0 || return 1
}
function ensure_dependencies
{
#install dependencies
for i in $@
do
if [ -z "`dpkg -s $i 2> /dev/null`" ]
then
echo "Need to install $i"
if machine_is Ubuntu
then
sudo apt-get install $i 1>&2
else
echo "ERROR: cannot install $i, cannot continue."
return 3
fi
fi
done
#check dependencies
for i in $@
do
if [ -z "`dpkg -s $i 2> /dev/null`" ]
then
echo "ERROR: cannot find $i, cannot continue."
return 3
fi
done
}
DEPS="parted smartmontools"
DEVS=$( sudo parted -l | grep Disk | awk '{print $2}' | sed 's/://g' )
#ensure the needed software is available
ensure_dependencies $DEPS || exit $?
if [ $# -eq 0 ]
then
MODE="routine"
else
MODE="$1"
fi
case $MODE in
--info|--all|--xall|--scan|--health|--capabilities|--attributes)
for i in $DEVS
do
echo "============== ( $i ) =============="
sudo smartctl $MODE $i
done
;;
/dev/*)
for i in --info --capabilities --health --log=error --log=selftest
do
echo "============== ( $i ) =============="
echo sudo smartctl $i $MODE
done
;;
panic)
su -
for i in $DEVS
do
echo Y | $0 --test=long || exit 3
done
#restart and force disk checks
sudo shutdown -rF now
;;
routine)
# echo "---------------------------------"
# echo " INFO"
# echo "---------------------------------"
# $0 --info || exit 3
# echo "---------------------------------"
# echo " CAPABILITIES"
# echo "---------------------------------"
# $0 --capabilities || exit 3
echo "---------------------------------"
echo " HEALTH"
echo "---------------------------------"
$0 --health | grep 'test result'
echo "---------------------------------"
echo " LOG=error"
echo "---------------------------------"
echo Y | $0 "--log=error --quietmode=errorsonly" || exit 3
echo "---------------------------------"
echo " LOG=selftest"
echo "---------------------------------"
echo Y | $0 "--log=selftest --quietmode=errorsonly" || exit 3
#need to know the DOY
DOY=$(date +%j)
#only run the short test once every ST days
ST=10
if [ $(( 10#$DOY - (10#$DOY / $ST) * $ST )) -eq 0 ]
then
echo "---------------------------------"
echo " TEST=short"
echo "---------------------------------"
echo Y | $0 --test=short || exit 3
fi
#only run the short test once every LT days
LT=$(( $ST * 5 + $ST / 2 ))
if [ $(( 10#$DOY - (10#$DOY / $LT) * $LT )) -eq 0 ]
then
echo "---------------------------------"
echo " TEST=long"
echo "---------------------------------"
echo Y | $0 --test=long || exit 3
fi
;;
*)
echo "The following command is going to be issued on all devices:"
echo "sudo smartctl $MODE <device>"
echo "Continue? [Y/n]"
read answer
if [ "$answer" == "N" ] || [ "$answer" == "n" ]
then
exit 3
fi
for i in $DEVS
do
echo "============== ( $i ) =============="
sudo smartctl $MODE $i || exit $?
done
;;
esac