2 # Nagios plugin for checking a domain name expiration date
4 # Copyright (c) 2005 Tomàs Núñez Lirola <tnunez@criptos.com>,
5 # 2009-2013 Elan Ruusamäe <glen@pld-linux.org>
7 # Licensed under GPL v2 License
8 # URL: http://git.pld-linux.org/?p=packages/nagios-plugin-check_domain.git;a=summary
14 # Default values (days):
19 args=$(getopt -o hd:w:c:P: --long help,domain:,warning:,critical:,path: -u -n $PROGRAM -- "$@")
21 echo >&2 "$PROGRAM: Could not parse arguments"
22 echo "Usage: $PROGRAM -h | -d <domain> [-c <critical>] [-w <warning>]"
37 Copyright (c) 2005 Tomàs Núñez Lirola <tnunez@criptos.com>, 2009-2013 Elan Ruusamäe <glen@pld-linux.org>
40 This plugin checks the expiration date of a domain name.
42 Usage: $PROGRAM -h | -d <domain> [-c <critical>] [-w <warning>]
43 NOTE: -d must be specified
51 Response time to result in warning status (days)
53 Response time to result in critical status (days)
55 This plugin will use whois service to get the expiration date for the domain name.
57 $PROGRAM -d domain.tld -w 30 -c 10
62 # convert long month name to month number (Month Of Year)
64 awk -vmonth="$1" 'BEGIN {
65 split("January February March April May June July August Sepember October November December", months, " ");
73 # convert short month name to month number (Month Of Year)
75 awk -vmonth="$1" 'BEGIN {
76 split("Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec", months, " ");
86 -c|--critical) critical=$2; shift 2;;
87 -w|--warning) warning=$2; shift 2;;
88 -d|--domain) domain=$2; shift 2;;
89 -P|--path) whoispath=$2; shift 2;;
90 -h|--help) fullusage; exit;;
92 *) die $STATE_UNKNOWN "Internal error!";;
96 if [ -z $domain ]; then
97 die $STATE_UNKNOWN "UNKNOWN - There is no domain name to check"
100 # Looking for whois binary
101 if [ -z $whoispath ]; then
102 type whois > /dev/null 2>&1 || die $STATE_UNKNOWN "UNKNOWN - Unable to find whois binary in your path. Is it installed? Please specify path."
105 [ -x "$whoispath/whois" ] || die $STATE_UNKNOWN "UNKNOWN - Unable to find whois binary, you specified an incorrect path"
106 whois="$whoispath/whois"
109 out=$($whois $domain)
111 [ -z "$out" ] && die $STATE_UNKNOWN "UNKNOWN - Domain $domain doesn't exist or no WHOIS server available."
113 # Calculate days until expiration
116 expiration=$(echo "$out" | awk '/paid-till:/{split($2, a, "."); printf("%s-%s-%s\n", a[1], a[2], a[3])}')
119 expiration=$(echo "$out" | awk '/expire:/{split($2, a, "."); printf("%s-%s-%s\n", a[3], a[2], a[1])}')
122 expiration=$(echo "$out" | awk -F: '/Expiration Date:/{print substr($0, length($1) + 3, 10)}')
125 # Expiry date: 2017/07/16
126 expiration=$(echo "$out" | awk '/Expiry date:/{split($3, a, "/"); printf("%s-%s-%s\n", a[1], a[2], a[3])}')
129 # renewal: 31-March-2016
130 set -- $(echo "$out" | awk '/renewal:/{split($2, a, "-"); printf("%s %s %s\n", a[3], a[2], a[1])}')
131 set -- "$1" "$(month2moy $2)" "$3"
132 expiration="$1-$2-$3"
135 # Expires: 2014-01-31
136 expiration=$(echo "$out" | awk '/Expires:/ {print $2}')
139 # Expiry date: 05-Dec-2014
140 set -- $(echo "$out" | awk '/Expiry date:/{split($3, a, "-"); printf("%s %s %s\n", a[3], a[2], a[1])}')
141 set -- "$1" "$(mon2moy $2)" "$3"
142 expiration="$1-$2-$3"
145 expiration=$(echo "$out" | awk -F: '/Expiration Date:/{print substr($0, length($1) + 2)}')
149 [ -z "$expiration" ] && die $STATE_UNKNOWN "UNKNOWN - Unable to figure out expiration date for $domain Domain."
151 expseconds=$(date +%s --date="$expiration")
152 expdate=$(date +'%Y-%m-%d' --date="$expiration")
153 nowseconds=$(date +%s)
154 diffseconds=$((expseconds-nowseconds))
155 expdays=$((diffseconds/86400))
157 # Trigger alarms if applicable
158 [ $expdays -lt 0 ] && die $STATE_CRITICAL "CRITICAL - Domain $domain expired on $expiration"
159 [ $expdays -lt $critical ] && die $STATE_CRITICAL "CRITICAL - Domain $domain will expire in $expdays days ($expdate)."
160 [ $expdays -lt $warning ] && die $STATE_WARNING "WARNING - Domain $domain will expire in $expdays days ($expdate)."
162 # No alarms? Ok, everything is right.
163 echo "OK - Domain $domain will expire in $expdays days ($expdate)."