fa464236deb1d88538de0f6db13e67314b360cac
[monitor.git] / nodequery.py
1 #!/usr/bin/python
2
3
4 import sys
5 import database
6 from nodecommon import *
7 from unified_model import Record
8 import glob
9 import os
10 import reboot
11 import traceback
12
13 import time
14 import re
15 import string
16
17 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 fb['nodes'].keys():
307                 if nodelist is not None: 
308                         if node not in nodelist: continue
309
310                 try:
311                         fb_noderec = FindbadNodeRecord.select(FindbadNodeRecord.q.hostname==node, 
312                                                                                            orderBy='date_checked').reversed()[0]
313                 except:
314                         continue
315
316                 
317                 fb_nodeinfo = fb_noderec.toDict()
318
319                 #fb_nodeinfo['pcu'] = color_pcu_state(fb_nodeinfo)
320                 #if 'plcnode' in fb_nodeinfo:
321                 #       fb_nodeinfo.update(fb_nodeinfo['plcnode'])
322
323                 #if verifyDBrecord(dict_query, fb_nodeinfo):
324                 if verify(dict_query, fb_nodeinfo):
325                         #print node #fb_nodeinfo
326                         hostnames.append(node)
327                 else:
328                         #print "NO MATCH", node
329                         pass
330         
331         return hostnames
332
333
334 def main():
335         global fb
336         global fbpcu
337
338         import parser as parsermodule
339         parser = parsermodule.getParser()
340
341         parser.set_defaults(node=None, fromtime=None, select=None, list=None, 
342                                                 pcuselect=None, nodelist=None, daysdown=None, fields=None)
343         parser.add_option("", "--daysdown", dest="daysdown", action="store_true",
344                                                 help="List the node state and days down...")
345         parser.add_option("", "--select", dest="select", metavar="key=value", 
346                                                 help="List all nodes with the given key=value pattern")
347         parser.add_option("", "--fields", dest="fields", metavar="key,list,...", 
348                                                 help="a list of keys to display for each entry.")
349         parser.add_option("", "--list", dest="list", action="store_true", 
350                                                 help="Write only the hostnames as output.")
351         parser.add_option("", "--pcuselect", dest="pcuselect", metavar="key=value", 
352                                                 help="List all nodes with the given key=value pattern")
353         parser.add_option("", "--nodelist", dest="nodelist", metavar="nodelist.txt", 
354                                                 help="A list of nodes to bring out of debug mode.")
355         parser.add_option("", "--fromtime", dest="fromtime", metavar="YYYY-MM-DD",
356                                         help="Specify a starting date from which to begin the query.")
357
358         parser = parsermodule.getParser(['defaults'], parser)
359         config = parsermodule.parse_args(parser)
360         
361         if config.fromtime:
362                 path = "archive-pdb"
363                 archive = database.SPickle(path)
364                 d = datetime_fromstr(config.fromtime)
365                 glob_str = "%s*.production.findbad.pkl" % d.strftime("%Y-%m-%d")
366                 os.chdir(path)
367                 #print glob_str
368                 file = glob.glob(glob_str)[0]
369                 #print "loading %s" % file
370                 os.chdir("..")
371                 fb = archive.load(file[:-4])
372         else:
373                 fbnodes = FindbadNodeRecord.select(FindbadNodeRecord.q.hostname, orderBy='date_checked',distinct=True).reversed()
374                 fb = database.dbLoad("findbad")
375
376         fbpcu = database.dbLoad("findbadpcus")
377         reboot.fb = fbpcu
378
379         if config.nodelist:
380                 nodelist = util.file.getListFromFile(config.nodelist)
381         else:
382                 nodelist = fb['nodes'].keys()
383
384         pculist = None
385         if config.select is not None and config.pcuselect is not None:
386                 nodelist = node_select(config.select, nodelist, fb)
387                 nodelist, pculist = pcu_select(config.pcuselect, nodelist)
388         elif config.select is not None:
389                 nodelist = node_select(config.select, nodelist, fb)
390         elif config.pcuselect is not None:
391                 nodelist, pculist = pcu_select(config.pcuselect, nodelist)
392
393         if pculist:
394                 for pcu in pculist:
395                         print pcu
396
397         for node in nodelist:
398                 config.node = node
399
400                 if node not in fb['nodes']:
401                         continue
402
403                 try:
404                         # Find the most recent record
405                         fb_noderec = FindbadNodeRecord.select(FindbadNodeRecord.q.hostname==node, 
406                                                                                            orderBy='date_checked').reversed()[0]
407                 except:
408                         print traceback.print_exc()
409                         pass #fb_nodeinfo  = fb['nodes'][node]['values']
410
411                 if config.list:
412                         print node
413                 else:
414                         if config.daysdown:
415                                 daysdown_print_nodeinfo(fb_nodeinfo, node)
416                         else:
417                                 fb_nodeinfo = fb_noderec.toDict()
418                                 if config.select:
419                                         if config.fields:
420                                                 fields = config.fields.split(",")
421                                         else:
422                                                 fields = None
423
424                                         fb_print_nodeinfo(fb_nodeinfo, node, fields)
425                                 elif not config.select and 'state' in fb_nodeinfo:
426                                         fb_print_nodeinfo(fb_nodeinfo, node)
427                                 else:
428                                         pass
429                 
430 if __name__ == "__main__":
431         main()