From 7edff864161faff124cd4e1d374941dae136372c Mon Sep 17 00:00:00 2001 From: Ashley Brooks Date: Mon, 17 Jul 2017 10:33:48 -0700 Subject: [PATCH] Adding examples for V2 Events API --- EVENTS_API_v2/ack/ack_incident.py | 39 +++++++++++++++++ EVENTS_API_v2/resolve/resolve_incident.py | 39 +++++++++++++++++ .../trigger/trigger_with_incident_key.py | 42 +++++++++++++++++++ .../trigger/trigger_without_incident_key.py | 41 ++++++++++++++++++ 4 files changed, 161 insertions(+) create mode 100644 EVENTS_API_v2/ack/ack_incident.py create mode 100644 EVENTS_API_v2/resolve/resolve_incident.py create mode 100644 EVENTS_API_v2/trigger/trigger_with_incident_key.py create mode 100644 EVENTS_API_v2/trigger/trigger_without_incident_key.py diff --git a/EVENTS_API_v2/ack/ack_incident.py b/EVENTS_API_v2/ack/ack_incident.py new file mode 100644 index 0000000..a31052c --- /dev/null +++ b/EVENTS_API_v2/ack/ack_incident.py @@ -0,0 +1,39 @@ +#!/usr/bin/env python +import json +import requests + + +SUBDOMAIN = "" # Enter your subdomain here +API_ACCESS_KEY = "" # Enter your subdomain's API access key here + + +def ack_incident(): + """Acknowledges a triggered incident using the customer's API access key and incident key.""" + + headers = { + 'Accept': 'application/vnd.pagerduty+json;version=2', + 'Authorization': 'Token token={0}'.format(API_ACCESS_KEY), + 'Content-type': 'application/json' + } + + payload = json.dumps({ + "service_key": "", # Enter the service key here + "incident_key": "", # Enter the incident key here + "event_type": "acknowledge", + "description": "Andrew now working on the problem.", # Enter your own description + "details": { + "work started": "2010-06-10 05:43" + } + }) + + r = requests.post('https://events.pagerduty.com/generic/2010-04-15/create_event.json', + headers=headers, + data=payload, + ) + + print r.status_code + print r.text + + +if __name__ == '__main__': + ack_incident() diff --git a/EVENTS_API_v2/resolve/resolve_incident.py b/EVENTS_API_v2/resolve/resolve_incident.py new file mode 100644 index 0000000..ba01bb0 --- /dev/null +++ b/EVENTS_API_v2/resolve/resolve_incident.py @@ -0,0 +1,39 @@ +#!/usr/bin/env python +import json +import requests + + +SUBDOMAIN = "" # Enter your subdomain here +API_ACCESS_KEY = "" # Enter your subdomain's API access key here + + +def resolve_incident(): + """Resolves a PagerDuty incident using customer's API access key and incident key.""" + + headers = { + 'Accept': 'application/vnd.pagerduty+json;version=2', + 'Authorization': 'Token token={0}'.format(API_ACCESS_KEY), + 'Content-type': 'application/json', + } + + payload = json.dumps({ + "service_key": "", # Enter service key here + "incident_key": "", # Enter incident key here + "event_type": "resolve", + "description": "Andrew fixed the problem.", # Enter personalized description + "details": { + "fixed at": "2010-06-10 06:00" + } + }) + + r = requests.post('https://events.pagerduty.com/generic/2010-04-15/create_event.json', + headers=headers, + data=payload, + ) + + print r.status_code + print r.text + + +if __name__ == '__main__': + resolve_incident() diff --git a/EVENTS_API_v2/trigger/trigger_with_incident_key.py b/EVENTS_API_v2/trigger/trigger_with_incident_key.py new file mode 100644 index 0000000..650cf7d --- /dev/null +++ b/EVENTS_API_v2/trigger/trigger_with_incident_key.py @@ -0,0 +1,42 @@ +#!/usr/bin/env python +import json +import requests + + +SUBDOMAIN = "" # Enter your subdomain here +API_ACCESS_KEY = "" # Enter your subdomain's API access key here + + +def trigger_incident(): + """Triggers an incident with a previously generated incident key.""" + + headers = { + 'Accept': 'application/vnd.pagerduty+json;version=2', + 'Authorization': 'Token token={0}'.format(API_ACCESS_KEY), + 'Content-type': 'application/json', + } + + payload = json.dumps({ + "service_key": "", # Enter service key here + "incident_key": "srv01/HTTP", + "event_type": "trigger", + "description": "FAILURE for production/HTTP on machine srv01.acme.com", + "client": "Sample Monitoring Service", + "client_url": "https://monitoring.service.com", + "details": { + "ping time": "1500ms", + "load avg": 0.75 + } + }) + + r = requests.post('https://events.pagerduty.com/generic/2010-04-15/create_event.json', + headers=headers, + data=payload, + ) + + print r.status_code + print r.text + + +if __name__ == '__main__': + trigger_incident() \ No newline at end of file diff --git a/EVENTS_API_v2/trigger/trigger_without_incident_key.py b/EVENTS_API_v2/trigger/trigger_without_incident_key.py new file mode 100644 index 0000000..ef01c60 --- /dev/null +++ b/EVENTS_API_v2/trigger/trigger_without_incident_key.py @@ -0,0 +1,41 @@ +#!/usr/bin/env python +import json +import requests + + +SUBDOMAIN = "" # Enter your subdomain here +API_ACCESS_KEY = "" # Enter your subdomain's API access key here + + +def trigger_incident(): + """Triggers a PagerDuty incident without a previously generated incident key.""" + + headers = { + 'Accept': 'application/vnd.pagerduty+json;version=2', + 'Authorization': 'Token token={0}'.format(API_ACCESS_KEY), + 'Content-type': 'application/json', + } + + payload = json.dumps({ + "service_key": "", # Enter service key here + "event_type": "trigger", + "description": "FAILURE for production/HTTP on machine srv01.acme.com", + "client": "Sample Monitoring Service", + "client_url": "https://monitoring.service.com", + "details": { + "ping time": "1500ms", + "load avg": 0.75 + } + }) + + r = requests.post('https://events.pagerduty.com/generic/2010-04-15/create_event.json', + headers=headers, + data=payload, + ) + + print r.status_code + print r.text + + +if __name__ == '__main__': + trigger_incident() \ No newline at end of file