AM nagios/plc2nagios.py
[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 import auth
18 api = plc.PLC(auth.auth, auth.plc)
19
20 from optparse import OptionParser
21 from sets import Set
22 from nodequery import verify,query_to_dict,node_select
23
24 from nodecommon import *
25 import soltesz
26
27 def main():
28         from config import config
29         fb = soltesz.dbLoad("findbad")
30
31         parser = OptionParser()
32         parser.set_defaults(nodegroup="Alpha",
33                                                 node=None,
34                                                 nodelist=None,
35                                                 list=True,
36                                                 add=False,
37                         nocolor=False,
38                                                 notng=False,
39                                                 delete=False,
40                                                 nodeselect=None,
41                                                 )
42         parser.add_option("", "--not", dest="notng", action="store_true", 
43                                                 help="All nodes NOT in nodegroup.")
44         parser.add_option("", "--nodegroup", dest="nodegroup", metavar="NodegroupName",
45                                                 help="Specify a nodegroup to perform actions on")
46         parser.add_option("", "--nodeselect", dest="nodeselect", metavar="querystring",
47                                                 help="Specify a query to perform on findbad db")
48         parser.add_option("", "--site", dest="site", metavar="site name",
49                                                 help="Specify a site to view node status")
50
51         parser.add_option("", "--nocolor", dest="nocolor", action="store_true", 
52                                                 help="Enable color")
53         parser.add_option("", "--list", dest="list", action="store_true", 
54                                                 help="List all nodes in the given nodegroup")
55         parser.add_option("", "--add", dest="add", action="store_true", 
56                                                 help="Add nodes to the given nodegroup")
57         parser.add_option("", "--delete", dest="delete", action="store_true", 
58                                                 help="Delete nodes from the given nodegroup")
59         parser.add_option("", "--node", dest="node", metavar="nodename.edu", 
60                                                 help="A single node name to add to the nodegroup")
61         parser.add_option("", "--nodelist", dest="nodelist", metavar="list.txt", 
62                                                 help="Use all nodes in the given file for operation.")
63         config = config(parser)
64         config.parse_args()
65
66         # COLLECT nodegroups, nodes and node lists
67         if config.node or config.nodelist:
68                 if config.node: 
69                         hostlist = [ config.node ] 
70                 else: 
71                         hostlist = config.getListFromFile(config.nodelist)
72
73                 # NOTE: preserve order given in file.  Otherwise, return values are not in order
74                 # given to GetNodes
75                 nodelist = []
76                 for h in hostlist:
77                         nodelist += api.GetNodes(h)
78
79                 #nodelist = api.GetNodes(hostlist)
80                 group_str = "Given"
81
82         elif config.site:
83                 site = api.GetSites(config.site)
84                 if len (site) > 0:
85                         site = site[0]
86                         nodelist = api.GetNodes(site['node_ids'])
87                 else:
88                         nodelist = []
89
90                 group_str = config.site
91
92         elif config.nodeselect:
93                 hostlist = node_select(config.nodeselect)
94                 nodelist = api.GetNodes(hostlist)
95
96                 group_str = "selection"
97                 
98         else:
99                 ng = api.GetNodeGroups({'name' : config.nodegroup})
100                 nodelist = api.GetNodes(ng[0]['node_ids'])
101
102                 group_str = config.nodegroup
103
104         if config.notng:
105                 # Get nodegroup nodes
106                 ng_nodes = nodelist
107
108                 # Get all nodes
109                 all_nodes = api.GetNodes({'peer_id': None})
110                 
111                 # remove ngnodes from all node list
112                 ng_list = [ x['hostname'] for x in ng_nodes ]
113                 all_list = [ x['hostname'] for x in all_nodes ]
114                 not_ng_nodes = Set(all_list) - Set(ng_list)
115
116                 # keep each node that *is* in the not_ng_nodes set
117                 nodelist = filter(lambda x : x['hostname'] in not_ng_nodes, all_nodes)
118
119         hostnames = [ n['hostname'] for n in nodelist ]
120
121         # commands:
122
123         if config.add and config.nodegroup:
124                 for node in hostnames:
125                         print "Adding %s to %s nodegroup" % (node, config.nodegroup)
126                         api.AddNodeToNodeGroup(node, config.nodegroup)
127
128         elif config.delete:
129                 for node in hostnames:
130                         print "Deleting %s from %s nodegroup" % (node, config.nodegroup)
131                         api.DeleteNodeFromNodeGroup(node, config.nodegroup)
132
133         elif config.list:
134                 print " ---- Nodes in the %s Node Group ----" % group_str
135                 i = 1
136                 for node in nodelist:
137                         print "%-2d" % i, 
138                         print nodegroup_display(node, fb, config)
139                         i += 1
140
141         else:
142                 print "no other options supported."
143
144 if __name__ == "__main__":
145         try:
146                 main()
147         except IOError:
148                 pass