#!/bin/sh # Little custom script to check expiration date of DNS domains # Default values #RANGE_WARNING=":2592000" # 30 days #RANGE_CRITICAL=":691200" # 8 days THRESHOLD_WARNING="2592000" # 30 days THRESHOLD_CRITICAL="691200" # 8 days TIMESTAMP_NOW="$( date +%s )" # Output OUTPUT_EXIT_STATUS=0 OUTPUT_DETAIL_WARNING="" OUTPUT_DETAIL_CRITICAL="" OUTPUT_PERFDATA="" PROGPATH=$( echo $0 | sed -e 's,[\\/][^\\/][^\\/]*$,,' ) REVISION="0.1" # Stop at the first non-catched error set -e # Include check_range() . $PROGPATH/utils.sh # No real need for this script but then you have to copy the status # Not really clean... #STATE_OK=0 #STATE_WARNING=1 #STATE_CRITICAL=2 #STATE_UNKNOWN=3 #STATE_DEPENDENT=4 # # Help function # usage() { cat </dev/null 2>&1; then echo "UNKNOWN: 'whois' command not found." exit 1 fi # # Parameters management # while [ "$#" -gt 0 ]; do while getopts hw:c: f; do case "$f" in 'h') usage exit ;; 'w') THRESHOLD_WARNING="$( convert_to_seconds "$OPTARG" )" ;; 'c') THRESHOLD_CRITICAL="$( convert_to_seconds "$OPTARG" )" ;; \?) usage exit 1 ;; esac done shift $( expr $OPTIND - 1 ) # Little checks if ! is_int "$THRESHOLD_WARNING" || ! is_int "$THRESHOLD_CRITICAL"; then echo "UNKNOWN invalid parameter : one of the threshold is not an integer." exit $STATE_UNKNOWN fi # End of the options, we get the domain name if [ -z "$1" ]; then # No more options but no domain specified ? Weird but well... break fi DOMAIN="$1" shift # get the expiration date for this domain EXPIRATION_DATE="$( whois "$DOMAIN" | extract_date_from_whois )" if [ -z "$EXPIRATION_DATE" ]; then # We couldn't get the date :-( if [ "$OUTPUT_EXIT_STATUS" -eq "$STATE_OK" ]; then OUTPUT_EXIT_STATUS="$STATE_UNKNOWN" fi OUTPUT_DETAIL_UNKNOWN="$OUTPUT_DETAIL_UNKNOWN $DOMAIN:could_not_get_date" break fi # Dispatch in the OK/Warning/Critical boxes OUTPUT_DOMAIN_DETAIL="$DOMAIN:$( date --date=@$EXPIRATION_DATE +%FT%T%z)" if [ "$EXPIRATION_DATE" -le "$( expr "$TIMESTAMP_NOW" + "$THRESHOLD_CRITICAL" )" ]; then # Domain is critical OUTPUT_EXIT_STATUS="$STATE_CRITICAL" OUTPUT_DETAIL_CRITICAL="$OUTPUT_DETAIL_CRITICAL $OUTPUT_DOMAIN_DETAIL" elif [ "$EXPIRATION_DATE" -le "$( expr "$TIMESTAMP_NOW" + "$THRESHOLD_WARNING" )" ]; then # Domain is warning OUTPUT_DETAIL_WARNING="$OUTPUT_DETAIL_WARNING $OUTPUT_DOMAIN_DETAIL" # we don't change if the status is already Critical # (but we take precedence over Unknown) if [ "$OUTPUT_EXIT_STATUS" -ne "$STATE_CRITICAL" ]; then OUTPUT_EXIT_STATUS="$STATE_WARNING" fi else # Domain is Ok OUTPUT_DETAIL_OK="$OUTPUT_DETAIL_OK $OUTPUT_DOMAIN_DETAIL" fi done # final output case "$OUTPUT_EXIT_STATUS" in "$STATE_OK") echo "OK $OUTPUT_DETAIL_OK" ;; "$STATE_WARNING") echo "WARNING $OUTPUT_DETAIL_WARNING" ;; "$STATE_CRITICAL") echo "CRITICAL $OUTPUT_DETAIL_CRITICAL" ;; "$STATE_UNKNOWN") echo "UNKNOWN $OUTPUT_DETAIL_UNKNOWN" ;; *) echo "WTF" ;; esac exit "$OUTPUT_EXIT_STATUS"