mirror of
https://github.com/metafy-social/python-scripts.git
synced 2025-02-25 09:28:42 +00:00
Create script.py
This commit is contained in:
parent
9c0438a2e2
commit
c3cbaf2edf
47
scripts/PagerDuty-Integration/script.py
Normal file
47
scripts/PagerDuty-Integration/script.py
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
import json
|
||||||
|
import requests
|
||||||
|
|
||||||
|
#This Key should be available in .env file
|
||||||
|
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()
|
Loading…
x
Reference in New Issue
Block a user