5 from monitor import database
6 from monitor.common import *
7 from monitor.model import Record
16 from monitor.wrapper import plc, plccache
17 api = plc.getAuthAPI()
19 from monitor.database.info.model import FindbadNodeRecord, FindbadPCURecord, session
20 from monitor import util
21 from monitor import config
24 class NoKeyException(Exception): pass
26 def daysdown_print_nodeinfo(fbnode, hostname):
27 fbnode['hostname'] = hostname
28 fbnode['daysdown'] = Record.getStrDaysDown(fbnode)
29 fbnode['intdaysdown'] = Record.getDaysDown(fbnode)
31 print "%(intdaysdown)5s %(hostname)-44s | %(state)10.10s | %(daysdown)s" % fbnode
33 def fb_print_nodeinfo(fbnode, hostname, fields=None):
34 #fbnode['hostname'] = hostname
35 #fbnode['checked'] = diff_time(fbnode['checked'])
36 if fbnode['bootcd_version']:
37 fbnode['bootcd_version'] = fbnode['bootcd_version'].split()[-1]
39 fbnode['bootcd_version'] = "unknown"
40 fbnode['pcu'] = color_pcu_state(fbnode)
43 if ( fbnode['observed_status'] is not None and \
44 'DOWN' in fbnode['observed_status'] ) or \
45 fbnode['kernel_version'] is None:
46 fbnode['kernel_version'] = ""
48 fbnode['kernel_version'] = fbnode['kernel_version'].split()[2]
50 if fbnode['plc_node_stats'] is not None:
51 fbnode['boot_state'] = fbnode['plc_node_stats']['boot_state']
53 fbnode['boot_state'] = "unknown"
56 if len(fbnode['nodegroups']) > 0:
57 fbnode['category'] = fbnode['nodegroups'][0]
59 #print "ERROR!!!!!!!!!!!!!!!!!!!!!"
62 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 format += "%%(%s)s " % f
70 indexes = path.split(".")
74 indexes = path.split(".")
77 if values and index in values:
78 values = values[index]
80 raise NoKeyException(index)
83 def verifyType(constraints, data):
85 constraints is a list of key, value pairs.
86 # [ {... : ...}==AND , ... , ... , ] == OR
89 for con in constraints:
90 #print "con: %s" % con
91 if len(con.keys()) == 0:
96 for key in con.keys():
97 #print "looking at key: %s" % key
105 if o.name() == "Match":
106 if get(data,key) is not None:
107 value_re = re.compile(o.value)
108 con_and_true = con_and_true & (value_re.search(get(data,key)) is not None)
111 elif o.name() == "ListMatch":
112 if get(data,key) is not None:
114 for listitem in get(data,key):
115 value_re = re.compile(o.value)
116 if value_re.search(listitem) is not None:
119 con_and_true = con_and_true & match
122 elif o.name() == "Is":
123 con_and_true = con_and_true & (get(data,key) == o.value)
124 elif o.name() == "FilledIn":
125 con_and_true = con_and_true & (len(get(data,key)) > 0)
126 elif o.name() == "PortOpen":
127 if get(data,key) is not None:
129 con_and_true = con_and_true & (v[str(o.value)] == "open")
133 value_re = re.compile(o.value)
134 con_and_true = con_and_true & (value_re.search(get(data,key)) is not None)
136 except NoKeyException, key:
137 print "missing key %s" % key,
139 #print "missing key %s" % key
140 #con_and_true = False
142 con_or_true = con_or_true | con_and_true
146 def verifyDBrecord(constraints, record):
148 constraints is a list of key, value pairs.
149 # [ {... : ...}==AND , ... , ... , ] == OR
151 def has_key(obj, key):
153 x = obj.__getattribute__(key)
158 def get_val(obj, key):
160 return obj.__getattribute__(key)
165 indexes = path.split("/")
166 value = get_val(obj,indexes[0])
167 if value is not None and len(indexes) > 1:
168 for key in indexes[1:]:
172 raise NoKeyException(key)
175 #print constraints, record
178 for con in constraints:
179 #print "con: %s" % con
180 if len(con.keys()) == 0:
185 for key in con.keys():
186 #print "looking at key: %s" % key
187 if has_key(record, key):
188 value_re = re.compile(con[key])
189 if type([]) == type(get(record,key)):
190 local_or_true = False
191 for val in get(record,key):
192 local_or_true = local_or_true | (value_re.search(val) is not None)
193 con_and_true = con_and_true & local_or_true
195 if get(record,key) is not None:
196 con_and_true = con_and_true & (value_re.search(get(record,key)) is not None)
198 print "missing key %s" % key,
201 con_or_true = con_or_true | con_and_true
205 def verify(constraints, data):
207 constraints is a list of key, value pairs.
208 # [ {... : ...}==AND , ... , ... , ] == OR
211 for con in constraints:
212 #print "con: %s" % con
213 if len(con.keys()) == 0:
218 for key in con.keys():
219 #print "looking at key: %s" % key
220 if first(key) in data:
221 value_re = re.compile(con[key])
222 if type([]) == type(get(data,key)):
223 local_or_true = False
224 for val in get(data,key):
225 local_or_true = local_or_true | (value_re.search(val) is not None)
226 con_and_true = con_and_true & local_or_true
228 if get(data,key) is not None:
229 con_and_true = con_and_true & (value_re.search(get(data,key)) is not None)
230 elif first(key) not in data:
231 print "missing key %s" % first(key)
233 con_or_true = con_or_true | con_and_true
237 def query_to_dict(query):
241 or_queries = query.split('||')
242 for or_query in or_queries:
243 and_queries = or_query.split('&&')
247 for and_query in and_queries:
248 (key, value) = and_query.split('=')
256 #if 'plcnode' in fbdata:
257 if 'plc_node_stats' in fbdata:
258 if fbdata['plc_node_stats'] and 'pcu_ids' in fbdata['plc_node_stats']:
259 if len(fbdata['plc_node_stats']['pcu_ids']) > 0:
263 def pcu_select(str_query, nodelist=None):
266 if str_query is None: return (nodenames, pcunames)
269 fbquery = FindbadNodeRecord.get_all_latest()
270 fb_nodelist = [ n.hostname for n in fbquery ]
272 # NOTE: this doesn't work when there are only a few records current.
273 # pcu_select should apply to all pcus globally, not just the most recent records.
274 fbpcuquery = FindbadPCURecord.get_all_latest()
275 fbpcu_list = [ p.plc_pcuid for p in fbpcuquery ]
277 dict_query = query_to_dict(str_query)
278 print "dict_query", dict_query
279 print 'length %s' % len(fbpcuquery.all())
281 for pcurec in fbpcuquery:
282 pcuinfo = pcurec.to_dict()
283 if verify(dict_query, pcuinfo):
284 #nodenames.append(noderec.hostname)
285 #print 'appending %s' % pcuinfo['plc_pcuid']
286 pcunames.append(pcuinfo['plc_pcuid'])
288 #for noderec in fbquery:
289 # if nodelist is not None:
290 # if noderec.hostname not in nodelist: continue
292 # fb_nodeinfo = noderec.to_dict()
293 # if pcu_in(fb_nodeinfo):
294 # pcurec = FindbadPCURecord.get_latest_by(plc_pcuid=get(fb_nodeinfo,
295 # 'plc_node_stats.pcu_ids')[0]).first()
297 # pcuinfo = pcurec.to_dict()
298 # if verify(dict_query, pcuinfo):
299 # nodenames.append(noderec.hostname)
300 # pcunames.append(pcuinfo['plc_pcuid'])
301 return (nodenames, pcunames)
303 def node_select(str_query, nodelist=None, fb=None):
306 if str_query is None: return hostnames
309 dict_query = query_to_dict(str_query)
312 for node in nodelist:
313 #if nodelist is not None:
314 # if node not in nodelist: continue
318 #fb_noderec = FindbadNodeRecord.query.filter(FindbadNodeRecord.hostname==node).order_by(FindbadNodeRecord.date_checked.desc()).first()
319 fb_noderec = FindbadNodeRecord.get_latest_by(hostname=node)
321 print traceback.print_exc()
325 fb_nodeinfo = fb_noderec.to_dict()
327 #fb_nodeinfo['pcu'] = color_pcu_state(fb_nodeinfo)
328 #if 'plcnode' in fb_nodeinfo:
329 # fb_nodeinfo.update(fb_nodeinfo['plcnode'])
331 #if verifyDBrecord(dict_query, fb_nodeinfo):
332 if verify(dict_query, fb_nodeinfo):
333 #print fb_nodeinfo.keys()
334 #print node #fb_nodeinfo
335 hostnames.append(node)
337 #print "NO MATCH", node
345 from monitor import parser as parsermodule
346 parser = parsermodule.getParser()
348 parser.set_defaults(node=None, fromtime=None, select=None, list=None, listkeys=False,
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("", "--listkeys", dest="listkeys", action="store_true",
363 help="A list of nodes to bring out of debug mode.")
364 parser.add_option("", "--fromtime", dest="fromtime", metavar="YYYY-MM-DD",
365 help="Specify a starting date from which to begin the query.")
367 parser = parsermodule.getParser(['defaults'], parser)
368 config = parsermodule.parse_args(parser)
372 archive = database.SPickle(path)
373 d = datetime_fromstr(config.fromtime)
374 glob_str = "%s*.production.findbad.pkl" % d.strftime("%Y-%m-%d")
377 file = glob.glob(glob_str)[0]
378 #print "loading %s" % file
380 fb = archive.load(file[:-4])
382 #fbnodes = FindbadNodeRecord.select(FindbadNodeRecord.q.hostname, orderBy='date_checked',distinct=True).reversed()
386 nodelist = util.file.getListFromFile(config.nodelist)
388 # NOTE: list of nodes should come from findbad db. Otherwise, we
389 # don't know for sure that there's a record in the db..
390 plcnodes = plccache.l_nodes
391 nodelist = [ node['hostname'] for node in plcnodes ]
392 #nodelist = ['planetlab-1.cs.princeton.edu']
395 if config.select is not None and config.pcuselect is not None:
396 nodelist = node_select(config.select, nodelist, fb)
397 nodelist, pculist = pcu_select(config.pcuselect, nodelist)
398 elif config.select is not None:
399 nodelist = node_select(config.select, nodelist, fb)
400 elif config.pcuselect is not None:
401 nodelist, pculist = pcu_select(config.pcuselect, nodelist)
407 for node in nodelist:
410 if node not in nodelist:
414 # Find the most recent record
415 fb_noderec = FindbadNodeRecord.get_latest_by(hostname=node)
416 if not fb_noderec: continue
417 fb_nodeinfo = fb_noderec.to_dict()
419 print traceback.print_exc()
423 print "Primary keys available in the findbad object:"
424 for key in fb_nodeinfo.keys():
433 daysdown_print_nodeinfo(fb_nodeinfo, node)
437 fields = config.fields.split(",")
441 fb_print_nodeinfo(fb_nodeinfo, node, fields)
442 elif not config.select and 'state' in fb_nodeinfo:
443 fb_print_nodeinfo(fb_nodeinfo, node)
447 if __name__ == "__main__":