0
0
Fork 0
mirror of https://github.com/alerta/alerta-contrib.git synced 2025-03-15 04:54:46 +00:00
alerta_alerta-contrib/plugins/pagerduty/alerta_pagerduty.py

100 lines
3.1 KiB
Python
Raw Normal View History

2017-09-17 14:56:55 +00:00
import logging
import os
import re
2023-03-20 22:39:34 +00:00
2015-02-02 15:19:08 +00:00
import requests
2023-03-20 22:39:34 +00:00
from alerta.plugins import PluginBase
2015-02-02 15:19:08 +00:00
2017-09-17 14:56:55 +00:00
try:
from alerta.plugins import app # alerta >= 5.0
except ImportError:
from alerta.app import app # alerta < 5.0
2015-02-02 15:19:08 +00:00
2016-09-15 09:16:11 +00:00
LOG = logging.getLogger('alerta.plugins.pagerduty')
2015-02-02 15:19:08 +00:00
PAGERDUTY_EVENTS_URL = 'https://events.pagerduty.com/generic/2010-04-15/create_event.json'
2023-03-20 22:39:34 +00:00
PAGERDUTY_SERVICE_KEY = os.environ.get(
'PAGERDUTY_SERVICE_KEY') or app.config['PAGERDUTY_SERVICE_KEY']
SERVICE_KEY_MATCHERS = os.environ.get(
'SERVICE_KEY_MATCHERS') or app.config['SERVICE_KEY_MATCHERS']
DASHBOARD_URL = os.environ.get(
'DASHBOARD_URL') or app.config.get('DASHBOARD_URL', '')
2015-02-02 15:19:08 +00:00
class TriggerEvent(PluginBase):
def pagerduty_service_key(self, resource):
if not SERVICE_KEY_MATCHERS:
2023-03-20 22:39:34 +00:00
LOG.debug('No matchers defined! Default service key: %s' %
(PAGERDUTY_SERVICE_KEY))
return PAGERDUTY_SERVICE_KEY
for mapping in SERVICE_KEY_MATCHERS:
if re.match(mapping['regex'], resource):
2023-03-20 22:39:34 +00:00
LOG.debug('Matched regex: %s, service key: %s' %
(mapping['regex'], mapping['api_key']))
return mapping['api_key']
2023-03-20 22:39:34 +00:00
LOG.debug('No regex match! Default service key: %s' %
(PAGERDUTY_SERVICE_KEY))
return PAGERDUTY_SERVICE_KEY
2015-02-02 15:19:08 +00:00
def pre_receive(self, alert):
return alert
def post_receive(self, alert):
if alert.repeat:
return
2023-03-20 22:39:34 +00:00
message = '{}: {} alert for {} - {} is {}'.format(
2015-02-02 15:19:08 +00:00
alert.environment, alert.severity.capitalize(),
','.join(alert.service), alert.resource, alert.event
)
2016-04-02 22:49:26 +00:00
if alert.severity in ['cleared', 'normal', 'ok']:
2023-03-20 22:39:34 +00:00
event_type = 'resolve'
else:
2023-03-20 22:39:34 +00:00
event_type = 'trigger'
2015-02-02 15:19:08 +00:00
payload = {
2023-03-20 22:39:34 +00:00
'service_key': self.pagerduty_service_key(alert.resource),
'incident_key': alert.id,
'event_type': event_type,
'description': message,
'client': 'alerta',
'client_url': '{}/#/alert/{}'.format(DASHBOARD_URL, alert.id),
'details': alert.get_body(history=False)
2015-02-02 15:19:08 +00:00
}
LOG.debug('PagerDuty payload: %s', payload)
try:
2016-04-02 22:49:26 +00:00
r = requests.post(PAGERDUTY_EVENTS_URL, json=payload, timeout=2)
2015-02-02 15:19:08 +00:00
except Exception as e:
2023-03-20 22:39:34 +00:00
raise RuntimeError('PagerDuty connection error: %s' % e)
2015-02-02 15:19:08 +00:00
LOG.debug('PagerDuty response: %s - %s', r.status_code, r.text)
def status_change(self, alert, status, text):
2016-04-02 22:49:26 +00:00
if status not in ['ack', 'assign']:
return
payload = {
2023-03-20 22:39:34 +00:00
'service_key': self.pagerduty_service_key(alert.resource),
'incident_key': alert.id,
'event_type': 'acknowledge',
'description': text,
'details': alert.get_body(history=False)
2016-04-02 22:49:26 +00:00
}
LOG.debug('PagerDuty payload: %s', payload)
try:
r = requests.post(PAGERDUTY_EVENTS_URL, json=payload, timeout=2)
except Exception as e:
2023-03-20 22:39:34 +00:00
raise RuntimeError('PagerDuty connection error: %s' % e)
2016-04-02 22:49:26 +00:00
LOG.debug('PagerDuty response: %s - %s', r.status_code, r.text)