-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_dns_server.py
executable file
·155 lines (122 loc) · 3.75 KB
/
check_dns_server.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
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
#!/usr/bin/env python
"""
This is a simple Nagios plugin to query a DNS server.
Returns OK if the server replies.
"""
__author__ = 'jcrsilva'
__license__ = 'The Unlicense'
__version__ = '1.0.0'
import argparse
from dns.exception import Timeout
from dns.resolver import NXDOMAIN, Resolver
import nagiosplugin
class CheckDNS(nagiosplugin.Resource):
"""CheckDNS class."""
__dns_resolver = None
@property
def _dns_resolver(self):
if not self.__dns_resolver:
self.__dns_resolver = Resolver()
self.__dns_resolver.nameservers = [self.args.hostname]
self.__dns_resolver.port = self.args.port
self.__dns_resolver.timeout = self.args.timeout
self.__dns_resolver.lifetime = self.args.timeout
return self.__dns_resolver
def __init__(self, args):
"""
Class constructor.
:param args: command line arguments
"""
self.args = args
def probe(self):
"""
Nagios probe method.
:return: Metrics
"""
try:
answer = self._dns_resolver.query(self.args.query)
return [
nagiosplugin.Metric(
name=str(answer.rrset),
value=True,
context='dns'
)
]
except Timeout as e:
return [
nagiosplugin.Metric(
name=e.message,
value=False,
context='dns'
)
]
except NXDOMAIN as e:
return [
nagiosplugin.Metric(
name=e.message,
value=False,
context='dns'
)
]
except Exception as e:
raise nagiosplugin.CheckError(e.message)
class CheckDNSContext(nagiosplugin.Context):
"""CheckDNS context for nagiosplugin."""
def __init__(self):
"""Constructor."""
super(self.__class__, self).__init__('dns')
def evaluate(self, metric, resource):
"""
Evaluate metric result.
:param metric: Metric
:param resource: Resource
:return: Metric state
"""
if metric.value:
return self.result_cls(nagiosplugin.state.Ok, metric=metric)
else:
return self.result_cls(nagiosplugin.state.Critical, metric=metric)
def describe(self, metric):
"""
Describe the metric.
:param metric: Metric
:return: Metric description
"""
return metric.name
def get_args():
"""
Grab args from CLI.
:return: Argument dict
"""
argp = argparse.ArgumentParser(
description=__doc__
)
argp.add_argument('-H', '--hostname', action='store', required=True,
help='Hostname/IP of the server to query'
)
argp.add_argument('-P', '--port', action='store', required=False,
default=53, type=int,
help='Port to check DNS service on, default: 53'
)
argp.add_argument('-Q', '--query', action='store', required=False,
default='example.com',
help='Domain to query, default: example.com - If NXDOMAIN critical'
)
argp.add_argument('-T', '--timeout', action='store', required=False,
default=5, type=int,
help='Timeout for DNS query'
)
return argp.parse_args()
@nagiosplugin.guarded
def main():
"""
Main.
:return:
"""
args = get_args()
nagiosplugin.Check(
CheckDNS(args),
CheckDNSContext(),
).main()
if __name__ == '__main__':
main()