Skip to content

Commit f0d321b

Browse files
authored
Merge pull request #334 from vallabhiaf/PagerDuty-Integration
Pager duty integration
2 parents 19f0244 + 7f010d4 commit f0d321b

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
## Check-System-Usage
2+
3+
![built by developers](http://ForTheBadge.com/images/badges/built-by-developers.svg)
4+
![python](https://img.shields.io/badge/language-Python-orange?style=for-the-badge)
5+
[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg?style=plasitc)](https://github.com/psf/black)
6+
![License](https://img.shields.io/github/license/GDSC-RCCIIT/General-Purpose-Scripts?color=blue&style=plasitc)
7+
8+
### About
9+
10+
A Python3 script to send your desired payload to PagerDuty and create an incident
11+
12+
13+
### Steps
14+
15+
* Make sure you have create a serive in your PagerDuty account. [Refer here](https://support.pagerduty.com/docs/services-and-integrations) if not done already
16+
* Copy the Integration Key from the settings of the service as shown below and paste it in your env file
17+
<img width="1344" alt="Screenshot 2022-10-11 at 12 09 33 AM" src="https://user-images.githubusercontent.com/10003129/194933099-13dd03b8-c139-4366-bf18-272fd2ae7392.png">
18+
19+
* Import the file as a module
20+
* Send your desired payload to the module
21+
* My example of payload
22+
```
23+
payload= {
24+
"resource_id" : resource_id,
25+
"system_data":"This a resource in the development system of XYZ corp.",
26+
"tags":"'HA System','Non-critical-system','SpringBoot-App'"
27+
```
28+
* Finalize your payload accoring the parameters(severity,components etc .)
29+
* Voila! If everything works fine you will recieve a Page
30+
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#!/usr/bin/env python
2+
3+
import json
4+
import requests
5+
from pathlib import Path
6+
from dotenv import load_dotenv
7+
env_path=Path('.')/'.env'
8+
load_dotenv(dotenv_path=env_path)
9+
10+
11+
#This Key should be available in .env file
12+
ROUTING_KEY = os.environ['ROUTING_KEY'] # ENTER EVENTS V2 API INTEGRATION KEY HERE
13+
14+
15+
# This function takes the payload info from the user and can be put in the right format
16+
17+
def trigger_incident(payload):
18+
# Triggers a PagerDuty incident without a previously generated incident key
19+
# Uses Events V2 API - documentation: https://v2.developer.pagerduty.com/docs/send-an-event-events-api-v2
20+
21+
header = {
22+
"Content-Type": "application/json"
23+
}
24+
25+
payload = { # Payload is built with the least amount of fields required to trigger an incident
26+
"routing_key": ROUTING_KEY,
27+
"event_action": "trigger",
28+
"payload": {
29+
"summary": "Azure Resource is expereiencing issues",
30+
"source": f"{payload['resource_id']}",
31+
"severity": "critical",
32+
"component":f"{payload['tags']}",
33+
"class":f"{payload['error_code']}",
34+
"custom_details":f"{payload['system_data']}"
35+
36+
37+
38+
39+
}
40+
}
41+
42+
response = requests.post('https://events.pagerduty.com/v2/enqueue',
43+
data=json.dumps(payload),
44+
headers=header)
45+
46+
if response.json()["status"] == "success":
47+
print('Incident created with with dedup key (also known as incident / alert key) of ' + '"' + response.json()['dedup_key'] + '"')
48+
else:
49+
print(response.text) # print error message if not successful
50+
51+
if __name__ == '__main__':
52+
trigger_incident()

0 commit comments

Comments
 (0)