8 from nodecommon import *
9 #from policy import Diagnose
10 from unified_model import Record
13 from reboot import pcu_name
23 from sqlobject import connectionForURI,sqlhub
24 connection = connectionForURI(config.sqlobjecturi)
25 sqlhub.processConnection = connection
26 from infovacuum.model.findbadrecord import *
33 class NoKeyException(Exception): pass
35 def daysdown_print_nodeinfo(fbnode, hostname):
36 fbnode['hostname'] = hostname
37 fbnode['daysdown'] = Record.getStrDaysDown(fbnode)
38 fbnode['intdaysdown'] = Record.getDaysDown(fbnode)
40 print "%(intdaysdown)5s %(hostname)-44s | %(state)10.10s | %(daysdown)s" % fbnode
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]
48 fbnode['bootcd_version'] = "unknown"
49 fbnode['pcu'] = color_pcu_state(fbnode)
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'] = ""
57 fbnode['kernel_version'] = fbnode['kernel_version'].split()[2]
59 if fbnode['plc_node_stats'] is not None:
60 fbnode['boot_state'] = fbnode['plc_node_stats']['boot_state']
62 fbnode['boot_state'] = "unknown"
65 if len(fbnode['nodegroups']) > 0:
66 fbnode['category'] = fbnode['nodegroups'][0]
68 #print "ERROR!!!!!!!!!!!!!!!!!!!!!"
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
75 format += "%%(%s)s " % f
79 indexes = path.split("/")
83 values = values[index]
85 raise NoKeyException(index)
88 def verifyType(constraints, data):
90 constraints is a list of key, value pairs.
91 # [ {... : ...}==AND , ... , ... , ] == OR
94 for con in constraints:
95 #print "con: %s" % con
96 if len(con.keys()) == 0:
101 for key in con.keys():
102 #print "looking at key: %s" % 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)
116 elif o.name() == "ListMatch":
117 if get(data,key) is not None:
119 for listitem in get(data,key):
120 value_re = re.compile(o.value)
121 if value_re.search(listitem) is not None:
124 con_and_true = con_and_true & match
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:
134 con_and_true = con_and_true & (v[str(o.value)] == "open")
138 value_re = re.compile(o.value)
139 con_and_true = con_and_true & (value_re.search(get(data,key)) is not None)
141 except NoKeyException, key:
142 print "missing key %s" % key,
144 #print "missing key %s" % key
145 #con_and_true = False
147 con_or_true = con_or_true | con_and_true
151 def verifyDBrecord(constraints, record):
153 constraints is a list of key, value pairs.
154 # [ {... : ...}==AND , ... , ... , ] == OR
156 def has_key(obj, key):
158 x = obj.__getattribute__(key)
163 def get_val(obj, key):
165 return obj.__getattribute__(key)
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:]:
177 raise NoKeyException(key)
180 #print constraints, record
183 for con in constraints:
184 #print "con: %s" % con
185 if len(con.keys()) == 0:
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
200 if get(record,key) is not None:
201 con_and_true = con_and_true & (value_re.search(get(record,key)) is not None)
203 print "missing key %s" % key,
206 con_or_true = con_or_true | con_and_true
210 def verify(constraints, data):
212 constraints is a list of key, value pairs.
213 # [ {... : ...}==AND , ... , ... , ] == OR
216 for con in constraints:
217 #print "con: %s" % con
218 if len(con.keys()) == 0:
223 for key in con.keys():
224 #print "looking at key: %s" % key
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
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,
239 con_or_true = con_or_true | con_and_true
243 def query_to_dict(query):
247 or_queries = query.split('||')
248 for or_query in or_queries:
249 and_queries = or_query.split('&&')
253 for and_query in and_queries:
254 (key, value) = and_query.split('=')
262 if 'plcnode' in fbdata:
263 if 'pcu_ids' in fbdata['plcnode']:
264 if len(fbdata['plcnode']['pcu_ids']) > 0:
268 def pcu_select(str_query, nodelist=None):
273 if str_query is None: return (nodenames, pcunames)
276 fb = database.dbLoad("findbad")
278 fbpcu = database.dbLoad("findbadpcus")
281 dict_query = query_to_dict(str_query)
284 for node in fb['nodes'].keys():
285 if nodelist is not None:
286 if node not in nodelist: continue
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)
299 def node_select(str_query, nodelist=None, fbdb=None):
303 if str_query is None: return hostnames
306 dict_query = query_to_dict(str_query)
312 for node in fb['nodes'].keys():
313 if nodelist is not None:
314 if node not in nodelist: continue
317 fb_noderec = FindbadNodeRecord.select(FindbadNodeRecord.q.hostname==node,
318 orderBy='date_checked').reversed()[0]
323 fb_nodeinfo = fb_noderec.toDict()
325 #fb_nodeinfo['pcu'] = color_pcu_state(fb_nodeinfo)
326 #if 'plcnode' in fb_nodeinfo:
327 # fb_nodeinfo.update(fb_nodeinfo['plcnode'])
329 #if verifyDBrecord(dict_query, fb_nodeinfo):
330 if verify(dict_query, fb_nodeinfo):
331 #print node #fb_nodeinfo
332 hostnames.append(node)
334 #print "NO MATCH", node
344 import parser as parsermodule
345 parser = parsermodule.getParser()
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.")
364 parser = parsermodule.getParser(['defaults'], parser)
365 config = parsermodule.parse_args(parser)
369 archive = database.SPickle(path)
370 d = datetime_fromstr(config.fromtime)
371 glob_str = "%s*.production.findbad.pkl" % d.strftime("%Y-%m-%d")
374 file = glob.glob(glob_str)[0]
375 #print "loading %s" % file
377 fb = archive.load(file[:-4])
379 fbnodes = FindbadNodeRecord.select(FindbadNodeRecord.q.hostname, orderBy='date_checked',distinct=True).reversed()
380 fb = database.dbLoad("findbad")
382 fbpcu = database.dbLoad("findbadpcus")
386 nodelist = util.file.getListFromFile(config.nodelist)
388 nodelist = fb['nodes'].keys()
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)
403 for node in nodelist:
406 if node not in fb['nodes']:
410 # Find the most recent record
411 fb_noderec = FindbadNodeRecord.select(FindbadNodeRecord.q.hostname==node,
412 orderBy='date_checked').reversed()[0]
414 print traceback.print_exc()
415 pass #fb_nodeinfo = fb['nodes'][node]['values']
421 daysdown_print_nodeinfo(fb_nodeinfo, node)
423 fb_nodeinfo = fb_noderec.toDict()
426 fields = config.fields.split(",")
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)
436 if __name__ == "__main__":