Example #1
0
def generate_context_topology(topo, name='context'):
    """Generate a context topology where nodes are components and resources,
    and edges are dependencies from components to resources, or from resources
    to the topology.

    :param str name: topology name.
    """

    # initialize context and topology
    context = Context()

    def addElt(elt):
        """
        Add input elt in topology.

        :param GraphElement elt: elt to add to topology.
        """

        topo.add_elts(elt.id)
        elt.save(manager)

    components = context.find({'$in': ['component', 'topo', 'selector']})
    for component in components:
        component_id = context.get_entity_id(component)
        component_node = TopoNode(entity=component_id)
        addElt(component_node)

        ctx, name = context.get_entity_context_and_name(component)
        ctx['component'] = name
        resources = context.find('resource', context=ctx)
        if resources:  # link component to all its resources with the same edge
            edge = TopoEdge(sources=component_node.id, targets=[])
            addElt(edge)  # add edge in topology
            for resource in resources:
                resource_id = context.get_entity_id(resource)
                resource_node = TopoNode(entity=resource_id)
                addElt(resource_node)  # save resource node
                # add resource from component
                edge.targets.append(resource_node.id)
                res2topo = TopoEdge(
                    sources=resource_node.id, targets=topo.id
                )
                addElt(res2topo)
            if not edge.targets:  # bind topology from component if not sources
                edge.targets.append(topo.id)
            addElt(edge)  # save edge in all cases
        else:  # if no resources, link the component to the topology
            edge = TopoEdge(sources=component_node.id, targets=topo.id)
            addElt(edge)  # add edge in topology
Example #2
0
class engine(Engine):
    etype = 'context'

    def __init__(self, *args, **kwargs):
        super(engine, self).__init__(*args, **kwargs)

        # get a context
        self.context = Context()
        """
        TODO: sla
        # get a storage for sla macro
        #self.storage = Middleware.get_middleware(
        #    protocol='storage', data_scope='global')

        #self.sla = None
        """

        self.entities_by_entity_ids = {}
        self.lock = Lock()
        self.beat()

    def beat(self):
        """
        TODO: sla

        .. code-block:: python

            sla = self.storage.find_elements(request={
                'crecord_type': 'sla',
                'objclass': 'macro'
            })

            if sla:
                self.sla = sla[0]
        """

        self.lock.acquire()
        entities_by_entity_ids = self.entities_by_entity_ids.copy()
        self.entities_by_entity_ids = {}
        self.lock.release()

        entities = self.context[Context.CTX_STORAGE].get_elements(
            ids=entities_by_entity_ids.keys()
        )

        for entity in entities:
            del entities_by_entity_ids[entity['_id']]

        if entities_by_entity_ids:
            context = self.context
            for entity_id in entities_by_entity_ids:
                _type, entity, ctx = entities_by_entity_ids[entity_id]
                context.put(
                    _type=_type, entity=entity, context=ctx
                )

    def work(self, event, *args, **kwargs):
        mCrit = 'PROC_CRITICAL'
        mWarn = 'PROC_WARNING'

        """
        TODO: sla

        .. code-block::

            if self.sla:
                mCrit = self.sla.data['mCrit']
                mWarn = self.sla.data['mWarn']
        """

        context = {}

        # Get event informations
        hostgroups = event.get('hostgroups', [])
        servicegroups = event.get('servicegroups', [])
        component = event.get('component')
        resource = event.get('resource')
        # quick fix when an event has an empty resource
        if 'resource' in event and not resource:
            del event['resource']

        # get a copy of event
        _event = event.copy()

        # add hostgroups
        for hostgroup in hostgroups:
            hostgroup_data = {
                Context.NAME: hostgroup
            }
            self.context.put(
                _type='hostgroup', entity=hostgroup_data, cache=True
            )
        # add servicegroups
        for servicegroup in servicegroups:
            servgroup_data = {
                Context.NAME: servicegroup
            }
            self.context.put(
                _type='servicegroup', entity=servgroup_data, cache=True
            )

        # get related entity
        entity = self.context.get_entity(
            _event, from_db=True, create_if_not_exists=True, cache=True
        )

        # set service groups and hostgroups
        if resource:
            context['component'] = component
            entity['servicegroups'] = servicegroups
        entity['hostgroups'] = hostgroups

        # set mCrit and mWarn
        entity['mCrit'] = _event.get(mCrit, None)
        entity['mWarn'] = _event.get(mWarn, None)

        context, name = self.context.get_entity_context_and_name(entity)

        if 'resource' in context and not context['resource']:
            del context['resource']
        if 'resource' in entity and not entity['resource']:
            del entity['resource']

        # put the status entity in the context
        self.context.put(
            _type=entity[Context.TYPE], entity=entity, context=context,
            cache=True
        )

        # udpdate context information with resource and component
        if resource:
            context['resource'] = name
        else:
            context['component'] = name

        # remove type from context because type will be metric
        del context[Context.TYPE]

        # add perf data (may be done in the engine perfdata)
        for perfdata in event.get('perf_data_array', []):
            perfdata_entity = {}
            name = perfdata['metric']
            perfdata_entity[Context.NAME] = name
            perfdata_entity['internal'] = perfdata['metric'].startswith('cps')
            self.context.put(
                _type='metric', entity=perfdata_entity, context=context,
                cache=True
            )

        return event