Skip to content

Commit

Permalink
Merge pull request #17 from edx/ihassan/GSRE_2064_added_pd_alert_script
Browse files Browse the repository at this point in the history
chore: Added pagerduty alert script
  • Loading branch information
syedimranhassan authored Jan 1, 2025
2 parents 8a05f17 + c5fbfd4 commit 8c7dd3e
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 0 deletions.
1 change: 1 addition & 0 deletions scripts/alert_pagerduty.py
88 changes: 88 additions & 0 deletions tubular/scripts/alert_pagerduty.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
"""
Command-line script to submit an alert to PagerDuty API.
"""

import logging
import sys
import traceback
import requests
import click

logging.basicConfig(stream=sys.stdout, level=logging.INFO)
LOG = logging.getLogger(__name__)

@click.command("alert_pagerduty")
@click.option(
'--routing_key',
required=True,
help="Routing key for PagerDuty Events API (Integration key).",
)
@click.option(
'--message',
required=True,
help="Summary or title of the PagerDuty alert.",
)
@click.option(
'--description',
required=True,
help="Details or description of the PagerDuty alert.",
)
@click.option(
'--severity',
default="error",
type=click.Choice(['critical', 'error', 'warning', 'info'], case_sensitive=False),
help="Severity of the alert (default: error).",
)
@click.option(
'--source',
default="custom-script",
help="Source of the alert (default: custom-script).",
)
def alert_pagerduty(routing_key, message, description, severity, source):
"""
Create a PagerDuty alert using the Events API.
Arguments:
routing_key: Integration key for PagerDuty.
message: The alert summary or title.
description: The alert details.
severity: The severity level of the alert.
source: The source of the alert.
"""
try:
logging.info("Creating alert on PagerDuty")

# Define the payload for the PagerDuty Events API
payload = {
"routing_key": routing_key,
"event_action": "trigger",
"payload": {
"summary": message,
"severity": severity,
"source": source,
"custom_details": {
"description": description
}
}
}

# Send the request to PagerDuty
response = requests.post(
"https://events.pagerduty.com/v2/enqueue",
json=payload
)

# Check the response status
if response.status_code == 202:
LOG.info("Alert successfully created on PagerDuty.")
else:
LOG.error("Failed to create alert. Response: %s", response.text)
response.raise_for_status()
except Exception as err: # pylint: disable=broad-except
traceback.print_exc()
click.secho(f"Error: {err}", fg='red')
sys.exit(1)


if __name__ == "__main__":
alert_pagerduty()

0 comments on commit 8c7dd3e

Please sign in to comment.