forked from inuits/monitoring-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_srv_record.py
executable file
·83 lines (70 loc) · 3.06 KB
/
check_srv_record.py
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
#!/usr/bin/env python2
# Copyright (c) 2018 Lander Van den Bulcke <landervandenbulcke at gmail.com>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import dns.resolver
import socket
import argparse
import sys
nagios_codes = [ 'OK', 'WARNING', 'CRITICAL', 'UNKNOWN' ]
def exit_now(status=3, message='Something went wrong'):
print "%s - %s" % (nagios_codes[status], message)
sys.exit(status)
def socket_is_open(ip, port, timeout, retries):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(int(timeout))
res = False
for i in range(int(retries)):
try:
s.connect((str(ip), int(port)))
s.shutdown(socket.SHUT_RDWR)
res = True
break;
except:
continue
finally:
s.close()
return res
def resolve_a_record(query):
resolv = dns.resolver.Resolver()
return str(resolv.query(query, "A")[0])
def resolve_srv_record(query):
try:
results = []
resolv = dns.resolver.Resolver()
srvs = resolv.query(query, "SRV")
for srv in srvs:
results.append((srv.target.to_text(omit_final_dot=True), resolve_a_record(srv.target), int(srv.port)))
return results
except:
exit_now(2, 'DNS lookup failed')
parser = argparse.ArgumentParser()
parser.add_argument('-Q', '--query', metavar='<query>', help='DNS SRV record to query for', dest='query', default=None)
parser.add_argument('-t', '--timeout', metavar='<timeout>', help='Timeout to try to connect to socket', dest='timeout', default=10)
parser.add_argument('-r', '--retries', metavar='<retries>', help='Number of times to retry connecting to socket', dest='retries', default=3)
args = parser.parse_args()
hosts_up = ''
targets = resolve_srv_record(args.query)
for target in targets:
result = socket_is_open(target[1], target[2], args.timeout, args.retries)
if not result:
exit_now(2, "%s:%s is not reachable" % (target[0], target[2]))
else:
hosts_up += "%s:%s, " % (target[0], target[2])
exit_now(0, "%s are reachable" % hosts_up[:-2])