-
Notifications
You must be signed in to change notification settings - Fork 1
/
check_vmstat.sh
executable file
·190 lines (157 loc) · 5.26 KB
/
check_vmstat.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#!/usr/bin/env sh
# set -x
# Default values
## General behaviour
RETURN_CODE=3 # UNKNOWN : Status if anything goes wrong past this line.
ERROR_PREFIX='' # No prefix for error message by default.
OK_PREFIX='' # No prefix for OK message by default.
WARNING=10
CRITICAL=20
VALUE='wa' # IO wait % is the default mode if unspecified.
DELAY=5
UNIT=''
SILENT='false' # false: Check the returned value against the thresholds.
# What pager is available?
if env less 2>/dev/null
then pager='less'
else pager='more'
fi
# Help message
help_message() {
$pager <<EOF
$(basename "$0")
Check some values from the vmstat output.
Only one value is checked at once, so is present in the performance data.
The checked value is common between GNU/Linux and AIX hosts. As an example, "in" matches the "bi" column on GNU/Linux hosts and the "fi" column on AIX hosts.
Usage: $(basename "$0") [-h] -w <warning threshold> -c <critical threshold> [-v <value>] [-d <delay>] [-S]
-w/--warning <threshold> Warning threshold.
-c/--critical <threshold> Critical threshold.
-v/--value <value> Value monitored. Default is "wa" (IOWait)
Supported values are: wa,in,out
- wa : CPU IO Wait % (wa)
- in : blk/s in on Linux (bi), file page-ins/s on AIX (fi)
- out : blk/s out on Linux (bo), file page-outs/s on AIX (fo)
-d/--delay <delay> Total delay in second. Probes are done every second.
So a delay of N means that the returned value is an average on N probes.
Default is 5.
-h/--help Show this help end exit with an UNKNOWN status.
-S/--silent Do not check the return value against the thresholds and always return OK (0).
EOF
}
# Arguments management #
## KISS way to handle long options
for arg in "${@}"; do
shift
case "${arg}" in
("--warning") set -- "${@}" "-w" ;;
("--critical") set -- "${@}" "-c" ;;
("--value") set -- "${@}" "-v" ;;
("--delay") set -- "${@}" "-d" ;;
("--help") set -- "${@}" "-h" ;;
("--silent") set -- "${@}" "-S" ;;
(*) set -- "${@}" "${arg}"
esac
done;
## Parse command line options
while getopts "w:c:v:d:hS" opt; do
case "${opt}" in
(w)
WARNING=${OPTARG}
;;
(c)
CRITICAL=${OPTARG}
;;
(v)
VALUE="${OPTARG}"
;;
(d)
DELAY="${OPTARG}"
;;
(S)
SILENT='true'
;;
(h)
help_message;
RETURN_CODE=3
exit "${RETURN_CODE}";
;;
(\?)
printf "%s\n" "Unsupported option…";
help_message;
RETURN_CODE=3
exit "${RETURN_CODE}";
;;
esac
done;
## Main
case "$(uname)" in
('Linux')
case "$VALUE" in
('wa')
CAPTION="IO Wait"
VMSTAT_CMD='vmstat -n 1 '$(( $DELAY + 1 ))"| tail -"$DELAY" |awk '{sum+=\$16} END {print sum}'"
UNIT='%'
;;
('in')
CAPTION="IO block ins"
VMSTAT_CMD='vmstat -n 1 '$(( $DELAY + 1 ))"| tail -"$DELAY" |awk '{sum+=\$9} END {print sum}'"
UNIT='blkps'
;;
('out')
CAPTION="IO block outs"
VMSTAT_CMD='vmstat -n 1 '$(( $DELAY + 1 ))"| tail -"$DELAY" |awk '{sum+=\$10} END {print sum}'"
UNIT='blkps'
;;
(*)
echo "Unsupported value for option -v." && exit 3
;;
esac
;;
('AIX')
case "$VALUE" in
('wa')
CAPTION="IO Wait"
VMSTAT_CMD='vmstat 1 '$DELAY"| tail -"$DELAY" |awk '{sum+=\$17} END {print sum}'"
UNIT='%'
;;
('in')
CAPTION="IO File page ins"
VMSTAT_CMD='vmstat -I 1 '$DELAY"| tail -"$DELAY" |awk '{sum+=\$6} END {print sum}'"
UNIT='pps'
;;
('out')
CAPTION="IO File page outs"
VMSTAT_CMD='vmstat -I 1 '$DELAY"| tail -"$DELAY" |awk '{sum+=\$7} END {print sum}'"
UNIT='pps'
;;
(*)
echo "Unsupported value for option -v." && exit 3
;;
esac
;;
esac
RETURN_VALUE=$(eval "$VMSTAT_CMD")
[ -z $RETURN_VALUE ] && RETURN_VALUE=0
[ $RETURN_VALUE -lt $(( $WARNING * $DELAY )) ] && { RETURN_CODE=0; }
[ $RETURN_VALUE -ge $(( $WARNING * $DELAY )) ] && { RETURN_CODE=1; }
[ $RETURN_VALUE -ge $(( $CRITICAL * $DELAY )) ] && { RETURN_CODE=2; }
REAL_VALUE=$(echo "scale=2;$RETURN_VALUE/$DELAY" |bc -l |awk '{printf "%.2f", $0}')
PERF_CAPTION=$(printf "%s" "$CAPTION" |sed -e 's/ /_/g')
PERFDATA="${PERF_CAPTION}=${REAL_VALUE}${UNIT};$WARNING;$CRITICAL"
case $RETURN_CODE in
(0)
RETURN_MESSAGE="${CAPTION}: ${REAL_VALUE} $UNIT ($DELAY)"
;;
(1)
RETURN_MESSAGE="${CAPTION}: ${REAL_VALUE} $UNIT ($DELAY)"
;;
(2)
RETURN_MESSAGE="${CAPTION}: ${REAL_VALUE} $UNIT ($DELAY)"
;;
esac
printf "%s\n" "${RETURN_MESSAGE}|$PERFDATA"
if $SILENT; then
exit 0
else
exit ${RETURN_CODE}
fi