-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcheck_udp_stats.sh
executable file
·354 lines (306 loc) · 10.7 KB
/
check_udp_stats.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
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#!/bin/sh
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
PROGNAME=`basename $0`
VERSION="Version 0.9.1,"
AUTHOR="2013, SvenVD, [email protected]"
TEMPFILE=/tmp/.prevudpstats$(id -nu)
print_version() {
echo "$VERSION $AUTHOR"
}
print_help() {
print_version $PROGNAME $VERSION
echo ""
echo "$PROGNAME is a Nagios plugin to monitor the UDP packets"
echo "It calculates the average per packet count per minute of recevied, error recevied, unknown received and sent"
echo ""
echo "$PROGNAME [-uw/--uwarning] [-uc/--ucritical][-ew/--ewarning] [-ec/--ecritical]"
echo ""
echo "Options:"
echo " --uwarning|-uw)"
echo " Sets a warning level for packets to unknown port received. Default is: 0"
echo " The unit is packets/min"
echo " --ucritical|-uc)"
echo " Sets a critical level for packets to unknown port received. Default is: 0"
echo " The unit is packets/min"
echo " --ewarning|-ew)"
echo " Sets a warning level for packet receive errors. Default is: 0"
echo " The unit is packets/min"
echo " --ecritical|-ec)"
echo " Sets a critical level for packet receive errors. Default is: 0"
echo " The unit is packets/min"
echo " --rwarning|-rw)"
echo " Sets an upper warning level for packet received. Default is: 0"
echo " The unit is packets/min"
echo " --rcritical|-rc)"
echo " Sets an upper critical level for packet received. Default is: 0"
echo " The unit is packets/min"
echo " --lrwarning|-lrw)"
echo " Sets a lower warning level for packet received. Default is: 0"
echo " The unit is packets/min"
echo " --lrcritical|-lrc)"
echo " Sets a lower critical level for packet received. Default is: 0"
echo " The unit is packets/min"
echo " --swarning|-sw)"
echo " Sets an upper warning level for packet sent. Default is: 0"
echo " The unit is packets/min"
echo " --scritical|-sc)"
echo " Sets an upper critical level for packet sent. Default is: 0"
echo " The unit is packets/min"
echo " --lswarning|-lsw)"
echo " Sets a lower warning level for packet sent. Default is: 0"
echo " The unit is packets/min"
echo " --lscritical|-lsc)"
echo " Sets a lower critical level for packet sent. Default is: 0"
echo " The unit is packets/min"
exit 3
}
while test -n "$1"; do
case "$1" in
--help|-h)
print_help
exit 3
;;
--version|-v)
print_version $PROGNAME $VERSION
exit 3
;;
--uwarning|-uw)
uwarn=$2
shift
;;
--ucritical|-uc)
ucrit=$2
shift
;;
--ewarning|-ew)
ewarn=$2
shift
;;
--ecritical|-ec)
ecrit=$2
shift
;;
--rwarning|-rw)
rwarn=$2
shift
;;
--rcritical|-rc)
rcrit=$2
shift
;;
--swarning|-sw)
swarn=$2
shift
;;
--scritical|-sc)
scrit=$2
shift
;;
--lrwarning|-lrw)
lrwarn=$2
shift
;;
--lrcritical|-lrc)
lrcrit=$2
shift
;;
--lswarning|-lsw)
lswarn=$2
shift
;;
--lscritical|-lsc)
lscrit=$2
shift
;;
*)
echo "Unknown argument: $1"
print_help
exit 3
;;
esac
shift
done
if [ -z $uwarn ];then uwarn=0; fi
if [ -z $ucrit ];then ucrit=0; fi
if [ -z $ewarn ];then ewarn=0; fi
if [ -z $ecrit ];then ecrit=0; fi
if [ -z $rwarn ];then rwarn=0; fi
if [ -z $rcrit ];then rcrit=0; fi
if [ -z $swarn ];then swarn=0; fi
if [ -z $scrit ];then scrit=0; fi
if [ -z $lrwarn ];then lrwarn=0; fi
if [ -z $lrcrit ];then lrcrit=0; fi
if [ -z $lswarn ];then lswarn=0; fi
if [ -z $lcrit ];then lscrit=0; fi
chk_OS_version(){
#Since output of netstat can change between OS and versions, we only allow this plugin to run on tested OS
cat /etc/redhat-release | egrep -q "CentOS release 6.*|Fedora release 17 \(Beefy Miracle\)"
if [ $? -ne 0 ];then
echo "UNKNOWN: $(cat /etc/redhat-release) is not a supported OS"
exit 3
fi
}
chk_OS_version
#Udp stats as seen on CentOS6
#Udp:
# 4328859 packets received
# 1177 packets to unknown port received.
# 0 packet receive errors
# 7022822 packets sent
#Get previous values
touch "$TEMPFILE"
. "$TEMPFILE"
#Get current values
CHECKCMD='netstat -us'
TIMESTAMP=$(date +%s)
PCKTREC=$($CHECKCMD| egrep "packets received" | sed -n 's/^[^0-9]*\([0-9]\+\).*/\1/p')
PCKTRECUNK=$($CHECKCMD| egrep "packets to unknown port received" | sed -n 's/^[^0-9]*\([0-9]\+\).*/\1/p')
PCKTRECERR=$($CHECKCMD| egrep "packet receive errors" | sed -n 's/^[^0-9]*\([0-9]\+\).*/\1/p' )
PCKTSENT=$($CHECKCMD| egrep "packets sent" | sed -n 's/^[^0-9]*\([0-9]\+\).*/\1/p' )
#Save for next check
echo "PREVTIMESTAMP=$TIMESTAMP" > "$TEMPFILE"
echo "PREVPCKTREC=$PCKTREC" >> "$TEMPFILE"
echo "PREVPCKTRECUNK=$PCKTRECUNK" >> "$TEMPFILE"
echo "PREVPCKTRECERR=$PCKTRECERR" >> "$TEMPFILE"
echo "PREVPCKTSENT=$PCKTSENT" >> "$TEMPFILE"
#On first run previous vars will be empty, substitute by currente value so diff will be 0 for all counters
if [ -z $PREVTIMESTAMP ];then PREVTIMESTAMP=$TIMESTAMP;fi
if [ -z $PREVPCKTREC ];then PREVPCKTREC=$PCKTREC;fi
if [ -z $PREVPCKTRECERR ];then PREVPCKTRECERR=$PCKTRECERR;fi
if [ -z $PREVPCKTRECUNK ];then PREVPCKTRECUNK=$PCKTRECUNK;fi
if [ -z $PREVPCKTSENT ];then PREVPCKTSENT=$PCKTSENT;fi
#Calculate differences and handle counter reset/rotation
TIMESTAMPDIFF=`expr $TIMESTAMP - $PREVTIMESTAMP`
if [ ! $PCKTREC = 0 ]
then
PCKTRECDIFF=`expr $PCKTREC - $PREVPCKTREC`
else
PCKTRECDIFF=0
fi
if [ ! $PCKTRECUNK = 0 ]
then
PCKTRECUNKDIFF=`expr $PCKTRECUNK - $PREVPCKTRECUNK`
else
PCKTRECUNKDIFF=0
fi
if [ ! $PCKTRECERR = 0 ]
then
PCKTRECERRDIFF=`expr $PCKTRECERR - $PREVPCKTRECERR`
else
PCKTRECERRDIFF=0
fi
if [ ! $PCKTSENT = 0 ]
then
PCKTSENTDIFF=`expr $PCKTSENT - $PREVPCKTSENT`
else
PCKTSENTDIFF=0
fi
#Calculate average/min
#(CUR-PREV)/(datediff/60)
if [ $TIMESTAMPDIFF -eq 0 ];then
AVGPCKTREC=0
AVGPCKTRECUNK=0
AVGPCKTRECERR=0
AVGPCKTSENT=0
else
AVGPCKTREC=$(echo "scale=2; $PCKTRECDIFF/($TIMESTAMPDIFF/60) "| bc -l)
AVGPCKTRECUNK=$(echo "scale=2; $PCKTRECUNKDIFF/($TIMESTAMPDIFF/60) "| bc -l)
AVGPCKTRECERR=$(echo "scale=2; $PCKTRECERRDIFF/($TIMESTAMPDIFF/60) "| bc -l)
AVGPCKTSENT=$(echo "scale=2; $PCKTSENTDIFF/($TIMESTAMPDIFF/60) "| bc -l)
fi
#Construct performance data output
PERFDATA="'avg_udp_received_pckts_min'=${AVGPCKTREC%%.*};$rwarn;$rcrit 'avg_udp_unknown_port_pckts_min'=${AVGPCKTRECUNK%%.*};$uwarn;$ucrit 'avg_udp_errors_pckts_min'=${AVGPCKTRECERR%%.*};$ewarn;$ecrit 'avg_udp_sent_pckts_min'=${AVGPCKTSENT%%.*};$swarn;$scrit"
#Alerting logic (since bash can not handle floating point comparisions, we use bc for the purpose)
#Check upper unknown count unknown critical
if [ $(echo "$AVGPCKTRECUNK > $ucrit" | bc -l) -eq 1 ];then
MSGNAGIOS="$MSGNAGIOS""$AVGPCKTRECUNK pckts/min unknown ports greater then threshold $ucrit:: "
EXITCODES="$EXITCODES"" 2"
else
if [ $(echo "$AVGPCKTRECUNK > $uwarn" | bc -l) -eq 1 ];then
#Check upper unknown count unknown warning
MSGNAGIOS="$MSGNAGIOS""$AVGPCKTRECUNK pckts/min unknown ports greater then threshold $uwarn:: "
EXITCODES="$EXITCODES"" 1"
fi
fi
#Check upper error count errors critical
if [ $(echo "$AVGPCKTRECERR > $ecrit" | bc -l) -eq 1 ];then
MSGNAGIOS="$MSGNAGIOS""$AVGPCKTRECERR pckts/min error greater then threshold $ecrit:: "
EXITCODES="$EXITCODES"" 2"
#Check upper error count errors warning
else
if [ $(echo "$AVGPCKTRECERR > $ewarn" | bc -l) -eq 1 ];then
MSGNAGIOS="$MSGNAGIOS""$AVGPCKTRECERR pckts/min error greater then threshold $ewarn:: "
EXITCODES="$EXITCODES"" 1"
fi
fi
#Check upper count received critical
if [ $(echo "$AVGPCKTREC > $rcrit" | bc -l) -eq 1 ];then
MSGNAGIOS="$MSGNAGIOS""$AVGPCKTREC pckts/min received greater then threshold $rcrit:: "
EXITCODES="$EXITCODES"" 2"
#Check upper count received warning
else
if [ $(echo "$AVGPCKTREC > $rwarn" | bc -l) -eq 1 ];then
MSGNAGIOS="$MSGNAGIOS""$AVGPCKTREC pckts/min received greater then threshold $rwarn:: "
EXITCODES="$EXITCODES"" 1"
fi
fi
#Check upper count sent critical
if [ $(echo "$AVGPCKTSENT > $scrit" | bc -l) -eq 1 ];then
MSGNAGIOS="$MSGNAGIOS""$AVGPCKTSENT pckts/min sent greater then threshold $scrit:: "
EXITCODES="$EXITCODES"" 2"
#Check upper count sent warning
else
if [ $(echo "$AVGPCKTSENT > $swarn" | bc -l) -eq 1 ];then
MSGNAGIOS="$MSGNAGIOS""$AVGPCKTSENT pckts/min sent greater then threshold $swarn:: "
EXITCODES="$EXITCODES"" 1"
fi
fi
##
#Check lower count received critical
if [ $(echo "$AVGPCKTREC < $lrcrit" | bc -l) -eq 1 ];then
MSGNAGIOS="$MSGNAGIOS""$AVGPCKTREC pckts/min received lower then threshold $lrcrit:: "
EXITCODES="$EXITCODES"" 2"
#Check lower count received warning
else
if [ $(echo "$AVGPCKTREC < $lrwarn" | bc -l) -eq 1 ];then
MSGNAGIOS="$MSGNAGIOS""$AVGPCKTREC pckts/min received lower then threshold $lrwarn:: "
EXITCODES="$EXITCODES"" 1"
fi
fi
#Check lower count sent critical
if [ $(echo "$AVGPCKTSENT < $lscrit" | bc -l) -eq 1 ];then
MSGNAGIOS="$MSGNAGIOS""$AVGPCKTSENT pckts/min sent lower then threshold $lscrit:: "
EXITCODES="$EXITCODES"" 2"
#Check lower count sent warning
else
if [ $(echo "$AVGPCKTSENT < $lswarn" | bc -l) -eq 1 ];then
MSGNAGIOS="$MSGNAGIOS""$AVGPCKTSENT pckts/min sent lower then threshold $lswarn:: "
EXITCODES="$EXITCODES"" 1"
fi
fi
####Generate nagiosoutput
if echo "$EXITCODES" | grep -q 2;then
echo "CRITICAL: $MSGNAGIOS|$PERFDATA"
exit 2;
elif echo "$EXITCODES" | grep -q 1;then
echo "WARNING: $MSGNAGIOS|$PERFDATA"
exit 1;
elif echo "$EXITCODES" | grep -q 3;then
echo "UNKNOWN: $MSGNAGIOS|$PERFDATA"
exit 3;
else
echo "OK: everything within thresholds | $PERFDATA"
exit 0
fi