def _get_event(cursor, event_id, event_name, model_id): e = event(event_id, event_name, model_id) _metrics = osqlite_util.query_event_metrics(cursor, [event_id]) if _metrics is not None: for event_id, comp_id, metric_id in _metrics: if model_id != USER_EVENT_MODEL_ID: mnames = osqlite_util.query_metrics(cursor, \ metric_id_list = [metric_id]) if mnames is None: raise OCSqliteError("Metric ID " + str(metric_id) + " not found") mname, metric_id, a1, a2, coll_comp, a2 = mnames[0] e.add_metric(metric_id, mname) # Query the component type and the identifier of the coll_comp comp = osqlite_util.query_components(cursor, comp_name = coll_comp) else: comp = osqlite_util.query_components(cursor, comp_id_list=[comp_id]) if comp is None: raise OCSqliteError(event.name + ": " + "component name " + \ coll_comp + " not found") if len(comp) > 1: raise OCSqliteError(event.name + ": node " + coll_comp + \ " not unique") comp_name, comp_type, identifier, comp_id, parent_id = comp[0] e.add_component(comp_id, comp_type, identifier) _actions = osqlite_util.query_event_msg_action(cursor, [event_id]) if _actions is None: return e for event_id, level, msg, action in _actions: level_name = osqlite_util.get_severity_level(level) e.add_severity_action(level_name, action) e.set_severity_msg(level_name, msg) return e
def get_apply_on_metric_component(cursor, metric_s, template): metric_info = {'extension': [], 'metric_id': []} metrics = metric_s.split(',') for metric in metrics: tmp = osqlite_util.process_metric_name_id_string(metric) if tmp is None: raise OCSqliteError(metric + ": Couldn't get metric_name and metric_id") metric_info['metric_id'].append(tmp['metric_id']) metric_info['extension'].append(tmp['extension']) tmp = ",".join(['?'] * len(metric_info['metric_id'])) cursor.execute("SELECT metrics.metric_id,components.type, components.identifier " \ "FROM components JOIN metrics WHERE " \ "metrics.prod_comp_id = components.comp_id AND " \ "metrics.metric_id in (" + tmp + ")", metric_info['metric_id']) queried_comps = cursor.fetchall() if len(queried_comps) < 1: raise OCSqliteError("Producer component of metric " + \ str(metric_info['metric_id']) + " not found") comps = {} comps_order = [] for metric_id, comp_type, identifier in queried_comps: if comp_type not in comps: comps[comp_type] = {'identifier': [], 'name': []} comps_order.append(comp_type) idx = metric_info['metric_id'].index(str(metric_id)) if identifier not in comps[comp_type]['identifier']: comps[comp_type]['identifier'].append(identifier) template_metrics_s = template.get_metric_text(comp_type) if ('#' not in template_metrics_s) and \ (metric_info['extension'][idx] != ""): comps[comp_type]['name'].append(metric_info['extension'][idx]) return (comps, comps_order)
def get_template_apply_on(cursor, all_templates): apply_on_hosts = {} order = [] tmp = osqlite_util.query_template_apply_on(cursor) if tmp is None: raise OCSqliteError("No apply on record") # Reverse the order because the parser inserts the apply_on in reverse order tmp.reverse() for host, template_name, metric_s in tmp: # for each template apply_on = None ''' Get the host to apply the templates on ''' _comp = osqlite_util.query_components(cursor, comp_name = host) if len(_comp) > 1: raise OCSqliteError("More than 1 component associated with the host " + _comp) name, comp_type, identifier, comp_id, parent_id = _comp[0] ''' Only one iteration ''' if name not in apply_on_hosts: apply_on_hosts[name] = apply_on_node(comp_type, identifier) order.append(name) apply_on = apply_on_hosts[name] template = all_templates[template_name] comps, comps_order = get_apply_on_metric_component(cursor, metric_s, template) temp = apply_on_node.apply_on_template(template_name) temp.set_components(comps, comps_order) apply_on.add_template(temp) return (apply_on_hosts, order)
def get_sampler_templates(cursor): templates = osqlite_util.query_template_info(cursor) # Nov 24, 2014 # Reverse the order because the oparser_sqlite inserts in reverse order # of the text file if templates is None: raise OCSqliteError("No sampler templates") sampler_templates = {} _sampler_templates_order = [] mtype_id_order = [] for name, ldmsd_set, cfg, metrics in templates: temp = sampler_template(name, ldmsd_set) tmp = get_template_metric_component_types(cursor, metrics) mtype_id, comp_metrics, order = tmp mtype_id_order.append(mtype_id) if comp_metrics is None: print "template " + name + \ ": failed to get component metrics." return None temp.set_component_and_metrics(comp_metrics, order) template_cfg, order = get_template_config(cfg) if template_cfg is None: raise OCSqliteError("template " + name + \ ": Failed to get template configuration") temp.set_cfg(template_cfg, order) sampler_templates[temp.name] = temp _sampler_templates_order.append(temp.name) templ_idx_order = sorted(xrange(len(mtype_id_order)), key = lambda k: mtype_id_order[k]) sampler_templates_order = [_sampler_templates_order[i] for i in templ_idx_order] return (sampler_templates, sampler_templates_order)
def get_template_metric_component_types(cursor, metric_s): metrics = metric_s.split(',') comp_type_metrics = {} comp_type_info = {} mtype_id_order = [] ctype_order = [] metric_info = {} for metric in metrics: tmp = osqlite_util.process_metric_name_id_string(metric) if tmp is None: raise OCSqliteError(metric + \ ": Couldn't get metric_name and metric_id") metric_info[int(tmp['metric_id'])] = tmp tmp = ",".join(['?'] * len(metric_info.keys())) cursor.execute("SELECT metric_id,metric_type_id,prod_comp_id,type,identifier " \ "FROM metrics JOIN components WHERE " \ "prod_comp_id = comp_id AND metric_id IN (" + tmp + ")", \ metric_info.keys()) queried_comps = cursor.fetchall() if len(queried_comps) < 1: raise OCSqliteError("producer component of metric " + \ str(metric_info['metric_id']) + " not found") for metric_id, mtype_id, prod_comp_id, comp_type, identifier in queried_comps: if comp_type not in comp_type_info: comp_type_info[comp_type] = {'mbase_name': [metric_info[metric_id]['base_name']], \ 'mfull_name': [metric_info[metric_id]['full_name']], \ 'identifier': {}} mtype_id_order.append(osqlite_util.get_metric_type_id(metric_id)) ctype_order.append(comp_type) else: if metric_info[metric_id]['base_name'] not in comp_type_info[comp_type]['mbase_name']: comp_type_info[comp_type]['mbase_name'].append(metric_info[metric_id]['base_name']) if metric_info[metric_id]['full_name'] not in comp_type_info[comp_type]['mfull_name']: comp_type_info[comp_type]['mfull_name'].append(metric_info[metric_id]['full_name']) if identifier not in comp_type_info[comp_type]['identifier']: comp_type_info[comp_type]['identifier'][identifier] = [metric_info[metric_id]['extension']] else: if metric_info[metric_id]['extension'] not in comp_type_info[comp_type]['identifier'][identifier]: comp_type_info[comp_type]['identifier'][identifier].append(metric_info[metric_id]['extension']) if len(ctype_order) == 0: return None ctype_order_idx = sorted(xrange(len(mtype_id_order)), key = lambda k: mtype_id_order[k]) order = [] for i in ctype_order_idx: comp_type = ctype_order[i] comp_type_metrics[comp_type] = [] key = 'mbase_name' if len(comp_type_info[comp_type]['identifier']) == 1: id = comp_type_info[comp_type]['identifier'].keys()[0] if len(comp_type_info[comp_type]['identifier'][id]) > 1: key = 'mfull_name' comp_type_metrics[comp_type] = comp_type_info[comp_type][key] order.append(comp_type) return (mtype_id_order[ctype_order_idx[0]], comp_type_metrics, order)
def get_component_types(cursor): all_comp_types = {} comp_types_order = [] all_comp_map = {} comp_types = osqlite_util.query_comp_type_info(cursor) if comp_types is None: raise OCSqliteError("No component types in the database") for comp_type, gif_path, visible, category in comp_types: if gif_path is None: # This is to filter out the cable components. # For all component types defined in components.conf, # gif_path is an empty string if it isn't given. # The parser should give a more reliable way # to detect whether a component should be printed # into the components.conf file or not continue comp_types_order.append(comp_type) ctype, all_comp_map = process_component_type(cursor, comp_type = comp_type, \ gif_path = gif_path, \ visible = visible, \ category = category, \ all_comp_map=all_comp_map) all_comp_types[comp_type] = ctype return (all_comp_types, comp_types_order, all_comp_map)
def get_cables(cursor, all_cable_types): cables = osqlite_util.query_cables(cursor) if cables is None: raise OCSqliteError("No cables") cables.reverse() cable_types = {} cable_types_order = [] for type_id, src_comp_id, dest_comp_id in cables: if type_id not in cable_types: cable_types[type_id] = [] cable_types_order.append(type_id) cb_type = all_cable_types[type_id].get_type() src = osqlite_util.query_component(cursor, src_comp_id) if src is None: raise OCSqliteError("comp_id " + str(src_comp_id) + " not found") src_type = src[1] src_identifier = src[2] dest = osqlite_util.query_component(cursor, dest_comp_id) if dest is None: raise Exception("comp_id " + str(dest_comp_id) + " not found") dest_type = dest[1] dest_identifier = dest[2] parent_id_s = str(src_comp_id) + "," + str(dest_comp_id) candidate_cables = osqlite_util.query_components(cursor, \ parent_id_s = parent_id_s) if candidate_cables is None: raise OCSqliteError("No cables that link between comp_ids " + \ str(src_comp_id) + " and " + str(dest_comp_id)) cb = None for name, type, identifier, comp_id, parent_id_s in candidate_cables: if type == cb_type: cb = cable(type, name) cb.set_src(src_type, src_identifier, src_comp_id) cb.set_dest(dest_type, dest_identifier, dest_comp_id) cable_types[type_id].append(cb) break if cb is None: raise OCSqliteError("No cables of type " + cb_type + "that link " \ "between comp_ids " + str(src_comp_id) + \ " and " + str(dest_comp_id)) cables = [] for cb_type in cable_types_order: cables += cable_types[cb_type] return cables
def process_component_type(cursor, comp_type, gif_path, category, visible, all_comp_map): identifiers = [] names = [] try: ctype = component_type(comp_type=comp_type, gif_path=gif_path, category=category, visible=visible) all_comps = osqlite_util.query_components(cursor, comp_type = comp_type) # ordered by comp_id if all_comps is None: raise OCSqliteError("No components of type " + comp_type) for name, _type, identifier, comp_id, parent_id in all_comps: ptn, fmt, num_var, vars = osqlite_util.get_format(identifier) comp = component(name, _type, comp_id, identifier) all_comp_map[comp_id] = comp ctype.add_component(comp) identifiers.append(identifier) names.append(name) give_names = get_give_name(identifiers, names) for give_name in give_names: ctype.add_give_name(give_name) return (ctype, all_comp_map) except AssertionError: raise except Exception: raise
def _get_component_tree_text(comp_tree, comp_id, num_indent): node = comp_tree.get_node_data(comp_id) if node is None: raise OCSqliteError("comp " + str(comp_id) + " not found") comp = node['comp'] leaves = {} leaves_order = [] if comp_tree.is_leaf(comp_id) or node['visited']: ''' If leave, just return the empty string and itself ''' node['visited'] = True return ("", node) else: s = (INDENT * num_indent) + comp.comp_type + "{" + comp.identifier + "}" s += "/" + NEWLINE num_indent += 1 for child in comp_tree.get_children(comp_id): tmp = _get_component_tree_text(comp_tree, child, num_indent) leaf = tmp[1] if leaf is not None: comp = leaf['comp'] if comp.comp_type not in leaves: leaves[comp.comp_type] = [] leaves_order.append(comp.comp_type) if comp.identifier not in leaves[comp.comp_type]: leaves[comp.comp_type].append(comp.identifier) else: s += tmp[0] if len(leaves) > 0: for child_type in leaves_order: s += (INDENT * num_indent) + child_type + \ "{" + osqlite_util.collapse_string(leaves[child_type]) + "}" \ + NEWLINE node['visited'] = True return (s, None)
def get_event_metrics_components_text(cursor, event): metrics_text = "" components_text = "" try: if event.model_id == USER_EVENT_MODEL_ID: components_text = _process_event_components(cursor, event) return (metrics_text, components_text) mname_fmt = {'base': "", 'ext': []} for metric_id in sorted(event.metrics.keys()): mname = event.metrics[metric_id] # Find the name format if 'baler_ptn' in mname: mname_fmt['base'] = '%baler_ptn%' idx = mname.index('#') ext = mname[:idx] elif '#' in mname: idx = mname.index('#') + 1 mname_fmt['base'] = mname[:idx] + "%" ext = mname[idx:] else: mname_fmt['base'] = mname ext = "" if ext not in mname_fmt['ext']: mname_fmt['ext'].append(ext) # Query distinct components collecting the metrics cursor.execute("SELECT COUNT(DISTINCT(comp_id)) FROM metrics " \ "JOIN components WHERE coll_comp = components.name AND " \ "metrics.name LIKE '" + mname_fmt['base'] + "' ORDER BY comp_id") exp_num = cursor.fetchone() if exp_num is None: raise Exception(event.name + ": components collecting the metrics not found") exp_num = exp_num[0] if len(event.components) == exp_num: components_text = "*" else: components_text = _process_event_components(cursor, event) # Find the total number of metrics with the name format cursor.execute("SELECT COUNT(DISTINCT(name)) FROM metrics " \ "WHERE name LIKE '" + mname_fmt['base'] + "'") exp_count = cursor.fetchone() if exp_count is None: raise OCSqliteError(event.name + ": metric " + mname_fmt + " not found") exp_count = exp_count[0] if len(mname_fmt['ext']) == exp_count: if 'baler_ptn' in mname_fmt['base']: metrics_text = "[*]#baler_ptn" else: metrics_text = mname_fmt['base'].replace("%", "[*]") else: mname_fmt['base'] = mname_fmt['base'].replace("%", "") if 'baler_ptn' in mname_fmt['base']: full_name_s = [s + "#" + mname_fmt['base'] for s in sorted(mname_fmt['ext'])] metrics_text = osqlite_util.collapse_string(full_name_s) else: full_name_s = [mname_fmt['base'] + s for s in sorted(mname_fmt['ext'])] metrics_text = osqlite_util.collapse_string(full_name_s) return (metrics_text, components_text) except Exception: raise