Add alarm_name field to alarm notification

Currently, alarm notification only has the alarm_id which can be used
to identify an alarm. But in some use cases, user may want to use
some customized information to mark the alarm. So this patch added the
alarm_name field into the notification. The name of alarm is tenant
unique and can be defined by user when alarm is created. When the alarm
consumer (e.g. OS::Heat::ScalingPolicy) receives an alarm, it can now
identify the alarm using the customized info stored in alarm_name field.

Change-Id: If9ffcca3f0de8f45483f30bc30ded91dd9d40d77
Closes-Bug: #1399067
This commit is contained in:
Yanyan Hu 2014-11-03 16:42:22 +08:00
parent 31e5435910
commit a48e664537
9 changed files with 38 additions and 23 deletions

View File

@ -25,11 +25,13 @@ class AlarmNotifier(object):
"""Base class for alarm notifier plugins."""
@abc.abstractmethod
def notify(self, action, alarm_id, previous, current, reason, reason_data):
def notify(self, action, alarm_id, alarm_name, previous, current,
reason, reason_data):
"""Notify that an alarm has been triggered.
:param action: The action that is being attended, as a parsed URL.
:param alarm_id: The triggered alarm.
:param alarm_name: The name of triggered alarm.
:param previous: The previous state of the alarm.
:param current: The current state of the alarm.
:param reason: The reason the alarm changed its state.

View File

@ -27,10 +27,11 @@ class LogAlarmNotifier(notifier.AlarmNotifier):
"Log alarm notifier."""
@staticmethod
def notify(action, alarm_id, previous, current, reason, reason_data):
def notify(action, alarm_id, alarm_name, previous, current, reason,
reason_data):
LOG.info(_(
"Notifying alarm %(alarm_id)s from %(previous)s "
"Notifying alarm %(alarm_name)s %(alarm_id)s from %(previous)s "
"to %(current)s with action %(action)s because "
"%(reason)s") % ({'alarm_id': alarm_id, 'previous': previous,
'current': current, 'action': action,
'reason': reason}))
"%(reason)s.") % ({'alarm_name': alarm_name, 'alarm_id': alarm_id,
'previous': previous, 'current': current,
'action': action, 'reason': reason}))

View File

