Last active
August 29, 2015 14:00
-
-
Save simonrad/11157838 to your computer and use it in GitHub Desktop.
Script to temporarily hush Pagerduty alerts.
This file contains hidden or 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
| """This script will periodically poll pagerduty for triggered incidents, and acknowledge them.""" | |
| import urllib2 | |
| import time | |
| import json | |
| import sys | |
| import getpass | |
| if __name__ == '__main__': | |
| USER = raw_input('Please enter your Pagerduty username (this is probably your full MoPub email address):') | |
| PASSWORD = getpass.getpass('Please enter your Pagerduty password:') | |
| POLL_PERIOD = 20 # Time in seconds between each poll iteration. | |
| MAX_RUNTIME = 60 * 60 # Time in seconds after which the program will automatically quit. | |
| # Create an OpenerDirector with support for Basic HTTP Authentication... | |
| auth_handler = urllib2.HTTPBasicAuthHandler() | |
| auth_handler.add_password(realm='Web Password', | |
| uri='https://mopub.pagerduty.com/api', | |
| user=USER, | |
| passwd=PASSWORD) | |
| opener = urllib2.build_opener(auth_handler) | |
| # ...and install it globally so it can be used with urlopen. | |
| urllib2.install_opener(opener) | |
| get_url = 'https://mopub.pagerduty.com/api/v1/incidents?status=triggered' | |
| set_url = 'https://mopub.pagerduty.com/api/v1/incidents/%s/acknowledge' | |
| headers = {'Content-type': 'application/json'} | |
| print 'This script will periodically poll pagerduty for triggered incidents, and acknowledge them.' | |
| print 'Press ctrl-C to quit.' | |
| for i in range(MAX_RUNTIME / POLL_PERIOD): | |
| print 'Fetching incidents:' | |
| print '-----------------------------------' | |
| req = urllib2.Request(get_url, headers=headers) | |
| response = json.loads(urllib2.urlopen(req).read()) | |
| print json.dumps(response) | |
| incidents = response['incidents'] | |
| for incident in incidents: | |
| if incident['assigned_to'][0]['object']['email'] == USER: | |
| try: | |
| id = incident['id'] | |
| print 'Acknowledging indicent %s:' % (id,) | |
| print '-----------------------------------' | |
| req = urllib2.Request(set_url % (id,), data='', headers=headers) | |
| req.get_method = lambda: 'PUT' | |
| response = json.loads(urllib2.urlopen(req).read()) | |
| print json.dumps(response) | |
| except urllib2.HTTPError as e: | |
| print 'HTTP ERROR!', e.code, e.reason | |
| print 'Sleeping...' | |
| time.sleep(POLL_PERIOD) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment