13 def get_success_ratio(measured):
14 return float(len(filter(lambda x: x > 0, measured)))/float(len(measured))
18 def timeit(*args, **kw):
20 result = method(*args, **kw)
23 #print '%r (%r, %r) %2.2f sec' % \
24 # (method.__name__, args, kw, te-ts)
25 return (result, te-ts)
30 def check_dns(ip, protocol='udp'):
33 ro = DNS.Request(name="www.yahoo.com", qtype="A", server=ip, timeout=45)
34 r = ro.req(protocol=protocol)
36 except DNS.Base.DNSError, e:
40 def get_nameserver_ips(filename):
41 ip_re = re.compile("\d+\.\d+\.\d+\.\d+")
43 if not os.path.exists(filename):
46 f = open(filename, 'r')
48 if 'resolv' in filename:
50 for field in l.strip().split():
51 if ip_re.match(field) and field not in ret:
54 if 'ifcfg' in filename:
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:
67 root_ips = get_nameserver_ips('/etc/resolv.conf')
68 slice_ips = get_nameserver_ips( '/vservers/princeton_comon/etc/resolv.conf')
70 context_list = ['root', 'slice']
71 proto_list = ['udp', 'tcp']
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]))
76 if set(root_ips.keys()) == set(slice_ips.keys()):
77 print "CONF-ROOT_SLICE-MATCH",
79 print "CONF-ROOT_SLICE-MISMATCH",
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)
86 print get_success_ratio(dns.get()),
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:]
99 if __name__ == "__main__":