-
-
Save alex-laties/5951320 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import urllib2 | |
import json | |
from supervisor import childutils | |
import sys | |
import socket | |
class PagerDutyNotifier(object): | |
def __init__(self, pd_service_key): | |
self.pd_service_key = pd_service_key | |
def run(self): | |
while True: | |
headers, payload = childutils.listener.wait() | |
sys.stderr.write(str(headers) + '\n') | |
payload = dict(v.split(':') for v in payload.split(' ')) | |
sys.stderr.write(str(payload) + '\n') | |
if headers['eventname'] == 'PROCESS_STATE_EXITED' and not int(payload['expected']): | |
data = {'service_key': self.pd_service_key, | |
'event_type': 'trigger', | |
'description': '{} service has crashed unexpectedly on {}'.format(payload['processname'], socket.gethostname()) | |
} | |
try: | |
res = urllib2.urlopen('https://events.pagerduty.com/generic/2010-04-15/create_event.json', json.dumps(data)) | |
except urllib2.HTTPError, ex: | |
sys.stderr.write('{} - {}\n{}'.format(ex.code, ex.reason, ex.read())) | |
else: | |
sys.stderr.write('{}, {}\n'.format(res.code, res.msg)) | |
childutils.listener.ok() | |
sys.stderr.flush() | |
if __name__ == '__main__': | |
pager_duty_service_key = sys.argv[1] | |
pager_duty_notifer = PagerDutyNotifier(pager_duty_service_key) | |
pager_duty_notifer.run() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment