From c3cbaf2edffc71618a4df3e26923883a98587ab9 Mon Sep 17 00:00:00 2001 From: Vallabh Chugh Date: Tue, 11 Oct 2022 00:04:46 +0530 Subject: [PATCH 1/8] Create script.py --- scripts/PagerDuty-Integration/script.py | 47 +++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 scripts/PagerDuty-Integration/script.py diff --git a/scripts/PagerDuty-Integration/script.py b/scripts/PagerDuty-Integration/script.py new file mode 100644 index 0000000..185f8ef --- /dev/null +++ b/scripts/PagerDuty-Integration/script.py @@ -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() From 3072b534c87b1fb831197277ae3728cb2490ec7a Mon Sep 17 00:00:00 2001 From: Vallabh Chugh Date: Tue, 11 Oct 2022 00:19:01 +0530 Subject: [PATCH 2/8] Create Readme.md --- scripts/PagerDuty-Integration/Readme.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 scripts/PagerDuty-Integration/Readme.md diff --git a/scripts/PagerDuty-Integration/Readme.md b/scripts/PagerDuty-Integration/Readme.md new file mode 100644 index 0000000..dd160ae --- /dev/null +++ b/scripts/PagerDuty-Integration/Readme.md @@ -0,0 +1,23 @@ +## 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 + Screenshot 2022-10-11 at 12 09 33 AM + + * Import the file as a module + * Send your desired payload to the module + * Finalize your payload accoring the parameters(severity,components) + * Voila! If everything works fine you will recieve a Page + From df8febbd6576175f3c739bf16fecbf9a4908805b Mon Sep 17 00:00:00 2001 From: Vallabh Chugh Date: Tue, 11 Oct 2022 00:20:38 +0530 Subject: [PATCH 3/8] Update script.py --- scripts/PagerDuty-Integration/script.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/scripts/PagerDuty-Integration/script.py b/scripts/PagerDuty-Integration/script.py index 185f8ef..feef8a0 100644 --- a/scripts/PagerDuty-Integration/script.py +++ b/scripts/PagerDuty-Integration/script.py @@ -2,9 +2,14 @@ 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 = "" # ENTER EVENTS V2 API INTEGRATION KEY HERE +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 From 93b75859d53c03cb02939052d5efed9e01e89426 Mon Sep 17 00:00:00 2001 From: Vallabh Chugh Date: Tue, 11 Oct 2022 00:21:14 +0530 Subject: [PATCH 4/8] Update Readme.md --- scripts/PagerDuty-Integration/Readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/PagerDuty-Integration/Readme.md b/scripts/PagerDuty-Integration/Readme.md index dd160ae..91b13e6 100644 --- a/scripts/PagerDuty-Integration/Readme.md +++ b/scripts/PagerDuty-Integration/Readme.md @@ -12,7 +12,7 @@ A Python3 script to send your desired payload to PagerDuty and create an inciden ### 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 + * 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 Screenshot 2022-10-11 at 12 09 33 AM From dd3bae5debfb5455f3a3ab507ba9c0a931b8d1f0 Mon Sep 17 00:00:00 2001 From: Vallabh Chugh Date: Tue, 11 Oct 2022 00:26:31 +0530 Subject: [PATCH 5/8] Update Readme.md --- scripts/PagerDuty-Integration/Readme.md | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/scripts/PagerDuty-Integration/Readme.md b/scripts/PagerDuty-Integration/Readme.md index 91b13e6..3adc5cf 100644 --- a/scripts/PagerDuty-Integration/Readme.md +++ b/scripts/PagerDuty-Integration/Readme.md @@ -18,6 +18,12 @@ A Python3 script to send your desired payload to PagerDuty and create an inciden * Import the file as a module * Send your desired payload to the module - * Finalize your payload accoring the parameters(severity,components) + * 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 From fb0e9422149db3bedee58772f13b3ca898a4e573 Mon Sep 17 00:00:00 2001 From: Vallabh Chugh Date: Tue, 11 Oct 2022 00:26:56 +0530 Subject: [PATCH 6/8] Update Readme.md --- scripts/PagerDuty-Integration/Readme.md | 1 + 1 file changed, 1 insertion(+) diff --git a/scripts/PagerDuty-Integration/Readme.md b/scripts/PagerDuty-Integration/Readme.md index 3adc5cf..fb18dc5 100644 --- a/scripts/PagerDuty-Integration/Readme.md +++ b/scripts/PagerDuty-Integration/Readme.md @@ -24,6 +24,7 @@ A Python3 script to send your desired payload to PagerDuty and create an inciden "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 From 6278f1a31ecab86c5f451a00480f27353e26c942 Mon Sep 17 00:00:00 2001 From: Vallabh Chugh Date: Tue, 11 Oct 2022 00:27:26 +0530 Subject: [PATCH 7/8] Update Readme.md --- scripts/PagerDuty-Integration/Readme.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/PagerDuty-Integration/Readme.md b/scripts/PagerDuty-Integration/Readme.md index fb18dc5..01bbe3f 100644 --- a/scripts/PagerDuty-Integration/Readme.md +++ b/scripts/PagerDuty-Integration/Readme.md @@ -19,12 +19,12 @@ A Python3 script to send your desired payload to PagerDuty and create an inciden * 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 From 7f010d43d7c33895fe4ed04f6c88209e61ab9363 Mon Sep 17 00:00:00 2001 From: Vallabh Chugh Date: Tue, 11 Oct 2022 00:31:25 +0530 Subject: [PATCH 8/8] Update Readme.md --- scripts/PagerDuty-Integration/Readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/PagerDuty-Integration/Readme.md b/scripts/PagerDuty-Integration/Readme.md index 01bbe3f..cac2d91 100644 --- a/scripts/PagerDuty-Integration/Readme.md +++ b/scripts/PagerDuty-Integration/Readme.md @@ -21,7 +21,7 @@ A Python3 script to send your desired payload to PagerDuty and create an inciden * My example of payload ``` payload= { - "resource_id" : resource_id, + "resource_id" : resource_id, "system_data":"This a resource in the development system of XYZ corp.", "tags":"'HA System','Non-critical-system','SpringBoot-App'" ```