Monitoring Domain Expiry

A check_mk local script for checking domain expiry. This script should work for all domain TLDs but let me know if you find any issues!

The way that I use this script, rather than it being called as a local check (I tried this way but you very quickly find yourself getting blocked from various whois servers!) is to set up a cron job to run this script once a day (eg 2am) and export to a text file – You can then use a Check_MK local check to ‘cat’ this file to confirm it has the most up to date version of this file.

Firstly, the script to pull the domain expiry status:

nano /scripts/check-domain-expiry

#!/bin/bash

DOMAINS=”domain1.com
domain2.eu
domain3.co.uk”

rm -rf /tmp/domain-expiry*

for i in $DOMAINS
do

### Pull TLD from Domain in i
t=$(echo $i | cut -d ‘.’ -f3 | tr ‘[A-Z]’ ‘[a-z]’)
if [ “$t” == “” ];
then
t=$(echo $i | cut -d ‘.’ -f2 | tr ‘[A-Z]’ ‘[a-z]’)
fi

### Whois Lookup done against whois server depending on TLD
if [ “$t” == “com” ];
then
w=”whois.verisign-grs.com”
elif [ “$t” == “uk” ];
then
w=”whois.nic.uk”
elif [ “$t” == “org” ];
then
w=”whois.pir.org”
elif [ “$t” == “biz” ];
then
w=”whois.biz”
elif [ “$t” == “org” ];
then
w=”whois.pir.org”
elif [ “$t” == “info” ];
then
w=”whois.afilias.info”
elif [ “$t” == “net” ];
then
w=”whois.verisign-grs.com”
elif [ “$t” == “website” ];
then
w=”whois.nic.website”
elif [ “$t” == “business” ];
then
w=”whois.donuts.co”
elif [ “$t” == “community” ];
then
w=”whois.donuts.co”
elif [ “$t” == “company” ];
then
w=”whois.donuts.co”
elif [ “$t” == “enterprises” ];
then
w=”whois.donuts.co”
elif [ “$t” == “gallery” ];
then
w=”whois.donuts.co”
elif [ “$t” == “co” ];
then
w=”whois.nic.co”
elif [ “$t” == “graphics” ];
then
w=”whois.donuts.co”
elif [ “$t” == “guru” ];
then
w=”whois.donuts.co”
elif [ “$t” == “media” ];
then
w=”whois.donuts.co”
elif [ “$t” == “online” ];
then
w=”whois.nic.online”
elif [ “$t” == “photography” ];
then
w=”whois.donuts.co”
elif [ “$t” == “photo” ];
then
w=”whois.uniregistry.net”
elif [ “$t” == “pictures” ];
then
w=”whois.donuts.co”
elif [ “$t” == “solutions” ];
then
w=”whois.donuts.co”
elif [ “$t” == “support” ];
then
w=”whois.donuts.co”
elif [ “$t” == “technology” ];
then
w=”whois.donuts.co”
elif [ “$t” == “tips” ];
then
w=”whois.donuts.co”
elif [ “$t” == “eu” ];
then
w=”whois.eu”
fi

d=$(whois -h $w $i | grep “Expir” | egrep -m 1 -o ‘[[:digit:]]{1,4}\-[[:digit:]]{1,2}\-[[:digit:]]{1,2}|[[:digit:]]{1,4}\-[[:alpha:]]{3}\-[[:digit:]]{1,2}|[[:alpha:]]{3} [[:alpha:]]{3} [[:digit:]]{2} [[:digit:]]{2}\:[[:digit:]]{2}\:[[:digit:]]{2} [[:alpha:]]{3} [[:digit:]]{4}’)
e=$(date +”%Y%m%d”)
f=$(date -d “$d” +”%Y%m%d”)

g=$(( ( $(date -ud $f +’%s’) – $(date -ud $e +’%s’) )/60/60/24 ))

if [ “$g” -gt “30” ]; then
s=0
st=”Domain $i has $g days left until Expiry (Expiry Date: $d)”
fi

if [ “$g” -lt “30” ]; then
s=1
st=”Domain $i has only $g days left until Expiry (Expiry Date: $d)”
fi

if [ “$g” -lt “15” ]; then
s=2
st=”Domain $i has only $g days left until Expiry.”
fi

if [ “$g” -lt “0” ]; then
s=2
st=”Domain $i has expired!”
fi

echo “$s Domain_$i count=$g;15;30;0; $st” >> /tmp/domain-expiry-$e.txt

done

chmod +x /scripts/check-domain-expiry

This script will go through each domain in the list at the top and pull the expiry date from the approrpiate whois server, before translating that into a common format and comparing it against the current date. Once it has that information it is able to output the information in the form of a Check_MK local check (Adjust crit / warning levels in the if statements at the bottom).

If you run this script when you have entered your domains, it should create a new .txt file in the /tmp/ directory with the date in it. We will now need to make a check_mk local check to cat this file so that it can be picked up by your check_mk server.

nano /usr/share/check-mk-agent/local/check-domain-check

#!/bin/bash

e=$(date +”%Y%m%d”)

cat /tmp/domain-expiry-$e.txt

chmod +x /usr/share/check-mk-agent/local/check-domain-check

If you now re-inventory your host in check_mk it should pull through a local check for each of your domains.

Any comments or questions? Get in touch here or Email me at [email protected]