Script to turn hostnames into their moira contacts
authorAlex Dehnert <adehnert@mit.edu>
Fri, 17 May 2013 05:45:46 +0000 (01:45 -0400)
committerAlex Dehnert <adehnert@mit.edu>
Fri, 17 May 2013 05:46:53 +0000 (01:46 -0400)
hosts-to-contacts [new file with mode: 0755]

diff --git a/hosts-to-contacts b/hosts-to-contacts
new file mode 100755 (executable)
index 0000000..3440a3e
--- /dev/null
@@ -0,0 +1,38 @@
+#!/usr/bin/python
+
+import collections
+import sys
+
+import moira
+
+def find_contact(hostname):
+    try:
+        result = moira.query('get_host', hostname.upper(), '*', '*', '*')
+        assert len(result) == 1
+        return result[0]['contact']
+    except moira.MoiraException, e:
+        print >>sys.stderr, "Got %s while processing %s" % (e, hostname, )
+        return "(err)"
+
+def find_contacts(hostnames):
+    contact_map = collections.defaultdict(list)
+    for hostname in hostnames:
+        contact = find_contact(hostname)
+        contact_map[contact].append(hostname)
+    return contact_map
+
+def print_contacts(contact_map):
+    sorted_map = sorted(contact_map.items(), key=lambda x: len(x[1]))
+    for contact, hosts in sorted_map:
+        print "%s\t%d\t%s" % (contact, len(hosts), hosts)
+
+def init():
+    moira.connect()
+
+if __name__ == '__main__':
+    hostnames = []
+    for line in sys.stdin:
+        hostnames.append(line.strip())
+    init()
+    contact_map = find_contacts(hostnames)
+    print_contacts(contact_map)