Created
May 5, 2015 18:16
-
-
Save ryanhoskin/f006c328f0f44d52c05a to your computer and use it in GitHub Desktop.
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) 2011-2012, 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. | |
import requests | |
import sys | |
def get_incident_count(since, until, headers): | |
""" | |
Retrieve the count of incidents in a given time-period, as an 'int'. | |
Dates should be in the format 'YYYY-MM-DD'. | |
""" | |
payload = {'since': since, 'until': until} | |
get_incident_count_url = 'https://rentrak-isg.pagerduty.com/api/v1/incidents/count' | |
r = requests.get(get_incident_count_url, params=payload, headers=headers) | |
return int(r.json()['total']) | |
def get_incident_ids(since, until, headers): | |
""" | |
Based on an incident-count used to create an 'offset', | |
retrieve incident-IDs, in batches-of-100, and return as a list. | |
Dates should be in the format 'YYYY-MM-DD'. | |
""" | |
id_list = [] | |
count = get_incident_count(since, until, headers) | |
get_incident_ids_url = 'https://rentrak-isg.pagerduty.com/api/v1/incidents' | |
payload = {'since': since, 'until': until} | |
for ea_hun in xrange(0,count) or int(ea_hun)==count: | |
if int(ea_hun)%100==0: | |
payload['offset'] = ea_hun | |
r = requests.get(get_incident_ids_url, params=payload, headers=headers, stream=True) | |
id_list = id_list + [ea_inc['id'] for ea_inc in r.json()['incidents']] | |
return id_list | |
def get_details_by_incident(since, until): | |
""" | |
Based on a list of incident-IDs, retrieve incident details. | |
Process json-payload and output to CSV file. | |
CLI Usage (Linux example): "./get_incident_details_csv.py 2011-10-01 2011-10-04". | |
""" | |
headers = { | |
'Authorization': 'Token token=****', | |
'Content-type': 'application/json', | |
} | |
id_list = get_incident_ids(since, until, headers) | |
test_file = open('test.txt', 'w') | |
fin_file = open('incident_report_{0}_to_{1}_details.csv'.format(since, until), 'w') | |
fin_file.write('IncidentID,Created-At,Summary\n') | |
for ea_id in id_list: | |
r = requests.get('https://rentrak-isg.pagerduty.com/api/v1/incidents/{0}/log_entries?include[]=channel'.format(ea_id), headers=headers, stream=True) | |
test_file.write(str(r.json())) | |
for ea_entry in reversed(r.json()['log_entries']): | |
if ea_entry['type'] == 'trigger': | |
if ea_entry['channel']['type']=='nagios': | |
fin_file.write(','.join([ea_id, ea_entry['created_at'], '"{0}"'.format(ea_entry['channel']['summary'])]) + '\n') | |
if ea_entry['channel']['type']=='api': | |
fin_file.write(','.join([ea_id, ea_entry['created_at'], '"{0}"'.format(ea_entry['channel']['description'])]) + '\n') | |
if ea_entry['channel']['type']=='web_trigger': | |
fin_file.write(','.join([ea_id, ea_entry['created_at'], '"{0}"'.format(ea_entry['channel']['summary'])]) + '\n') | |
if ea_entry['channel']['type']=='email': | |
fin_file.write(','.join([ea_id, ea_entry['created_at'], '"{0}"'.format(ea_entry['channel']['summary'])]) + '\n') | |
else: | |
pass | |
fin_file.close() | |
if __name__ == '__main__': | |
get_details_by_incident(sys.argv[1], sys.argv[2]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment