68 | 68 |
# - Use arpping to detect another host with our same IP address
|
69 | 69 |
# - Check other TODOs inline in the code
|
70 | 70 |
|
|
71 |
while getopts ":hsv:" Option
|
|
72 |
do
|
|
73 |
case $Option in
|
|
74 |
h ) cat <<- EOF
|
|
75 |
Usage: $0 [-s][-v <num>]
|
|
76 |
|
|
77 |
-s Also log messages to local3 syslog facility
|
|
78 |
-v 0 Silent run
|
|
79 |
-v 1 Show only error messages
|
|
80 |
-v 2 Show error and warning messages
|
|
81 |
-v 3 Fully verbose (default)
|
|
82 |
|
|
83 |
EOF
|
|
84 |
exit 0;;
|
|
85 |
v ) VERB=$OPTARG;;
|
|
86 |
s ) LOG=1;;
|
|
87 |
esac
|
|
88 |
done
|
71 | 89 |
|
72 | 90 |
# BEGIN configuration
|
73 | 91 |
# Configure to your needs, these values will be used when
|
|
86 | 104 |
LC_ALL=C
|
87 | 105 |
export PATH LC_ALL
|
88 | 106 |
|
|
107 |
# error reporting and logging functions
|
|
108 |
info () {
|
|
109 |
[ "$VERB" -gt 2 ] && echo "INFO: $1"
|
|
110 |
[ "$VERB" -gt 2 ] && [ "$LOG" ] && logger -p local3.info "$0 INFO: $1"
|
|
111 |
}
|
|
112 |
|
|
113 |
warn () {
|
|
114 |
[ "$VERB" -gt 1 ] && echo "WARN: $1"
|
|
115 |
[ "$VERB" -gt 1 ] && [ "$LOG" ] && logger -p local3.warn "$0 WARN: $1"
|
|
116 |
}
|
|
117 |
|
|
118 |
err () {
|
|
119 |
[ "$VERB" -gt 0 ] && echo "ERR: $1" >&2
|
|
120 |
[ "$VERB" -gt 0 ] && [ "$LOG" ] && logger -p local3.err "$0 ERR: $1"
|
|
121 |
}
|
|
122 |
|
|
123 |
|
89 | 124 |
# Check if all commands we need are available
|
90 | 125 |
# NOTE: if using nslookup add "nslookup dnsutils"
|
91 | 126 |
( echo -e "netstat net-tools\nifconfig net-tools\n\
|
|
94 | 129 |
nc netcat" |
|
95 | 130 |
while read cmd package; do
|
96 | 131 |
if ! `which $cmd 2>/dev/null >&2`; then
|
97 | |
echo "$cmd is not available! (please install $package)" >&2
|
|
132 |
err "$cmd is not available! (please install $package)"
|
98 | 133 |
exit 1
|
99 | 134 |
fi
|
100 | 135 |
done ) || exit 1
|
|
102 | 137 |
( echo -e "ethtool ethtool" |
|
103 | 138 |
while read cmd package; do
|
104 | 139 |
if ! `which $cmd 2>/dev/null >&2`; then
|
105 | |
echo "$cmd is not available (consider installing $package)" >&2
|
|
140 |
warn "$cmd is not available (consider installing $package)"
|
106 | 141 |
exit 1
|
107 | 142 |
fi
|
108 | 143 |
done )
|
|
114 | 149 |
# Other needs
|
115 | 150 |
# We need /proc/net
|
116 | 151 |
if [ ! -d /proc/net ] ; then
|
117 | |
echo "/proc is not available! Please mount it ('mount -t /proc')" >&2
|
|
152 |
err "/proc is not available! Please mount it ('mount -t /proc')"
|
118 | 153 |
exit 1
|
119 | 154 |
fi
|
120 | 155 |
|
|
125 | 160 |
defaultroutes=`netstat -nr |grep ^0.0.0.0 | wc -l`
|
126 | 161 |
if [ -z "$defaultif" ] ; then
|
127 | 162 |
defaultif=none
|
128 | |
echo "WARN: This system does not have a default route"
|
|
163 |
warn "This system does not have a default route"
|
129 | 164 |
elif [ "$defaultroutes" -gt 1 ] ; then
|
130 | |
echo "WARN: This system has more than one default route"
|
|
165 |
warn "This system has more than one default route"
|
131 | 166 |
else
|
132 | |
echo "INFO: This system has exactly one default route"
|
|
167 |
info "This system has exactly one default route"
|
133 | 168 |
fi
|
134 | 169 |
|
135 | 170 |
|
|
143 | 178 |
# Check 127.0.0.1 instead (not everybody uses this IP address however,
|
144 | 179 |
# although its the one commonly used)
|
145 | 180 |
if ! check_host 127.0.0.1 1; then
|
146 | |
echo "ERR: Cannot ping localhost (127.0.0.1), loopback is broken in this system"
|
|
181 |
err "Cannot ping localhost (127.0.0.1), loopback is broken in this system"
|
147 | 182 |
else
|
148 | |
echo "ERR: Localhost is not answering but 127.0.0.1, check /etc/hosts and verify localhost points to 127.0.0.1"
|
|
183 |
err "Localhost is not answering but 127.0.0.1, check /etc/hosts and verify localhost points to 127.0.0.1"
|
149 | 184 |
fi
|
150 | 185 |
else
|
151 | |
echo "INFO: Loopback interface is working properly"
|
|
186 |
info "Loopback interface is working properly"
|
152 | 187 |
fi
|
153 | 188 |
|
154 | 189 |
else
|
155 | |
echo "ERR: There is no loopback interface in this system"
|
|
190 |
err "There is no loopback interface in this system"
|
156 | 191 |
status=1
|
157 | 192 |
fi
|
158 | 193 |
status=0
|
|
234 | 269 |
# Print results
|
235 | 270 |
if [ $status -ne 0 ] ; then
|
236 | 271 |
if [ "$ifname" = "$defaultif" ] ; then
|
237 | |
echo "ERR: The $ifname interface that is associated with your default route has no link!"
|
|
272 |
err "The $ifname interface that is associated with your default route has no link!"
|
238 | 273 |
else
|
239 | |
echo "WARN: Interface $ifname does not have link"
|
|
274 |
warn "Interface $ifname does not have link"
|
240 | 275 |
fi
|
241 | 276 |
fi
|
242 | 277 |
# Find IP addresses for $ifname
|
243 | 278 |
inetaddr=`ip addr show $ifname | grep inet | awk '{print $2}'`
|
244 | 279 |
if [ -z "$inetaddr" ] ; then
|
245 | |
echo "WARN: The $ifname interface does not have an IP address assigned"
|
|
280 |
warn "The $ifname interface does not have an IP address assigned"
|
246 | 281 |
status=1
|
247 | 282 |
else
|
248 | 283 |
# TODO: WARN if more than 2 IP addresses?
|
249 | 284 |
echo $inetaddr | while read ipaddr; do
|
250 | |
echo "INFO: The $ifname interface has IP address $ipaddr assigned"
|
|
285 |
info "The $ifname interface has IP address $ipaddr assigned"
|
251 | 286 |
done
|
252 | 287 |
fi
|
253 | 288 |
|
|
261 | 296 |
# TODO: Check also frames and collisions, to detect faulty cables
|
262 | 297 |
# or network devices (cheap hubs)
|
263 | 298 |
if [ "$txpkts" -eq 0 ] && [ "$rxpkts" -eq 0 ] ; then
|
264 | |
echo "ERR: The $ifname interface has not tx or rx any packets. Link down?"
|
|
299 |
err "The $ifname interface has not tx or rx any packets. Link down?"
|
265 | 300 |
status=1
|
266 | 301 |
elif [ "$txpkts" -eq 0 ]; then
|
267 | |
echo "WARN: The $ifname interface has not transmitted any packets."
|
|
302 |
warn "The $ifname interface has not transmitted any packets."
|
268 | 303 |
elif [ "$rxpkts" -eq 0 ] ; then
|
269 | |
echo "WARN: The $ifname interface has not received any packets."
|
270 | |
else
|
271 | |
echo "INFO: The $ifname interface has tx and rx packets."
|
|
304 |
warn "The $ifname interface has not received any packets."
|
|
305 |
else
|
|
306 |
info "The $ifname interface has tx and rx packets."
|
272 | 307 |
fi
|
273 | 308 |
# TODO: It should be best if there was a comparison with tx/rx packets.
|
274 | 309 |
# a few errors are not uncommon if the card has been running for a long
|
275 | 310 |
# time. It would be better if a relative comparison was done (i.e.
|
276 | 311 |
# less than 1% ok, more than 20% warning, over 80% major issue, etc.)
|
277 | 312 |
if [ "$txerrors" -ne 0 ]; then
|
278 | |
echo "WARN: The $ifname interface has tx errors."
|
|
313 |
warn "The $ifname interface has tx errors."
|
279 | 314 |
fi
|
280 | 315 |
if [ "$rxerrors" -ne 0 ]; then
|
281 | |
echo "WARN: The $ifname interface has rx errors."
|
|
316 |
warn "The $ifname interface has rx errors."
|
282 | 317 |
fi
|
283 | 318 |
return $status
|
284 | 319 |
}
|
|
293 | 328 |
# twice.
|
294 | 329 |
if [ -n "`echo $status | grep NO-CARRIER`" ] ; then
|
295 | 330 |
if [ "$ifname" = "$defaultif" ] ; then
|
296 | |
echo "ERR: The $ifname interface that is associated with your default route is down!"
|
|
331 |
err "The $ifname interface that is associated with your default route is down!"
|
297 | 332 |
status=1
|
298 | 333 |
elif [ "$ifname" = "lo" ] ; then
|
299 | |
echo "ERR: Your lo interface is down, this might cause issues with local applications (but not necessarily with network connectivity)"
|
|
334 |
err "Your lo interface is down, this might cause issues with local applications (but not necessarily with network connectivity)"
|
300 | 335 |
else
|
301 | |
echo "WARN: The $ifname interface is down"
|
|
336 |
warn "The $ifname interface is down"
|
302 | 337 |
fi
|
303 | 338 |
else
|
304 | 339 |
# Check network routes associated with this interface
|
305 | |
echo "INFO: The $ifname interface is up"
|
|
340 |
info "The $ifname interface is up"
|
306 | 341 |
check_if $ifname
|
307 | 342 |
check_netroute $ifname
|
308 | 343 |
fi
|
|
318 | 353 |
# For each gw that is not the default one, ping it
|
319 | 354 |
if [ "$gw" != "0.0.0.0" ] ; then
|
320 | 355 |
if ! check_router $gw ; then
|
321 | |
echo "ERR: The default route is not available since the default router is unreachable"
|
|
356 |
err "The default route is not available since the default router is unreachable"
|
322 | 357 |
fi
|
323 | 358 |
fi
|
324 | 359 |
done
|
|
333 | 368 |
# see if we have an arp. We use 5 packets since it is in our local network.
|
334 | 369 |
ping -n -q -c 5 "$router" >/dev/null 2>&1
|
335 | 370 |
if [ "$?" -ne 0 ]; then
|
336 | |
echo "WARN: Router $router does not answer to ICMP pings"
|
|
371 |
warn "Router $router does not answer to ICMP pings"
|
337 | 372 |
# Router does not answer, check arp
|
338 | 373 |
routerarp=`arp -n | grep "^$router" | grep -v incomplete`
|
339 | 374 |
if [ -z "$routerarp" ] ; then
|
340 | |
echo "ERR: We cannot retrieve a MAC address for router $router"
|
|
375 |
err "We cannot retrieve a MAC address for router $router"
|
341 | 376 |
status=1
|
342 | 377 |
fi
|
343 | 378 |
fi
|
344 | 379 |
if [ "$status" -eq 0 ] ; then
|
345 | |
echo "INFO: The router $router is reachable"
|
|
380 |
info "The router $router is reachable"
|
346 | 381 |
fi
|
347 | 382 |
return $status
|
348 | 383 |
}
|
|
362 | 397 |
status=0
|
363 | 398 |
ping -n -q -c $COUNT "$host" >/dev/null 2>&1
|
364 | 399 |
if [ "$?" -ne 0 ]; then
|
365 | |
echo "WARN: Host $host does not answer to ICMP pings"
|
366 | |
status=1
|
367 | |
else
|
368 | |
echo "INFO: Host $host answers to ICMP pings"
|
|
400 |
warn "Host $host does not answer to ICMP pings"
|
|
401 |
status=1
|
|
402 |
else
|
|
403 |
info "Host $host answers to ICMP pings"
|
369 | 404 |
fi
|
370 | 405 |
return $status
|
371 | 406 |
}
|
|
375 | 410 |
status=1
|
376 | 411 |
nsfound=0
|
377 | 412 |
nsok=0
|
378 | |
tempfile=`mktemp -t tmptestnet.XXXXXX` || { echo "ERR: Cannot create temporary file! Aborting! " >&2 ; exit 1; }
|
|
413 |
tempfile=`mktemp -t tmptestnet.XXXXXX` || { err "Cannot create temporary file! Aborting! " ; exit 1; }
|
379 | 414 |
trap " [ -f \"$tempfile\" ] && /bin/rm -f -- \"$tempfile\"" 0 1 2 3 13 15
|
380 | 415 |
cat /etc/resolv.conf | grep -v ^# | grep nameserver |
|
381 | 416 |
awk '/nameserver/ { for (i=2;i<=NF;i++) { print $i ; } }' >$tempfile
|
382 | 417 |
for nameserver in `cat $tempfile`; do
|
383 | 418 |
nsfound=$(( $nsfound + 1 ))
|
384 | |
echo "INFO: This system is configured to use nameserver $nameserver"
|
|
419 |
info "This system is configured to use nameserver $nameserver"
|
385 | 420 |
check_host $nameserver 5
|
386 | 421 |
if check_ns $nameserver ; then
|
387 | 422 |
nsok=$(( $nsok +1 ))
|
|
394 | 429 |
/bin/rm -f -- "$tempfile"
|
395 | 430 |
trap 0 1 2 3 13 15
|
396 | 431 |
if [ "$nsfound" -eq 0 ] ; then
|
397 | |
echo "ERR: The system does not have any nameserver configured"
|
|
432 |
err "The system does not have any nameserver configured"
|
398 | 433 |
else
|
399 | 434 |
if [ "$status" -ne 0 ] ; then
|
400 | 435 |
if [ "$nsfound" -eq 1 ] ; then
|
401 | |
echo -e "ERR: There is one nameserver configured for this system but it does not work properly"
|
|
436 |
err "There is one nameserver configured for this system but it does not work properly"
|
402 | 437 |
else
|
403 | |
echo "ERR: There are $nsfound nameservers configured for this system and none of them works properly"
|
|
438 |
err "There are $nsfound nameservers configured for this system and none of them works properly"
|
404 | 439 |
fi
|
405 | 440 |
else
|
406 | 441 |
if [ "$nsfound" -eq 1 ] ; then
|
407 | |
echo "INFO: The nameserver configured for this system works properly"
|
|
442 |
info "The nameserver configured for this system works properly"
|
408 | 443 |
else
|
409 | |
echo "INFO: There are $nsfound nameservers is configured for this system and $nsok are working properly"
|
|
444 |
info "There are $nsfound nameservers is configured for this system and $nsok are working properly"
|
410 | 445 |
fi
|
411 | 446 |
fi
|
412 | 447 |
fi
|
|
424 | 459 |
# Using dnscheck:
|
425 | 460 |
dnscheck=`host -t A $CHECK_HOST $nameserver 2>&1 | tail -1`
|
426 | 461 |
if [ -n "`echo $dnscheck |grep NXDOMAIN`" ] ; then
|
427 | |
echo "ERR: Dns server $nameserver does not resolv properly"
|
|
462 |
err "Dns server $nameserver does not resolv properly"
|
428 | 463 |
elif [ -n "`echo $dnscheck | grep \"timed out\"`" ] ; then
|
429 | |
echo "ERR: Dns server $nameserver is not available"
|
|
464 |
err "Dns server $nameserver is not available"
|
430 | 465 |
elif [ -z "`echo $dnscheck | egrep \"$CHECK_RESULT\"`" ] ; then
|
431 | |
echo "WARN: Dns server $nameserver did not return the expected result for $CHECK_HOST"
|
432 | |
else
|
433 | |
echo "INFO: Dns server $nameserver resolved correctly $CHECK_HOST"
|
|
466 |
warn "Dns server $nameserver did not return the expected result for $CHECK_HOST"
|
|
467 |
else
|
|
468 |
info "Dns server $nameserver resolved correctly $CHECK_HOST"
|
434 | 469 |
status=0
|
435 | 470 |
fi
|
436 | 471 |
|
437 | 472 |
# Using dlint
|
438 | 473 |
# dlint $CHECK_HOST @$nameserver >/dev/null 2>&1
|
439 | 474 |
# if [ $? -eq 2 ] ; then
|
440 | |
# echo "ERR: Dns server $nameserver does not resolv properly"
|
|
475 |
# err "Dns server $nameserver does not resolv properly"
|
441 | 476 |
# elif [ $? -ne 0 ]; then
|
442 | |
# echo "ERR: Unexpected error when testing $nameserver"
|
|
477 |
# err "Unexpected error when testing $nameserver"
|
443 | 478 |
# else
|
444 | |
# echo "INFO: Dns server $nameserver resolved correctly $CHECK_HOST"
|
|
479 |
# info "Dns server $nameserver resolved correctly $CHECK_HOST"
|
445 | 480 |
# status=0
|
446 | 481 |
# fi
|
447 | 482 |
|
|
451 | 486 |
check_conn () {
|
452 | 487 |
# Checks network connectivity
|
453 | 488 |
if ! check_host $CHECK_WEB_HOST >/dev/null ; then
|
454 | |
echo "WARN: System does not seem to reach Internet host $CHECK_WEB_HOST through ICMP"
|
455 | |
else
|
456 | |
echo "INFO: System can reach Internet host $CHECK_WEB_HOST"
|
|
489 |
warn "System does not seem to reach Internet host $CHECK_WEB_HOST through ICMP"
|
|
490 |
else
|
|
491 |
info "System can reach Internet host $CHECK_WEB_HOST"
|
457 | 492 |
fi
|
458 | 493 |
status=0
|
459 | 494 |
# Check web access, using nc
|
|
464 | 499 |
# fooled by transparent proxies)
|
465 | 500 |
echo -e "HEAD / HTTP/1.0\n\n" |nc -w 20 $CHECK_WEB_HOST $CHECK_WEB_PORT >/dev/null 2>&1
|
466 | 501 |
if [ $? -ne 0 ] ; then
|
467 | |
echo "ERR: Cannot access web server at Internet host $CHECK_WEB_HOST"
|
468 | |
status=1
|
469 | |
else
|
470 | |
echo "INFO: System can access web server at Internet host $CHECK_WEB_HOST"
|
|
502 |
err "Cannot access web server at Internet host $CHECK_WEB_HOST"
|
|
503 |
status=1
|
|
504 |
else
|
|
505 |
info "System can access web server at Internet host $CHECK_WEB_HOST"
|
471 | 506 |
fi
|
472 | 507 |
return $status
|
473 | 508 |
}
|