Various updates; Add logAxis patch for graphite; Add namespaces to summary scripts.
[myops.git] / web / collect / client / check_dns_new.py
1 #!/usr/bin/python
2
3 import commands
4 import os
5 import re
6 import socket
7 import struct
8 import DNS
9 import time
10 from history import *
11
12
13 def get_success_ratio(measured):
14     return float(len(filter(lambda x: x > 0, measured)))/float(len(measured))
15
16 def timed(method):
17
18     def timeit(*args, **kw):
19         ts = time.time()
20         result = method(*args, **kw)
21         te = time.time()
22
23         #print '%r (%r, %r) %2.2f sec' % \
24         #      (method.__name__, args, kw, te-ts)
25         return (result, te-ts)
26
27     return timeit
28
29 @timed
30 def check_dns(ip, protocol='udp'):
31     try:
32         #ip = ip[:-1] + "0"
33         ro = DNS.Request(name="www.yahoo.com", qtype="A", server=ip, timeout=45)
34         r = ro.req(protocol=protocol)
35         r = "OK"
36     except DNS.Base.DNSError, e:
37         r = "Error: %s" % e
38     return r
39         
40 def get_nameserver_ips(filename):
41     ip_re = re.compile("\d+\.\d+\.\d+\.\d+")
42     ret = {}
43     if not os.path.exists(filename):
44         return ret
45
46     f = open(filename, 'r')
47
48     if 'resolv' in filename:
49         for l in f:
50             for field in l.strip().split():
51                 if ip_re.match(field) and field not in ret:
52                     ret[field] = 0
53
54     if 'ifcfg' in filename:
55         for l in f:
56             if 'DNS' not in l:
57                 continue
58             for field in l.strip().split('='):
59                 field = field.replace('"', '')
60                 field = field.replace("'", '')
61                 if ip_re.match(field) and field not in ret:
62                     ret[field] = 0
63     return ret
64
65 def main():
66
67     root_ips  = get_nameserver_ips('/etc/resolv.conf')
68     slice_ips = get_nameserver_ips( '/vservers/princeton_comon/etc/resolv.conf')
69
70     context_list = ['root', 'slice']
71     proto_list = ['udp', 'tcp']
72
73     sequence_list  = zip(sorted(context_list*2), zip(root_ips.keys(),sorted(proto_list*2)[::-1]))
74     sequence_list += zip(sorted(context_list*2), zip(slice_ips.keys(),sorted(proto_list*2)[::-1]))
75
76     if set(root_ips.keys()) == set(slice_ips.keys()):
77         print "CONF-ROOT_SLICE-MATCH",
78     else:
79         print "CONF-ROOT_SLICE-MISMATCH",
80
81     for i,(context,(ip,proto)) in enumerate(sequence_list):
82         (s,t) = check_dns(ip, proto)
83         if "Error" in s: t = -1
84         dns = HistoryFile("dns_history_%s_%s%s.dat" % (context, proto, i), DNSHistory)
85         dns.append(t)
86         print get_success_ratio(dns.get()),
87         dns.close()
88     
89     c_dns = os.popen("curl -s http://localhost:3121 | grep -a DNSFail").read().strip()
90     if len(c_dns) > 9 and "DNS" in c_dns:
91         c_dns = "cm " + c_dns[9:]
92     else:
93         c_dns = ""
94     print c_dns,
95
96     print ""
97
98
99 if __name__ == "__main__":
100     main()
101