forked from rohit01/nrpe-plugins
-
Notifications
You must be signed in to change notification settings - Fork 2
/
traceroute_udp.sh
executable file
·338 lines (309 loc) · 10.3 KB
/
traceroute_udp.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
#!/bin/bash
#
# Nagios NRPE plugin for traceroute checks (max hop + delay)
#
AUTHOR="Rohit Gupta - @rohit01"
PROGNAME=`basename $0`
VERSION="Version 1.0,"
# nagios exit status variables
ST_OK=0
ST_WR=1
ST_CR=2
ST_UK=3
## Global variables
hop_count="0"
last_hop_delay="*"
avg_hop_delay="0"
star_hop_count="0"
exit_status="${ST_OK}"
reason_for_failure=""
print_version() {
echo "$PROGNAME: $VERSION $AUTHOR"
}
print_help() {
echo "$PROGNAME is a custom Nagios plugin to restart any init service"
echo "and ensure it is listening to the given TCP/UDP port."
echo ""
echo "Usage: $PROGNAME -s <service name> [-p <port no> -t <udp/tcp>]"
echo ""
echo "Options:"
echo " -H/--hostname)"
echo " Hostname for which MTR check needs to be executed (Mandatory)"
echo " -t/--timeout)"
echo " Timeout for traceroute command. Default: 10 (in sec)"
echo " -m/--maxhops)"
echo " maxhops to try in traceroute command. Default: 50"
echo " -w/--warning)"
echo " hop count warning level. Default: 15"
echo " -c/--critical)"
echo " hop count critical level. Default: 30"
echo " -a/--averagedelaywarning)"
echo " Average delay warning level. Default: 200 (in ms)"
echo " -A/--averagedelaycritical)"
echo " Average delay critical level. Default: 1000 (in ms)"
echo " -d/--lasthopdelaywarning)"
echo " Last hop delay warning level. Default: 300 (in ms)"
echo " -D/--lasthopdelaycritical)"
echo " Last hop delay critical level. Default: 2000 (in ms)"
echo " -h/--help)"
echo " Print this help message & exit"
echo " -v/--version)"
echo " Print version of this script & exit"
echo ""
echo "Examples:"
echo " $PROGNAME -H example.com -w 10 -c 20 -D 1000"
}
## Set defaults for optional arguments ##
timeout="1"
maxhops="50"
warning="15"
critical="30"
averagedelaywarning="200"
averagedelaycritical="1000"
lasthopdelaywarning="300"
lasthopdelaycritical="2000"
## parse arguments passed ##
while test -n "$1"; do
case "$1" in
--help|-h)
print_help
exit $ST_UK
;;
--version|-v)
print_version
exit $ST_UK
;;
--hostname|-H)
hostname=$2
shift
;;
--timeout|-t)
timeout=$2
shift
;;
--maxhops|-m)
maxhops=$2
shift
;;
--warning|-w)
warning=$2
shift
;;
--critical|-c)
critical=$2
shift
;;
--averagedelaywarning|-a)
averagedelaywarning=$2
shift
;;
--averagedelaycritical|-A)
averagedelaycritical=$2
shift
;;
--lasthopdelaywarning|-d)
lasthopdelaywarning=$2
shift
;;
--lasthopdelaycritical|-D)
lasthopdelaycritical=$2
shift
;;
*)
echo "Unknown argument: '$1'"
echo ""
print_help
exit $ST_UK
;;
esac
shift
done
######################### Validate arguments passed ##########################
if echo ${hostname} | grep -v -e "^[a-zA-Z][a-zA-Z0-9\.-]*$" \
-e "^[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}$" >/dev/null
then
echo "Invalid value: '${hostname}' for option -H/--hostname. Possible" \
" values: A valid FQDN or IPv4 address"
exit $ST_UK
fi
if echo ${timeout} | grep -v -e "^[0-9][0-9]*$" >/dev/null; then
echo "Invalid value: '${timeout}' for option -t/--timeout. Possible" \
" value: Positive integer"
exit $ST_UK
fi
if echo ${maxhops} | grep -v -e "^[0-9][0-9]*$" >/dev/null; then
echo "Invalid value: '${maxhops}' for option -m/--maxhops. Possible" \
" value: Positive integer"
exit $ST_UK
fi
if echo ${warning} | grep -v -e "^[0-9][0-9]*$" >/dev/null; then
echo "Invalid value: '${warning}' for option -w/--warning. Possible" \
" value: Positive integer"
exit $ST_UK
fi
if echo ${critical} | grep -v -e "^[0-9][0-9]*$" >/dev/null; then
echo "Invalid value: '${critical}' for option -c/--critical. Possible" \
" value: Positive integer"
exit $ST_UK
fi
if echo ${averagedelaywarning} | grep -v -e "^[0-9][0-9]*$" >/dev/null; then
echo "Invalid value: '${averagedelaywarning}' for option " \
"-a/--averagedelaywarning. Possible value: Positive integer"
exit $ST_UK
fi
if echo ${averagedelaycritical} | grep -v -e "^[0-9][0-9]*$" >/dev/null; then
echo "Invalid value: '${averagedelaycritical}' for option " \
"-A/--averagedelaycritical. Possible value: Positive integer"
exit $ST_UK
fi
if echo ${lasthopdelaywarning} | grep -v -e "^[0-9][0-9]*$" >/dev/null; then
echo "Invalid value: '${lasthopdelaywarning}' for option " \
"-d/--lasthopdelaywarning. Possible value: Positive integer"
exit $ST_UK
fi
if echo ${lasthopdelaycritical} | grep -v -e "^[0-9][0-9]*$" >/dev/null; then
echo "Invalid value: '${lasthopdelaycritical}' for option " \
"-D/--lasthopdelaycritical. Possible value: Positive integer"
exit $ST_UK
fi
##############################################################################
traceroute_command="traceroute -w ${timeout} -q 1 -m ${maxhops} -U ${hostname}"
TEMP_FILE="/tmp/._traceroute_${PROGNAME}_${hostname}_${RANDOM}.tmp"
check_exit_status() {
if [ $? -ne 0 ]; then
echo "CRITICAL - '${traceroute_command}' command failed"
rm -f $TEMP_FILE || true
exit $ST_CR
fi
}
execute_check() {
${traceroute_command} > ${TEMP_FILE}
check_exit_status
}
round_of() {
decimal_part=`echo ${round_of_variable} | awk -F \. '{print $2}'`
if echo $decimal_part | grep "^$" >/dev/null; then
decimal_part=0
fi
round_of_variable=`echo ${round_of_variable} | awk -F \. '{print $1}'`
if [ "$decimal_part" -ge 5 ]
then
round_of_variable=`expr ${round_of_variable} + 1`
fi
echo $round_of_variable
}
parse_collected_date() {
# Initialize variables
hop_count="0"
last_hop_delay="*"
avg_hop_delay="0"
star_hop_count="0"
# Find meaningful data
temp_delay_sum="0"
temp_ifs=${IFS}
IFS='N'
for i in $(cat ${TEMP_FILE} | tr -s " " | sed "s/^ *\(.*\) *$/\1/" \
| grep "^[0-9].*$" | tr "[:upper:]" "[:lower:]" | tr '\n' 'N');
do
hop_count=$(expr ${hop_count} + 1)
last_hop_delay=$(echo $i | cut -d" " -f 4 | grep -v "^\s*$" || echo '*')
if [ "X${last_hop_delay}" != "X*" ]; then
temp_delay_sum=$(bc <<EOF
scale=4
${temp_delay_sum} + ${last_hop_delay}
EOF
)
else
star_hop_count="$(expr ${star_hop_count} + 1)"
fi
done
IFS=${temp_ifs}
if [ "X${hop_count}" = "X${star_hop_count}" ]; then
avg_hop_delay="*"
else
avg_hop_delay=$(bc <<EOF
scale=2
${temp_delay_sum} / (${hop_count} - ${star_hop_count})
EOF
)
avg_hop_delay="$(round_of_variable=${avg_hop_delay}; round_of)"
fi
if [ "X${last_hop_delay}" != "X*" ]; then
last_hop_delay="$(round_of_variable=${last_hop_delay}; round_of)"
fi
}
calc_exit_status() {
# Hop count
if [ ${hop_count} -ge ${critical} ]; then
reason_for_failure="${reason_for_failure}Hop Count >= ${critical}(CR); "
exit_status="${ST_CR}"
elif [ ${hop_count} -ge ${warning} ]; then
reason_for_failure="${reason_for_failure}Hop Count >= ${warning}(WR); "
if [ "X${exit_status}" = "X${ST_OK}" ] || \
[ "X${exit_status}" = "X${ST_UK}" ]; then
exit_status="${ST_WR}"
fi
fi
# Last hop delay
if [ "X${last_hop_delay}" = "X*" ]; then
reason_for_failure="${reason_for_failure}Packet LOST(CR); "
exit_status="${ST_CR}"
elif [ ${last_hop_delay} -ge ${lasthopdelaycritical} ]; then
reason_for_failure="${reason_for_failure}Last hop delay >= ${lasthopdelaycritical}(CR); "
exit_status="${ST_CR}"
elif [ ${last_hop_delay} -ge ${lasthopdelaywarning} ]; then
reason_for_failure="${reason_for_failure}Last hop delay >= ${lasthopdelaywarning}(WR); "
if [ "X${exit_status}" = "X${ST_OK}" ] || \
[ "X${exit_status}" = "X${ST_UK}" ]; then
exit_status="${ST_WR}"
fi
fi
# Agerage hop delay
if [ "X${avg_hop_delay}" = "X" ]; then
true
elif [ "X${avg_hop_delay}" = "X*" ]; then
exit_status="${ST_CR}"
elif [ ${avg_hop_delay} -ge ${averagedelaycritical} ]; then
reason_for_failure="${reason_for_failure}Avg. hop delay >= ${averagedelaycritical}(CR); "
exit_status="${ST_CR}"
elif [ ${avg_hop_delay} -ge ${averagedelaywarning} ]; then
reason_for_failure="${reason_for_failure}Avg. hop delay >= ${averagedelaywarning}(WR); "
if [ "X${exit_status}" = "X${ST_OK}" ] || \
[ "X${exit_status}" = "X${ST_UK}" ]; then
exit_status="${ST_WR}"
fi
fi
reason_for_failure="$(echo "${reason_for_failure}" | sed 's/^ *\(.*\); *$/\1/')"
}
formatted_current_status() {
message="Hop count: ${hop_count}; * hops: ${star_hop_count}"
if [ "X${last_hop_delay}" != "X*" ]; then
message="${message}; Last hop delay: ${last_hop_delay} ms"
fi
if [ "X${avg_hop_delay}" != "X*" ]; then
message="${message}; Avg hop delay: ${avg_hop_delay} ms"
fi
echo "${message}"
}
##############################################################################
############################ EXECUTE THE TEST ################################
##############################################################################
execute_check
parse_collected_date
calc_exit_status
rm -f ${TEMP_FILE} || true
if [ "X${reason_for_failure}" != "X" ]; then
reason_for_failure=". Reason: ${reason_for_failure}"
fi
if [ "X${exit_status}" = "X${ST_OK}" ]; then
message="OK - "
elif [ "X${exit_status}" = "X${ST_WR}" ]; then
message="WARNING - "
elif [ "X${exit_status}" = "X${ST_CR}" ]; then
message="CRITICAL - "
else
message="UNKNOWN - "
fi
message="${message}$(formatted_current_status)${reason_for_failure}"
echo "${message}"
exit "${exit_status}"