mass commit. updates for the new db schema in findbad, findbadpcu, nodequery,
[monitor.git] / nodequery.py
1 #!/usr/bin/python
2
3
4 import sys
5 from monitor import database
6 from nodecommon import *
7 from unified_model import Record
8 import glob
9 import os
10 import traceback
11
12 import time
13 import re
14 import string
15
16 from monitor.pcu import reboot
17 from monitor.wrapper import plc
18 api = plc.getAuthAPI()
19
20 from monitor.database import FindbadNodeRecord, FindbadNodeRecordSync
21 from monitor import util
22 from monitor import config
23
24 fb = None
25 fbpcu = None
26
27 class NoKeyException(Exception): pass
28
29 def daysdown_print_nodeinfo(fbnode, hostname):
30         fbnode['hostname'] = hostname
31         fbnode['daysdown'] = Record.getStrDaysDown(fbnode)
32         fbnode['intdaysdown'] = Record.getDaysDown(fbnode)
33
34         print "%(intdaysdown)5s %(hostname)-44s | %(state)10.10s | %(daysdown)s" % fbnode
35
36 def fb_print_nodeinfo(fbnode, hostname, fields=None):
37         #fbnode['hostname'] = hostname
38         #fbnode['checked'] = diff_time(fbnode['checked'])
39         if fbnode['bootcd_version']:
40                 fbnode['bootcd_version'] = fbnode['bootcd_version'].split()[-1]
41         else:
42                 fbnode['bootcd_version'] = "unknown"
43         fbnode['pcu'] = color_pcu_state(fbnode)
44
45         if not fields:
46                 if ( fbnode['observed_status'] is not None and \
47                    'DOWN' in fbnode['observed_status'] ) or \
48                    fbnode['kernel_version'] is None:
49                         fbnode['kernel_version'] = ""
50                 else:
51                         fbnode['kernel_version'] = fbnode['kernel_version'].split()[2]
52
53                 if fbnode['plc_node_stats'] is not None:
54                         fbnode['boot_state'] = fbnode['plc_node_stats']['boot_state']
55                 else:
56                         fbnode['boot_state'] = "unknown"
57
58                 try:
59                         if len(fbnode['nodegroups']) > 0:
60                                 fbnode['category'] = fbnode['nodegroups'][0]
61                 except:
62                         #print "ERROR!!!!!!!!!!!!!!!!!!!!!"
63                         pass
64
65                 print "%(hostname)-45s | %(date_checked)11.11s | %(boot_state)5.5s| %(observed_status)8.8s | %(ssh_status)5.5s | %(pcu)6.6s | %(bootcd_version)6.6s | %(kernel_version)s" % fbnode
66         else:
67                 format = ""
68                 for f in fields:
69                         format += "%%(%s)s " % f
70                 print format % fbnode
71
72 def get(fb, path):
73     indexes = path.split("/")
74     values = fb
75     for index in indexes:
76         if index in values:
77             values = values[index]
78         else:
79             raise NoKeyException(index)
80     return values
81
82 def verifyType(constraints, data):
83         """
84                 constraints is a list of key, value pairs.
85                 # [ {... : ...}==AND , ... , ... , ] == OR
86         """
87         con_or_true = False
88         for con in constraints:
89                 #print "con: %s" % con
90                 if len(con.keys()) == 0:
91                         con_and_true = False
92                 else:
93                         con_and_true = True
94
95                 for key in con.keys():
96                         #print "looking at key: %s" % key
97                         if data is None:
98                                 con_and_true = False
99                                 break
100
101                         try:
102                                 get(data,key)
103                                 o = con[key]
104                                 if o.name() == "Match":
105                                         if get(data,key) is not None:
106                                                 value_re = re.compile(o.value)
107                                                 con_and_true = con_and_true & (value_re.search(get(data,key)) is not None)
108                                         else:
109                                                 con_and_true = False
110                                 elif o.name() == "ListMatch":
111                                         if get(data,key) is not None:
112                                                 match = False
113                                                 for listitem in get(data,key):
114                                                         value_re = re.compile(o.value)
115                                                         if value_re.search(listitem) is not None:
116                                                                 match = True
117                                                                 break
118                                                 con_and_true = con_and_true & match
119                                         else:
120                                                 con_and_true = False
121                                 elif o.name() == "Is":
122                                         con_and_true = con_and_true & (get(data,key) == o.value)
123                                 elif o.name() == "FilledIn":
124                                         con_and_true = con_and_true & (len(get(data,key)) > 0)
125                                 elif o.name() == "PortOpen":
126                                         if get(data,key) is not None:
127                                                 v = get(data,key)
128                                                 con_and_true = con_and_true & (v[str(o.value)] == "open")
129                                         else:
130                                                 con_and_true = False
131                                 else:
132                                         value_re = re.compile(o.value)
133                                         con_and_true = con_and_true & (value_re.search(get(data,key)) is not None)
134
135                         except NoKeyException, key:
136                                 print "missing key %s" % key,
137                                 pass
138                                 #print "missing key %s" % key
139                                 #con_and_true = False
140
141                 con_or_true = con_or_true | con_and_true
142
143         return con_or_true
144
145 def verifyDBrecord(constraints, record):
146         """
147                 constraints is a list of key, value pairs.
148                 # [ {... : ...}==AND , ... , ... , ] == OR
149         """
150         def has_key(obj, key):
151                 try:
152                         x = obj.__getattribute__(key)
153                         return True
154                 except:
155                         return False
156
157         def get_val(obj, key):
158                 try:
159                         return obj.__getattribute__(key)
160                 except:
161                         return None
162
163         def get(obj, path):
164                 indexes = path.split("/")
165                 value = get_val(obj,indexes[0])
166                 if value is not None and len(indexes) > 1:
167                         for key in indexes[1:]:
168                                 if key in value:
169                                         value = value[key]
170                                 else:
171                                         raise NoKeyException(key)
172                 return value
173
174         #print constraints, record
175
176         con_or_true = False
177         for con in constraints:
178                 #print "con: %s" % con
179                 if len(con.keys()) == 0:
180                         con_and_true = False
181                 else:
182                         con_and_true = True
183
184                 for key in con.keys():
185                         #print "looking at key: %s" % key
186                         if has_key(record, key):
187                                 value_re = re.compile(con[key])
188                                 if type([]) == type(get(record,key)):
189                                         local_or_true = False
190                                         for val in get(record,key):
191                                                 local_or_true = local_or_true | (value_re.search(val) is not None)
192                                         con_and_true = con_and_true & local_or_true
193                                 else:
194                                         if get(record,key) is not None:
195                                                 con_and_true = con_and_true & (value_re.search(get(record,key)) is not None)
196                         else:
197                                 print "missing key %s" % key,
198                                 pass
199
200                 con_or_true = con_or_true | con_and_true
201
202         return con_or_true
203
204 def verify(constraints, data):
205         """
206                 constraints is a list of key, value pairs.
207                 # [ {... : ...}==AND , ... , ... , ] == OR
208         """
209         con_or_true = False
210         for con in constraints:
211                 #print "con: %s" % con
212                 if len(con.keys()) == 0:
213                         con_and_true = False
214                 else:
215                         con_and_true = True
216
217                 for key in con.keys():
218                         #print "looking at key: %s" % key
219                         if key in data: 
220                                 value_re = re.compile(con[key])
221                                 if type([]) == type(data[key]):
222                                         local_or_true = False
223                                         for val in data[key]:
224                                                 local_or_true = local_or_true | (value_re.search(val) is not None)
225                                         con_and_true = con_and_true & local_or_true
226                                 else:
227                                         if data[key] is not None:
228                                                 con_and_true = con_and_true & (value_re.search(data[key]) is not None)
229                         elif key not in data:
230                                 print "missing key %s" % key,
231                                 pass
232
233                 con_or_true = con_or_true | con_and_true
234
235         return con_or_true
236
237 def query_to_dict(query):
238         
239         ad = []
240
241         or_queries = query.split('||')
242         for or_query in or_queries:
243                 and_queries = or_query.split('&&')
244
245                 d = {}
246
247                 for and_query in and_queries:
248                         (key, value) = and_query.split('=')
249                         d[key] = value
250
251                 ad.append(d)
252         
253         return ad
254
255 def pcu_in(fbdata):
256         if 'plcnode' in fbdata:
257                 if 'pcu_ids' in fbdata['plcnode']:
258                         if len(fbdata['plcnode']['pcu_ids']) > 0:
259                                 return True
260         return False
261
262 def pcu_select(str_query, nodelist=None):
263         global fb
264         global fbpcu
265         pcunames = []
266         nodenames = []
267         if str_query is None: return (nodenames, pcunames)
268
269         if fb is None:
270                 fb = database.dbLoad("findbad")
271         if fbpcu is None:
272                 fbpcu = database.dbLoad("findbadpcus")
273
274         #print str_query
275         dict_query = query_to_dict(str_query)
276         #print dict_query
277
278         for node in fb['nodes'].keys():
279                 if nodelist is not None: 
280                         if node not in nodelist: continue
281         
282                 fb_nodeinfo  = fb['nodes'][node]['values']
283                 if pcu_in(fb_nodeinfo):
284                         pcuinfo = fbpcu['nodes']['id_%s' % fb_nodeinfo['plcnode']['pcu_ids'][0]]['values']
285                         if verify(dict_query, pcuinfo):
286                                 nodenames.append(node)
287                                 str = "cmdhttps/locfg.pl -s %s -f iloxml/License.xml -u %s -p '%s' | grep MESSAGE" % \
288                                                         (reboot.pcu_name(pcuinfo), pcuinfo['username'], pcuinfo['password'])
289                                 #pcunames.append(str)
290                                 pcunames.append(pcuinfo['pcu_id'])
291         return (nodenames, pcunames)
292
293 def node_select(str_query, nodelist=None, fbdb=None):
294         global fb
295
296         hostnames = []
297         if str_query is None: return hostnames
298
299         #print str_query
300         dict_query = query_to_dict(str_query)
301         #print dict_query
302
303         if fbdb is not None:
304                 fb = fbdb
305
306         for node in nodelist:
307                 #if nodelist is not None: 
308                 #       if node not in nodelist: continue
309
310                 try:
311                         fb_noderec = None
312                         fb_noderec = FindbadNodeRecord.query.filter(FindbadNodeRecord.hostname==node).order_by(FindbadNodeRecord.date_checked.desc()).first()
313                 except:
314                         print traceback.print_exc()
315                         continue
316
317                 if fb_noderec:
318                         fb_nodeinfo = fb_noderec.to_dict()
319
320                         #fb_nodeinfo['pcu'] = color_pcu_state(fb_nodeinfo)
321                         #if 'plcnode' in fb_nodeinfo:
322                         #       fb_nodeinfo.update(fb_nodeinfo['plcnode'])
323
324                         #if verifyDBrecord(dict_query, fb_nodeinfo):
325                         if verify(dict_query, fb_nodeinfo):
326                                 #print node #fb_nodeinfo
327                                 hostnames.append(node)
328                         else:
329                                 #print "NO MATCH", node
330                                 pass
331         
332         return hostnames
333
334
335 def main():
336         global fb
337         global fbpcu
338
339         from monitor import parser as parsermodule
340         parser = parsermodule.getParser()
341
342         parser.set_defaults(node=None, fromtime=None, select=None, list=None, 
343                                                 pcuselect=None, nodelist=None, daysdown=None, fields=None)
344         parser.add_option("", "--daysdown", dest="daysdown", action="store_true",
345                                                 help="List the node state and days down...")
346         parser.add_option("", "--select", dest="select", metavar="key=value", 
347                                                 help="List all nodes with the given key=value pattern")
348         parser.add_option("", "--fields", dest="fields", metavar="key,list,...", 
349                                                 help="a list of keys to display for each entry.")
350         parser.add_option("", "--list", dest="list", action="store_true", 
351                                                 help="Write only the hostnames as output.")
352         parser.add_option("", "--pcuselect", dest="pcuselect", metavar="key=value", 
353                                                 help="List all nodes with the given key=value pattern")
354         parser.add_option("", "--nodelist", dest="nodelist", metavar="nodelist.txt", 
355                                                 help="A list of nodes to bring out of debug mode.")
356         parser.add_option("", "--fromtime", dest="fromtime", metavar="YYYY-MM-DD",
357                                         help="Specify a starting date from which to begin the query.")
358
359         parser = parsermodule.getParser(['defaults'], parser)
360         config = parsermodule.parse_args(parser)
361         
362         if config.fromtime:
363                 path = "archive-pdb"
364                 archive = database.SPickle(path)
365                 d = datetime_fromstr(config.fromtime)
366                 glob_str = "%s*.production.findbad.pkl" % d.strftime("%Y-%m-%d")
367                 os.chdir(path)
368                 #print glob_str
369                 file = glob.glob(glob_str)[0]
370                 #print "loading %s" % file
371                 os.chdir("..")
372                 fb = archive.load(file[:-4])
373         else:
374                 #fbnodes = FindbadNodeRecord.select(FindbadNodeRecord.q.hostname, orderBy='date_checked',distinct=True).reversed()
375                 #fb = database.dbLoad("findbad")
376                 fb = None
377
378         fbpcu = database.dbLoad("findbadpcus")
379         reboot.fb = fbpcu
380
381         if config.nodelist:
382                 nodelist = util.file.getListFromFile(config.nodelist)
383         else:
384                 # NOTE: list of nodes should come from findbad db.   Otherwise, we
385                 # don't know for sure that there's a record in the db..
386                 plcnodes = database.dbLoad("l_plcnodes")
387                 nodelist = [ node['hostname'] for node in plcnodes ]
388                 #nodelist = ['planetlab-1.cs.princeton.edu']
389
390         pculist = None
391         if config.select is not None and config.pcuselect is not None:
392                 nodelist = node_select(config.select, nodelist, fb)
393                 nodelist, pculist = pcu_select(config.pcuselect, nodelist)
394         elif config.select is not None:
395                 nodelist = node_select(config.select, nodelist, fb)
396         elif config.pcuselect is not None:
397                 nodelist, pculist = pcu_select(config.pcuselect, nodelist)
398
399         if pculist:
400                 for pcu in pculist:
401                         print pcu
402
403         for node in nodelist:
404                 config.node = node
405
406                 if node not in nodelist:
407                         continue
408
409                 try:
410                         # Find the most recent record
411                         fb_noderec = FindbadNodeRecord.query.filter(FindbadNodeRecord.hostname==node).order_by(FindbadNodeRecord.date_checked.desc()).first()
412                 except:
413                         print traceback.print_exc()
414                         pass #fb_nodeinfo  = fb['nodes'][node]['values']
415
416                 if config.list:
417                         print node
418                 else:
419                         if config.daysdown:
420                                 daysdown_print_nodeinfo(fb_nodeinfo, node)
421                         else:
422                                 fb_nodeinfo = fb_noderec.to_dict()
423                                 if config.select:
424                                         if config.fields:
425                                                 fields = config.fields.split(",")
426                                         else:
427                                                 fields = None
428
429                                         fb_print_nodeinfo(fb_nodeinfo, node, fields)
430                                 elif not config.select and 'state' in fb_nodeinfo:
431                                         fb_print_nodeinfo(fb_nodeinfo, node)
432                                 else:
433                                         pass
434                 
435 if __name__ == "__main__":
436         main()