From 7fb840f2b981c8c75158b76d2dd4b0af7635d681 Mon Sep 17 00:00:00 2001 From: Joey Hagedorn Date: Mon, 1 Aug 2005 14:57:24 +0000 Subject: Added Pretty Print 2005/07/19 17:21:35-05:00 anl.gov!joey write hostinfo.xml to disk 2005/07/19 16:15:08-05:00 anl.gov!joey Initial version of GenerateHostInfo, a file that should be run regularly to update a file that contains information about hosts, including FQDN index as well as pingability 2005/07/19 12:47:36-05:00 anl.gov!joey (Logical change 1.272) git-svn-id: https://svn.mcs.anl.gov/repos/bcfg/trunk/bcfg2@1104 ce84e21b-d406-0410-9b95-82705330c041 --- src/sbin/GenerateHostInfo | 73 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) (limited to 'src/sbin/GenerateHostInfo') diff --git a/src/sbin/GenerateHostInfo b/src/sbin/GenerateHostInfo index e69de29bb..96d44a974 100644 --- a/src/sbin/GenerateHostInfo +++ b/src/sbin/GenerateHostInfo @@ -0,0 +1,73 @@ +#!/usr/bin/env python +# Jul 17 2005 +#GenerateHostInfo - Joey Hagedorn - hagedorn@mcs.anl.gov + +__revision__ = '$Revision$' +'''Generates hostinfo.xml at a regular interval''' + +from ConfigParser import ConfigParser +from elementtree.ElementTree import * +from xml.parsers.expat import ExpatError +from os import system +from socket import gethostbyname, gethostbyaddr, gaierror + +def buildfqdncache(domain_list, host_list): + '''build dictionary of hostname to fqdns''' + fqdncache = {} + for nodename in host_list: + fqdncache[nodename] = "" + for domain in domain_list: + try: + fqdn = "%s.%s" % (nodename, domain) + ipaddr = gethostbyname(fqdn) + fqdncache[nodename] = fqdn + break + except gaierror: + continue + return fqdncache + +def pretty_print(element, level=0): + '''Produce a pretty-printed text representation of element''' + if element.text: + fmt = "%s<%%s %%s>%%s" % (level*" ") + data = (element.tag, (" ".join(["%s='%s'" % keyval for keyval in element.attrib.iteritems()])), + element.text, element.tag) + if element._children: + fmt = "%s<%%s %%s>\n" % (level*" ",) + (len(element._children) * "%s") + "%s\n" % (level*" ") + data = (element.tag, ) + (" ".join(["%s='%s'" % keyval for keyval in element.attrib.iteritems()]),) + data += tuple([pretty_print(entry, level+2) for entry in element._children]) + (element.tag, ) + else: + fmt = "%s<%%s %%s/>\n" % (level * " ") + data = (element.tag, " ".join(["%s='%s'" % keyval for keyval in element.attrib.iteritems()])) + return fmt % data + + +if __name__ == '__main__': + c = ConfigParser() + c.read(['/sandbox/hagedorn/bcfg2.conf']) + #hostinfopath = "%s/hostinfo.xml" % c.get('server', 'metadata') + hostinfopath = "/sandbox/hagedorn/hostinfo.xml" + metadatapath = "%s/metadata.xml" % c.get('server', 'metadata') + configpath = "%s/report-configuration.xml" % c.get('server', 'metadata') + domainlist = c.get('statistics', 'domainlist').replace("\'","").split(",") + sendmailpath = c.get('statistics','sendmailpath') + + metaElement = parse(metadatapath) + hostlist = map(lambda x:x.get("name"), metaElement.findall("Client")) + + fqdn_cache = buildfqdncache(domainlist, hostlist) + + + HostInfo = Element("HostInformation") + for host in hostlist: + record = SubElement(HostInfo, "HostInfo", + attrib={"name" : host,"fqdn" : fqdn_cache[host]}) + if system( 'ping -c 1 ' + fqdn_cache[host] + ' &>/dev/null') != 0: + record.set("pingable", 'N') + else: + record.set("pingable", 'Y') + + fout = open(hostinfopath, 'w') + fout.write(pretty_print(HostInfo)) + fout.close() + -- cgit v1.2.3-1-g7c22