Skip to content

Commit c3cbaf2

Browse files
authored
Create script.py
1 parent 9c0438a commit c3cbaf2

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#!/usr/bin/env python
2+
3+
import json
4+
import requests
5+
6+
#This Key should be available in .env file
7+
ROUTING_KEY = "" # ENTER EVENTS V2 API INTEGRATION KEY HERE
8+
9+
10+
# This function takes the payload info from the user and can be put in the right format
11+
12+
def trigger_incident(payload):
13+
# Triggers a PagerDuty incident without a previously generated incident key
14+
# Uses Events V2 API - documentation: https://v2.developer.pagerduty.com/docs/send-an-event-events-api-v2
15+
16+
header = {
17+
"Content-Type": "application/json"
18+
}
19+
20+
payload = { # Payload is built with the least amount of fields required to trigger an incident
21+
"routing_key": ROUTING_KEY,
22+
"event_action": "trigger",
23+
"payload": {
24+
"summary": "Azure Resource is expereiencing issues",
25+
"source": f"{payload['resource_id']}",
26+
"severity": "critical",
27+
"component":f"{payload['tags']}",
28+
"class":f"{payload['error_code']}",
29+
"custom_details":f"{payload['system_data']}"
30+
31+
32+
33+
34+
}
35+
}
36+
37+
response = requests.post('https://events.pagerduty.com/v2/enqueue',
38+
data=json.dumps(payload),
39+
headers=header)
40+
41+
if response.json()["status"] == "success":
42+
print('Incident created with with dedup key (also known as incident / alert key) of ' + '"' + response.json()['dedup_key'] + '"')
43+
else:
44+
print(response.text) # print error message if not successful
45+
46+
if __name__ == '__main__':
47+
trigger_incident()

0 commit comments

Comments
 (0)