Nagios Bash script to monitor Domain Expiration

This little script here, lets you use nagios to monitor domain name expiration and warn you ahead of time. You can also use it to just run some cron jobs, our extract the portion you need and use in other places :

#!/bin/bash #Author : Amit K Nepal domain=$1

if [ -z $1 ];then echo "Usage : $0 <domain name>" exit; fi WARN_DAYS=90 CRITICAL_DAYS=30 secondsToMonths=2592000 secondsToDays=86400 function getMonthAndDays() { seconds=$1 m=$((${seconds}/$secondsToMonths)) d=$(((${seconds}% (${secondsToMonths}))/${secondsToDays})) echo "$m Months and $d Days" }

 

expiration=`whois ${domain} | egrep -i 'Expiration|Expires on' | grep -oE '[0-9]{4}-[0-9]{2}-[0-9]{2}'` expirationSeconds=`date --date="${expiration}" +%s` today=`date +"%s"` diff=$(($expirationSeconds-$today)) DaysLeft=$(($diff/86400)) #convert to day message="`getMonthAndDays $diff`, Expires on $expiration" if [[ "$DaysLeft" -gt $CIRITCAL_DAYS ]];then echo "$message" exit 0; elif [[ "$DaysLeft" -lt $CRITICAL_DAYS ]];then echo "$message" exit 2; elif [[ "$DaysLeft" -lt $WARN_DAYS ]];then echo "$message" exit 1; else echo "Unable to check Domain Expiration" exit 3; fi