This commit changes the 'soltesz.py' module into 'moncommands.py' and
[monitor.git] / nodequery.py
1 #!/usr/bin/python
2
3 import plc
4 import auth
5 api = plc.PLC(auth.auth, auth.plc)
6
7 import sys
8 import database
9 from nodecommon import *
10 from policy import Diagnose
11 import glob
12 import os
13 from reboot import pcu_name
14
15 import time
16 import re
17
18 #fb = {}
19 fb = database.dbLoad("findbad")
20 fbpcu = {}
21
22 class NoKeyException(Exception): pass
23
24 def daysdown_print_nodeinfo(fbnode, hostname):
25         fbnode['hostname'] = hostname
26         fbnode['daysdown'] = Diagnose.getStrDaysDown(fbnode)
27         fbnode['intdaysdown'] = Diagnose.getDaysDown(fbnode)
28
29         print "%(intdaysdown)5s %(hostname)-44s | %(state)10.10s | %(daysdown)s" % fbnode
30
31 def fb_print_nodeinfo(fbnode, hostname, fields=None):
32         fbnode['hostname'] = hostname
33         fbnode['checked'] = diff_time(fbnode['checked'])
34         if fbnode['bootcd']:
35                 fbnode['bootcd'] = fbnode['bootcd'].split()[-1]
36         else:
37                 fbnode['bootcd'] = "unknown"
38         fbnode['pcu'] = color_pcu_state(fbnode)
39
40         if not fields:
41                 if 'ERROR' in fbnode['category']:
42                         fbnode['kernel'] = ""
43                 else:
44                         fbnode['kernel'] = fbnode['kernel'].split()[2]
45                 fbnode['boot_state'] = fbnode['plcnode']['boot_state']
46
47                 print "%(hostname)-39s | %(checked)11.11s | %(boot_state)5.5s| %(state)8.8s | %(ssh)5.5s | %(pcu)6.6s | %(bootcd)6.6s | %(category)8.8s | %(kernel)s" % fbnode
48         else:
49                 format = ""
50                 for f in fields:
51                         format += "%%(%s)s " % f
52                 print format % fbnode
53
54 def get(fb, path):
55     indexes = path.split("/")
56     values = fb
57     for index in indexes:
58         if index in values:
59             values = values[index]
60         else:
61             raise NoKeyException(index)
62     return values
63
64 def verifyType(constraints, data):
65         """
66                 constraints is a list of key, value pairs.
67                 # [ {... : ...}==AND , ... , ... , ] == OR
68         """
69         con_or_true = False
70         for con in constraints:
71                 #print "con: %s" % con
72                 if len(con.keys()) == 0:
73                         con_and_true = False
74                 else:
75                         con_and_true = True
76
77                 for key in con.keys():
78                         #print "looking at key: %s" % key
79                         if data is None:
80                                 con_and_true = False
81                                 break
82
83                         try:
84                                 get(data,key)
85                                 o = con[key]
86                                 if o.name() == "Match":
87                                         if get(data,key) is not None:
88                                                 value_re = re.compile(o.value)
89                                                 con_and_true = con_and_true & (value_re.search(get(data,key)) is not None)
90                                         else:
91                                                 con_and_true = False
92                                 elif o.name() == "ListMatch":
93                                         if get(data,key) is not None:
94                                                 match = False
95                                                 for listitem in get(data,key):
96                                                         value_re = re.compile(o.value)
97                                                         if value_re.search(listitem) is not None:
98                                                                 match = True
99                                                                 break
100                                                 con_and_true = con_and_true & match
101                                         else:
102                                                 con_and_true = False
103                                 elif o.name() == "Is":
104                                         con_and_true = con_and_true & (get(data,key) == o.value)
105                                 elif o.name() == "FilledIn":
106                                         con_and_true = con_and_true & (len(get(data,key)) > 0)
107                                 elif o.name() == "PortOpen":
108                                         if get(data,key) is not None:
109                                                 v = get(data,key)
110                                                 con_and_true = con_and_true & (v[str(o.value)] == "open")
111                                         else:
112                                                 con_and_true = False
113                                 else:
114                                         value_re = re.compile(o.value)
115                                         con_and_true = con_and_true & (value_re.search(get(data,key)) is not None)
116
117                         except NoKeyException, key:
118                                 print "missing key %s" % key,
119                                 pass
120                                 #print "missing key %s" % key
121                                 #con_and_true = False
122
123                 con_or_true = con_or_true | con_and_true
124
125         return con_or_true
126
127 def verify(constraints, data):
128         """
129                 constraints is a list of key, value pairs.
130                 # [ {... : ...}==AND , ... , ... , ] == OR
131         """
132         con_or_true = False
133         for con in constraints:
134                 #print "con: %s" % con
135                 if len(con.keys()) == 0:
136                         con_and_true = False
137                 else:
138                         con_and_true = True
139
140                 for key in con.keys():
141                         #print "looking at key: %s" % key
142                         if key in data: 
143                                 value_re = re.compile(con[key])
144                                 con_and_true = con_and_true & (value_re.search(data[key]) is not None)
145                         elif key not in data:
146                                 print "missing key %s" % key,
147                                 pass
148                                 #print "missing key %s" % key
149                                 #con_and_true = False
150
151                 con_or_true = con_or_true | con_and_true
152
153         return con_or_true
154
155 def query_to_dict(query):
156         
157         ad = []
158
159         or_queries = query.split('||')
160         for or_query in or_queries:
161                 and_queries = or_query.split('&&')
162
163                 d = {}
164
165                 for and_query in and_queries:
166                         (key, value) = and_query.split('=')
167                         d[key] = value
168
169                 ad.append(d)
170         
171         return ad
172
173 def pcu_in(fbdata):
174         if 'plcnode' in fbdata:
175                 if 'pcu_ids' in fbdata['plcnode']:
176                         if len(fbdata['plcnode']['pcu_ids']) > 0:
177                                 return True
178         return False
179
180 def pcu_select(str_query, nodelist=None):
181         pcunames = []
182         nodenames = []
183         if str_query is None: return (nodenames, pcunames)
184
185         #print str_query
186         dict_query = query_to_dict(str_query)
187         #print dict_query
188
189         for node in fb['nodes'].keys():
190                 if nodelist is not None: 
191                         if node not in nodelist: continue
192         
193                 fb_nodeinfo  = fb['nodes'][node]['values']
194                 if pcu_in(fb_nodeinfo):
195                         pcuinfo = fbpcu['nodes']['id_%s' % fb_nodeinfo['plcnode']['pcu_ids'][0]]['values']
196                         if verify(dict_query, pcuinfo):
197                                 nodenames.append(node)
198                                 str = "cmdhttps/locfg.pl -s %s -f iloxml/License.xml -u %s -p '%s' | grep MESSAGE" % \
199                                                         (pcu_name(pcuinfo), pcuinfo['username'], pcuinfo['password'])
200                                 pcunames.append(str)
201         return (nodenames, pcunames)
202
203 def node_select(str_query, nodelist=None, fbdb=None):
204         hostnames = []
205         if str_query is None: return hostnames
206
207         #print str_query
208         dict_query = query_to_dict(str_query)
209         #print dict_query
210         global fb
211
212         if fbdb is not None:
213                 fb = fbdb
214
215         for node in fb['nodes'].keys():
216                 if nodelist is not None: 
217                         if node not in nodelist: continue
218         
219                 fb_nodeinfo  = fb['nodes'][node]['values']
220
221                 if fb_nodeinfo == []:
222                         #print node, "has lost values"
223                         continue
224                         #sys.exit(1)
225                 fb_nodeinfo['pcu'] = color_pcu_state(fb_nodeinfo)
226                 fb_nodeinfo['hostname'] = node
227                 if 'plcnode' in fb_nodeinfo:
228                         fb_nodeinfo.update(fb_nodeinfo['plcnode'])
229
230                 if verify(dict_query, fb_nodeinfo):
231                         #print node #fb_nodeinfo
232                         hostnames.append(node)
233                 else:
234                         #print "NO MATCH", node
235                         pass
236         
237         return hostnames
238
239
240 def main():
241         global fb
242         global fbpcu
243
244         from config import config
245         from optparse import OptionParser
246         parser = OptionParser()
247         parser.set_defaults(node=None, fromtime=None, select=None, list=None, pcuselect=None, nodelist=None, daysdown=None, fields=None)
248         parser.add_option("", "--daysdown", dest="daysdown", action="store_true",
249                                                 help="List the node state and days down...")
250         parser.add_option("", "--select", dest="select", metavar="key=value", 
251                                                 help="List all nodes with the given key=value pattern")
252         parser.add_option("", "--fields", dest="fields", metavar="key,list,...", 
253                                                 help="a list of keys to display for each entry.")
254         parser.add_option("", "--list", dest="list", action="store_true", 
255                                                 help="Write only the hostnames as output.")
256         parser.add_option("", "--pcuselect", dest="pcuselect", metavar="key=value", 
257                                                 help="List all nodes with the given key=value pattern")
258         parser.add_option("", "--nodelist", dest="nodelist", metavar="nodelist.txt", 
259                                                 help="A list of nodes to bring out of debug mode.")
260         parser.add_option("", "--fromtime", dest="fromtime", metavar="YYYY-MM-DD",
261                                         help="Specify a starting date from which to begin the query.")
262         config = config(parser)
263         config.parse_args()
264         
265         if config.fromtime:
266                 path = "archive-pdb"
267                 archive = database.SPickle(path)
268                 d = datetime_fromstr(config.fromtime)
269                 glob_str = "%s*.production.findbad.pkl" % d.strftime("%Y-%m-%d")
270                 os.chdir(path)
271                 #print glob_str
272                 file = glob.glob(glob_str)[0]
273                 #print "loading %s" % file
274                 os.chdir("..")
275                 fb = archive.load(file[:-4])
276         else:
277                 fb = database.dbLoad("findbad")
278
279         fbpcu = database.dbLoad("findbadpcus")
280
281         if config.nodelist:
282                 nodelist = config.getListFromFile(config.nodelist)
283         else:
284                 nodelist = fb['nodes'].keys()
285
286         pculist = None
287         if config.select is not None and config.pcuselect is not None:
288                 nodelist = node_select(config.select, nodelist)
289                 nodelist, pculist = pcu_select(config.pcuselect, nodelist)
290         elif config.select is not None:
291                 nodelist = node_select(config.select, nodelist)
292         elif config.pcuselect is not None:
293                 nodelist, pculist = pcu_select(config.pcuselect, nodelist)
294
295
296         if pculist:
297                 for pcu in pculist:
298                         print pcu
299
300         for node in nodelist:
301                 config.node = node
302
303                 if node not in fb['nodes']:
304                         continue
305
306                 fb_nodeinfo  = fb['nodes'][node]['values']
307
308                 if config.list:
309                         print node
310                 else:
311                         if config.daysdown:
312                                 daysdown_print_nodeinfo(fb_nodeinfo, node)
313                         else:
314                                 if config.select:
315                                         if config.fields:
316                                                 fields = config.fields.split(",")
317                                         else:
318                                                 fields = None
319
320                                         fb_print_nodeinfo(fb_nodeinfo, node, fields)
321                                 elif not config.select and 'state' in fb_nodeinfo:
322                                         fb_print_nodeinfo(fb_nodeinfo, node)
323                                 else:
324                                         pass
325                 
326 if __name__ == "__main__":
327         main()