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
|
#!/usr/bin/env python
#GenerateHostInfo - Joey Hagedorn - hagedorn@mcs.anl.gov
'''Generates hostinfo.xml at a regular interval'''
__revision__ = '$Revision$'
from os import dup2, execl, fork, uname, wait
import lxml.etree, sys, time, ConfigParser
from Bcfg2.Settings import settings
if __name__ == '__main__':
# override default settings
if '-C' in sys.argv:
settings.read_config_file(sys.argv[sys.argv.index('-C') + 1])
clientdatapath = "%s/Metadata/clients.xml" % settings.SERVER_REPOSITORY
try:
clientElement = lxml.etree.parse(clientdatapath)
except:
print "Failed to parse '%s'" % clientdatapath
raise SystemExit, 1
hostlist = [client.get('name') for client in clientElement.findall("Client")]
pids = {}
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 in ['Darwin', 'FreeBSD']:
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]
elm = clientElement.xpath("//Client[@name='%s']"%chost)[0]
if status == 0:
elm.set("pingable",'Y')
elm.set("pingtime", str(time.time()))
else:
elm.set("pingable",'N')
fout = open(clientdatapath, 'w')
fout.write(lxml.etree.tostring(clientElement.getroot()))
fout.close()
|