Merge 0e82da41f8
into 64e013029d
This commit is contained in:
commit
ea0fa91b0a
2 changed files with 28 additions and 48 deletions
plugins/pagerduty
|
@ -3,6 +3,7 @@ import logging
|
|||
import os
|
||||
import re
|
||||
import requests
|
||||
import pdpyras
|
||||
|
||||
try:
|
||||
from alerta.plugins import app # alerta >= 5.0
|
||||
|
@ -10,9 +11,8 @@ except ImportError:
|
|||
from alerta.app import app # alerta < 5.0
|
||||
from alerta.plugins import PluginBase
|
||||
|
||||
LOG = logging.getLogger('alerta.plugins.pagerduty')
|
||||
LOG = logging.getLogger('alerta.plugins')
|
||||
|
||||
PAGERDUTY_EVENTS_URL = 'https://events.pagerduty.com/generic/2010-04-15/create_event.json'
|
||||
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', '')
|
||||
|
@ -22,21 +22,23 @@ class TriggerEvent(PluginBase):
|
|||
|
||||
def pagerduty_service_key(self, resource):
|
||||
if not SERVICE_KEY_MATCHERS:
|
||||
LOG.debug('No matchers defined! Default service key: %s' % (PAGERDUTY_SERVICE_KEY))
|
||||
LOG.warning('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):
|
||||
LOG.debug('Matched regex: %s, service key: %s' % (mapping['regex'], mapping['api_key']))
|
||||
LOG.warning('Matched regex: %s, service key: %s' % (mapping['regex'], mapping['api_key']))
|
||||
return mapping['api_key']
|
||||
|
||||
LOG.debug('No regex match! Default service key: %s' % (PAGERDUTY_SERVICE_KEY))
|
||||
LOG.warning('No regex match! Default service key: %s' % (PAGERDUTY_SERVICE_KEY))
|
||||
return PAGERDUTY_SERVICE_KEY
|
||||
|
||||
def pre_receive(self, alert):
|
||||
def pre_receive(self, alert, **kwargs):
|
||||
return alert
|
||||
|
||||
def post_receive(self, alert):
|
||||
def post_receive(self, alert, **kwargs):
|
||||
|
||||
LOG.warning('Sending PagerDuty notice')
|
||||
|
||||
if alert.repeat:
|
||||
return
|
||||
|
@ -46,48 +48,25 @@ class TriggerEvent(PluginBase):
|
|||
','.join(alert.service), alert.resource, alert.event
|
||||
)
|
||||
|
||||
if alert.severity in ['cleared', 'normal', 'ok']:
|
||||
event_type = "resolve"
|
||||
else:
|
||||
event_type = "trigger"
|
||||
|
||||
payload = {
|
||||
"service_key": self.pagerduty_service_key(alert.resource),
|
||||
"incident_key": alert.id,
|
||||
"event_type": event_type,
|
||||
"description": message,
|
||||
"client": "alerta",
|
||||
"client_url": '%s/#/alert/%s' % (DASHBOARD_URL, alert.id),
|
||||
"details": alert.get_body(history=False)
|
||||
}
|
||||
|
||||
LOG.debug('PagerDuty payload: %s', payload)
|
||||
session = pdpyras.EventsAPISession(self.pagerduty_service_key(alert.resource))
|
||||
|
||||
try:
|
||||
r = requests.post(PAGERDUTY_EVENTS_URL, json=payload, timeout=2)
|
||||
if alert.severity in ['cleared', 'normal', 'ok']:
|
||||
pd_incident = session.resolve(alert.id)
|
||||
else:
|
||||
pd_incident = session.trigger(
|
||||
message,
|
||||
alert.resource,
|
||||
dedup_key=alert.id,
|
||||
severity=alert.severity,
|
||||
custom_details=alert.get_body(history=False),
|
||||
links=['%s/#/alert/%s' % (DASHBOARD_URL, alert.id)]
|
||||
)
|
||||
|
||||
except Exception as e:
|
||||
raise RuntimeError("PagerDuty connection error: %s" % e)
|
||||
|
||||
LOG.debug('PagerDuty response: %s - %s', r.status_code, r.text)
|
||||
LOG.warning('PagerDuty notice sent')
|
||||
|
||||
def status_change(self, alert, status, text):
|
||||
|
||||
if status not in ['ack', 'assign']:
|
||||
return
|
||||
|
||||
payload = {
|
||||
"service_key": self.pagerduty_service_key(alert.resource),
|
||||
"incident_key": alert.id,
|
||||
"event_type": "acknowledge",
|
||||
"description": text,
|
||||
"details": alert.get_body(history=False)
|
||||
}
|
||||
|
||||
LOG.debug('PagerDuty payload: %s', payload)
|
||||
|
||||
try:
|
||||
r = requests.post(PAGERDUTY_EVENTS_URL, json=payload, timeout=2)
|
||||
except Exception as e:
|
||||
raise RuntimeError("PagerDuty connection error: %s" % e)
|
||||
|
||||
LOG.debug('PagerDuty response: %s - %s', r.status_code, r.text)
|
||||
def status_change(self, alert, status, text, **kwargs):
|
||||
LOG.warn('PagerDuty status change ignored.')
|
|
@ -1,7 +1,7 @@
|
|||
|
||||
from setuptools import setup, find_packages
|
||||
|
||||
version = '5.3.1'
|
||||
version = '5.3.5'
|
||||
|
||||
setup(
|
||||
name="alerta-pagerduty",
|
||||
|
@ -14,7 +14,8 @@ setup(
|
|||
packages=find_packages(),
|
||||
py_modules=['alerta_pagerduty'],
|
||||
install_requires=[
|
||||
'requests'
|
||||
'requests',
|
||||
'pdpyras'
|
||||
],
|
||||
include_package_data=True,
|
||||
zip_safe=True,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue