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