moved nodecommon.py into monitor module
[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 import database
12 from monitor.database import FindbadNodeRecord
13
14 def network_config_to_str(net):
15
16         str = ""
17         static_keys = ['method', 'ip', 'gateway', 'network', 'broadcast', 'netmask', 'dns1', 'dns2', 'mac', 'is_primary']
18         for k in static_keys:
19                 str += "%15s == %s\n" % (k, net[k])
20
21         return str
22         
23
24 def main():
25
26         parser = parsermodule.getParser()
27         parser.set_defaults(nodelist=None,
28                                                 list=False,
29                                                 add=False,
30                                                 notng=False,
31                                                 delete=False,
32                                                 )
33         parser.add_option("", "--nodelist", dest="nodelist", metavar="list.txt", 
34                                                 help="Use all nodes in the given file for operation.")
35         parser = parsermodule.getParser(['defaults'], parser)
36         config = parsermodule.parse_args(parser)
37
38         # COLLECT nodegroups, nodes and node lists
39         for node in config.args:
40
41                 try:
42                         n = api.GetNodes(node)[0]
43                         #print n
44                         net = api.GetNodeNetworks(n['nodenetwork_ids'])[0]
45                         #print net
46
47                         node_keys = ['boot_state', 'key', 'last_updated', 'last_contact']
48                         for k in node_keys:
49                                 if 'last' in k:
50                                         print "%15s == %s" % (k, diff_time(n[k]))
51                                 else:
52                                         print "%15s == %s" % (k, n[k])
53
54                         print network_config_to_str(net)
55
56                         #for k in net.keys():
57                         #       print k, "==" , net[k]
58                 except:
59                         print "Error with %s" % node
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