def test_snooze(self): event = { 'connector': 'test', 'connector_name': 'test0', 'timestamp': 0, 'output': 'test message', 'duration': 3600, } task = get_task('alerts.useraction.snooze') alarm = task( self.manager, self.alarm, 'testauthor', 'test message', event, ) self.assertIsNot(alarm[AlarmField.snooze.value], None) self.assertEqual(alarm[AlarmField.snooze.value]['t'], 0) self.assertEqual(alarm[AlarmField.snooze.value]['a'], 'testauthor') self.assertEqual(alarm[AlarmField.snooze.value]['m'], 'test message') self.assertEqual(alarm[AlarmField.snooze.value]['val'], 0 + 3600) self.assertTrue( alarm[AlarmField.snooze.value] is get_previous_step(alarm, 'snooze') )
def restore(manager, alarm, author, message, event): """ Called when an alarm is restored by a user. """ step = { '_t': 'uncancel', 't': event['timestamp'], 'a': author, 'm': message, 'role': event.get('role', None) } canceled = alarm[AlarmField.canceled.value] alarm[AlarmField.canceled.value] = None alarm[AlarmField.steps.value].append(step) status = None if manager.restore_event: status = get_previous_step(alarm, ['statusinc', 'statusdec'], ts=canceled['t']) if status is not None: status = status['val'] else: # This is not supposed to happen since a restored alarm # should have a status before its cancelation status = OFF else: status = compute_status(manager, alarm) return alarm, status
def test_acknowledge(self): event = { 'timestamp': 0, 'source_type': 'component', 'connector': 'c', 'connector_name': 'cn', 'component': 'cm', } task = get_task('alerts.useraction.ack') alarm = task( self.manager, self.alarm, 'testauthor', 'test message', event ) self.assertTrue(alarm[AlarmField.ack.value] is not None) self.assertEqual(alarm[AlarmField.ack.value]['t'], 0) self.assertEqual(alarm[AlarmField.ack.value]['a'], 'testauthor') self.assertEqual(alarm[AlarmField.ack.value]['m'], 'test message') self.assertTrue(alarm[AlarmField.ack.value] is get_previous_step(alarm, 'ack')) self.event_publisher.publish_statcounterinc_event.assert_not_called() self.event_publisher.publish_statduration_event.assert_called_once_with( 0, StatDurations.ack_time, 0, {}, alarm, 'testauthor')
def test_restore(self): event = {'timestamp': 0} task = get_task('alerts.useraction.uncancel') self.alarm[AlarmField.canceled.value] = { '_t': 'cancel', 't': 0, 'a': 'testauthor', 'm': 'test message' } alarm, _ = task( self.manager, self.alarm, 'testauthor', 'test message', event ) self.assertTrue(alarm[AlarmField.canceled.value] is None) uncancel = get_previous_step(alarm, 'uncancel') self.assertFalse(uncancel is None) self.assertEqual(uncancel['t'], 0) self.assertEqual(uncancel['a'], 'testauthor') self.assertEqual(uncancel['m'], 'test message')
def test_get_previous_step(self): expected = { '_t': 'teststep', 't': 0, 'a': 'test', 'm': 'test' } self.alarm[AlarmField.steps.value].append(expected) step = get_previous_step(self.alarm, 'teststep') self.assertTrue(expected is step)
def test_get_previous_step(self): expected = { '_t': 'teststep', 't': 0, 'a': 'test', 'm': 'test' } self.alarm['steps'].append(expected) step = get_previous_step(self.alarm, 'teststep') self.assertTrue(expected is step)
def resolved_alarm_stats(eventmgr, usermgr, alertsmgr, storage, logger): resolved_alarms = alertsmgr.get_alarms( resolved=True, exclude_tags='stats-resolved' ) for entity_id in resolved_alarms: for docalarm in resolved_alarms[entity_id]: docalarm[storage.DATA_ID] = entity_id alarm = docalarm[storage.VALUE] alarm_ts = docalarm[storage.TIMESTAMP] extra = copy(alarm['extra']) solved_delay = alarm['resolved'] - alarm_ts eventmgr.alarm_solved_delay( delay=solved_delay, extra_fields=extra ) if alarm['ack'] is not None: ack_ts = alarm['ack']['t'] eventmgr.alarm_ack_solved_delay( delay=alarm['resolved'] - ack_ts, extra_fields=extra ) alarm_events = alertsmgr.get_events(docalarm) for event in alarm_events: if event['event_type'] == 'ack': ack_ts = event['timestamp'] ackremove = get_previous_step( alarm, 'ackremove', ts=ack_ts ) ref_ts = alarm_ts if ackremove is None else ackremove['t'] ack_delay = ack_ts - ref_ts usermgr.alarm_ack_delay( user=event['author'], delay=ack_delay, extra_fields=extra ) alertsmgr.update_current_alarm( docalarm, alarm, tags='stats-resolved' )
def test_acknowledge(self): event = {'timestamp': 0} task = get_task('alerts.useraction.ack') alarm = task( self.manager, self.alarm, 'testauthor', 'test message', event ) self.assertTrue(alarm['ack'] is not None) self.assertEqual(alarm['ack']['t'], 0) self.assertEqual(alarm['ack']['a'], 'testauthor') self.assertEqual(alarm['ack']['m'], 'test message') self.assertTrue(alarm['ack'] is get_previous_step(alarm, 'ack'))
def test_cancel(self): event = {'timestamp': 0} task = get_task('alerts.useraction.cancel') alarm, statusval = task( self.manager, self.alarm, 'testauthor', 'test message', event ) self.assertEqual(statusval, CANCELED) self.assertTrue(alarm[AlarmField.canceled.value] is not None) self.assertEqual(alarm[AlarmField.canceled.value]['t'], 0) self.assertEqual(alarm[AlarmField.canceled.value]['a'], 'testauthor') self.assertEqual(alarm[AlarmField.canceled.value]['m'], 'test message') self.assertTrue( alarm[AlarmField.canceled.value] is get_previous_step(alarm, 'cancel') )
def test_status_increase(self): event = { 'connector': 'test', 'connector_name': 'test0', 'timestamp': 0, 'output': 'test message' } statusval = 2 task = get_task('alerts.systemaction.status_increase') alarm = task(self.manager, self.alarm, statusval, event) self.assertTrue(alarm['status'] is not None) self.assertEqual(alarm['status']['t'], 0) self.assertEqual(alarm['status']['a'], 'test.test0') self.assertEqual(alarm['status']['m'], 'test message') self.assertEqual(alarm['status']['val'], statusval) self.assertTrue( alarm['status'] is get_previous_step(alarm, 'statusinc') )
def test_status_decrease(self): event = { 'connector': 'test', 'connector_name': 'test0', 'timestamp': 0, 'output': 'test message' } statusval = 0 task = get_task('alerts.systemaction.status_decrease') alarm = task(self.manager, self.alarm, statusval, event) self.assertTrue(alarm[AlarmField.status.value] is not None) self.assertEqual(alarm[AlarmField.status.value]['t'], 0) self.assertEqual(alarm[AlarmField.status.value]['a'], DEFAULT_AUTHOR) self.assertEqual(alarm[AlarmField.status.value]['m'], 'test message') self.assertEqual(alarm[AlarmField.status.value]['val'], statusval) self.assertTrue( alarm[AlarmField.status.value] is get_previous_step(alarm, 'statusdec') )
def test_declare_ticket(self): event = {'timestamp': 0} task = get_task('alerts.useraction.declareticket') alarm = task( self.manager, self.alarm, 'testauthor', 'test message', event ) self.assertTrue(alarm[AlarmField.ticket.value] is not None) self.assertEqual(alarm[AlarmField.ticket.value]['t'], 0) self.assertEqual(alarm[AlarmField.ticket.value]['a'], 'testauthor') self.assertEqual(alarm[AlarmField.ticket.value]['m'], None) self.assertEqual(alarm[AlarmField.ticket.value]['val'], None) self.assertTrue( alarm[AlarmField.ticket.value] is get_previous_step(alarm, 'declareticket') )
def test_state_increase(self): event = { 'connector': 'test', 'connector_name': 'test0', 'timestamp': 0, 'output': 'test message' } state = 2 task = get_task('alerts.systemaction.state_increase') alarm, _ = task(self.manager, self.alarm, state, event) self.assertTrue(alarm[AlarmField.state.value] is not None) self.assertEqual(alarm[AlarmField.state.value]['t'], 0) self.assertEqual(alarm[AlarmField.state.value]['a'], 'test.test0') self.assertEqual(alarm[AlarmField.state.value]['m'], 'test message') self.assertEqual(alarm[AlarmField.state.value]['val'], state) self.assertTrue( alarm[AlarmField.state.value] is get_previous_step(alarm, 'stateinc') )
def test_unacknowledge(self): event = {'timestamp': 0} task = get_task('alerts.useraction.ackremove') alarm = task( self.manager, self.alarm, 'testauthor', 'test message', event ) self.assertTrue(alarm[AlarmField.ack.value] is None) unack = get_previous_step(alarm, 'ackremove') self.assertEqual(unack['t'], 0) self.assertEqual(unack['a'], 'testauthor') self.assertEqual(unack['m'], 'test message') self.event_publisher.publish_statcounterinc_event.assert_not_called() self.event_publisher.publish_statduration_event.assert_not_called()
def test_assoc_ticket(self): event = { 'timestamp': 0, 'ticket': 1234 } task = get_task('alerts.useraction.assocticket') alarm = task( self.manager, self.alarm, 'testauthor', 'test message', event ) self.assertTrue(alarm['ticket'] is not None) self.assertEqual(alarm['ticket']['t'], 0) self.assertEqual(alarm['ticket']['a'], 'testauthor') self.assertEqual(alarm['ticket']['m'], 'test message') self.assertEqual(alarm['ticket']['val'], 1234) self.assertTrue( alarm['ticket'] is get_previous_step(alarm, 'assocticket') )
def test_change_state(self): event = { 'timestamp': 0, 'state': 2 } task = get_task('alerts.useraction.changestate') alarm = task( self.manager, self.alarm, 'testauthor', 'test message', event ) self.assertTrue(alarm['state'] is not None) self.assertEqual(alarm['state']['t'], 0) self.assertEqual(alarm['state']['a'], 'testauthor') self.assertEqual(alarm['state']['m'], 'test message') self.assertEqual(alarm['state']['val'], 2) self.assertTrue( alarm['state'] is get_previous_step(alarm, 'changestate') )
def restore(manager, alarm, author, message, event): """ Called when an alarm is restored by a user. """ step = { '_t': 'uncancel', 't': event['timestamp'], 'a': author, 'm': message } canceled = alarm[AlarmField.canceled.value] alarm[AlarmField.canceled.value] = None alarm[AlarmField.steps.value].append(step) status = None if manager.restore_event: status = get_previous_step( alarm, ['statusinc', 'statusdec'], ts=canceled['t'] ) if status is not None: status = status['val'] else: # This is not supposed to happen since a restored alarm # should have a status before its cancelation status = OFF else: status = compute_status(manager, alarm) return alarm, status
def test_change_state(self): event = { 'timestamp': 0, 'state': 2 } task = get_task('alerts.useraction.changestate') alarm = task( self.manager, self.alarm, 'testauthor', 'test message', event ) self.assertTrue(alarm[AlarmField.state.value] is not None) self.assertEqual(alarm[AlarmField.state.value]['t'], 0) self.assertEqual(alarm[AlarmField.state.value]['a'], 'testauthor') self.assertEqual(alarm[AlarmField.state.value]['m'], 'test message') self.assertEqual(alarm[AlarmField.state.value]['val'], 2) self.assertTrue( alarm[AlarmField.state.value] is get_previous_step(alarm, States.changestate.value) ) self.assertTrue(is_keeped_state(alarm))
def beat_processing( engine, sessionmgr=None, eventmgr=None, usermgr=None, alertsmgr=None, logger=None, **kwargs ): if sessionmgr is None: sessionmgr = singleton_per_scope(Session) if eventmgr is None: eventmgr = singleton_per_scope(EventMetricProducer) if usermgr is None: usermgr = singleton_per_scope(UserMetricProducer) if alertsmgr is None: alertsmgr = singleton_per_scope(Alerts) storage = alertsmgr[alertsmgr.ALARM_STORAGE] events = sessionmgr.duration() with engine.Lock(engine, 'alarm_stats_computation') as l: if l.own(): resolved_alarms = alertsmgr.get_alarms( resolved=True, exclude_tags='stats' ) for data_id in resolved_alarms: for docalarm in resolved_alarms[data_id]: docalarm[storage.DATA_ID] = data_id alarm = docalarm[storage.VALUE] alarm_ts = docalarm[storage.TIMESTAMP] alarm_events = alertsmgr.get_events(docalarm) solved_delay = alarm['resolved'] - alarm_ts events.append(eventmgr.alarm_solved_delay(solved_delay)) if alarm['ack'] is not None: ack_ts = alarm['ack']['t'] ackremove = get_previous_step( alarm, 'ackremove', ts=ack_ts ) ts = alarm_ts if ackremove is None else ackremove['t'] ack_delay = ack_ts - ts events.append(eventmgr.alarm_ack_delay(ack_delay)) events.append( eventmgr.alarm_ack_solved_delay( solved_delay - ack_delay ) ) events.append(usermgr.alarm_ack_delay( alarm['ack']['a'], ack_delay )) if len(alarm_events) > 0: events.append(eventmgr.alarm(alarm_events[0])) for event in alarm_events: if event['event_type'] == 'ack': events.append(eventmgr.alarm_ack(event)) events.append( usermgr.alarm_ack(event, event['author']) ) elif event['timestamp'] == alarm['resolved']: events.append(eventmgr.alarm_solved(event)) if alarm['ack'] is not None: events.append(eventmgr.alarm_ack_solved(event)) events.append( usermgr.alarm_ack_solved( alarm['ack']['a'], alarm['resolved'] - alarm['ack']['t'] ) ) events.append( usermgr.alarm_solved( alarm['ack']['a'], alarm['resolved'] - alarm_ts ) ) alertsmgr.update_current_alarm( docalarm, alarm, tags='stats' ) for event in events: publish(publisher=engine.amqp, event=event, logger=logger)