From 40b529c26ad00b87c3c96853a1f136d4b72d7c7b Mon Sep 17 00:00:00 2001 From: Alex Dehnert Date: Fri, 17 May 2013 01:45:46 -0400 Subject: [PATCH] Script to turn hostnames into their moira contacts --- hosts-to-contacts | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100755 hosts-to-contacts diff --git a/hosts-to-contacts b/hosts-to-contacts new file mode 100755 index 0000000..3440a3e --- /dev/null +++ b/hosts-to-contacts @@ -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) -- 2.34.1