-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_arin
executable file
·166 lines (139 loc) · 4.88 KB
/
check_arin
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#!/usr/bin/python26
# -*- coding: iso-8859-15 -*-
# Nagios plugin for checking a domain name expiration date
#
# Copyright (c) 2013 Andrew Teixeira <[email protected]>
# Derived from original work by
# * Tomàs Núñez Lirola <[email protected]>
# * Elan Ruusamäe <[email protected]>
#
# Licensed under GPL v3 License
#
from datetime import date, datetime, timedelta
import getopt
import os
import re
import sys
import whois
#. $PROGPATH/utils.sh
VERSION = "1.0"
def usage():
print "Usage: %s -h | -d <domain> [-c <critical>] [-w <warning>]" % os.path.basename(__file__)
def fullusage():
u = [
"check_domain - v%s" % VERSION,
"Copyright (c) 2013 Andrew Teixeira",
"under GPL License\n",
"This plugin checks the expiration date of a domain name.\n",
"Usage: %s -h | -d <domain> [-c <critical>] [-w <warning>]" % os.path.basename(__file__),
"NOTE: -d must be specified\n",
"Options:",
"-h",
" Print detailed help",
"-d",
" Domain name to check",
"-w",
" Response time to result in warning status (days)",
"-c",
" Response time to result in critical status (days)\n",
"This plugin will use whois service to get the expiration date for the",
"domain name.\n",
"Example:",
" %s -d domain.tld -w 30 -c 10" % os.path.basename(__file__)
]
print "\n" . join(u)
def getStates():
state = {}
# Get state values from the util.sh file
u = os.path.dirname(os.path.abspath(__file__))
util = os.path.join(u, "utils.sh")
if os.path.isfile(util):
f = open(util, "r")
for l in f:
if not l:
continue
elif re.search("^STATE_", l):
s = l.split("=")
sn = s[0].split("_")
state[sn[1]] = int(s[1].rstrip())
else:
continue
else:
print "Cannot open util.sh file...exiting!"
sys.exit(10)
return state
def die(rc, msg):
print "%s" % msg
sys.exit(rc)
def main():
# Parse arguments
shortargs = "hd:w:c:P:"
longargs = [ "help", "domain=", "warning=", "critical", "path=" ]
# Option values
o_help = [ "-h", "--help" ]
o_crit = [ "-c", "--critical" ]
o_warn = [ "-w", "--warning" ]
o_domain = [ "-d", "--domain" ]
o_path = [ "-P", "--path" ]
# Default values:
CRITICAL = 7
WARNING = 30
DOMAIN = None
# Get the Nagios states from the util.sh file
state = getStates()
try:
opts, args = getopt.getopt(sys.argv[1:], shortargs, longargs)
except getopt.GetoptError as err:
# print help information and exit:
print str(err)
usage()
sys.exit(state["UNKNOWN"])
for o in opts:
if o[0] in o_help:
fullusage()
sys.exit(state["UNKNOWN"])
if o[0] in o_crit:
if is_int(o[1]):
CRITICAL = int(o[0])
else:
print "Invalid value for critical."
usage()
sys.exit(state["UNKNOWN"])
if o[0] in o_warn:
if is_int(o[1]):
WARNING = int(o[0])
else:
print "Invalid value for warning."
usage()
sys.exit(state["UNKNOWN"])
if o[0] in o_domain:
DOMAIN = o[1]
if not DOMAIN:
die(state["UNKNOWN"], "UNKNOWN - There is no domain name to check")
# Start looking up whois information
who = whois.whois(DOMAIN).query(False)
if not who[1]:
die(state["UNKNOWN"], "UNKNOWN - Domain %s doesn't exist or no WHOIS server available." % DOMAIN)
info = whois.Parser(DOMAIN, who[1]).parse()
if not "ExpirationDate" in info.keys():
die(state["UNKNOWN"], "UNKNOWN - Unable to figure out expiration date for %s Domain." % DOMAIN)
# Get today's timestamp
today = datetime.today()
# Convert expiration date found from whois
t = whois.convertDate(info["ExpirationDate"][0])
d = datetime.fromtimestamp(t)
# Figure out how many days are left for the domain
diff = d - today
# Trigger alarms if applicable
if diff.days < 0:
die(state["CRITICAL"], "CRITICAL - Domain %s expired on %s." % (DOMAIN, d.strftime("%Y-%m-%d")))
elif diff.days < CRITICAL:
die(state["CRITICAL"], "CRITICAL - Domain %s will expire in %s days (%s)." % (DOMAIN, diff.days, d.strftime("%Y-%m-%d")))
elif diff.days < WARNING:
die(state["WARNING"], "WARNING - Domain %s will expire in %s days (%s)." % (DOMAIN, diff.days, d.strftime("%Y-%m-%d")))
# No alarms? Ok, everything is right.
die(state["OK"], "OK - Domain %s will expire in %s days (%s)." % (DOMAIN, diff.days, d.strftime("%Y-%m-%d")))
# Standard boilerplate to call the main() function to begin the program.
if __name__ == "__main__":
ret = main()
sys.exit(ret)