Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Added pagerduty alert script #17

Merged
merged 1 commit into from
Jan 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
Loading