Skip to content

Commit a4ad72d

Browse files
Refactor send_alert.py and init_alertmanager.sh scripts
1 parent c958cfc commit a4ad72d

File tree

2 files changed

+16
-9
lines changed

2 files changed

+16
-9
lines changed

send_alert.py

+9-5
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
11
import requests
22
import json
3+
import time
34
from src.utils import get_ip_address
45

56
def send_test_alert(alertmanager_url, alert_name, severity, instance):
6-
# Define the alert data
7+
# Generate a unique alert name by appending the current timestamp
8+
unique_alert_name = f"{alert_name}_{int(time.time())}"
9+
10+
# Define the alert data with the unique alert name
711
alert_data = [
812
{
913
"labels": {
10-
"alertname": alert_name,
14+
"alertname": unique_alert_name,
1115
"severity": severity,
1216
"instance": instance
1317
},
1418
"annotations": {
15-
"description": "This is a test alert",
19+
"description": f"This is a test alert generated at {time.strftime('%Y-%m-%d %H:%M:%S')}",
1620
"summary": "Test alert to check Slack notifications"
1721
}
1822
}
@@ -32,7 +36,7 @@ def send_test_alert(alertmanager_url, alert_name, severity, instance):
3236

3337
# Check the response
3438
if response.status_code == 202:
35-
print("Test alert sent successfully!")
39+
print(f"Test alert '{unique_alert_name}' sent successfully!")
3640
else:
3741
print(f"Failed to send alert. Status code: {response.status_code}, Response: {response.text}")
3842

@@ -47,5 +51,5 @@ def send_test_alert(alertmanager_url, alert_name, severity, instance):
4751
alertmanager_port = "9093"
4852
alertmanager_url = f"http://{alertmanager_ip}:{alertmanager_port}"
4953

50-
# Send a test alert
54+
# Send a test alert with a unique name
5155
send_test_alert(alertmanager_url, "ScriptAlert", "critical", "instance_name")

src/scripts/initialization/init_alertmanager.sh

+7-4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
# Set the path for the Alertmanager configuration file
44
ALERTMANAGER_CONFIG_FILE="prometheus_config/alertmanager.yml"
55
IP_ADDRESS=$(hostname -I | awk '{print $1}')
6+
# take slack_webhook from .env file
7+
SLACK_WEBHOOK_URL=$(grep SLACK_WEBHOOK_URL .env | cut -d '=' -f2)
68

79
# Create the Alertmanager configuration
810
cat > "$ALERTMANAGER_CONFIG_FILE" <<EOL
@@ -19,13 +21,13 @@ route:
1921
routes:
2022
- receiver: 'slack-notifications'
2123
continue: true # This allows the alert to also be sent to the next receiver
22-
- receiver: 'custom-webhook'
24+
- receiver: 'systemguard-webhook'
2325
2426
receivers:
2527
- name: 'slack-notifications'
2628
slack_configs:
2729
- send_resolved: true
28-
api_url: '<SLACK_WEBHOOK_URL>'
30+
api_url: '$SLACK_WEBHOOK_URL'
2931
channel: '#general'
3032
username: 'Alertmanager'
3133
icon_emoji: ':warning:'
@@ -36,11 +38,12 @@ receivers:
3638
*Description:* {{ .CommonAnnotations.description }}
3739
*Summary:* {{ .CommonAnnotations.summary }}
3840
39-
- name: 'custom-webhook'
41+
- name: 'systemguard-webhook'
4042
webhook_configs:
4143
- send_resolved: true
4244
url: 'http://$IP_ADDRESS:5001/alerts'
43-
max_alerts: 0 # Set to 0 to send all alerts in one webhook request
45+
max_alerts: 0 # Send all alerts in one webhook request
46+
4447
EOL
4548

4649
# Output message

0 commit comments

Comments
 (0)