Changed 'import auth' statements to use plc.py or monitorconfig.py
[monitor.git] / nodeconfig.py
1 #!/usr/bin/python
2
3
4 import plc
5 api = plc.getAuthAPI()
6
7 from optparse import OptionParser
8 from sets import Set
9
10 from nodecommon import *
11 import database
12
13 def network_config_to_str(net):
14
15         str = ""
16         static_keys = ['method', 'ip', 'gateway', 'network', 'broadcast', 'netmask', 'dns1', 'dns2', 'mac', 'is_primary']
17         for k in static_keys:
18                 str += "%15s == %s\n" % (k, net[k])
19
20         return str
21         
22
23 def main():
24         from config import config
25         fb = database.dbLoad("findbad")
26
27         parser = OptionParser()
28         parser.set_defaults(nodelist=None,
29                                                 list=False,
30                                                 add=False,
31                                                 notng=False,
32                                                 delete=False,
33                                                 )
34         parser.add_option("", "--nodelist", dest="nodelist", metavar="list.txt", 
35                                                 help="Use all nodes in the given file for operation.")
36         config = config(parser)
37         config.parse_args()
38
39         # COLLECT nodegroups, nodes and node lists
40         for node in config.args:
41
42                 try:
43                         n = api.GetNodes(node)[0]
44                         #print n
45                         net = api.GetNodeNetworks(n['nodenetwork_ids'])[0]
46                         #print net
47
48                         node_keys = ['boot_state', 'key', 'last_updated', 'last_contact']
49                         for k in node_keys:
50                                 if 'last' in k:
51                                         print "%15s == %s" % (k, diff_time(n[k]))
52                                 else:
53                                         print "%15s == %s" % (k, n[k])
54
55                         print network_config_to_str(net)
56
57                         #for k in net.keys():
58                         #       print k, "==" , net[k]
59                 except:
60                         print "Error with %s" % node
61                         import traceback; print traceback.print_exc()
62                         pass
63
64         # commands:
65         if False:
66                 if config.list:
67                         print " ---- Nodes in the %s Node Group ----" % group_str
68                         i = 1
69                         for node in nodelist:
70                                 print "%-2d" % i, 
71                                 print nodegroup_display(node, fb)
72                                 i += 1
73
74                 elif config.add and config.nodegroup:
75                         for node in hostnames:
76                                 print "Adding %s to %s nodegroup" % (node, config.nodegroup)
77                                 api.AddNodeToNodeGroup(node, config.nodegroup)
78
79                 elif config.delete:
80                         for node in hostnames:
81                                 print "Deleting %s from %s nodegroup" % (node, config.nodegroup)
82                                 api.DeleteNodeFromNodeGroup(node, config.nodegroup)
83
84                 else:
85                         print "no other options supported."
86
87 if __name__ == "__main__":
88         try:
89                 main()
90         except IOError:
91                 pass