def test_get_last_state(self): got = get_last_state(self.alarm) self.assertEqual(got, Check.OK) self.alarm['state'] = { '_t': 'stateinc', 't': 0, 'a': 'test', 'm': 'test', 'val': Check.CRITICAL } got = get_last_state(self.alarm) self.assertEqual(got, Check.CRITICAL)
def test_get_last_state(self): got = get_last_state(self.alarm) self.assertEqual(got, Check.OK) self.alarm[AlarmField.state.value] = { '_t': 'stateinc', 't': 0, 'a': 'test', 'm': 'test', 'val': Check.CRITICAL } got = get_last_state(self.alarm) self.assertEqual(got, Check.CRITICAL)
def update_state(self, alarm, state, event): """ Update alarm state if needed. :param alarm: Alarm associated to state change event :type alarm: dict :param state: New state to archive :type state: int :param event: Associated event :type event: dict :return: updated alarm :rtype: dict """ value = alarm.get(self[Alerts.ALARM_STORAGE].VALUE) old_state = get_last_state(value, ts=event['timestamp']) if state != old_state: return self.change_of_state(alarm, old_state, state, event) return alarm
def update_state(self, alarm, state, event): """ Update alarm state if needed. :param dict alarm: Alarm associated to state change event :param int state: New state to archive :param dict event: Associated event :return: updated alarm :rtype: dict """ value = alarm.get(self.alerts_storage.VALUE) if self.record_last_event_date: value[AlarmField.last_event_date.value] = int(time()) old_state = get_last_state(value, ts=event['timestamp']) if state != old_state: return self.change_of_state(alarm, old_state, state, event) return alarm
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 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_storage alarm_id = alarm[storage.DATA_ID] alarm = alarm[storage.VALUE] entity = self.context_manager.get_entities_by_id(alarm_id) try: entity = entity[0] except IndexError: entity = {} no_author_types = ['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': States.changestate.value, 'snooze': 'snooze' } valmap = { 'stateinc': Check.STATE, 'statedec': Check.STATE, 'changestate': Check.STATE, 'statusinc': Check.STATUS, 'statusdec': Check.STATUS, 'assocticket': 'ticket', 'snooze': 'duration' } events = [] eventmodel = self.context_manager.get_event(entity) try: eventmodel.pop("_id") eventmodel.pop("depends") eventmodel.pop("impact") eventmodel.pop("infos") eventmodel.pop("measurements") eventmodel.pop("type") except KeyError: # FIXME : A logger would be nice pass for step in alarm[AlarmField.steps.value]: 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_types: event[self.AUTHOR] = step['a'] if step['_t'] in check_referer_types: event['ref_rk'] = get_routingkey(event) if Check.STATE not in event: event[Check.STATE] = get_last_state(alarm) # set event_type to step['_t'] if we don't have any valid mapping. event['event_type'] = typemap.get(step['_t'], step['_t']) for field in self.extra_fields: if field in alarm[AlarmField.extra.value]: event[field] = alarm[AlarmField.extra.value][field] events.append(event) return events