-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathactions.py
More file actions
43 lines (40 loc) · 1.84 KB
/
Copy pathactions.py
File metadata and controls
43 lines (40 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import requests
import config
import logging
# Configure logging
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
def handle_pod_crash_looping_alert(alert):
"""
Handle alerts for crash looping pods.
This function will send a notification to Slack.
"""
try:
logger.info(f"Handling pod crash looping alert for pod: {alert['labels']['pod']} in namespace: {alert['labels']['namespace']}")
send_slack_notification(alert)
logger.info("Handled pod crash looping alert successfully")
except Exception as e:
logger.error(f"Error handling pod crash looping alert: {e}", exc_info=True)
def send_slack_notification(alert):
"""
Send a notification to Slack with alert details.
"""
try:
slack_webhook_url = config.SLACK_WEBHOOK_URL
enriched_data = alert.get('enriched_data', {})
message = {
"text": f"Critical Alert: {alert['labels']['alertname']}\n"
f"Description: {alert['annotations']['description']}\n"
f"Pod: {alert['labels']['pod']}\n"
f"Namespace: {alert['labels']['namespace']}\n"
f"Severity: {alert['labels']['severity']}\n"
f"Timestamp: {alert['startsAt']}\n"
f"Enriched Data: CPU: {enriched_data.get('cpu_utilization', 'N/A')}, Memory: {enriched_data.get('memory_utilization', 'N/A')}"
}
response = requests.post(slack_webhook_url, json=message)
if response.status_code == 200:
logger.info("Sent Slack notification successfully")
else:
logger.error(f"Failed to send Slack notification: {response.text}")
except Exception as e:
logger.error(f"Error sending Slack notification: {e}", exc_info=True)