Exemplo n.º 1
0
def get_services_aggregator(agg_conf):
    services = {}
    add_attr = {}
    add_attr_order = []
    added_hosts = []
    tmp_add_verb_attr = []
    for name, hostname, verb, _attr_values in agg_conf:
        if hostname not in services:
            services[hostname] = service(name, hostname)
        service_obj = services[hostname]

        attr_values, order = process_service_attr_values(_attr_values)
        if verb == "add":
            host = attr_values['host']
            if "sets" in attr_values:
                attr_values['sets'] = attr_values['sets'].replace(host + "/", "")

            attr_values['host'] = ""
            if add_attr == attr_values:
                added_hosts.append(host)
            else:
                """
                Done with the current 'add' verb. This is true because the query
                is ordered by the cmd_id. All hosts (specified with the
                current host) that have the same interval, xprt, port, etc
                as the current one are found already.
                """
                if len(added_hosts) > 0:
                    added_hosts.sort()
                    add_attr['host'] = osqlite_util.collapse_string(added_hosts)
                    tmp_add_verb_attr.append({'verb': verb, \
                                              'attr_values': (add_attr, add_attr_order)})
                add_attr = attr_values
                add_attr_order = order
                added_hosts = [host]
        else:
            if len(added_hosts) > 0:
                added_hosts.sort()
                add_attr['host'] = osqlite_util.collapse_string(added_hosts)
                tmp_add_verb_attr.append({'verb': "add", \
                                          'attr_values': (add_attr, add_attr_order)})
                tmp_add_verb_attr.reverse()
                service_obj.merge_verb_attr(tmp_add_verb_attr)
                tmp_add_verb_attr = []
                add_attr = {}
                add_attr_order = []
                added_hosts = []

            if "hosts" in attr_values:
                value_s = osqlite_util.collapse_string(attr_values['hosts'].split(","))
                attr_values['hosts'] = value_s

            service_obj.add_verb_attr(verb, attr_values, order)
    return services
Exemplo n.º 2
0
 def __str__(self):
     s = INDENT + "template_name: " + self.name + NEWLINE
     name_s = ""
     for comp_type in self.component_ordering:
         ids = self.component[comp_type]['identifier']
         names = self.component[comp_type]['name']
         id_s = osqlite_util.collapse_string(ids)
         if len(names) > 0:
             name_s = osqlite_util.collapse_string(names)
         s += INDENT + INDENT + "component: " + comp_type + "{" + id_s + "}" + NEWLINE
         if name_s != "":
             s += INDENT + INDENT + INDENT + "name: " + name_s + NEWLINE
     return s
Exemplo n.º 3
0
def process_balerd_hosts(s):
    hosts = s.split(";")
    names = []
    for host in hosts:
        tmp = host.split(":")
        names.append(tmp[0])
    return ({'names': osqlite_util.collapse_string(names)}, ["names"])
Exemplo n.º 4
0
def process_identifier_name_map(identifiers, names):
    # TODO: call with the sony-test
    n_names = len(names)
    n_identifiers = len(identifiers)

    if (n_names == 0) or (n_identifiers == 0):
        raise ValueError("Unexpected identifier and name map. " \
                         "The list of either names or identifiers is empty")

    if n_names == 1:
        return (osqlite_util.collapse_string(identifiers), names[0])

    # Number of names > 1
    if n_names != n_identifiers:
        error_s = "Number of names and number of identifiers are different. " \
                  "Identifiers: " + ",".join(identifiers) + ". " \
                  "Names: " + ",".join(names)
        raise ValueError("Unexpected identifier and name map. " + error_s)

    identifiers_s = osqlite_util.collapse_string(identifiers)
    names_s = osqlite_util.collapse_string(names)
    if identifiers_s.count("[") > 1:
        pass
    return (identifiers_s, names_s)
Exemplo n.º 5
0
def _process_event_components(cursor, event):
    components_text = ""
    comp_types = {}
    for comp_id in sorted(event.components.keys()):
        ctype = event.components[comp_id]['comp_type']
        if ctype not in comp_types:
            comp_types[ctype] = []
        comp_types[ctype].append(event.components[comp_id]['identifier'])
    for ctype in comp_types:
        cursor.execute("SELECT COUNT(*) FROM components WHERE type = ?", (ctype,))
        num = cursor.fetchone()[0]
        if components_text != "":
            components_text += ","
        if num == len(comp_types[ctype]):
            components_text += ctype + "{*}"
        else:
            components_text += ctype + "{"
            components_text += osqlite_util.collapse_string(comp_types[ctype])
            components_text += "}"
    return components_text
Exemplo n.º 6
0
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)
Exemplo n.º 7
0
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
Exemplo n.º 8
0
 def _process_components_text(self):
     if len(self.components) == 0:
         return ""
     comps = sorted(self.components.keys())
     comps = map(str, comps)
     return osqlite_util.collapse_string(comps)
Exemplo n.º 9
0
 def _process_metric_names(self):
     tmp = [self.metrics[x] for x in sorted(self.metrics.keys())]
     return osqlite_util.collapse_string(tmp)