Skip to content

Instantly share code, notes, and snippets.

@lgtml
Forked from reshefm/supervisor-pagerduty.py
Created July 18, 2013 23:42
Show Gist options
  • Save lgtml/6034015 to your computer and use it in GitHub Desktop.
Save lgtml/6034015 to your computer and use it in GitHub Desktop.
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