Created
October 6, 2016 18:03
-
-
Save ryanhoskin/dd96a4ea5c69350b79ad973f51f088d4 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
#!/usr/bin/python | |
import requests | |
import sys | |
import json | |
from datetime import date | |
import pprint | |
#Your PagerDuty API key. A read-only key will work for this. | |
auth_token = 'API_KEY_HERE' | |
#The PagerDuty subdomain | |
pd_subdomain = 'YOUR_PD_SUBDOMAIN' | |
#The PagerDuty service id (leave blank for all services or comma separate multiple services) | |
pd_service = '' | |
#Action to take (must be one of 'acknowledge' or 'resolve') | |
incident_action = 'resolve' | |
#User ID of who will be marked as performing these actions | |
incident_action_userid = 'P4G5FIY' | |
HEADERS = { | |
'Authorization': 'Token token={0}'.format(auth_token), | |
'Content-type': 'application/json', | |
} | |
PARAMETERS = { | |
'is_overview': 'true', | |
'service': pd_service, | |
'date_range': 'all', | |
} | |
def mass_update_incidents(): | |
offset = 0 | |
while True: | |
if incident_action == 'resolve': | |
PARAMETERS['status'] = 'triggered,acknowledged' | |
elif incident_action == 'acknowledge': | |
PARAMETERS['status'] = 'triggered' | |
PARAMETERS['offset'] = offset | |
offset = offset + 25 # We update afterwards to avoid another if statement at the end duplicating this one | |
incidents = get_open_incidents(PARAMETERS) | |
if len(incidents) == 0: | |
break | |
for incident in incidents: | |
ack_or_resolve_incident(incident['id'], incident_action, incident_action_userid) | |
def get_open_incidents(params): | |
incidents = requests.get( | |
'https://{0}.pagerduty.com/api/v1/incidents'.format(pd_subdomain), | |
headers=HEADERS, | |
params=params | |
) | |
if incidents.status_code != 200: | |
print("Error getting incidents: {0}".format(incidents.status_code)) | |
print("Response: {0}".format(incidents.text)) | |
return incidents.json()['incidents'] | |
def ack_or_resolve_incident(incident, action, requester): | |
incidents = requests.put( | |
'https://{0}.pagerduty.com/api/v1/incidents/{1}/{2}'.format(pd_subdomain, incident, action), | |
headers=HEADERS, | |
params={ | |
'requester_id': requester, | |
} | |
) | |
if incidents.status_code != 200: | |
print("Error updating incident: {0}".format(incidents.status_code)) | |
print("Response: {0}".format(incidents.text)) | |
def main(argv=None): | |
if argv is None: | |
argv = sys.argv | |
mass_update_incidents() | |
if __name__=='__main__': | |
sys.exit(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment