summaryrefslogtreecommitdiff
path: root/arecordalertfailzero.sh
blob: fadeba8a348d7bccb95a811435f3d1c33f54bb3b (plain)
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
#!/bin/bash

# FailZero Early Warning System (FZ EWS)
# Monitor critical hosts and alert on Telegram on failure

# === INSERT YOUR TELEGRAM CREDENTIALS BELOW ===
BOT_TOKEN="8031184325:AAEGj3gzwYF8HaLjWHVe0gOG5bzo63tcRbU"
CHAT_ID="1559582356"

# === INSERT YOUR CRITICAL HOSTNAMES BELOW ===
CRITICAL_HOSTS=(
  "da.genesishostingtechnologies.com"
  "zcluster.technodrome1.sshjunkie.com"
  "zcluster.technodrome2.sshjunkie.com"
  "krang.core.sshjunkie.com"
  "tt.themediahub.org"
  "toot.themediahub.org"
  "chatwithus.live"
  "genesishostingtechnologies.com"
  "portal.genesishostingtechnologies.com"
  "brandoncharles.us"
  # Add more hostnames here, one per line inside quotes
)

LOG_FILE="/home/doc/fz_ews.log"
COOLDOWN_FILE="/home/doc/fz_ews_cooldown"

# Cooldown period in seconds to prevent alert spam (e.g., 3600 = 1 hour)
ALERT_COOLDOWN=3600

send_telegram_alert() {
  local message="$1"
  curl -s -X POST "https://api.telegram.org/bot$BOT_TOKEN/sendMessage" \
       -d chat_id="$CHAT_ID" \
       -d text="🚨 FailZero EWS Alert: $message" > /dev/null
}

log() {
  echo "$(date '+%Y-%m-%d %H:%M:%S') - $1" >> "$LOG_FILE"
}

check_cooldown() {
  local host="$1"
  local now=$(date +%s)
  local last_alert=$(grep "^$host " "$COOLDOWN_FILE" 2>/dev/null | awk '{print $2}')
  if [[ -z "$last_alert" ]]; then
    return 0
  fi
  local elapsed=$((now - last_alert))
  if (( elapsed > ALERT_COOLDOWN )); then
    return 0
  else
    return 1
  fi
}

update_cooldown() {
  local host="$1"
  local now=$(date +%s)
  # Remove existing entry for host if any
  grep -v "^$host " "$COOLDOWN_FILE" 2>/dev/null > "${COOLDOWN_FILE}.tmp"
  mv "${COOLDOWN_FILE}.tmp" "$COOLDOWN_FILE"
  # Append new timestamp
  echo "$host $now" >> "$COOLDOWN_FILE"
}

check_host() {
  local host="$1"
  if ping -c 2 -W 3 "$host" > /dev/null 2>&1; then
    log "$host is UP"
  else
    log "$host is DOWN"
    if check_cooldown "$host"; then
      send_telegram_alert "$host is DOWN or unreachable!"
      update_cooldown "$host"
    else
      log "Cooldown active for $host; alert suppressed"
    fi
  fi
}

main() {
  for host in "${CRITICAL_HOSTS[@]}"; do
    check_host "$host"
  done
}

main