summaryrefslogtreecommitdiffstats
path: root/src/sbin/GenerateHostInfo
blob: e6d166877762d73ed3ec3c915ec99e6b929fc6af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/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 Element, SubElement, parse
from os import fork, execl, dup2, wait
import sys

def pretty_print(element, level=0):
    '''Produce a pretty-printed text representation of element'''
    if element.text:
        fmt = "%s<%%s %%s>%%s</%%s>" % (level*" ")
        data = (element.tag, (" ".join(["%s='%s'" % keyval for keyval in element.attrib.iteritems()])),
                element.text, element.tag)
    children = element.getchildren()
    if children:
        fmt = "%s<%%s %%s>\n" % (level*" ",) + (len(children) * "%s") + "%s</%%s>\n" % (level*" ")
        data = (element.tag, ) + (" ".join(["%s='%s'" % (key, element.attrib[key]) for key in element.attrib]),)
        data += tuple([pretty_print(entry, level+2) for entry in children]) + (element.tag, )
    else:
        fmt = "%s<%%s %%s/>\n" % (level * " ")
        data = (element.tag, " ".join(["%s='%s'" % (key, element.attrib[key]) for key in element.attrib]))
    return fmt % data

                                                                                                                
if __name__ == '__main__':
    c = ConfigParser()
    c.read(['/etc/bcfg2.conf'])
    configpath = "%s/etc/report-configuration.xml" % c.get('server', 'repository')
    #hostinfopath = "%s/etc/hostinfo.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")]

    #HostInfo = Element("HostInformation")
    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:
            try:
                (cpid, status) = wait()
                chost = pids[cpid]
                del pids[cpid]
                if status == 0:
                    #SubElement(HostInfo, "HostInfo", name=chost, fqdn=chost, pingable='Y')
                    clientElement.xpath("/Clients/Client[@name=%s]"%chost).set("pingable",'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], pingable='N')
                        clientElement.xpath("/Clients/Client[@name=%s]"%chost).set("pingable",'N')
            except:
                pass

    fout = open(clientdatapath, 'w')
    fout.write(pretty_print(clientElement))
    fout.close()