+

Last commit for check_domain: 8e391f7ee6fed2c8c9a25b655a89bb9df5842cee

Fix .org TLD check

Benjamin Renard [2017-07-28 21:51:36]
Fix .org TLD check
#!/bin/bash

WARN=5
CRIT=1
DOMAIN=$1
DEBUG=0
[ "$2" == "-d" ] && DEBUG=1

TLD="`echo $DOMAIN|sed 's/^.*\.\([^\.]*\)$/\1/g'`"
[ $DEBUG -eq 1 ] && echo "TLD : $TLD"

WHOIS=`whois $DOMAIN`
RET=$?

[ $DEBUG -eq 1 ] && echo "Whois return :" && echo -e "$WHOIS"

if [ $RET -ne 0 -a "$TLD" != "net" -a "$TLD" != "com" ]
then
	echo "UNKNOWN : Impossible to get expiration date."
	exit 3
fi

case $TLD in
	fr|re|pm|tf|yt|wf)
		EXTXT=`echo -e "$WHOIS"|egrep "^Expiry Date"|tail -n 1`
		[ $DEBUG -eq 1 ] && echo "Whois expire date : $EXTXT"

		EXDATE=`echo $EXTXT|sed 's/ *[^:]*: *\(.*\)$/\1/'`
		[ $DEBUG -eq 1 ] && echo "Expire date (in french format) : $EXDATE"

		D=$( echo $EXDATE|cut -d'/' -f 1 )
		M=$( echo $EXDATE|cut -d'/' -f 2 )
		Y=$( echo $EXDATE|cut -d'/' -f 3 )
		[ $DEBUG -eq 1 ] && echo "Parse expire date : Y=$Y / M=$M / D=$D"

		EXTIME=`date +%s -d"$Y-$M-$D"`
		[ $DEBUG -eq 1 ] && echo "Expire time : $EXTIME"
	;;
	be)
		STATUS="`echo -e "$WHOIS"|grep "^Status:"|sed 's/Status:[ \t]*//'`"
		[ $DEBUG -eq 1 ] && echo "Status : $STATUS"
		SINCE="`echo -e "$WHOIS"|grep "^Registered:"|sed 's/^Registered: *//'`"
		[ $DEBUG -eq 1 ] && echo "Since : $SINCE"
		if [ "$STATUS" != "REGISTERED" -a "$STATUS" != "NOT AVAILABLE" ]
		then
			echo "CRITICAL : $DOMAIN status is not REGISTERED or NOT AVAILABLE (value : $STATUS)"
			exit 2
		else
			echo "OK : $DOMAIN registered since $SINCE"
			exit 0
		fi
	;;
	es)
		echo "UNKNOWN : Dot ES TLD has no whois server. Check domain $DOMAIN is not possible."
		exit 3
	;;
	eu)
		if [ ! -n "$2" ]
		then
			echo "UNKNOWN : Dot EU TLD not provide expire date in WHOIS result. You can provide manually expiration date in second parameter."
			exit 3
		fi

		if [ $( echo -e "$WHOIS"|grep -c "AVAILABLE" ) -gt 0 ]
		then
			echo "CRITICAL - Domain $DOMAIN not registred !"
			exit 2
		fi

		EXDATE=$2
		EXTIME=$( date +%s --date=$2 )
		if [ $? -ne 0 ]
		then
			echo "UNKNOWN : Expire date provide not recognized. Ex : YYYYMMDD"
		fi
	;;
	*)
		EXTXT=`echo -e "$WHOIS"|grep "^expires:"|tail -n 1`
		[ $DEBUG -eq 1 ] && echo "expire result format 1 : $EXTXT"
		[ ! -n "$EXTXT" ] && EXTXT=`echo -e "$WHOIS"|egrep "([^:]*Expiration Date|Renewal date|Registry Expiry Date)"|head -n 1`
		[ $DEBUG -eq 1 ] && echo "expire result format 2 : $EXTXT"

		[ $DEBUG -eq 1 ] && echo "Whois expire date : $EXTXT"



		EXDATE=`echo $EXTXT|sed 's/ *[^:]*: *\(.*\)$/\1/'`
		[ $DEBUG -eq 1 ] && echo "Expire date : $EXDATE"
		EXTIME=`date +%s -d"$EXDATE"`
		[ $? -ne 0 ] && EXDATE=`echo "$EXDATE"|sed 's/Z$//'` && EXTIME=`date +%s -d"$EXDATE"`
		[ $DEBUG -eq 1 ] && echo "Expire time : $EXTIME"
esac

CURTIME=`date +%s`
[ $DEBUG -eq 1 ] && echo "Current time : $CURTIME"

let DIFF=EXTIME-CURTIME
[ $DEBUG -eq 1 ] && echo "Diff time : $DIFF"
let nb=DIFF/86400
[ $DEBUG -eq 1 ] && echo "Nb day(s) : $nb"

if [ "$nb" -le $CRIT ]
then
	if [ "$nb" -lt 0 ]
	then
		let nb=nb*-1
		echo "CRITICAL : $DOMAIN is expired since $nb day(s) !"
	else
		echo "CRITICAL : $DOMAIN expire in $nb day(s) !"
	fi
	exit 2
elif [ "$nb" -le $WARN ]
then
	echo "WARNING : $DOMAIN expire in $nb day(s) !"
	exit 1
else
	echo "OK : $DOMAIN expire in $nb day(s)."
	exit 0
fi
ViewGit