summaryrefslogtreecommitdiff
path: root/watchman.sh
diff options
context:
space:
mode:
Diffstat (limited to 'watchman.sh')
-rwxr-xr-xwatchman.sh40
1 files changed, 40 insertions, 0 deletions
diff --git a/watchman.sh b/watchman.sh
new file mode 100755
index 0000000..085fd0d
--- /dev/null
+++ b/watchman.sh
@@ -0,0 +1,40 @@
+#!/bin/bash
+
+# === Your actual setup ===
+HOST="38.102.127.168" # Montreal's IP to probe
+THRESHOLD_LATENCY=150 # in ms
+THRESHOLD_LOSS=5 # in %
+CF_ZONE_ID="c5099d42caa2d9763227267c597cb758" # Replace with your real Cloudflare zone ID
+CF_RECORD_ID="7001484a25f0fe5c323845b6695f7544" # Replace with your real DNS record ID
+CF_API_TOKEN="lCz1kH6nBZPJL0EWrNI-xEDwfR0oOLpg05fq6M81" # Cloudflare API token
+LINODE_IP="172.238.63.162" # Linode IP
+LOG_FILE="/var/log/tt_failover.log"
+DATE=$(date '+%Y-%m-%d %H:%M:%S')
+
+# === Run mtr probe ===
+mtr -r -c 10 $HOST > /tmp/mtr_output.txt
+
+# === Extract average latency and packet loss ===
+# Average latency from ping (use 10 pings)
+AVG_LATENCY=$(ping -c 10 $HOST | tail -1| awk '{print $4}' | cut -d '/' -f 2)
+
+# Packet loss from ping (last line)
+LOSS=$(ping -c 10 $HOST | grep -oP '\d+(?=% packet loss)')
+
+echo "[$DATE] DEBUG: AVG_LATENCY=$AVG_LATENCY, LOSS=$LOSS" >> $LOG_FILE
+
+
+# === Logging ===
+echo "[$DATE] Latency: $AVG_LATENCY ms, Loss: $LOSS%" >> $LOG_FILE
+
+# === Decision ===
+if (( $(echo "$AVG_LATENCY > $THRESHOLD_LATENCY" | bc -l) )) || (( $(echo "$LOSS > $THRESHOLD_LOSS" | bc -l) )); then
+ echo "[$DATE] 🔴 Montreal is glitchy! Flipping to Linode ($LINODE_IP)..." >> $LOG_FILE
+ curl -X PUT "https://api.cloudflare.com/client/v4/zones/$CF_ZONE_ID/dns_records/$CF_RECORD_ID" \
+ -H "Authorization: Bearer $CF_API_TOKEN" \
+ -H "Content-Type: application/json" \
+ --data '{"type":"A","name":"tt.themediahub.org","content":"'"$LINODE_IP"'","ttl":60,"proxied":false}' >> $LOG_FILE 2>&1
+ echo "[$DATE] ✅ Failover to Linode triggered." >> $LOG_FILE
+else
+ echo "[$DATE] ✅ Montreal healthy. No action taken." >> $LOG_FILE
+fi