@ -57,23 +57,23 @@ class RestAlarmNotifier(notifier.AlarmNotifier):
"""Rest alarm notifier."""
@staticmethod
def notify(action, alarm_id, previous, current, reason, reason_data,
headers=None):
def notify(action, alarm_id, alarm_name, previous, current, reason,
reason_data, headers=None):
headers = headers or {}
if not headers.get('x-openstack-request-id'):
headers['x-openstack-request-id'] = context.generate_request_id()
LOG.info(_(
"Notifying alarm %(alarm_id)s from %(previous)s "
"to %(current)s with action %(action)s because "
"%(reason)s. request-id: %(request_id)s") %
({'alarm_id': alarm_id, 'previous': previous,
'current': current, 'action': action,
'reason': reason,
"Notifying alarm %(alarm_name)s %(alarm_id)s from "
"%(previous)s to %(current)s with action %(action)s because "
"%(reason)s. request-id: %(request_id)s ") %
({'alarm_name': alarm_name, 'alarm_id': alarm_id,
'previous': previous, 'current': current,
'action': action, 'reason': reason,
'request_id': headers['x-openstack-request-id']}))
body = {'alarm_id': alarm_id, 'previous': previous,
'current': current, 'reason': reason,
'reason_data': reason_data}
body = {'alarm_name': alarm_name, 'alarm_id': alarm_id,
'previous': previous, 'current': current,
'reason': reason, 'reason_data': reason_data}
headers['content-type'] = 'application/json'
kwargs = {'data': jsonutils.dumps(body),
'headers': headers}

View File

@ -25,9 +25,11 @@ class TestAlarmNotifier(notifier.AlarmNotifier):
def __init__(self):
self.notifications = []
def notify(self, action, alarm_id, previous, current, reason, reason_data):
def notify(self, action, alarm_id, alarm_name, previous, current,
reason, reason_data):
self.notifications.append((action,
alarm_id,
alarm_name,
previous,
current,
reason,

View File

@ -36,7 +36,8 @@ class TrustRestAlarmNotifier(rest.RestAlarmNotifier):
"""
@staticmethod
def notify(action, alarm_id, previous, current, reason, reason_data):
def notify(action, alarm_id, alarm_name, previous, current,
reason, reason_data):
trust_id = action.username
auth_url = cfg.CONF.service_credentials.os_auth_url.replace(
@ -61,4 +62,5 @@ class TrustRestAlarmNotifier(rest.RestAlarmNotifier):
headers = {'X-Auth-Token': client.auth_token}
rest.RestAlarmNotifier.notify(
action, alarm_id, previous, current, reason, reason_data, headers)
action, alarm_id, alarm_name, previous, current, reason,
reason_data, headers)

View File

@ -64,6 +64,7 @@ class RPCAlarmNotifier(object):
'notify_alarm', data={
'actions': actions,
'alarm_id': alarm.alarm_id,
'alarm_name': alarm.name,
'previous': previous,
'current': alarm.state,
'reason': six.text_type(reason),

View File

@ -253,7 +253,7 @@ class AlarmNotifierService(os_service.Service):
self.rpc_server.stop()
super(AlarmNotifierService, self).stop()
def _handle_action(self, action, alarm_id, previous,
def _handle_action(self, action, alarm_id, alarm_name, previous,
current, reason, reason_data):
try:
action = netutils.urlsplit(action)
@ -276,7 +276,7 @@ class AlarmNotifierService(os_service.Service):
try:
LOG.debug(_("Notifying alarm %(id)s with action %(act)s") % (
{'id': alarm_id, 'act': action}))
notifier.notify(action, alarm_id, previous,
notifier.notify(action, alarm_id, alarm_name, previous,
current, reason, reason_data)
except Exception:
LOG.exception(_("Unable to notify alarm %s"), alarm_id)
@ -291,6 +291,7 @@ class AlarmNotifierService(os_service.Service):
- actions, the URL of the action to run; this is mapped to
extensions automatically
- alarm_id, the ID of the alarm that has been triggered
- alarm_name, the name of the alarm that has been triggered
- previous, the previous state of the alarm
- current, the new state the alarm has transitioned to
- reason, the reason the alarm changed its state
@ -304,6 +305,7 @@ class AlarmNotifierService(os_service.Service):
for action in actions:
self._handle_action(action,
data.get('alarm_id'),
data.get('alarm_name'),
data.get('previous'),
data.get('current'),
data.get('reason'),

View File

@ -28,11 +28,12 @@ from ceilometer.tests import base as tests_base
DATA_JSON = anyjson.loads(
'{"current": "ALARM", "alarm_id": "foobar",'
'{"current": "ALARM", "alarm_id": "foobar", "alarm_name": "testalarm",'
' "reason": "what ?", "reason_data": {"test": "test"},'
' "previous": "OK"}'
)
NOTIFICATION = dict(alarm_id='foobar',
alarm_name='testalarm',
condition=dict(threshold=42),
reason='what ?',
reason_data={'test': 'test'},
@ -63,6 +64,7 @@ class TestAlarmNotifier(tests_base.BaseTestCase):
data = {
'actions': ['test://'],
'alarm_id': 'foobar',
'alarm_name': 'testalarm',
'previous': 'OK',
'current': 'ALARM',
'reason': 'Everything is on fire',
@ -73,6 +75,7 @@ class TestAlarmNotifier(tests_base.BaseTestCase):
self.assertEqual(1, len(notifications))
self.assertEqual((urlparse.urlsplit(data['actions'][0]),
data['alarm_id'],
data['alarm_name'],
data['previous'],
data['current'],
data['reason'],

View File

@ -107,6 +107,8 @@ class TestRPCAlarmNotifier(tests_base.BaseTestCase):
actions = getattr(a, models.Alarm.ALARM_ACTIONS_MAP[a.state])
self.assertEqual(self.alarms[i].alarm_id,
self.notifier_server.notified[i]["alarm_id"])
self.assertEqual(self.alarms[i].name,
self.notifier_server.notified[i]["alarm_name"])
self.assertEqual(actions,
self.notifier_server.notified[i]["actions"])
self.assertEqual(previous[i],