]>
Commit | Line | Data |
---|---|---|
7ffedf25 | 1 | #!/bin/sh |
0f39660c ER |
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 | ||
ae24cf76 | 10 | PROGRAM=${0##*/} |
7ffedf25 | 11 | PROGPATH=${0%/*} |
b476842b ER |
12 | . $PROGPATH/utils.sh |
13 | ||
14 | # Default values (days): | |
15 | critical=7 | |
16 | warning=30 | |
17 | ||
18 | # Parse arguments | |
ae24cf76 | 19 | args=$(getopt -o hd:w:c:P: --long help,domain:,warning:,critical:,path: -u -n $PROGRAM -- "$@") |
7ffedf25 | 20 | if [ $? != 0 ]; then |
b73b7254 | 21 | echo >&2 "$PROGRAM: Could not parse arguments" |
ae24cf76 | 22 | echo "Usage: $PROGRAM -h | -d <domain> [-c <critical>] [-w <warning>]" |
7ffedf25 ER |
23 | exit 1 |
24 | fi | |
b476842b ER |
25 | set -- $args |
26 | ||
7ffedf25 ER |
27 | die() { |
28 | local rc=$1 | |
3661484b ER |
29 | local msg="$2" |
30 | echo "$msg" | |
7ffedf25 ER |
31 | exit $rc |
32 | } | |
33 | ||
b73b7254 ER |
34 | fullusage() { |
35 | cat <<EOF | |
a012b111 | 36 | check_domain - v1.2.4 |
3661484b | 37 | Copyright (c) 2005 Tomàs Núñez Lirola <tnunez@criptos.com>, 2009-2013 Elan Ruusamäe <glen@pld-linux.org> |
b73b7254 ER |
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 | ||
acbdd30d ER |
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 | ||
6e141f7a ER |
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 | ||
7ffedf25 ER |
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;; | |
b73b7254 | 90 | -h|--help) fullusage; exit;; |
7ffedf25 ER |
91 | --) shift; break;; |
92 | *) die $STATE_UNKNOWN "Internal error!";; | |
93 | esac | |
b476842b ER |
94 | done |
95 | ||
7ffedf25 ER |
96 | if [ -z $domain ]; then |
97 | die $STATE_UNKNOWN "UNKNOWN - There is no domain name to check" | |
98 | fi | |
b476842b ER |
99 | |
100 | # Looking for whois binary | |
101 | if [ -z $whoispath ]; then | |
ae24cf76 | 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." |
a51e1bde | 103 | whois=whois |
b476842b | 104 | else |
7ffedf25 | 105 | [ -x "$whoispath/whois" ] || die $STATE_UNKNOWN "UNKNOWN - Unable to find whois binary, you specified an incorrect path" |
a51e1bde | 106 | whois="$whoispath/whois" |
b476842b ER |
107 | fi |
108 | ||
a51e1bde ER |
109 | out=$($whois $domain) |
110 | ||
6e141f7a ER |
111 | [ -z "$out" ] && die $STATE_UNKNOWN "UNKNOWN - Domain $domain doesn't exist or no WHOIS server available." |
112 | ||
b476842b | 113 | # Calculate days until expiration |
0cf44c7c ER |
114 | case "$domain" in |
115 | *.ru) | |
a51e1bde | 116 | expiration=$(echo "$out" | awk '/paid-till:/{split($2, a, "."); printf("%s-%s-%s\n", a[1], a[2], a[3])}') |
0cf44c7c | 117 | ;; |
e59ac1f5 | 118 | *.ee) |
a51e1bde | 119 | expiration=$(echo "$out" | awk '/expire:/{split($2, a, "."); printf("%s-%s-%s\n", a[3], a[2], a[1])}') |
e59ac1f5 | 120 | ;; |
6267dde5 ER |
121 | *.tv) |
122 | expiration=$(echo "$out" | awk -F: '/Expiration Date:/{print substr($0, length($1) + 3, 10)}') | |
123 | ;; | |
20464939 ER |
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 | ;; | |
acbdd30d ER |
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 | ;; | |
a012b111 ER |
134 | *.dk) |
135 | # Expires: 2014-01-31 | |
136 | expiration=$(echo "$out" | awk '/Expires:/ {print $2}') | |
137 | ;; | |
6e141f7a ER |
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 | ;; | |
0cf44c7c | 144 | *) |
a51e1bde | 145 | expiration=$(echo "$out" | awk -F: '/Expiration Date:/{print substr($0, length($1) + 2)}') |
0cf44c7c ER |
146 | ;; |
147 | esac | |
e6876751 | 148 | |
6e141f7a | 149 | [ -z "$expiration" ] && die $STATE_UNKNOWN "UNKNOWN - Unable to figure out expiration date for $domain Domain." |
ae24cf76 | 150 | |
7ffedf25 | 151 | expseconds=$(date +%s --date="$expiration") |
0cf44c7c | 152 | expdate=$(date +'%Y-%m-%d' --date="$expiration") |
7ffedf25 ER |
153 | nowseconds=$(date +%s) |
154 | diffseconds=$((expseconds-nowseconds)) | |
b476842b ER |
155 | expdays=$((diffseconds/86400)) |
156 | ||
157 | # Trigger alarms if applicable | |
22a17815 ER |
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)." | |
b476842b ER |
161 | |
162 | # No alarms? Ok, everything is right. | |
22a17815 | 163 | echo "OK - Domain $domain will expire in $expdays days ($expdate)." |
b476842b | 164 | exit $STATE_OK |