From 6524e283574659d46777d0b0ae05cfe7c808c861 Mon Sep 17 00:00:00 2001 From: Narayan Desai Date: Wed, 28 Sep 2005 20:22:43 +0000 Subject: fix fqdn matchups no stdout, please 2005/09/28 15:05:27-05:00 anl.gov!desai speed up 2005/09/28 14:26:59-05:00 anl.gov!desai fix looping (Logical change 1.326) git-svn-id: https://svn.mcs.anl.gov/repos/bcfg/trunk/bcfg2@1328 ce84e21b-d406-0410-9b95-82705330c041 --- src/sbin/GenerateHostInfo | 59 +++++++++++++++++++++++++---------------------- 1 file changed, 31 insertions(+), 28 deletions(-) (limited to 'src') diff --git a/src/sbin/GenerateHostInfo b/src/sbin/GenerateHostInfo index 547237bbc..d6fd69a25 100644 --- a/src/sbin/GenerateHostInfo +++ b/src/sbin/GenerateHostInfo @@ -7,25 +7,8 @@ __revision__ = '$Revision$' from ConfigParser import ConfigParser from elementtree.ElementTree import Element, SubElement, parse -from os import system -from socket import gethostbyname, gaierror - -def buildfqdncache(domain_list, host_list): - '''build dictionary of hostname to fqdns''' - fqdncache = {} - for nodename in host_list: - if nodename.count('.') > 0: - fqdncache[nodename] = nodename - fqdncache[nodename] = "" - for domain in domain_list: - try: - fqdn = "%s.%s" % (nodename, domain) - gethostbyname(fqdn) - fqdncache[nodename] = fqdn - break - except gaierror: - continue - return fqdncache +from os import system, fork, execl, dup2, wait +import sys def pretty_print(element, level=0): '''Produce a pretty-printed text representation of element''' @@ -55,17 +38,37 @@ if __name__ == '__main__': metaElement = parse(metadatapath) hostlist = [client.get('name') for client in 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') + pids = {} + fullnames = {} + null = open('/dev/null', 'w+') + while hostlist or pids: + if hostlist and len(pids.keys()) < 15: + host = hostlist.pop() + pid = fork() + if pid == 0: + # in child + dup2(null.fileno(), sys.__stdin__.fileno()) + dup2(null.fileno(), sys.__stdout__.fileno()) + dup2(null.fileno(), sys.__stderr__.fileno()) + execl('/bin/ping', 'ping', '-W', '5', '-c', '1', host) + else: + pids[pid] = host else: - record.set("pingable", 'Y') + try: + (cpid, status) = wait() + chost = pids[cpid] + del pids[cpid] + if status == 0: + SubElement(HostInfo, "HostInfo", name=chost, fqdn=chost, pingeable='Y') + else: + if chost.count('.') > 0: + fullnames[chost.split('.')[0]] = chost + hostlist.append(chost.split('.')[0]) + else: + SubElement(HostInfo, "HostInfo", name=fullnames[chost], fqdn=fullnames[chost], pingeable='N') + except: + pass fout = open(hostinfopath, 'w') fout.write(pretty_print(HostInfo)) -- cgit v1.2.3-1-g7c22