Files

125 lines
2.5 KiB
Bash
Raw Permalink Normal View History

#!/usr/bin/env bash
# Universal Alert Sender
# Sends alerts via configured channels
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)"
CONFIG_FILE="${PROJECT_ROOT}/smom-dbis-138/.env.alerts"
# Load configuration
if [ -f "$CONFIG_FILE" ]; then
source "$CONFIG_FILE"
fi
SEVERITY="${1:-WARNING}"
TITLE="${2:-Alert}"
MESSAGE="${3:-}"
send_email_alert() {
if [ "${ALERT_EMAIL_ENABLED:-false}" != "true" ]; then
return 0
fi
local to="${ALERT_EMAIL_TO:-}"
if [ -z "$to" ]; then
return 0
fi
echo "$MESSAGE" | mail -s "[$SEVERITY] $TITLE" "$to" 2>/dev/null || true
}
send_webhook_alert() {
local url="${1:-}"
if [ -z "$url" ] || [ "${ALERT_WEBHOOK_ENABLED:-false}" != "true" ]; then
return 0
fi
local payload=$(cat <<EOF
{
"severity": "$SEVERITY",
"title": "$TITLE",
"message": "$MESSAGE",
"timestamp": "$(date -Iseconds)"
}
EOF
)
curl -X POST "$url" \
-H "Content-Type: application/json" \
-d "$payload" \
2>/dev/null || true
}
send_slack_alert() {
if [ "${ALERT_SLACK_ENABLED:-false}" != "true" ]; then
return 0
fi
local webhook="${ALERT_SLACK_WEBHOOK_URL:-}"
if [ -z "$webhook" ]; then
return 0
fi
local color="warning"
case "$SEVERITY" in
CRITICAL|ERROR) color="danger" ;;
WARNING) color="warning" ;;
INFO) color="good" ;;
esac
local payload=$(cat <<EOF
{
"attachments": [{
"color": "$color",
"title": "$TITLE",
"text": "$MESSAGE",
"footer": "Blockchain Monitoring",
"ts": $(date +%s)
}]
}
EOF
)
curl -X POST "$webhook" \
-H "Content-Type: application/json" \
-d "$payload" \
2>/dev/null || true
}
send_discord_alert() {
if [ "${ALERT_DISCORD_ENABLED:-false}" != "true" ]; then
return 0
fi
local webhook="${ALERT_DISCORD_WEBHOOK_URL:-}"
if [ -z "$webhook" ]; then
return 0
fi
local payload=$(cat <<EOF
{
"embeds": [{
"title": "$TITLE",
"description": "$MESSAGE",
"color": $([ "$SEVERITY" = "CRITICAL" ] && echo "16711680" || echo "16776960"),
"timestamp": "$(date -Iseconds)"
}]
}
EOF
)
curl -X POST "$webhook" \
-H "Content-Type: application/json" \
-d "$payload" \
2>/dev/null || true
}
# Send alerts via all enabled channels
send_email_alert
send_webhook_alert "${ALERT_WEBHOOK_URL:-}"
send_slack_alert
send_discord_alert