Here’s a quick Python Script that connects to PagerDuty’s API and pulls down the incident ID, description, and notes for all incidents in a given date range. It’s useful for appending to an existing CSV download via VLOOKUP, particularly PagerDuty’s new Advanced CSV Export.
I hope this is useful to you!
# -*- coding: utf-8 -*- # This script requires pygerduty: https://github.com/dropbox/pygerduty # Given a start date and end date, this sample will export the description and # notes from all incidents to a CSV file. The CSV file is formatted for use # with Excel. This will export to incident_notes_[start date]_-_[end date].csv # This would be most useful to join with a CSV file as exported from PagerDuty. # Usage: edit subdomain and specify an API key (must have full access) below. # (What's the subdomain? Example: http://demo.pagerduty.com --> "demo") # Then run: python incidents.py [start date: YYYY-MM-DD] [end date: YYYY-MM-DD] # Example: python incidents.py 2015-01-01 2015-03-31 import pygerduty pager = pygerduty.PagerDuty("SUBDOMAIN", "APIKEY") import sys startdate, enddate = sys.argv[1:] # This function escapes quotes in notes in a way that's Excel compatible. # All other characters, including comma and new-line, are handled correctly # by way of being included within the surrounding quotes. def escape( escape_me ): escape_me = escape_me.replace("\"", "\"\"") return escape_me my_filename = "incident_notes_%s_-_%s.csv" % (startdate, enddate) with open(my_filename, 'w',1) as the_file: the_file.write("Incident ID,Description,Notes\n") for incident in pager.incidents.list(since=startdate, until=enddate): if hasattr(incident.trigger_summary_data, 'subject'): my_description = incident.trigger_summary_data.subject elif hasattr(incident.trigger_summary_data, 'description'): my_description = incident.trigger_summary_data.description my_line = '%s,"%s","' % (incident.incident_number, escape(my_description)) my_count = 0 for note in incident.notes.list(incident_id=incident.incident_number): my_count += 1 if my_count > 1: my_line += "\n" my_line += "%s, %s: %s" % (escape(note.user.name), escape(note.created_at), escape(note.content)) my_line += "\"\n" the_file.write(my_line)
Helpful and nice.
PS: You should never use data object types as variable name, in this case: “str” .
Goes crazy while throwing errors, or worse, throws errors.
Thanks for teaching me! 🙂
I am getting the error : TypeError: __init__() got multiple values for keyword argument ‘self’
Traceback (most recent call last):
File “incidents.py”, line 39, in
for incident in pager.incidents.list():
File “/usr/local/lib/python2.7/site-packages/pygerduty/__init__.py”, line 146, in list
this_paginated_result = self._list_no_pagination(**these_kwargs)
File “/usr/local/lib/python2.7/site-packages/pygerduty/__init__.py”, line 128, in _list_no_pagination
File “/usr/local/lib/python2.7/site-packages/pygerduty/__init__.py”, line 113, in _list_response
TypeError: __init__() got multiple values for keyword argument ‘self’
Hi! Sorry it’s been years since I’ve worked on this (or at PagerDuty). The issue might be coming from pygerduty but I’m honestly not sure.
Was the issue resolved?
Don’t know. If it works for you, great! If not and you find a solution, I’d be happy to update my post. I don’t work at PagerDuty anymore and don’t have access to a PagerDuty account, so I can’t fix it myself. Best of luck!
To correct script you will need to update these lines.
pager = pygerduty.PagerDuty(“SUBDOMAIN”, “APIKEY”)
pager = pygerduty.v2.PagerDuty(“APIKEY”)