Merge pull request #334 from vallabhiaf/PagerDuty-Integration

Pager duty integration
This commit is contained in:
Advaita Saha 2022-10-11 01:18:20 +05:30 committed by GitHub
commit f0d321b300
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 82 additions and 0 deletions

View File

@ -0,0 +1,30 @@
## Check-System-Usage
![built by developers](http://ForTheBadge.com/images/badges/built-by-developers.svg)
![python](https://img.shields.io/badge/language-Python-orange?style=for-the-badge)
[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg?style=plasitc)](https://github.com/psf/black)
![License](https://img.shields.io/github/license/GDSC-RCCIIT/General-Purpose-Scripts?color=blue&style=plasitc)
### About
A Python3 script to send your desired payload to PagerDuty and create an incident
### Steps
* 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
* Copy the Integration Key from the settings of the service as shown below and paste it in your env file
<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">
* Import the file as a module
* Send your desired payload to the module
* My example of payload
```
payload= {
"resource_id" : resource_id,
"system_data":"This a resource in the development system of XYZ corp.",
"tags":"'HA System','Non-critical-system','SpringBoot-App'"
```
* Finalize your payload accoring the parameters(severity,components etc .)
* Voila! If everything works fine you will recieve a Page

View File

@ -0,0 +1,52 @@
#!/usr/bin/env python
import json
import requests
from pathlib import Path
from dotenv import load_dotenv
env_path=Path('.')/'.env'
load_dotenv(dotenv_path=env_path)
#This Key should be available in .env file
ROUTING_KEY = os.environ['ROUTING_KEY'] # ENTER EVENTS V2 API INTEGRATION KEY HERE
# This function takes the payload info from the user and can be put in the right format
def trigger_incident(payload):
# Triggers a PagerDuty incident without a previously generated incident key
# Uses Events V2 API - documentation: https://v2.developer.pagerduty.com/docs/send-an-event-events-api-v2
header = {
"Content-Type": "application/json"
}
payload = { # Payload is built with the least amount of fields required to trigger an incident
"routing_key": ROUTING_KEY,
"event_action": "trigger",
"payload": {
"summary": "Azure Resource is expereiencing issues",
"source": f"{payload['resource_id']}",
"severity": "critical",
"component":f"{payload['tags']}",
"class":f"{payload['error_code']}",
"custom_details":f"{payload['system_data']}"
}
}
response = requests.post('https://events.pagerduty.com/v2/enqueue',
data=json.dumps(payload),
headers=header)
if response.json()["status"] == "success":
print('Incident created with with dedup key (also known as incident / alert key) of ' + '"' + response.json()['dedup_key'] + '"')
else:
print(response.text) # print error message if not successful
if __name__ == '__main__':
trigger_incident()