diff --git a/nagios/check_dns_quickcheck_fullresolv.sh b/nagios/check_dns_quickcheck_fullresolv.sh new file mode 100755 index 0000000..6595e1b --- /dev/null +++ b/nagios/check_dns_quickcheck_fullresolv.sh @@ -0,0 +1,102 @@ +#!/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 </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" diff --git a/nagios/check_dns_quickcheck_fullresolv.sh.test b/nagios/check_dns_quickcheck_fullresolv.sh.test new file mode 100644 index 0000000..8a60c10 --- /dev/null +++ b/nagios/check_dns_quickcheck_fullresolv.sh.test @@ -0,0 +1,41 @@ +#!/bin/sh + +SCRIPTNAME="$( dirname $0)/check_dns_quickcheck_fullresolv.sh" + +TESTNAME="Test with no domain" +EXPECTED_OUTPUT='OK \(no domains tested\)' +EXPECTED_EXIT_STATUS=0 +OUTPUT="$( $SCRIPTNAME 2>&1 )" +if [ "$?" -ne "$EXPECTED_EXIT_STATUS" ]; then echo "ERROR: $TESTNAME exit code = $? (instead of $EXPECTED_EXIT_STATUS)"; exit; fi +if [ "$( echo "$OUTPUT" | egrep -c "$EXPECTED_OUTPUT" 2>&1 )" != "1" ]; then echo "ERROR: $TESTNAME outputs '$OUTPUT' (instead of '$EXPECTED_OUTPUT')"; exit 1; fi + +TESTNAME="Test with one existent domain" +EXPECTED_OUTPUT='^OK www\.example\.net|'t_www\.example\.net'=[0-9]+ms;;;0$' +EXPECTED_EXIT_STATUS=0 +OUTPUT="$( $SCRIPTNAME -d www.example.net 2>&1 )" +if [ "$?" -ne "$EXPECTED_EXIT_STATUS" ]; then echo "ERROR: $TESTNAME exit code = $? (instead of $EXPECTED_EXIT_STATUS)"; exit; fi +if [ "$( echo "$OUTPUT" | egrep -c "$EXPECTED_OUTPUT" 2>&1 )" != "1" ]; then echo "ERROR: $TESTNAME outputs '$OUTPUT' (instead of '$EXPECTED_OUTPUT')"; exit 1; fi + +TESTNAME="Test with one existent domain (ending with a '.')" +EXPECTED_OUTPUT='^OK www\.example\.net\.|'t_www\.example\.net'=[0-9]+ms;;;0$' +EXPECTED_EXIT_STATUS=0 +OUTPUT="$( $SCRIPTNAME -d www.example.net. 2>&1 )" +if [ "$?" -ne "$EXPECTED_EXIT_STATUS" ]; then echo "ERROR: $TESTNAME exit code = $? (instead of $EXPECTED_EXIT_STATUS)"; exit; fi +if [ "$( echo "$OUTPUT" | egrep -c "$EXPECTED_OUTPUT" 2>&1 )" != "1" ]; then echo "ERROR: $TESTNAME outputs '$OUTPUT' (instead of '$EXPECTED_OUTPUT')"; exit 1; fi + +TESTNAME="Test with two existent domains" +EXPECTED_OUTPUT='^OK www\.example\.net www\.example\.org|'t_www\.example\.net'=[0-9]+ms;;;0 't_www\.example\.org.'=[0-9]+ms;;;0$' +EXPECTED_EXIT_STATUS=0 +OUTPUT="$( $SCRIPTNAME -d www.example.net -d www.example.org. 2>&1 )" +if [ "$?" -ne "$EXPECTED_EXIT_STATUS" ]; then echo "ERROR: $TESTNAME exit code = $? (instead of $EXPECTED_EXIT_STATUS)"; exit; fi +if [ "$( echo "$OUTPUT" | egrep -c "$EXPECTED_OUTPUT" 2>&1 )" != "1" ]; then echo "ERROR: $TESTNAME outputs '$OUTPUT' (instead of '$EXPECTED_OUTPUT')"; exit 1; fi + +TESTNAME="Test with one existent and one inexistent domain" +EXPECTED_OUTPUT='^CRITICAL inexistent\.example\.net$' +EXPECTED_EXIT_STATUS=2 +OUTPUT="$( $SCRIPTNAME -d www.example.net -d inexistent.example.net 2>&1 )" +if [ "$?" -ne "$EXPECTED_EXIT_STATUS" ]; then echo "ERROR: $TESTNAME exit code = $? (instead of $EXPECTED_EXIT_STATUS)"; exit; fi +if [ "$( echo "$OUTPUT" | egrep -c "$EXPECTED_OUTPUT" 2>&1 )" != "1" ]; then echo "ERROR: $TESTNAME outputs '$OUTPUT' (instead of '$EXPECTED_OUTPUT')"; exit 1; fi + + +# TODO: test timeout