0
0
Fork 0
mirror of https://github.com/alerta/alerta.git synced 2025-01-30 03:33:59 +00:00
alerta_alerta/alerta/webhooks/newrelic.py
Nick Satterly 17ef9f0ceb
Support webhooks with a subpath (#1150)
* Support webhooks with a subpath

* Add tests for webhooks with subpath
2020-02-14 23:00:59 +01:00

56 lines
2 KiB
Python

from typing import Any, Dict
from alerta.models.alert import Alert
from . import WebhookBase
JSON = Dict[str, Any]
class NewRelicWebhook(WebhookBase):
"""
New Relic webhook notification channel
See https://docs.newrelic.com/docs/alerts/new-relic-alerts/managing-notification-channels/notification-channels-control-where-send-alerts
"""
def incoming(self, path, query_string, payload):
if 'version' not in payload:
raise ValueError('New Relic Legacy Alerting is not supported')
status = payload['current_state'].lower()
if status == 'open':
severity = payload['severity'].lower()
elif status == 'acknowledged':
severity = payload['severity'].lower()
status = 'ack'
elif status == 'closed':
severity = 'ok'
elif payload['severity'].lower() == 'info':
severity = 'informational'
status = 'open'
else:
severity = payload['severity'].lower()
status = 'open'
attributes = dict()
if 'incident_url' in payload:
attributes['moreInfo'] = '<a href="%s" target="_blank">Incident URL</a>' % payload['incident_url']
if 'runbook_url' in payload:
attributes['runBook'] = '<a href="%s" target="_blank">Runbook URL</a>' % payload['runbook_url']
return Alert(
resource=payload['targets'][0]['name'],
event=payload['condition_name'],
environment='Production',
severity=severity,
status=status,
service=[payload['account_name']],
group=payload['targets'][0]['type'],
text=payload['details'],
tags=['{}:{}'.format(key, value) for (key, value) in payload['targets'][0]['labels'].items()],
attributes=attributes,
origin='New Relic/v%s' % payload['version'],
event_type=payload['event_type'].lower(),
raw_data=payload
)