|
| 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