-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdown-webhook-handler.sh
More file actions
executable file
·180 lines (143 loc) · 5.59 KB
/
Copy pathupdown-webhook-handler.sh
File metadata and controls
executable file
·180 lines (143 loc) · 5.59 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#!/bin/bash
#
# updown.io Webhook Handler
#
# This script can be called by updown.io webhooks to trigger log analysis
# when downtime is detected
#
# Setup:
# 1. Make this script executable and place on your server
# 2. Setup webhook in updown.io to POST to your endpoint
# 3. Use a simple webhook receiver (PHP, Node.js, etc.) to call this script
#
# Usage:
# ./updown-webhook-handler.sh <site_url> <event_type>
#
# Example:
# ./updown-webhook-handler.sh example.com down
#
set -e
# ============================================================================
# Configuration
# ============================================================================
SITE="${1:-example.com}"
EVENT="${2:-down}"
# Log file paths - set these according to your Trellis configuration:
# For per-site logs: LOG_FILE="/srv/www/${SITE}/logs/access.log"
# For global logs: LOG_FILE="/var/log/nginx/access.log"
LOG_FILE="${LOG_FILE:-/srv/www/${SITE}/logs/access.log}"
ERROR_LOG="${ERROR_LOG:-/srv/www/${SITE}/logs/error.log}"
MONITORING_DIR="/home/web/monitoring"
REPORTS_DIR="/home/web/monitoring/updown-alerts"
ALERT_EMAIL="${ALERT_EMAIL:-}"
# ============================================================================
# Functions
# ============================================================================
log_message() {
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1"
}
create_reports_dir() {
mkdir -p "$REPORTS_DIR"
}
# ============================================================================
# Event Handlers
# ============================================================================
handle_downtime() {
log_message "Downtime detected for $SITE - analyzing logs..."
local timestamp
timestamp=$(date '+%Y-%m-%d-%H%M%S')
local report_file="$REPORTS_DIR/downtime-$timestamp.txt"
{
echo "================================================================================"
echo "DOWNTIME ALERT: $SITE"
echo "Time: $(date)"
echo "================================================================================"
echo ""
echo "--- Recent Server Errors (5xx) ---"
grep 'HTTP/1.[01]" 5[0-9][0-9]' "$LOG_FILE" | tail -20 || echo "No 5xx errors found"
echo ""
echo "--- Recent Nginx Error Log ---"
tail -30 "$ERROR_LOG" || echo "Cannot read error log"
echo ""
echo "--- Recent High Request IPs (last hour) ---"
"$MONITORING_DIR/traffic-monitor.sh" "$LOG_FILE" 1 | grep -A 10 "Top 10 IP Addresses" || echo "Cannot run traffic analysis"
echo ""
echo "--- Security Issues (last hour) ---"
"$MONITORING_DIR/security-monitor.sh" "$LOG_FILE" 1 100 | grep -E "\[ALERT\]|\[WARNING\]" || echo "No security alerts"
echo ""
echo "--- System Resources ---"
echo "Disk usage:"
df -h / || echo "Cannot check disk"
echo ""
echo "Memory:"
free -h || echo "Cannot check memory"
echo ""
echo "--- Active Connections ---"
ss -tn | grep -E ':80|:443' | wc -l || echo "Cannot check connections"
echo ""
echo "================================================================================"
echo "Analysis complete. Check above for root cause."
echo "================================================================================"
} > "$report_file"
# Display to stdout
cat "$report_file"
# Send email if configured
if [[ -n "$ALERT_EMAIL" ]] && command -v mail &> /dev/null; then
mail -s "[DOWNTIME] $SITE - $(date +%Y-%m-%d\ %H:%M)" "$ALERT_EMAIL" < "$report_file"
log_message "Alert email sent to $ALERT_EMAIL"
fi
log_message "Downtime analysis saved to $report_file"
}
handle_uptime_restored() {
log_message "Uptime restored for $SITE"
local timestamp
timestamp=$(date '+%Y-%m-%d-%H%M%S')
local report_file="$REPORTS_DIR/recovery-$timestamp.txt"
{
echo "================================================================================"
echo "UPTIME RESTORED: $SITE"
echo "Time: $(date)"
echo "================================================================================"
echo ""
echo "Site is back online. Recent activity:"
echo ""
echo "--- Recent Successful Requests ---"
grep 'HTTP/1.[01]" 200' "$LOG_FILE" | tail -10 || echo "No 200 responses yet"
echo ""
echo "================================================================================"
} > "$report_file"
cat "$report_file"
log_message "Recovery logged to $report_file"
}
handle_ssl_expiry() {
log_message "SSL certificate expiry warning for $SITE"
if [[ -n "$ALERT_EMAIL" ]] && command -v mail &> /dev/null; then
echo "SSL certificate for $SITE is expiring soon. Check updown.io for details." \
| mail -s "[SSL WARNING] $SITE Certificate Expiring" "$ALERT_EMAIL"
fi
}
# ============================================================================
# Main Logic
# ============================================================================
main() {
create_reports_dir
case "$EVENT" in
down|downtime)
handle_downtime
;;
up|uptime)
handle_uptime_restored
;;
ssl)
handle_ssl_expiry
;;
*)
log_message "Unknown event type: $EVENT"
exit 1
;;
esac
}
# ============================================================================
# Script Entry Point
# ============================================================================
main "$@"