class DowntimeProcessingTest(TestCase): def setUp(self): self.downtimes = PBehaviorManager(data_scope='test_pbehavior') self.events = Event(data_scope='test_events') self.context = Context(data_scope='test_context') def tearDown(self): self.downtimes.remove() self.events.remove() self.context.remove()
def handle_task(self, job): self.link_list_manager = Linklist() self.context = Context() self.event = Event() self.entity_link_manager = Entitylink() """ This task computes all links associated to an entity. Link association are managed by entity link system. """ for entity_id in self.context.iter_ids(): self.entity_link_manager.put(entity_id, { 'computed_links': [] }) links = {} # Computes links for all context elements # may cost some memory depending on filters and context size for linklist in self.link_list_manager.find(): # condition to proceed a list link is they must be set name = linklist['name'] l_filter = linklist.get('mfilter') l_list = linklist.get('filterlink') self.logger.debug(u'proceed linklist {}'.format(name)) if not l_list or not l_filter: self.logger.info(u'Cannot proceed linklist for {}'.format(name)) else: # Find context element ids matched by filter context_ids = self.get_ids_for_filter(l_filter) # Add all linklist to matched context element for context_id in context_ids: if context_id not in links: links[context_id] = [] # Append all links/labels to the context element links[context_id] += l_list self.logger.debug(u'links') self.logger.debug(links) entities = self.context.get_entities(links.keys()) for entity in entities: self.update_context_with_links( entity, links[entity['_id']] ) return (0, 'Link list computation complete')
class engine(TaskHandler): etype = 'tasklinklist' event_projection = { 'resource': 1, 'source_type': 1, 'component': 1, 'connector_name': 1, 'connector': 1, 'event_type': 1, } def handle_task(self, job): self.link_list_manager = Linklist() self.context = Context() self.event = Event() self.entity_link_manager = Entitylink() """ This task computes all links associated to an entity. Link association are managed by entity link system. """ for entity_id in self.context.iter_ids(): self.entity_link_manager.put(entity_id, { 'computed_links': [] }) links = {} # Computes links for all context elements # may cost some memory depending on filters and context size for linklist in self.link_list_manager.find(): # condition to proceed a list link is they must be set name = linklist['name'] l_filter = linklist.get('mfilter') l_list = linklist.get('filterlink') self.logger.debug(u'proceed linklist {}'.format(name)) if not l_list or not l_filter: self.logger.info(u'Cannot proceed linklist for {}'.format(name)) else: # Find context element ids matched by filter context_ids = self.get_ids_for_filter(l_filter) # Add all linklist to matched context element for context_id in context_ids: if context_id not in links: links[context_id] = [] # Append all links/labels to the context element links[context_id] += l_list self.logger.debug(u'links') self.logger.debug(links) entities = self.context.get_entities(links.keys()) for entity in entities: self.update_context_with_links( entity, links[entity['_id']] ) return (0, 'Link list computation complete') def update_context_with_links(self, entity, links): """ Upsert computed links to the entity link storage """ self.logger.debug(u' + entity') self.logger.debug(entity) self.logger.debug(u' + links') self.logger.debug(links) context = { 'computed_links': links } _id = self.context.get_entity_id(entity) self.entity_link_manager.put(_id, context) def get_ids_for_filter(self, l_filter): """ Retrieve a list of id from event collection. Can be performance killer as matching mfilter is only available on the event collection at the moment """ context_ids = [] try: l_filter = loads(l_filter) except Exception as e: self.logger.error( 'Unable to parse mfilter, query aborted {}'.format(e) ) return context_ids events = self.event.find( query=l_filter, projection=self.event_projection ) for event in events: self.logger.debug(u'rk : {}'.format(event['_id'])) entity = self.context.get_entity(event) entity_id = self.context.get_entity_id(entity) context_ids.append(entity_id) return context_ids
def __init__( self, storage, rk, template, timewindow_dict, sla_warning, sla_critical, alert_level, display_name, logger=None ): self.storage = storage self.eventmanager = Event() self.type = 'sla' if logger: self.logger = logger else: self.logger = getLogger('Sla') self.states = [0, 1, 2, 3] now = time() timewindow = timewindow_dict['seconds'] timewindow_date_start = now - timewindow self.logger.debug(u'Timewindow is {}, timestamp is {}'.format( timewindow, timewindow_date_start )) self.logger.debug(u'Computing sla for selector {}'.format(rk)) # Retrieve sla information from selector record sla_information = self.get_sla_information( rk, timewindow_date_start, now ) self.logger.debug(u'Sla length information is {}'.format( len(sla_information) )) # Compute effective sla dict to be able to fill the ouput template sla_measures, sla_times = self.compute_sla( sla_information, now ) self.logger.debug(u'Sla measures is {}'.format(sla_measures)) self.logger.debug(u'Sla times is {}'.format(sla_times)) self.logger.debug(u'Alert level is {}'.format(alert_level)) # Compute alerts precent depending on user algorithm alerts_percent, alerts_duration = \ self.get_alert_percent( sla_measures, sla_times, alert_level ) self.logger.debug(u'Alert percent : {} , Alert duration : {}'.format( alerts_percent, alerts_duration )) avail_duration = timewindow - alerts_duration # Compute template from sla measures output = self.compute_output( template, sla_measures, alerts_percent, alerts_duration, avail_duration, timewindow_date_start, ) state = self.compute_state( alerts_percent, sla_warning, sla_critical ) self.logger.debug(u'Sla computed state is {}'.format(state)) self.logger.debug(u'thresholds : warning {}, critical {}'.format( sla_warning, sla_critical )) self.event = self.prepare_event( display_name, sla_measures, output, state, alerts_percent, alerts_duration, avail_duration, timewindow_dict, now )
def get_events(self, alarm): """ Rebuild events from alarm history. :param alarm: Alarm to use for events reconstruction :type alarm: dict :returns: Array of events """ storage = self[Alerts.ALARM_STORAGE] alarm_id = alarm[storage.DATA_ID] alarm = alarm[storage.VALUE] entity = self[Alerts.CONTEXT_MANAGER].get_entity_by_id(alarm_id) no_author_tupes = ['stateinc', 'statedec', 'statusinc', 'statusdec'] check_referer_types = [ 'ack', 'ackremove', 'cancel', 'uncancel', 'declareticket', 'assocticket', 'changestate' ] typemap = { 'stateinc': Check.EVENT_TYPE, 'statedec': Check.EVENT_TYPE, 'statusinc': Check.EVENT_TYPE, 'statusdec': Check.EVENT_TYPE, 'ack': 'ack', 'ackremove': 'ackremove', 'cancel': 'cancel', 'uncancel': 'uncancel', 'declareticket': 'declareticket', 'assocticket': 'assocticket', 'changestate': 'changestate' } valmap = { 'stateinc': Check.STATE, 'statedec': Check.STATE, 'changestate': Check.STATE, 'statusinc': Check.STATUS, 'statusdec': Check.STATUS, 'assocticket': 'ticket' } events = [] eventmodel = self[Alerts.CONTEXT_MANAGER].get_event(entity) for step in alarm['steps']: event = eventmodel.copy() event['timestamp'] = step['t'] event['output'] = step['m'] if step['_t'] in valmap: field = valmap[step['_t']] event[field] = step['val'] if step['_t'] not in no_author_tupes: event['author'] = step['a'] if step['_t'] in check_referer_types: event['event_type'] = 'check' event['ref_rk'] = Event.get_rk(event) if Check.STATE not in event: event[Check.STATE] = get_last_state(alarm) event['event_type'] = typemap[step['_t']] for field in self.extra_fields: if field in alarm['extra']: event[field] = alarm['extra'][field] events.append(event) return events
def setUp(self): self.downtimes = PBehaviorManager(data_scope='test_pbehavior') self.events = Event(data_scope='test_events') self.context = Context(data_scope='test_context')