3fe9a849e1594b317e4aaee712906d3537f631b9
[monitor.git] / nodeconfig.py
1 #!/usr/bin/python
2
3
4 from monitor.wrapper import plc
5 api = plc.getAuthAPI()
6
7 from monitor import parser as parsermodule
8 from sets import Set
9
10 from monitor.common import *
11 from monitor.database.info.model import FindbadNodeRecord
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
25         parser = parsermodule.getParser()
26         parser.set_defaults(nodelist=None,
27                                                 list=False,
28                                                 add=False,
29                                                 notng=False,
30                                                 delete=False,
31                                                 )
32         parser.add_option("", "--nodelist", dest="nodelist", metavar="list.txt", 
33                                                 help="Use all nodes in the given file for operation.")
34         parser = parsermodule.getParser(['defaults'], parser)
35         config = parsermodule.parse_args(parser)
36
37         # COLLECT nodegroups, nodes and node lists
38         for node in config.args:
39
40                 try:
41                         n = api.GetNodes(node)[0]
42                         #print n
43                         net = api.GetNodeNetworks(n['nodenetwork_ids'])[0]
44                         #print net
45
46                         node_keys = ['boot_state', 'key', 'last_updated', 'last_contact']
47                         for k in node_keys:
48                                 if 'last' in k:
49                                         print "%15s == %s" % (k, diff_time(n[k]))
50                                 else:
51                                         print "%15s == %s" % (k, n[k])
52
53                         print network_config_to_str(net)
54
55                         #for k in net.keys():
56                         #       print k, "==" , net[k]
57                 except:
58                         print "Error with %s" % node
59                         email_exception()
60                         import traceback; print traceback.print_exc()
61                         pass
62
63         # commands:
64         if False:
65                 if config.list:
66                         print " ---- Nodes in the %s Node Group ----" % group_str
67                         i = 1
68                         for node in nodelist:
69                                 print "%-2d" % i, 
70                                 fbdata = FindbadNodeRecord.get_latest_by(hostname=node['hostname'])
71                                 print nodegroup_display(node, fbdata.to_dict())
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