Various updates; Add logAxis patch for graphite; Add namespaces to summary scripts.
[myops.git] / web / collect / client / check_dns_new.py
diff --git a/web/collect/client/check_dns_new.py b/web/collect/client/check_dns_new.py
new file mode 100755 (executable)
index 0000000..64ebb9c
--- /dev/null
@@ -0,0 +1,101 @@
+#!/usr/bin/python
+
+import commands
+import os
+import re
+import socket
+import struct
+import DNS
+import time
+from history import *
+
+
+def get_success_ratio(measured):
+    return float(len(filter(lambda x: x > 0, measured)))/float(len(measured))
+
+def timed(method):
+
+    def timeit(*args, **kw):
+        ts = time.time()
+        result = method(*args, **kw)
+        te = time.time()
+
+        #print '%r (%r, %r) %2.2f sec' % \
+        #      (method.__name__, args, kw, te-ts)
+        return (result, te-ts)
+
+    return timeit
+
+@timed
+def check_dns(ip, protocol='udp'):
+    try:
+        #ip = ip[:-1] + "0"
+        ro = DNS.Request(name="www.yahoo.com", qtype="A", server=ip, timeout=45)
+        r = ro.req(protocol=protocol)
+        r = "OK"
+    except DNS.Base.DNSError, e:
+        r = "Error: %s" % e
+    return r
+        
+def get_nameserver_ips(filename):
+    ip_re = re.compile("\d+\.\d+\.\d+\.\d+")
+    ret = {}
+    if not os.path.exists(filename):
+        return ret
+
+    f = open(filename, 'r')
+
+    if 'resolv' in filename:
+        for l in f:
+            for field in l.strip().split():
+                if ip_re.match(field) and field not in ret:
+                    ret[field] = 0
+
+    if 'ifcfg' in filename:
+        for l in f:
+            if 'DNS' not in l:
+                continue
+            for field in l.strip().split('='):
+                field = field.replace('"', '')
+                field = field.replace("'", '')
+                if ip_re.match(field) and field not in ret:
+                    ret[field] = 0
+    return ret
+
+def main():
+
+    root_ips  = get_nameserver_ips('/etc/resolv.conf')
+    slice_ips = get_nameserver_ips( '/vservers/princeton_comon/etc/resolv.conf')
+
+    context_list = ['root', 'slice']
+    proto_list = ['udp', 'tcp']
+
+    sequence_list  = zip(sorted(context_list*2), zip(root_ips.keys(),sorted(proto_list*2)[::-1]))
+    sequence_list += zip(sorted(context_list*2), zip(slice_ips.keys(),sorted(proto_list*2)[::-1]))
+
+    if set(root_ips.keys()) == set(slice_ips.keys()):
+        print "CONF-ROOT_SLICE-MATCH",
+    else:
+        print "CONF-ROOT_SLICE-MISMATCH",
+
+    for i,(context,(ip,proto)) in enumerate(sequence_list):
+        (s,t) = check_dns(ip, proto)
+        if "Error" in s: t = -1
+        dns = HistoryFile("dns_history_%s_%s%s.dat" % (context, proto, i), DNSHistory)
+        dns.append(t)
+        print get_success_ratio(dns.get()),
+        dns.close()
+    
+    c_dns = os.popen("curl -s http://localhost:3121 | grep -a DNSFail").read().strip()
+    if len(c_dns) > 9 and "DNS" in c_dns:
+        c_dns = "cm " + c_dns[9:]
+    else:
+        c_dns = ""
+    print c_dns,
+
+    print ""
+
+
+if __name__ == "__main__":
+    main()
+