1
0
Fork 0
scripts-admin-quickndirty-p.../nagios/check_dns_quickcheck_fullresolv.sh

103 lines
2.2 KiB
Bash
Executable file

#!/bin/sh
# Quick and dirty script around dig +trace
# Stop on any uncaucht error
set -e
# Initialization
OUTPUT_EXIT_STATUS=0
OUTPUT_DETAIL_OK=""
OUTPUT_DETAIL_CRITICAL=""
OUTPUT_DETAIL_UNKNOWN=""
OUTPUT_PERFDATA=""
LOGGER=""
TIMEOUT=3
#
# Help function
#
usage() {
cat <<EOF
Usage:
$0 -d example.net
$0 -h (this help output)
EOF
}
if ! which dig >/dev/null 2>&1; then
echo "UNKNOWN 'dig' not found."
exit 3
fi
if [ -z "$LOGGER" ] && which logger >/dev/null 2>&1; then
LOGGER="logger"
fi
#
# Gestion des paramètres
#
while getopts hd: f; do
case "$f" in
'h')
usage
exit
;;
'd')
# Let's chronometer the resolution
CHRONO="$(( $( date +%s%N ) / 1000000 ))"
OUTPUT="$( dig +trace +all +timeout=$TIMEOUT "$OPTARG" )"
DIG_STATUS_CODE="$?"
CHRONO="$(( $( date +%s%N ) / 1000000 - $CHRONO ))"
# Store the perfdata for each domain
OUTPUT_PERFDATA="$OUTPUT_PERFDATA 't_$( echo $OPTARG | tr -cd 'a-zA-Z0-9. ' )'=$CHRONO""ms;;;0"
if [ "$DIG_STATUS_CODE" -ne "0" ]; then
# 'dig' output an error -> status unknown.
OUTPUT_DETAIL_UNKNOWN=" dig returned error code $DIG_STATUS_CODE"
OUTPUT_EXIT_STATUS=3
break # I don't feel like we should go on, should we ?
else
# Unfortunately, I couldn't get a proper way to know if the resolution
# was successful. So parsing the output it is...
# We're looking for a line like 'www.kernel.org. 3600 IN ...'
if ! echo "$OUTPUT" | egrep "^$OPTARG.?[[:space:]]+[0-9]+[[:space:]]+IN[[:space:]]+" >/dev/null 2>&1; then
[ -z "$LOGGER" ] || echo "$OUTPUT" | $LOGGER
OUTPUT_DETAIL_CRITICAL="$OUTPUT_DETAIL_CRITICAL $OPTARG"
OUTPUT_EXIT_STATUS=2
else
OUTPUT_DETAIL_OK="$OUTPUT_DETAIL_OK $OPTARG"
fi
fi
;;
\?)
usage
exit 1
;;
esac
done
case "$OUTPUT_EXIT_STATUS" in
'0')
if [ -z "$OUTPUT_DETAIL_OK" ]; then
OUTPUT_DETAIL_OK=" (no domains tested)"
fi
# Trick for the perfdata : convert the first space to the separator '|'
printf "OK%s%s\n" "$OUTPUT_DETAIL_OK" "$( echo "$OUTPUT_PERFDATA" | sed 's/^ /|/' )"
;;
'2')
printf "CRITICAL%s\n" "$OUTPUT_DETAIL_CRITICAL"
;;
'3')
printf "UNKOWN%s\n" "$OUTPUT_DETAIL_CRITICAL"
;;
esac
exit "$OUTPUT_EXIT_STATUS"