-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmonitor-bots.sh
More file actions
executable file
·95 lines (82 loc) · 3.59 KB
/
monitor-bots.sh
File metadata and controls
executable file
·95 lines (82 loc) · 3.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
#!/bin/bash
# CSFloat Bot Monitor - Discord Notifications
# Checks if bots are online and sends Discord webhook if any are offline
# Configuration
API_URL="http://localhost:3002/api/stats"
DISCORD_WEBHOOK_URL="https://discord.com/api/webhooks/1430175040204181520/2DVFasIxOCS_khJOGuELpejAnxpk3Ho5evI7qOM5aHXXQzrfkcFdTnjDSKnYWqMdUyVo"
STATE_FILE="/tmp/csfloat-bot-monitor-state"
# Fetch bot stats
STATS=$(curl -s "$API_URL")
# Extract values using grep/sed (works without jq)
BOTS_ONLINE=$(echo "$STATS" | grep -o '"bots_online":[0-9]*' | grep -o '[0-9]*')
BOTS_TOTAL=$(echo "$STATS" | grep -o '"bots_total":[0-9]*' | grep -o '[0-9]*')
QUEUE_SIZE=$(echo "$STATS" | grep -o '"queue_size":[0-9]*' | grep -o '[0-9]*')
# Check if we got valid data
if [ -z "$BOTS_ONLINE" ] || [ -z "$BOTS_TOTAL" ]; then
echo "Error: Failed to fetch bot stats"
exit 1
fi
# Current timestamp
TIMESTAMP=$(date '+%Y-%m-%d %H:%M:%S UTC')
# Check if bots are offline
if [ "$BOTS_ONLINE" -lt "$BOTS_TOTAL" ]; then
# Check if we've already sent an alert (to avoid spam)
if [ ! -f "$STATE_FILE" ]; then
# Create state file to track that we've alerted
echo "offline" > "$STATE_FILE"
# Determine alert color and message
if [ "$BOTS_ONLINE" -eq 0 ]; then
COLOR=15158332 # Red
SEVERITY="🚨 CRITICAL"
else
COLOR=16776960 # Yellow
SEVERITY="⚠️ WARNING"
fi
BOTS_OFFLINE=$((BOTS_TOTAL - BOTS_ONLINE))
# Send Discord webhook
curl -H "Content-Type: application/json" \
-X POST \
-d "{
\"embeds\": [{
\"title\": \"$SEVERITY: CSFloat Bots Offline\",
\"description\": \"**$BOTS_OFFLINE out of $BOTS_TOTAL bots are offline!**\",
\"color\": $COLOR,
\"fields\": [
{\"name\": \"Bots Online\", \"value\": \"$BOTS_ONLINE/$BOTS_TOTAL\", \"inline\": true},
{\"name\": \"Queue Size\", \"value\": \"$QUEUE_SIZE\", \"inline\": true},
{\"name\": \"Time\", \"value\": \"$TIMESTAMP\", \"inline\": false}
],
\"footer\": {\"text\": \"CSFloat API Monitor\"}
}]
}" \
"$DISCORD_WEBHOOK_URL"
echo "[$TIMESTAMP] Alert sent: $BOTS_ONLINE/$BOTS_TOTAL bots online"
else
echo "[$TIMESTAMP] Bots still offline ($BOTS_ONLINE/$BOTS_TOTAL), alert already sent"
fi
else
# All bots are online
if [ -f "$STATE_FILE" ]; then
# Bots came back online, send recovery notification
rm "$STATE_FILE"
curl -H "Content-Type: application/json" \
-X POST \
-d "{
\"embeds\": [{
\"title\": \"✅ RECOVERED: All CSFloat Bots Online\",
\"description\": \"All bots have successfully reconnected!\",
\"color\": 3066993,
\"fields\": [
{\"name\": \"Bots Online\", \"value\": \"$BOTS_ONLINE/$BOTS_TOTAL\", \"inline\": true},
{\"name\": \"Queue Size\", \"value\": \"$QUEUE_SIZE\", \"inline\": true},
{\"name\": \"Time\", \"value\": \"$TIMESTAMP\", \"inline\": false}
],
\"footer\": {\"text\": \"CSFloat API Monitor\"}
}]
}" \
"$DISCORD_WEBHOOK_URL"
echo "[$TIMESTAMP] Recovery alert sent: All bots online"
else
echo "[$TIMESTAMP] All bots online ($BOTS_ONLINE/$BOTS_TOTAL)"
fi
fi