clearer names for actions, and infer actions better
[monitor.git] / monitor / 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.database.info.model import FindbadNodeRecord
11
12 def network_config_to_str(net):
13
14         str = ""
15         static_keys = ['method', 'ip', 'gateway', 'network', 'broadcast', 'netmask', 'dns1', 'dns2', 'mac', 'is_primary']
16         for k in static_keys:
17                 str += "%15s == %s\n" % (k, net[k])
18
19         return str
20         
21
22 def main():
23
24         parser = parsermodule.getParser()
25         parser.set_defaults(nodelist=None,
26                                                 list=False,
27                                                 add=False,
28                                                 notng=False,
29                                                 delete=False,
30                                                 )
31         parser.add_option("", "--nodelist", dest="nodelist", metavar="list.txt", 
32                                                 help="Use all nodes in the given file for operation.")
33         parser = parsermodule.getParser(['defaults'], parser)
34         config = parsermodule.parse_args(parser)
35
36         # COLLECT nodegroups, nodes and node lists
37         for node in config.args:
38
39                 try:
40                         n = api.GetNodes(node)[0]
41                         #print n
42                         net = api.GetInterfaces(n['interface_ids'])[0]
43                         #print net
44
45                         node_keys = ['boot_state', 'key', 'last_updated', 'last_contact']
46                         for k in node_keys:
47                                 if 'last' in k:
48                                         #print "%15s == %s" % (k, diff_time(n[k]))
49                                         print "%15s == %s" % (k, 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                         #from monitor.common import email_exception
59                         print "Error with %s" % node
60                         #email_exception()
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                                 fbdata = FindbadNodeRecord.get_latest_by(hostname=node['hostname'])
72                                 print nodegroup_display(node, fbdata.to_dict())
73                                 i += 1
74
75                 elif config.add and config.nodegroup:
76                         for node in hostnames:
77                                 print "Adding %s to %s nodegroup" % (node, config.nodegroup)
78                                 api.AddNodeToNodeGroup(node, config.nodegroup)
79
80                 elif config.delete:
81                         for node in hostnames:
82                                 print "Deleting %s from %s nodegroup" % (node, config.nodegroup)
83                                 api.DeleteNodeFromNodeGroup(node, config.nodegroup)
84
85                 else:
86                         print "no other options supported."
87
88 if __name__ == "__main__":
89         try:
90                 main()
91         except IOError:
92                 pass