summaryrefslogtreecommitdiffstats
path: root/src/sbin/bcfg2-query
blob: 3f0209192bd6e88ae3a23eccb1924aa88d14c765 (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
#!/usr/bin/python

import lxml.etree, sys, ConfigParser

CP = ConfigParser.ConfigParser()
CP.read(['/etc/bcfg2.conf'])
try:
    prefix = CP.get('server', 'repository')
except:
    prefix = "/var/lib/bcfg2"

if len(sys.argv) < 2:
    print "Usage bcfg2-query -d|u|p <profile name>"
    print "\t -d\t\t shows the clients that are currently down"
    print "\t -u\t\t shows the clients that are currently up"
    print "\t -c\t\t prints node names in a comma delimited list"
    print "\t -s\t\t prints node names in a space delimited list"
    print "\t -n\t\t prints node names in a newline delimited list (default)"
    print "\t -p <profile name>\t shows all the clients of that profile"
    print "\t -a\t shows all clients"
    sys.exit(1)

xml = lxml.etree.parse('%s/Metadata/clients.xml'%prefix)
if '-p' in sys.argv:
    profile = sys.argv[sys.argv.index('-p') + 1]
    clients = xml.xpath(".//Client[@profile='%s']" % (profile))
elif '-d' in sys.argv:
    clients = xml.xpath(".//Client[@pingable='N']")
elif '-u' in sys.argv:
    clients = xml.xpath(".//Client[@pingable='Y']")
elif '-a' in sys.argv:
    clients = xml.xpath(".//Client")

cnames = [client.get('name') for client in clients]
if '-c' in sys.argv:
    print ",".join(cnames)
elif '-s' in sys.argv:
    print " ".join(cnames)
else:
    print "\n".join(cnames)