Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed requested changes of #353 #386

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion plugins/pagerduty/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,4 @@ References
License
-------

Copyright (c) 2016 Nick Satterly. Available under the MIT License.
Copyright (c) 2022 Nick Satterly. Available under the MIT License.
62 changes: 21 additions & 41 deletions plugins/pagerduty/alerta_pagerduty.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import os
import re
import requests
import pdpyras

try:
from alerta.plugins import app # alerta >= 5.0
Expand All @@ -12,7 +13,6 @@

LOG = logging.getLogger('alerta.plugins.pagerduty')

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', '')
Expand All @@ -33,10 +33,12 @@ def pagerduty_service_key(self, resource):
LOG.debug('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.debug('Sending PagerDuty notice')

if alert.repeat:
return
Expand All @@ -46,48 +48,26 @@ def post_receive(self, alert):
','.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)

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)
LOG.info('PagerDuty notice sent')

try:
r = requests.post(PAGERDUTY_EVENTS_URL, json=payload, timeout=2)
except Exception as e:
raise RuntimeError("PagerDuty connection error: %s" % e)
def status_change(self, alert, status, text, **kwargs):
LOG.debug('PagerDuty status change ignored.')

LOG.debug('PagerDuty response: %s - %s', r.status_code, r.text)
5 changes: 3 additions & 2 deletions plugins/pagerduty/setup.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

from setuptools import setup, find_packages

version = '5.3.1'
version = '5.3.5'

setup(
name="alerta-pagerduty",
Expand All @@ -14,7 +14,8 @@
packages=find_packages(),
py_modules=['alerta_pagerduty'],
install_requires=[
'requests'
'requests',
'pdpyras'
],
include_package_data=True,
zip_safe=True,
Expand Down