-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck_disk_smart
More file actions
executable file
·278 lines (244 loc) · 7.88 KB
/
check_disk_smart
File metadata and controls
executable file
·278 lines (244 loc) · 7.88 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
#!/bin/bash
set -f
PROGNAME=${0##*/}
NAGIOS_STATUS_TEXT=(
[0]='OK'
[1]='WARNING'
[2]='CRITICAL'
[3]='UNKNOWN'
)
NAGIOS_STATUS_PRIO_TR=( 0 2 3 1 )
DEVICE=
IS_REGEX=
ENV=
HOST=
PORT=
SSH_USER=
SSH_IDENTITY=
TIMEOUT=
SUDO_USER=
SNMP_OID=
SNMP_OPTIONS=()
function nagios_exit_usage() {
echo "\
Usage: $PROGNAME [OPTION...] [-d DEVICE]
Nagios plugin to check for certificates expiration in an OpenSSL CA
Common options:
-d, --device PATH Disk device path, eg: /dev/sda
-r, --regex IBOOL Given device is a regex, 0 or 1
-S, --sudo-user USER Run smartctl command with sudo -u USER
-E, --bash-env STRING Environment to export, eg: PATH=\$PATH:/sbin
-h, --help Display this help
SSH options:
-s, --ssh CMD Custom SSH command, see documentation below
-H, --host HOST SSH host
-p, --port INT SSH port
-u, --ssh-user USER SSH username
-i, --ssh-identity FILE SSH identity key file
-t, --timeout INT SSH connect timeout
SNMP options:
-H, --host HOST SNMP host
-p, --port INT SNMP port
-t, --timeout INT Alias for SNMP option -t (timeout)
-O, --snmp-oid OID OID that return result in get_smart format
-P, --snmp-options ARGS SNMP options, eg: -v 2c -c public, end with --
-T, --snmp-sample-script Display sample SNMP extend script
When using option -s, --ssh, classic SSH options get ignored. It is then
up to the caller to properly escape args and handle SSH options like
identity, port, user, connect timeout.
Usage example: check_disk_smart -s 'ssh -i key cmd@bastion host' ...
When using option -O, --snmp-oid, common option are ignored and the
result is retrieved from the given OID. The result is expected to be in
get_smart format, one device per line: <device>\t<status>, eg:
/dev/sda PASSED
/dev/sdb PASSED
A sample script for the snmpd extend OID is printed on stdout with option
-T, --snmp-sample-script. Put it on the target host and add an extend line
in your snmpd.conf to access its output.
"
exit 3
}
function sample_snmp_script() {
cat <<'EOF'
#!/bin/bash
# sudoers sample for Debian
# root@sd-131018:~# cat /etc/sudoers.d/Debian-snmp
# Debian-snmp ALL=(ALL) NOPASSWD: /opt/ztools/bin/get-smart
#
# snmpd.conf extend line
# OID for script output .1.3.6.1.4.1.8072.1.3.2.3.1.2.5.115.109.97.114.116
# extend smart /path/to/script
set -f
export LC_ALL=C
[[ $UID == 0 ]] || exec sudo -n "$0" "$@"
OIFS=$IFS
IFS=$'\n'
disks=( $(smartctl --scan) )
IFS=$OIFS
(( ${#disks[@]} == 0 )) && exit 1
for i in "${disks[@]}"; do
opts=${i%%#*}
[[ $opts == *,* ]] && subdev=":${opts##*,}" || subdev=
status=$(smartctl -H $opts |
sed -nre 's,.*(Health Status|overall-health self-assessment test result): (.+),\2,p')
printf '%s\t%s\n' "${i%% *}${subdev}" "$status"
done
EOF
exit 3
}
# $1: Nagios status code
# $2: Plugin output message
# $@: Optional lines of long plugin output
function nagios_die() {
echo "${NAGIOS_STATUS_TEXT[$1]}: $2"
(( $# > 2 )) && (IFS=$'\n'; echo "${*:3}")
exit "$1"
}
# $1: current state
# $2: want state
function increase_prio() {
REPLY=$1
if (( NAGIOS_STATUS_PRIO_TR[$2] > NAGIOS_STATUS_PRIO_TR[$1] )); then
REPLY=$2
fi
}
function nagopt_var() {
[[ -z $2 || $2 == '$' ]] && return
declare -g "$1=$2"
}
function nagopt_arglist() {
local var=$1; shift
local value=()
while (( $# > 0 )) && [[ $1 != -- ]]; do
value+=( "$1" )
shift
done
declare -ga "$var=( \"\${value[@]}\" )"
}
function get_cmd() {
local cmd=( "$@" )
local sshopts sshargs ret
if [[ -n $SSH_CMD || -n $HOST ]]; then
printf -v sshargs "%q " "${cmd[@]}"
if [[ -n $SSH_CMD ]]; then
# custom ssh command: it is up to the caller to properly escape args
# and handle ssh options like identity, port, user, connect timeout
cmd=( $SSH_CMD -- "$sshargs" )
elif [[ -n $HOST ]]; then
cmd=(
ssh ${SSH_IDENTITY:+-i "$SSH_IDENTITY"} ${SSH_USER:+-l "$SSH_USER"}
${PORT:+-p "$PORT"} -q -C -T -o StrictHostKeyChecking=no
-o UserKnownHostsFile=/dev/null -o PreferredAuthentications=publickey
${TIMEOUT:+-o "ConnectTimeout=$TIMEOUT"} "$HOST" -- "$sshargs"
)
fi
fi
REPLY=$( "${cmd[@]}" ); ret=$?
echo "$REPLY"
return "$ret"
}
function get_smart() {
local smartctl=( ${SUDO_USER:+sudo -n -u "$SUDO_USER"} smartctl )
local disks=()
local i status
if [[ $IS_REGEX == 1 ]]; then
local OIFS=$IFS IFS=$'\n'
disks+=( $("${smartctl[@]}" --scan |awk -v "DEVICE=$DEVICE" '$1 ~ DEVICE {print}') )
local IFS=$OIFS
(( ${#disks[@]} == 0 )) && return 1
else
disks+=( "$DEVICE" )
fi
for i in "${disks[@]}"; do
opts=${i%%#*}
[[ $opts == *,* ]] && subdev=":${opts##*,}" || subdev=
status=$(smartctl -H $opts |
sed -nre 's,.*(Health Status|overall-health self-assessment test result): (.+),\2,p')
printf '%s\t%s\n' "${i%% *}${subdev}" "$status"
done
return 0
}
function get_snmp() {
snmpget -OqevU "${SNMP_OPTIONS[@]}" ${TIMEOUT:+-t "$TIMEOUT"} "${HOST}${PORT:+:$PORT}" "$SNMP_OID" |
tr -d '"' |grep -Fv 'No Such' || return 2
}
function reply_get_smart() {
local OIFS=$IFS
local IFS=$'\n'
REPLY=($(
if [[ -n $SNMP_OID && -n $HOST ]]; then
get_snmp
else
{ echo ${ENV:+"export $ENV"}
echo 'export LC_ALL=C'
declare -p DEVICE IS_REGEX SUDO_USER
declare -f get_smart
echo get_smart
} |IFS=$OIFS get_cmd bash
fi
))
}
while (( $# > 0 )); do
case "$1" in
# common
-d|--device) nagopt_var DEVICE "$2"; shift ;;
-r|--regex) nagopt_var IS_REGEX "$2"; shift ;;
-S|--sudo-user) nagopt_var SUDO_USER "$2"; shift ;;
-E|--bash-env) nagopt_var ENV "$2"; shift ;;
-h|--help) nagios_exit_usage ;;
# ssh, snmp
-H|--host) nagopt_var HOST "$2"; shift ;;
-p|--port) nagopt_var PORT "$2"; shift ;;
-t|--timeout) nagopt_var TIMEOUT "$2"; shift ;;
# ssh
-s|--ssh) nagopt_var SSH_CMD "$2"; shift ;;
-u|--ssh-user) nagopt_var SSH_USER "$2"; shift ;;
-i|--ssh-identity) nagopt_var SSH_IDENTITY "$2"; shift ;;
# snmp
-O|--snmp-oid) nagopt_var SNMP_OID "$2"; shift ;;
-P|--snmp-options) nagopt_arglist SNMP_OPTIONS "${@:2}"; shift $((${#SNMP_OPTIONS[@]}+1)) ;;
-T|--snmp-sample-script) sample_snmp_script ;;
*) nagios_exit_usage ;;
esac
shift
done
[[ -z $IS_REGEX ]] && IS_REGEX=1
if [[ $IS_REGEX != 0 && $IS_REGEX != 1 ]]; then
nagios_die 3 'Invalid regex flag, 0 or 1 expected'
fi
if [[ -z $DEVICE ]]; then
[[ $IS_REGEX == 0 ]] && nagios_die 3 'Device required'
DEVICE=^
fi
if [[ -n $TIMEOUT && -n ${TIMEOUT//[0-9]} ]]; then
nagios_die 3 'Invalid SSH connect timeout, integer expected'
fi
if [[ -n $SSH_IDENTITY && ! -r $SSH_IDENTITY ]]; then
nagios_die 3 'Cannot read SSH identity file'
fi
reply_get_smart
ret=$?
if [[ ( -n $SSH_CMD || (-n $HOST && -z $SNMP_OID) ) ]] && (( ret == 255 )); then
nagios_die 3 'SSH connect failed'
fi
if [[ -n $IS_REGEX ]] && (( ret == 1 )); then
nagios_die 3 'No disk mathing given pattern'
fi
if (( ret != 0 )); then
nagios_die 3 'Failed to get smart data'
fi
status=0
message=
for i in "${REPLY[@]}"; do
IFS=$'\t' set -- $i
if [[ -z $2 ]]; then
increase_prio "$status" 3; status=$REPLY
message="**$1 <unknown>**${message:+, $message}"
elif [[ $2 != PASSED && $2 != OK ]]; then
increase_prio "$status" 2; status=$REPLY
message="**$1 $2**${message:+, $message}"
else
message+="${message:+, }$1 $2"
fi
done
nagios_die "$status" "SMART status: $message"