11#!/usr/bin/env python
2+
23import json
34import requests
45
5-
6- SUBDOMAIN = "" # Enter your subdomain here
7- API_ACCESS_KEY = "" # Enter your subdomain's API access key here
8-
6+ ROUTING_KEY = "" # ENTER EVENTS V2 API INTEGRATION KEY HERE
7+ INCIDENT_KEY = "" # ENTER INCIDENT KEY HERE
98
109def trigger_incident ():
11- """Triggers an incident with a previously generated incident key."""
10+ # Triggers a PagerDuty incident without a previously generated incident key
11+ # Uses Events V2 API - documentation: https://v2.developer.pagerduty.com/docs/send-an-event-events-api-v2
1212
13- headers = {
14- 'Accept' : 'application/vnd.pagerduty+json;version=2' ,
15- 'Authorization' : 'Token token={0}' .format (API_ACCESS_KEY ),
16- 'Content-type' : 'application/json' ,
13+ header = {
14+ "Content-Type" : "application/json"
1715 }
1816
19- payload = json .dumps ({
20- "service_key" : "" , # Enter service key here
21- "incident_key" : "srv01/HTTP" ,
22- "event_type" : "trigger" ,
23- "description" : "FAILURE for production/HTTP on machine srv01.acme.com" ,
24- "client" : "Sample Monitoring Service" ,
25- "client_url" : "https://monitoring.service.com" ,
26- "details" : {
27- "ping time" : "1500ms" ,
28- "load avg" : 0.75
17+ payload = { # Payload is built with the least amount of fields required to trigger an incident
18+ "routing_key" : ROUTING_KEY ,
19+ "event_action" : "trigger" ,
20+ "dedup_key" : INCIDENT_KEY ,
21+ "payload" : {
22+ "summary" : "Example alert on host1.example.com" ,
23+ "source" : "monitoringtool:cloudvendor:central-region-dc-01:852559987:cluster/api-stats-prod-003" ,
24+ "severity" : "critical"
2925 }
30- })
31-
32- r = requests .post ('https://events.pagerduty.com/generic/2010-04-15/create_event.json' ,
33- headers = headers ,
34- data = payload ,
35- )
36-
37- print r .status_code
38- print r .text
26+ }
3927
28+ response = requests .post ('https://events.pagerduty.com/v2/enqueue' ,
29+ data = json .dumps (payload ),
30+ headers = header )
31+
32+ if response .json ()["status" ] == "success" :
33+ print ('Incident Created' )
34+ else :
35+ print response .text # print error message if not successful
4036
4137if __name__ == '__main__' :
4238 trigger_incident ()
0 commit comments