def reload_selectors(self): """ Loads selectors crecords to find out witch event in work method match selector. When an event match, selector name and dispay name fields are added to the event tag list """ self.selectors = [] self.selByRk.clear() selectors_json = self.storage.find({ 'crecord_type': 'selector', 'enable': True }, namespace="object") for selector_json in selectors_json: selector_dump = selector_json.dump() # add selector name to tag when if 'rk' in selector_dump: # Extract ids resolved by selectors sel = selector_dump.get('crecord_name') ids = selector_dump.get('ids', []) if isinstance(ids, list): for rk in ids: if rk in self.selByRk: self.selByRk[rk].append(sel) else: self.selByRk[rk] = [sel] # Put selector witch can tag event in cache if 'dostate' in selector_dump and selector_dump['dostate']: selector = Selector( storage=self.storage, record=selector_json, logging_level=self.logging_level) # tag field is defined here only selector.tags = [] for selector_tag in ['crecord_name', 'display_name']: if (selector_tag in selector_dump and selector_dump[selector_tag]): selector.tags.append(selector_dump[selector_tag]) self.selectors.append(selector) self.logger.debug('Reloaded %s selectors' % (len(self.selectors)))
def test_01_InitPutGet(self): global selector selector = Selector( name="myselector", namespace='unittest', storage=storage) selector.nocache = True _id = selector._id print("Selector Id: %s" % _id) dump = selector.dump() ## Put in db storage.put(selector) ## Load selector = Selector(name="myselector", storage=storage) ndump = selector.dump() if dump['_id'] != ndump['_id'] or dump['namespace'] != ndump['namespace'] or dump['mfilter'] != ndump['mfilter'] or dump['aaa_owner'] != ndump['aaa_owner']: print(dump) print(ndump) raise Exception('Invalid dump ...')
def work(self, event, *args, **kwargs): # Loads associated class selector = Selector( storage=self.storage, record=Record(event), logging_level=self.logging_level ) name = selector.display_name self.logger.debug(u'Start processing selector: {0}'.format(name)) # Selector event have to be published when do state is true. if selector.dostate: rk, event, publish_ack = selector.event() # Compute previous event to know if any difference next turn selector.data['previous_metrics'] = { metric['metric']: metric['value'] for metric in event['perf_data_array'] } do_publish_event = selector.have_to_publish(event) if do_publish_event: self.publish_event(selector, rk, event, publish_ack) # When selector computed, sla may be asked to be computed. if selector.dosla: self.logger.debug(u'Start processing SLA') # Retrieve user ui settings # This template should be always set template = selector.get_sla_output_tpl() # Timewindow computation duration timewindow = selector.get_sla_timewindow() sla_warning = selector.get_sla_warning() sla_critical = selector.get_sla_critical() alert_level = selector.get_alert_level() display_name = selector.display_name rk = get_routingkey(event) sla = Sla( self.storage, rk, template, timewindow, sla_warning, sla_critical, alert_level, display_name, logger=self.logger ) self.publish_sla_event( sla.get_event(), display_name ) else: self.logger.debug(u'Nothing to do') selector.save()