Skip to content

Instantly share code, notes, and snippets.

@Deconstrained
Last active October 4, 2018 10:52
Show Gist options
  • Save Deconstrained/64eb1f344fecca9884cc138a0bc897f8 to your computer and use it in GitHub Desktop.
Save Deconstrained/64eb1f344fecca9884cc138a0bc897f8 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# Prints a list of PagerDuty incidents created on a given PagerDuty service over
# the past 24 hours.
import argparse
import time
try:
import pypd
except ImportError:
print """Missing dependency pypd and/or ujson.
Download the API client library pypd:
https://github.com/PagerDuty/pagerduty-api-python-client/
Make a symbolic link to (or copy the pypd folder to) to the curent
working directory
Install ujson via 'pip install ujson' from the Python package index;
https://pypi.python.org/pypi/ujson"""
def main():
parser = argparse.ArgumentParser(description="""Get incidents for a service
that happened over the past 24 hours""")
parser.add_argument('-k', '--key', required=True, dest='api_key',
help="""API key (V2 only) to use""")
parser.add_argument('-i', '--id', required=False, dest='service_id',
default=None, help="""ID(s) of the service in question
(comma-delineated); if none specified, all incidents will be queried.""")
parser.add_argument('-t', '--team', required=False, dest='team_id',
default=None, help="""ID(s) of the teams for which to export incidents
(comma-delineated); if none specified, the query will apply to all
teams.""")
args = parser.parse_args()
pypd.api_key=args.api_key
now = int(time.time())
yesterday = now - 86400
t_now = time.strftime('%Y-%m-%dT%H:%M:%S-00',time.gmtime(now))
t_yesterday = time.strftime('%Y-%m-%dT%H:%M:%S-00',time.gmtime(yesterday))
kwargs = {
'since':t_yesterday,
'until':t_now
}
if args.service_id:
kwargs['service_ids'] = [t.strip() for t in args.service_id.split(',')]
if args.team_id:
kwargs['team_ids'] = [t.strip() for t in args.team_id.split(',')]
incidents = pypd.Incident.find(**kwargs)
# Note, you can print more details of the incident; see the response schema
# for /incidents:
#
for i in incidents:
print i['summary']
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment