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