8 from nodecommon import *
9 #from policy import Diagnose
10 from unified_model import Record
13 from reboot import pcu_name
15 from monitor import util
23 from monitor.database import FindbadNodeRecord, FindbadNodeRecordSync
24 #from sqlobject import connectionForURI,sqlhub
25 #connection = connectionForURI(config.sqlobjecturi)
26 #sqlhub.processConnection = connection
27 #from infovacuum.model.findbadrecord import *
34 class NoKeyException(Exception): pass
36 def daysdown_print_nodeinfo(fbnode, hostname):
37 fbnode['hostname'] = hostname
38 fbnode['daysdown'] = Record.getStrDaysDown(fbnode)
39 fbnode['intdaysdown'] = Record.getDaysDown(fbnode)
41 print "%(intdaysdown)5s %(hostname)-44s | %(state)10.10s | %(daysdown)s" % fbnode
43 def fb_print_nodeinfo(fbnode, hostname, fields=None):
44 #fbnode['hostname'] = hostname
45 #fbnode['checked'] = diff_time(fbnode['checked'])
46 if fbnode['bootcd_version']:
47 fbnode['bootcd_version'] = fbnode['bootcd_version'].split()[-1]
49 fbnode['bootcd_version'] = "unknown"
50 fbnode['pcu'] = color_pcu_state(fbnode)
53 if ( fbnode['observed_status'] is not None and \
54 'DOWN' in fbnode['observed_status'] ) or \
55 fbnode['kernel_version'] is None:
56 fbnode['kernel_version'] = ""
58 fbnode['kernel_version'] = fbnode['kernel_version'].split()[2]
60 if fbnode['plc_node_stats'] is not None:
61 fbnode['boot_state'] = fbnode['plc_node_stats']['boot_state']
63 fbnode['boot_state'] = "unknown"
66 if len(fbnode['nodegroups']) > 0:
67 fbnode['category'] = fbnode['nodegroups'][0]
69 #print "ERROR!!!!!!!!!!!!!!!!!!!!!"
72 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
76 format += "%%(%s)s " % f
80 indexes = path.split("/")
84 values = values[index]
86 raise NoKeyException(index)
89 def verifyType(constraints, data):
91 constraints is a list of key, value pairs.
92 # [ {... : ...}==AND , ... , ... , ] == OR
95 for con in constraints:
96 #print "con: %s" % con
97 if len(con.keys()) == 0:
102 for key in con.keys():
103 #print "looking at key: %s" % key
111 if o.name() == "Match":
112 if get(data,key) is not None:
113 value_re = re.compile(o.value)
114 con_and_true = con_and_true & (value_re.search(get(data,key)) is not None)
117 elif o.name() == "ListMatch":
118 if get(data,key) is not None:
120 for listitem in get(data,key):
121 value_re = re.compile(o.value)
122 if value_re.search(listitem) is not None:
125 con_and_true = con_and_true & match
128 elif o.name() == "Is":
129 con_and_true = con_and_true & (get(data,key) == o.value)
130 elif o.name() == "FilledIn":
131 con_and_true = con_and_true & (len(get(data,key)) > 0)
132 elif o.name() == "PortOpen":
133 if get(data,key) is not None:
135 con_and_true = con_and_true & (v[str(o.value)] == "open")
139 value_re = re.compile(o.value)
140 con_and_true = con_and_true & (value_re.search(get(data,key)) is not None)
142 except NoKeyException, key:
143 print "missing key %s" % key,
145 #print "missing key %s" % key
146 #con_and_true = False
148 con_or_true = con_or_true | con_and_true
152 def verifyDBrecord(constraints, record):
154 constraints is a list of key, value pairs.
155 # [ {... : ...}==AND , ... , ... , ] == OR
157 def has_key(obj, key):
159 x = obj.__getattribute__(key)
164 def get_val(obj, key):
166 return obj.__getattribute__(key)
171 indexes = path.split("/")
172 value = get_val(obj,indexes[0])
173 if value is not None and len(indexes) > 1:
174 for key in indexes[1:]:
178 raise NoKeyException(key)
181 #print constraints, record
184 for con in constraints:
185 #print "con: %s" % con
186 if len(con.keys()) == 0:
191 for key in con.keys():
192 #print "looking at key: %s" % key
193 if has_key(record, key):
194 value_re = re.compile(con[key])
195 if type([]) == type(get(record,key)):
196 local_or_true = False
197 for val in get(record,key):
198 local_or_true = local_or_true | (value_re.search(val) is not None)
199 con_and_true = con_and_true & local_or_true
201 if get(record,key) is not None:
202 con_and_true = con_and_true & (value_re.search(get(record,key)) is not None)
204 print "missing key %s" % key,
207 con_or_true = con_or_true | con_and_true
211 def verify(constraints, data):
213 constraints is a list of key, value pairs.
214 # [ {... : ...}==AND , ... , ... , ] == OR
217 for con in constraints:
218 #print "con: %s" % con
219 if len(con.keys()) == 0:
224 for key in con.keys():
225 #print "looking at key: %s" % key
227 value_re = re.compile(con[key])
228 if type([]) == type(data[key]):
229 local_or_true = False
230 for val in data[key]:
231 local_or_true = local_or_true | (value_re.search(val) is not None)
232 con_and_true = con_and_true & local_or_true
234 if data[key] is not None:
235 con_and_true = con_and_true & (value_re.search(data[key]) is not None)
236 elif key not in data:
237 print "missing key %s" % key,
240 con_or_true = con_or_true | con_and_true
244 def query_to_dict(query):
248 or_queries = query.split('||')
249 for or_query in or_queries:
250 and_queries = or_query.split('&&')
254 for and_query in and_queries:
255 (key, value) = and_query.split('=')
263 if 'plcnode' in fbdata:
264 if 'pcu_ids' in fbdata['plcnode']:
265 if len(fbdata['plcnode']['pcu_ids']) > 0:
269 def pcu_select(str_query, nodelist=None):
274 if str_query is None: return (nodenames, pcunames)
277 fb = database.dbLoad("findbad")
279 fbpcu = database.dbLoad("findbadpcus")
282 dict_query = query_to_dict(str_query)
285 for node in fb['nodes'].keys():
286 if nodelist is not None:
287 if node not in nodelist: continue
289 fb_nodeinfo = fb['nodes'][node]['values']
290 if pcu_in(fb_nodeinfo):
291 pcuinfo = fbpcu['nodes']['id_%s' % fb_nodeinfo['plcnode']['pcu_ids'][0]]['values']
292 if verify(dict_query, pcuinfo):
293 nodenames.append(node)
294 str = "cmdhttps/locfg.pl -s %s -f iloxml/License.xml -u %s -p '%s' | grep MESSAGE" % \
295 (pcu_name(pcuinfo), pcuinfo['username'], pcuinfo['password'])
296 #pcunames.append(str)
297 pcunames.append(pcuinfo['pcu_id'])
298 return (nodenames, pcunames)
300 def node_select(str_query, nodelist=None, fbdb=None):
304 if str_query is None: return hostnames
307 dict_query = query_to_dict(str_query)
313 for node in fb['nodes'].keys():
314 if nodelist is not None:
315 if node not in nodelist: continue
318 fb_noderec = FindbadNodeRecord.select(FindbadNodeRecord.q.hostname==node,
319 orderBy='date_checked').reversed()[0]
324 fb_nodeinfo = fb_noderec.toDict()
326 #fb_nodeinfo['pcu'] = color_pcu_state(fb_nodeinfo)
327 #if 'plcnode' in fb_nodeinfo:
328 # fb_nodeinfo.update(fb_nodeinfo['plcnode'])
330 #if verifyDBrecord(dict_query, fb_nodeinfo):
331 if verify(dict_query, fb_nodeinfo):
332 #print node #fb_nodeinfo
333 hostnames.append(node)
335 #print "NO MATCH", node
345 import parser as parsermodule
346 parser = parsermodule.getParser()
348 parser.set_defaults(node=None, fromtime=None, select=None, list=None,
349 pcuselect=None, nodelist=None, daysdown=None, fields=None)
350 parser.add_option("", "--daysdown", dest="daysdown", action="store_true",
351 help="List the node state and days down...")
352 parser.add_option("", "--select", dest="select", metavar="key=value",
353 help="List all nodes with the given key=value pattern")
354 parser.add_option("", "--fields", dest="fields", metavar="key,list,...",
355 help="a list of keys to display for each entry.")
356 parser.add_option("", "--list", dest="list", action="store_true",
357 help="Write only the hostnames as output.")
358 parser.add_option("", "--pcuselect", dest="pcuselect", metavar="key=value",
359 help="List all nodes with the given key=value pattern")
360 parser.add_option("", "--nodelist", dest="nodelist", metavar="nodelist.txt",
361 help="A list of nodes to bring out of debug mode.")
362 parser.add_option("", "--fromtime", dest="fromtime", metavar="YYYY-MM-DD",
363 help="Specify a starting date from which to begin the query.")
365 parser = parsermodule.getParser(['defaults'], parser)
366 config = parsermodule.parse_args(parser)
370 archive = database.SPickle(path)
371 d = datetime_fromstr(config.fromtime)
372 glob_str = "%s*.production.findbad.pkl" % d.strftime("%Y-%m-%d")
375 file = glob.glob(glob_str)[0]
376 #print "loading %s" % file
378 fb = archive.load(file[:-4])
380 fbnodes = FindbadNodeRecord.select(FindbadNodeRecord.q.hostname, orderBy='date_checked',distinct=True).reversed()
381 fb = database.dbLoad("findbad")
383 fbpcu = database.dbLoad("findbadpcus")
387 nodelist = util.file.getListFromFile(config.nodelist)
389 nodelist = fb['nodes'].keys()
392 if config.select is not None and config.pcuselect is not None:
393 nodelist = node_select(config.select, nodelist, fb)
394 nodelist, pculist = pcu_select(config.pcuselect, nodelist)
395 elif config.select is not None:
396 nodelist = node_select(config.select, nodelist, fb)
397 elif config.pcuselect is not None:
398 nodelist, pculist = pcu_select(config.pcuselect, nodelist)
404 for node in nodelist:
407 if node not in fb['nodes']:
411 # Find the most recent record
412 fb_noderec = FindbadNodeRecord.select(FindbadNodeRecord.q.hostname==node,
413 orderBy='date_checked').reversed()[0]
415 print traceback.print_exc()
416 pass #fb_nodeinfo = fb['nodes'][node]['values']
422 daysdown_print_nodeinfo(fb_nodeinfo, node)
424 fb_nodeinfo = fb_noderec.toDict()
427 fields = config.fields.split(",")
431 fb_print_nodeinfo(fb_nodeinfo, node, fields)
432 elif not config.select and 'state' in fb_nodeinfo:
433 fb_print_nodeinfo(fb_nodeinfo, node)
437 if __name__ == "__main__":