Add a field for the currently observed status as well as the PLC db
[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 config import config
21 from optparse import OptionParser
22
23 import soltesz
24 fb = soltesz.dbLoad("findbad")
25
26 def get_current_state(fbnode):
27         state = fbnode['state']
28         l = state.lower()
29         if l == "debug": return 'dbg'
30         return l
31
32 parser = OptionParser()
33 parser.set_defaults(nodegroup="Alpha",
34                                         node=None,
35                                         nodelist=None,
36                                         list=False,
37                                         add=False,
38                                         delete=False,
39                                         )
40 parser.add_option("", "--nodegroup", dest="nodegroup", metavar="NodegroupName",
41                                         help="Specify a nodegroup to perform actions on")
42 parser.add_option("", "--list", dest="list", action="store_true", 
43                                         help="List all nodes in the given nodegroup")
44 parser.add_option("", "--add", dest="add", action="store_true", 
45                                         help="Add nodes to the given nodegroup")
46 parser.add_option("", "--delete", dest="delete", action="store_true", 
47                                         help="Delete nodes from the given nodegroup")
48 parser.add_option("", "--node", dest="node", metavar="nodename.edu", 
49                                         help="A single node name to add to the nodegroup")
50 parser.add_option("", "--nodelist", dest="nodelist", metavar="list.txt", 
51                                         help="Use all nodes in the given file for operation.")
52 config = config(parser)
53 config.parse_args()
54
55 # COLLECT nodegroups, nodes and node lists
56 ng = api.GetNodeGroups({'name' : config.nodegroup})
57 nodelist = api.GetNodes(ng[0]['node_ids'])
58 hostnames = [ n['hostname'] for n in nodelist ]
59
60 if config.node or config.nodelist:
61         if config.node: hostnames = [ config.node ] 
62         else: hostnames = config.getListFromFile(config.nodelist)
63
64 # commands:
65 if config.list:
66         print " ---- Nodes in the %s Node Group ----" % config.nodegroup
67         i = 0
68         for node in nodelist:
69                 print "%-2d" % i, 
70                 if node['hostname'] in fb['nodes']:
71                         node['current'] = get_current_state(fb['nodes'][node['hostname']]['values'])
72                 else:
73                         node['current'] = 'none'
74                 print "%(hostname)-38s %(boot_state)5s %(current)5s %(key)s" % node
75                 i += 1
76
77 elif config.add:
78         for node in hostnames:
79                 print "Adding %s to %s nodegroup" % (config.node, config.nodegroup)
80                 api.AddNodeToNodeGroup(config.node, config.nodegroup)
81
82 elif config.delete:
83         for node in hostnames:
84                 print "Deleting %s from %s nodegroup" % (config.node, config.nodegroup)
85                 api.DeleteNodeFromNodeGroup(config.node, config.nodegroup)
86
87 else:
88         print "no other options supported."