#!/usr/bin/env python # Jul 17 2005 #GenerateHostInfo - Joey Hagedorn - hagedorn@mcs.anl.gov '''Generates hostinfo.xml at a regular interval''' __revision__ = '$Revision$' from ConfigParser import ConfigParser from lxml.etree import parse, tostring from os import fork, execl, dup2, wait, uname import sys if __name__ == '__main__': c = ConfigParser() c.read(['/etc/bcfg2.conf']) configpath = "%s/etc/report-configuration.xml" % c.get('server', 'repository') clientdatapath = "%s/Metadata/clients.xml" % c.get('server', 'repository') sendmailpath = c.get('statistics','sendmailpath') clientElement = parse(clientdatapath) hostlist = [client.get('name') for client in clientElement.findall("Client")] pids = {} fullnames = {} null = open('/dev/null', 'w+') #use uname to detect OS and use -t for darwin and -w for linux #/bin/ping on linux /sbin/ping on os x osname = uname()[0] 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()) if osname == 'Linux': execl('/bin/ping', 'ping', '-w', '5', '-c', '1', host) elif osname == 'Darwin': execl('/sbin/ping', 'ping', '-t', '5', '-c', '1', host) elif osname == 'SunOS': execl('/usr/sbin/ping', 'ping', host, '56', '1') else: #default execl('/bin/ping', 'ping', '-w', '5', '-c', '1', host) else: pids[pid] = host else: try: (cpid, status) = wait() except OSError: continue chost = pids[cpid] del pids[cpid] if status == 0: try: clientElement.xpath("//Client[@name='%s']"%chost)[0].set("pingable",'Y') except:#i think this is for a problem with aliases? clientElement.xpath("//Client[@name='%s']"%fullnames[chost])[0].set("pingable",'Y') #also set pingtime, if you can get it else: if chost.count('.') > 0: fullnames[chost.split('.')[0]] = chost hostlist.append(chost.split('.')[0]) else: clientElement.xpath("//Client[@name='%s']"%(fullnames[chost]))[0].set("pingable",'N') #also set pingtime if you can get it fout = open(clientdatapath, 'w') fout.write(tostring(clientElement.getroot())) fout.close()