Skip to content

Instantly share code, notes, and snippets.

@lfepp lfepp/get_alerts_csv.py
Last active May 25, 2016

Embed
What would you like to do?
Programatically acess and download the alerts for a PagerDuty incident as a CSV file
#!/usr/bin/env python
#
# Copyright (c) 2016, PagerDuty, Inc. <info@pagerduty.com>
# 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
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 + '/log_entries'
headers = {
'Accept': 'application/vnd.pagerduty+json;version=2',
'Authorization': 'Token token=' + api_key
}
payload = {
'time_zone': time_zone,
'include': ['channels']
}
r = requests.get(url, headers=headers, params=json.dumps(payload))
csvfile = open('test.csv', 'w')
fieldnames = ['date', 'alert_type', 'address', 'user_id', 'user_summary']
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
for i in r.json()['log_entries']:
if i['type'] == 'notify_log_entry':
row = {
'date': i['created_at'],
'alert_type': i['channel']['notification']['type'],
'address': i['channel']['notification']['address'],
'user_id': i['user']['id'],
'user_summary': i['user']['summary']
}
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
You can’t perform that action at this time.