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 close alert script #18

Merged
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/close_pagerduty_alert.py
67 changes: 67 additions & 0 deletions tubular/scripts/close_pagerduty_alert.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import logging
import sys
import traceback
import requests
import click

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

@click.command("close_pagerduty_alert")
@click.option(
'--routing_key',
required=True,
help="Routing key for PagerDuty Events API v2.",
)
@click.option(
'--dedup_key',
required=True,
help="Deduplication key of the PagerDuty alert to close.",
)
@click.option(
'--source',
default=None,
help="Source of the request (optional)."
)
def close_pagerduty_alert(routing_key, dedup_key, source):
"""
Close a PagerDuty alert

Arguments:
routing_key: Events API v2 routing key
dedup_key: The deduplication key of the alert
source: The source of the request (optional)
"""
try:
logging.info(f"Closing alert with deduplication key: {dedup_key}")

# Define the payload for the Events API v2
payload = {
"routing_key": routing_key,
"event_action": "resolve", # "resolve" is used to close an alert
"dedup_key": dedup_key,
}

# Add optional source if provided
if source:
payload["payload"] = {"source": source}

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

# Check the response status
if response.status_code == 202:
logging.info("Alert successfully closed in PagerDuty.")
else:
logging.error(f"Failed to close alert: {response.text}")
response.raise_for_status()

except Exception as err: # pylint: disable=broad-except
traceback.print_exc()
click.secho(f"{err}", fg="red")
sys.exit(1)


if __name__ == "__main__":
close_pagerduty_alert()
Loading