]> git.pld-linux.org Git - packages/nagios-plugin-check_domain.git/blob - check_domain.sh
v1.2.4: added .dk support, thanks Troels Hansen!
[packages/nagios-plugin-check_domain.git] / check_domain.sh
1 #!/bin/sh
2 # Nagios plugin for checking a domain name expiration date
3 #
4 # Copyright (c) 2005 Tomàs Núñez Lirola <tnunez@criptos.com>,
5 # 2009-2013 Elan Ruusamäe <glen@pld-linux.org>
6 #
7 # Licensed under GPL v2 License
8 # URL: http://git.pld-linux.org/?p=packages/nagios-plugin-check_domain.git;a=summary
9
10 PROGRAM=${0##*/}
11 PROGPATH=${0%/*}
12 . $PROGPATH/utils.sh
13
14 # Default values (days):
15 critical=7
16 warning=30
17
18 # Parse arguments
19 args=$(getopt -o hd:w:c:P: --long help,domain:,warning:,critical:,path: -u -n $PROGRAM -- "$@")
20 if [ $? != 0 ]; then
21         echo >&2 "$PROGRAM: Could not parse arguments"
22         echo "Usage: $PROGRAM -h | -d <domain> [-c <critical>] [-w <warning>]"
23         exit 1
24 fi
25 set -- $args
26
27 die() {
28         local rc=$1
29         local msg="$2"
30         echo "$msg"
31         exit $rc
32 }
33
34 fullusage() {
35         cat <<EOF
36 check_domain - v1.2.4
37 Copyright (c) 2005 Tomàs Núñez Lirola <tnunez@criptos.com>, 2009-2013 Elan Ruusamäe <glen@pld-linux.org>
38 under GPL License
39
40 This plugin checks the expiration date of a domain name.
41
42 Usage: $PROGRAM -h | -d <domain> [-c <critical>] [-w <warning>]
43 NOTE: -d must be specified
44
45 Options:
46 -h
47      Print detailed help
48 -d
49      Domain name to check
50 -w
51      Response time to result in warning status (days)
52 -c
53      Response time to result in critical status (days)
54
55 This plugin will use whois service to get the expiration date for the domain name.
56 Example:
57      $PROGRAM -d domain.tld -w 30 -c 10
58
59 EOF
60 }
61
62 # convert long month name to month number (Month Of Year)
63 month2moy() {
64         awk -vmonth="$1" 'BEGIN {
65                 split("January February March April May June July August Sepember October November December", months, " ");
66                 for (i in months) {
67                         Month[months[i]] = i;
68                 }
69                 print Month[month];
70         }'
71 }
72
73 # convert short month name to month number (Month Of Year)
74 mon2moy() {
75         awk -vmonth="$1" 'BEGIN {
76                 split("Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec", months, " ");
77                 for (i in months) {
78                         Month[months[i]] = i;
79                 }
80                 print Month[month];
81         }'
82 }
83
84 while :; do
85         case "$1" in
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;;
91                 --) shift; break;;
92                 *)  die $STATE_UNKNOWN "Internal error!";;
93         esac
94 done
95
96 if [ -z $domain ]; then
97         die $STATE_UNKNOWN "UNKNOWN - There is no domain name to check"
98 fi
99
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."
103         whois=whois
104 else
105         [ -x "$whoispath/whois" ] || die $STATE_UNKNOWN "UNKNOWN - Unable to find whois binary, you specified an incorrect path"
106         whois="$whoispath/whois"
107 fi
108
109 out=$($whois $domain)
110
111 [ -z "$out" ] && die $STATE_UNKNOWN "UNKNOWN - Domain $domain doesn't exist or no WHOIS server available."
112
113 # Calculate days until expiration
114 case "$domain" in
115 *.ru)
116         expiration=$(echo "$out" | awk '/paid-till:/{split($2, a, "."); printf("%s-%s-%s\n", a[1], a[2], a[3])}')
117         ;;
118 *.ee)
119         expiration=$(echo "$out" | awk '/expire:/{split($2, a, "."); printf("%s-%s-%s\n", a[3], a[2], a[1])}')
120         ;;
121 *.tv)
122         expiration=$(echo "$out" | awk -F: '/Expiration Date:/{print substr($0, length($1) + 3, 10)}')
123         ;;
124 *.ca)
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])}')
127         ;;
128 *.ie)
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"
133         ;;
134 *.dk)
135         # Expires: 2014-01-31
136         expiration=$(echo "$out" | awk '/Expires:/ {print $2}')
137         ;;
138 *.uk)
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"
143         ;;
144 *)
145         expiration=$(echo "$out" | awk -F: '/Expiration Date:/{print substr($0, length($1) + 2)}')
146         ;;
147 esac
148
149 [ -z "$expiration" ] && die $STATE_UNKNOWN "UNKNOWN - Unable to figure out expiration date for $domain Domain."
150
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))
156
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)."
161
162 # No alarms? Ok, everything is right.
163 echo "OK - Domain $domain will expire in $expdays days ($expdate)."
164 exit $STATE_OK
This page took 0.059638 seconds and 3 git commands to generate.