Record the state transition reason in alarm's history data when evaluating

For now, the alarm history only record the alarm info and alarm change
type, if users create alarms without alarm actions specified, and the
alarms triggered, there is no way to know why the alarms triggered.
It is better to record the reason when alarm's state changed.

Change-Id: Ic38644daa25d7f5815e6f7028570187cb4a852c2
Closes-Bug: #1583885
This commit is contained in:
LiuSheng 2016-07-06 17:29:35 +08:00
parent 6f186567bb
commit c41e1117fc
3 changed files with 13 additions and 6 deletions

View File

@ -78,11 +78,12 @@ class Evaluator(object):
self.storage_conn = storage.get_connection_from_config(self.conf)
return self.storage_conn
def _record_change(self, alarm):
def _record_change(self, alarm, reason):
if not self.conf.record_history:
return
type = models.AlarmChange.STATE_TRANSITION
detail = json.dumps({'state': alarm.state})
detail = json.dumps({'state': alarm.state,
'transition_reason': reason})
user_id, project_id = self.ks_client.user_id, self.ks_client.project_id
on_behalf_of = alarm.project_id
now = timeutils.utcnow()
@ -124,7 +125,7 @@ class Evaluator(object):
"alarm: %s has been deleted"),
alarm.alarm_id)
else:
self._record_change(alarm)
self._record_change(alarm, reason)
self.notifier.notify(alarm, previous, reason, reason_data)
elif alarm.repeat_actions:
self.notifier.notify(alarm, previous, reason, reason_data)

View File

@ -46,7 +46,7 @@ class TestEvaluatorBaseClass(base.BaseTestCase):
ev._refresh(mock.MagicMock(), mock.MagicMock(),
mock.MagicMock(), mock.MagicMock())
ev.storage_conn.update_alarm.assert_called_once_with(mock.ANY)
ev._record_change.assert_called_once_with(mock.ANY)
ev._record_change.assert_called_once_with(mock.ANY, mock.ANY)
self.assertTrue(self.called)
@mock.patch.object(timeutils, 'utcnow')

View File

@ -218,12 +218,18 @@ class TestEvaluate(base.TestEvaluatorBase):
def _construct_payloads(self):
payloads = []
reasons = ["Transition to alarm due to 5 samples outside threshold, "
"most recent: 85.0",
"Transition to alarm due to 4 samples outside threshold, "
"most recent: 7.0"]
for alarm in self.alarms:
num = self.alarms.index(alarm)
type = models.AlarmChange.STATE_TRANSITION
detail = json.dumps({'state': alarm.state})
detail = json.dumps({'state': alarm.state,
'transition_reason': reasons[num]})
on_behalf_of = alarm.project_id
payload = dict(
event_id='fake_event_id_%s' % self.alarms.index(alarm),
event_id='fake_event_id_%s' % num,
alarm_id=alarm.alarm_id,
type=type,
detail=detail,