forked from Napsty/check_zpools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_zpools.sh
executable file
·173 lines (162 loc) · 7.23 KB
/
check_zpools.sh
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
#!/bin/sh
#########################################################################
# Script: check_zpools.sh
# Purpose: Nagios plugin to monitor status of zfs pool
# Authors: Aldo Fabi First version (2006-09-01)
# Vitaliy Gladkevitch Forked (2013-02-04)
# Claudio Kuenzler Complete redo, perfdata, etc (2013-2014)
# Alexander Skwar Posix compliant
# Doc: http://www.claudiokuenzler.com/nagios-plugins/check_zpools.php
# History:
# 2006-09-01 Original first version
# 2006-10-04 Updated (no change history known)
# 2013-02-04 Forked and released
# 2013-05-08 Make plugin work on different OS, pepp up plugin
# 2013-05-09 Bugfix in exit code handling
# 2013-05-10 Removed old exit vars (not used anymore)
# 2013-05-21 Added performance data (percentage used)
# 2013-07-11 Bugfix in zpool health check
# 2014-02-10 Bugfix in threshold comparison
# 2014-03-11 Allow plugin to run without enforced thresholds
# 2014-10-02 Made script Posix compliant - remove bash dependency
# 2014-10-07 Fixed various errors... Too bad, that there are no arrays
#########################################################################
### Begin vars
STATE_OK="0" # define the exit code if status is OK
STATE_WARNING="1" # define the exit code if status is Warning
STATE_CRITICAL="2" # define the exit code if status is Critical
STATE_UNKNOWN="3" # define the exit code if status is Unknown
# Set path
PATH="$PATH:/usr/sbin:/sbin"
export PATH
### End vars
#########################################################################
help="check_zpools.sh (c) 2006-2014 several authors\n
Usage: $0 -p (poolname|ALL) [-w warnpercent] [-c critpercent]\n
Example: $0 -p ALL -w 80 -c 90\n"
#########################################################################
# Check necessary commands are available
for cmd in zpool tr; do
# http://stackoverflow.com/questions/592620/how-to-check-if-a-program-exists-from-a-bash-script
type "$cmd" >/dev/null 2>&1 || { echo >&2 "UNKNOWN: ${cmd} does not exist, please check if command exists and PATH is correct"; exit $STATE_UNKNOWN; }
done
#########################################################################
# Check for people who need help - aren't we all nice ;-)
if [ "${1}" = "--help" -o "${#}" = "0" ]; then
printf "${help}"
exit "$STATE_UNKNOWN"
fi
#########################################################################
# Get user-given variables
warn=-1
crit=-1
while getopts "p:w:c:" Input; do
case $Input in
p) pool="$OPTARG";;
w) warn="$OPTARG";;
c) crit="$OPTARG";;
*) printf "$help"
exit "$STATE_UNKNOWN"
;;
esac
done
#########################################################################
# Did user obey to usage?
if [ -z "$pool" ]; then printf "$help"; exit "$STATE_UNKNOWN"; fi
#########################################################################
# Verify threshold sense
if [ "$warn" != "-1" ] && [ -n "$warn" ] && [ "$crit" = "-1" ]; then echo "Both warning and critical thresholds must be set"; exit "$STATE_UNKNOWN"; fi
if [ "$crit" != "-1" ] && [ "$warn" = "-1" ] && [ -n "$crit" ]; then echo "Both warning and critical thresholds must be set"; exit "$STATE_UNKNOWN"; fi
# Check if values are not empty and numeric
if test -n "$warn" && ! test "$warn" -eq "$warn" 2>/dev/null; then echo "Warning value \`$warn' does not seem to be a number"; exit "$STATE_UNKNOWN"; fi
if test -n "$crit" && ! test "$crit" -eq "$crit" 2>/dev/null; then echo "Critical value \`$crit' does not seem to be a number"; exit "$STATE_UNKNOWN"; fi
# Warning <= Critical?
if [ "$warn" -gt "$crit" ]; then echo "Warning threshold cannot be greater than critical"; exit "$STATE_UNKNOWN"; fi
#########################################################################
# What needs to be checked?
## Check all pools
error=""
errorCount=0
if [ $pool = "ALL" ]; then
POOLS=` zpool list -Ho name `
p=0
for POOL in $POOLS; do
# Initialize variables, which might be used later on
CAPACITY=` zpool list -Ho capacity $POOL | tr -d '%' `
HEALTH=` zpool list -Ho health $POOL `
errorNum="error$p"
perfdataNum="perfdata$p"
fcrit="-1"
if [ "$warn" != "-1" ] && [ "$crit" != "-1" ]; then
# Check with thresholds
if [ "$HEALTH" != "ONLINE" ]; then
eval ` echo $errorNum=\"$POOL health is $HEALTH\" `
fcrit=1
errorCount=` expr $errorCount + 1 `
elif [ "$CAPACITY" -ge "$crit" ]; then
eval ` echo $errorNum=\"POOL $POOL usage is CRITICAL \($CAPACITY%\)\" `
fcrit=1
errorCount=` expr $errorCount + 1 `
elif [ "$CAPACITY" -ge "$warn" ] && [ "$CAPACITY" -lt "$crit" ]; then
eval ` echo $errorNum=\"POOL $POOL usage is WARNING \($CAPACITY%\)\" `
errorCount=` expr $errorCount + 1 `
fi
else
# Check without thresholds
if [ "$HEALTH" != "ONLINE" ]; then
eval ` echo $errorNum=\"$POOL health is $HEALTH\" `
fcrit=1
errorCount=` expr $errorCount + 1 `
fi
fi
eval ` echo $perfdataNum="$POOL=${CAPACITY}% " `
p=` expr "$p" + 1 `
done
if [ "$errorCount" != 0 ]; then
if [ "$fcrit" -eq 1 ]; then exit_code="$STATE_CRITICAL"; else exit_code="$STATE_WARNING"; fi
printf "ZFS POOL ALARM: "
poolNum=0
while [ "$poolNum" -le "$p" ]; do
printf "%s " "` eval echo \\$error$poolNum `"
poolNum=` expr $poolNum + 1 `
done
printf "|"
poolNum=0
while [ "$poolNum" -le "$p" ]; do
printf "%s " "` eval echo \\$perfdata$poolNum `"
poolNum=` expr $poolNum + 1 `
done
echo
exit "$exit_code"
else
printf "ALL ZFS POOLS OK ("; printf "$POOLS" | tr '\n' ' '; printf ")|"
poolNum=0
while [ "$poolNum" -le "$p" ]; do
perfdataNum="perfdata$poolNum"
eval echo \$$perfdataNum
poolNum=` expr $poolNum + 1 `
done | tr '\n' ' '
echo
exit "$STATE_OK"
fi
## Check single pool
else
CAPACITY=` zpool list -Ho capacity $pool | tr -d '%' `
HEALTH=` zpool list -Ho health $pool `
if [ "$warn" != "-1" ] && [ "$crit" != "-1" ]; then
# Check with thresholds
if [ "$HEALTH" != "ONLINE" ]; then echo "ZFS POOL $pool health is $HEALTH|$pool=$CAPACITY%"; exit "$STATE_CRITICAL"
elif [ "$CAPACITY" -gt "$crit" ]; then echo "ZFS POOL $pool usage is CRITICAL ($CAPACITY%|$pool=$CAPACITY%)"; exit "$STATE_CRITICAL"
elif [ "$CAPACITY" -gt "$warn" ] && [ "$CAPACITY" -lt "$crit" ]; then echo "ZFS POOL $pool usage is WARNING ($CAPACITY%)|$pool=$CAPACITY%"; exit "$STATE_WARNING"
else echo "ALL ZFS POOLS OK ($pool)|$pool=$CAPACITY%"; exit "$STATE_OK"
fi
else
# Check without thresholds
if [ "$HEALTH" != "ONLINE" ]; then echo "ZFS POOL $pool health is $HEALTH|$pool=$CAPACITY%"; exit "$STATE_CRITICAL"
else echo "ALL ZFS POOLS OK ($pool)|$pool=$CAPACITY%"; exit "$STATE_OK"
fi
fi
fi
echo "UKNOWN - Should never reach this part"
exit "$STATE_UNKNOWN"
# EOF