changes for 3.0
[monitor.git] / nodeconfig.py
1 #!/usr/bin/python
2
3
4 import plc
5 api = plc.getAuthAPI()
6
7 import parser as parsermodule
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         fb = database.dbLoad("findbad")
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.GetInterfaces(n['interface_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                         from nodecommon import email_exception
62                         email_exception()
63                         pass
64
65         # commands:
66         if False:
67                 if config.list:
68                         print " ---- Nodes in the %s Node Group ----" % group_str
69                         i = 1
70                         for node in nodelist:
71                                 print "%-2d" % i, 
72                                 print nodegroup_display(node, fb)
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