Last active
September 28, 2018 07:53
-
-
Save Deconstrained/9d83fe401635d278df9f6191723504a1 to your computer and use it in GitHub Desktop.
Generate a report of alerts for a given incident
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
#!/usr/bin/env python | |
# | |
# Copyright (c) 2017, PagerDuty, Inc. <[email protected]> | |
# All rights reserved. | |
# | |
# Redistribution and use in source and binary forms, with or without | |
# modification, are permitted provided that the following conditions are met: | |
# * Redistributions of source code must retain the above copyright | |
# notice, this list of conditions and the following disclaimer. | |
# * Redistributions in binary form must reproduce the above copyright | |
# notice, this list of conditions and the following disclaimer in the | |
# documentation and/or other materials provided with the distribution. | |
# * Neither the name of PagerDuty Inc nor the | |
# names of its contributors may be used to endorse or promote products | |
# derived from this software without specific prior written permission. | |
# | |
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
# AND | |
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |
# DISCLAIMED. IN NO EVENT SHALL PAGERDUTY INC BE LIABLE FOR ANY | |
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | |
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | |
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
# | |
# A sample script to programatically acess and download the alerts for an | |
# incident as a CSV file | |
# Currently, time_zone defaults to UTC | |
# CLI USAGE: './get_alerts_csv api_key incident_id [time_zone]' | |
# api_key: PagerDuty API access token | |
# incident_id: Unique ID for the incident you want to pull | |
# time_zone: Time zone in which dates will be returned. Documentation on time | |
# zone options is available at | |
# https://developer.pagerduty.com/documentation/rest/types | |
# Based on Luke Epp's script, this actually gets *alert records* versus incident | |
# notifications. The original script can be found here: | |
# https://gist.github.com/lfepp/69a2288d898248800752d38e593323c1 | |
import requests | |
import sys | |
import csv | |
import json | |
def get_alerts(api_key, incident_id, time_zone='UTC'): | |
url = 'https://api.pagerduty.com/incidents/' + incident_id + '/alerts' | |
headers = { | |
'Accept': 'application/vnd.pagerduty+json;version=2', | |
'Authorization': 'Token token=' + api_key | |
} | |
r = requests.get(url, headers=headers) | |
csvfile = open('test.csv', 'w') | |
fieldnames = ['timestamp', 'title', 'status', 'alert_key', 'severity'] | |
writer = csv.DictWriter(csvfile, fieldnames=fieldnames) | |
writer.writeheader() | |
for i in r.json()['alerts']: | |
row = { | |
'timestamp': i['created_at'], | |
'title': i['summary'], | |
'status': i['status'], | |
'alert_key': i['alert_key'], | |
'severity': i['severity'] | |
} | |
writer.writerow(row) | |
csvfile.close() | |
if __name__ == '__main__': | |
if len(sys.argv) == 1: | |
print "Error: You did not enter any parameters.\nUsage: ./get_alerts_csv | |
api_key incident_id [time_zone]\n\tapi_key: PagerDuty API access | |
token\n\tincident_id: Unique ID for the incident you want to pull\n\ttime_zone: | |
Time zone in which dates will be returned. Documentation on time zone options is | |
available at https://developer.pagerduty.com/documentation/rest/types" | |
elif len(sys.argv) == 2: | |
print "Error: You did not include an incident_id.\nUsage: | |
./get_alerts_csv api_key incident_id [time_zone]\n\tapi_key: PagerDuty API | |
access token\n\tincident_id: Unique ID for the incident you want to | |
pull\n\ttime_zone: Time zone in which dates will be returned. Documentation on | |
time zone options is available at | |
https://developer.pagerduty.com/documentation/rest/types" | |
elif len(sys.argv) == 3: | |
get_alerts(sys.argv[1], sys.argv[2]) | |
else: | |
get_alerts(sys.argv[1], sys.argv[2], sys.argv[3]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment