www/database.php
[monitor.git] / nodegroups.py
1 #!/usr/bin/python
2
3 # This script is used to manipulate the operational state of nodes in
4 # different node groups.  These are basically set operations on nodes via the
5 # PLC api.
6
7 # Take the ng name as an argument....
8 # optionally, 
9 #  * restart them all.
10 #  * Set some or all in the set to rins.
11 #  * get a list of nodes in the Alpha nodegroup.
12
13 # Given a nodelist, it could tag each one with a nodegroup name.
14 #  * 
15
16 import plc
17 api = plc.getAuthAPI()
18
19 import parser as parsermodule
20 from sets import Set
21 from nodequery import verify,query_to_dict,node_select
22
23 from nodecommon import *
24 import database
25
26 def main():
27         fb = database.dbLoad("findbad")
28
29         parser = parsermodule.getParser(['nodesets'])
30         parser.set_defaults( list=True,
31                                                 add=False,
32                         nocolor=False,
33                                                 notng=False,
34                                                 delete=False,)
35
36         parser.add_option("", "--not", dest="notng", action="store_true", 
37                                                 help="All nodes NOT in nodegroup.")
38         parser.add_option("", "--nocolor", dest="nocolor", action="store_true", 
39                                                 help="Enable color")
40         parser.add_option("", "--list", dest="list", action="store_true", 
41                                                 help="List all nodes in the given nodegroup")
42         parser.add_option("", "--add", dest="add", action="store_true", 
43                                                 help="Add nodes to the given nodegroup")
44         parser.add_option("", "--delete", dest="delete", action="store_true", 
45                                                 help="Delete nodes from the given nodegroup")
46
47         parser = parsermodule.getParser(['defaults'], parser)
48         config = parsermodule.parse_args(parser)
49
50         # COLLECT nodegroups, nodes and node lists
51         if config.node or config.nodelist:
52                 if config.node: 
53                         hostlist = [ config.node ] 
54                 else: 
55                         hostlist = config.getListFromFile(config.nodelist)
56
57                 # NOTE: preserve order given in file.  Otherwise, return values are not in order
58                 # given to GetNodes
59                 nodelist = []
60                 for h in hostlist:
61                         nodelist += api.GetNodes(h)
62
63                 #nodelist = api.GetNodes(hostlist)
64                 group_str = "Given"
65
66         elif config.site:
67                 site = api.GetSites(config.site)
68                 if len (site) > 0:
69                         site = site[0]
70                         nodelist = api.GetNodes(site['node_ids'])
71                 else:
72                         nodelist = []
73
74                 group_str = config.site
75
76         elif config.nodeselect:
77                 hostlist = node_select(config.nodeselect)
78                 nodelist = api.GetNodes(hostlist)
79
80                 group_str = "selection"
81                 
82         else:
83                 ng = api.GetNodeGroups({'name' : config.nodegroup})
84                 nodelist = api.GetNodes(ng[0]['node_ids'])
85
86                 group_str = config.nodegroup
87
88         if config.notng:
89                 # Get nodegroup nodes
90                 ng_nodes = nodelist
91
92                 # Get all nodes
93                 all_nodes = api.GetNodes({'peer_id': None})
94                 
95                 # remove ngnodes from all node list
96                 ng_list = [ x['hostname'] for x in ng_nodes ]
97                 all_list = [ x['hostname'] for x in all_nodes ]
98                 not_ng_nodes = Set(all_list) - Set(ng_list)
99
100                 # keep each node that *is* in the not_ng_nodes set
101                 nodelist = filter(lambda x : x['hostname'] in not_ng_nodes, all_nodes)
102
103         hostnames = [ n['hostname'] for n in nodelist ]
104
105         # commands:
106
107         if config.add and config.nodegroup:
108                 for node in hostnames:
109                         print "Adding %s to %s nodegroup" % (node, config.nodegroup)
110                         api.AddNodeToNodeGroup(node, config.nodegroup)
111
112         elif config.delete:
113                 for node in hostnames:
114                         print "Deleting %s from %s nodegroup" % (node, config.nodegroup)
115                         api.DeleteNodeFromNodeGroup(node, config.nodegroup)
116
117         elif config.list:
118                 print " ---- Nodes in the %s Node Group ----" % group_str
119                 print "   Hostname                                   plc  obs     pcu     key         kernel                        last_contact, last change, comon uptime"
120                 i = 1
121                 for node in nodelist:
122                         print "%-2d" % i, 
123                         print nodegroup_display(node, fb, config)
124                         i += 1
125
126         else:
127                 print "no other options supported."
128
129 if __name__ == "__main__":
130         try:
131                 main()
132         except IOError:
133                 pass