retire da-backup checks
[mirror/dsa-nagios.git] / dsa-nagios-checks / checks / dsa-check-resolver
1 #!/usr/bin/python
2
3 # Copyright 2011 Peter Palfrader
4 #
5 # Permission is hereby granted, free of charge, to any person obtaining
6 # a copy of this software and associated documentation files (the
7 # "Software"), to deal in the Software without restriction, including
8 # without limitation the rights to use, copy, modify, merge, publish,
9 # distribute, sublicense, and/or sell copies of the Software, and to
10 # permit persons to whom the Software is furnished to do so, subject to
11 # the following conditions:
12 #
13 # The above copyright notice and this permission notice shall be
14 # included in all copies or substantial portions of the Software.
15 #
16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
24 import datetime
25 import socket
26 import optparse
27 import sys
28
29 parser = optparse.OptionParser()
30 parser.set_usage("%prog <hostname> [<hostname> ..]")
31 parser.add_option("-t", "--timeout", dest="timeout", metavar="TIMEOUT",
32   type="int", default=500,
33   help="Resolve timeout in msecs [500].")
34 (options, args) = parser.parse_args()
35
36 if len(args) == 0:
37     parser.print_help()
38     sys.exit(4)
39
40 def check(hostname):
41     now = datetime.datetime.now()
42     try:
43         socket.gethostbyname(hostname)
44     except:
45         return None
46     then = datetime.datetime.now()
47
48     td = then - now
49     msecs = td.microseconds/1000 + (td.seconds + td.days * 24 * 3600) * 1000
50     return msecs
51
52 msg = []
53 ret = 0
54 oklist = []
55 slowlist = []
56 faillist = []
57 for hostname in args:
58     time = check(hostname)
59     if time is None:
60         faillist.append(hostname)
61         ret = 2
62     elif time > options.timeout:
63         slowlist.append(hostname)
64         ret = max(ret, 1)
65     else:
66         oklist.append(hostname)
67
68 if len(faillist) > 0: msg.append("FAILED: %s"%(', '.join(faillist)))
69 if len(slowlist) > 0: msg.append("SLOW: %s"%(', '.join(slowlist)))
70 if len(oklist) > 0: msg.append("OK: %s"%(', '.join(oklist)))
71
72 print '; '.join(msg)
73 sys.exit(ret)
74
75 # vim:set et:
76 # vim:set ts=4:
77 # vim:set shiftwidth=4: