summaryrefslogtreecommitdiff
path: root/status.sh
blob: 5f58329f80681bf9485af2431df1e2a72a083d4b (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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/bin/bash
# mirror-status.sh
# Generate a status page for FailZero mirrors

MIRROR_BASE="/mnt/brimstone/mirror"
STATUS_DIR="/mnt/brimstone/mirror/status"
STATUS_FILE="$STATUS_DIR/index.html"
LOG_DIR="/var/log/mirrors"

# List your mirrors (folder name => display name)
declare -A MIRRORS=(
  ["archlinux"]="Arch Linux"
  ["debian"]="Debian"
  ["gentoo"]="Gentoo"
  ["hardenedbsd"]="HardenedBSD"
  ["slackware"]="Slackware"
  ["void"]="Void Linux"
  ["rocky"]="Rocky Linux"
  ["alma"]="AlmaLinux"
)

mkdir -p "$STATUS_DIR"

cat > "$STATUS_FILE" <<EOF
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>FailZero Mirror Status</title>
  <style>
    body { font-family: monospace; background: #111; color: #eee; }
    h1 { color: #f33; }
    table { border-collapse: collapse; width: 100%; }
    th, td { padding: 8px 12px; border: 1px solid #444; }
    th { background: #222; }
    tr.ok { background: #1a2; }
    tr.warn { background: #662; }
    tr.fail { background: #a22; }
  </style>
</head>
<body>
  <h1>FailZero Mirror Status</h1>
  <p>Updated: $(date)</p>
  <table>
    <tr><th>Distro</th><th>Last Sync (log)</th></tr>
EOF

for key in "${!MIRRORS[@]}"; do
  DISTRO="${MIRRORS[$key]}"
  DIR="$MIRROR_BASE/$key"
  LOG="$LOG_DIR/${key}-mirror-sync.log"

  STATUS="FAIL"
  CLASS="fail"
  LASTSYNC="no log"
  FRESHNESS="unknown"

  # 1. Parse rsync log exitcode
  if [ -f "$LOG" ]; then
    LASTSYNC=$(stat -c %y "$LOG" | cut -d. -f1)
    EXITCODE=$(grep "EXITCODE:" "$LOG" | tail -n1 | cut -d: -f2)

    if [ -n "$EXITCODE" ]; then
      if [ "$EXITCODE" = "0" ]; then
        STATUS="OK"
        CLASS="ok"
      else
        STATUS="RSYNC FAIL ($EXITCODE)"
        CLASS="fail"
      fi
    else
      STATUS="NO EXITCODE"
      CLASS="warn"
    fi
  fi

  # 2. Check lastupdate if present (for freshness)
#  if [ -f "$DIR/lastupdate" ]; then
#    TS=$(tr -d '[:space:]' < "$DIR/lastupdate" 2>/dev/null)
#    if [[ "$TS" =~ ^[0-9]+$ ]]; then
#      NOW=$(date +%s)
#      AGE=$(( NOW - TS ))
#      if [ "$AGE" -lt 7200 ]; then
#        FRESHNESS="$((AGE/60)) min ago"
#      elif [ "$AGE" -lt 86400 ]; then
#        FRESHNESS="$((AGE/3600)) hr ago"
#        [ "$STATUS" = "OK" ] && CLASS="warn"
#      else
#        FRESHNESS="$((AGE/86400)) days ago"
#        [ "$STATUS" = "OK" ] && CLASS="warn"
#      fi
#    fi
#  fi

  echo "    <tr class=\"$CLASS\"><td>$DISTRO</td><td>$LASTSYNC</td>" >> "$STATUS_FILE"
done

cat >> "$STATUS_FILE" <<EOF
  </table>
  <p style="margin-top:20px;font-size:smaller;">FailZero Infrastructure – Mirror Ops</p>
</body>
</html>
EOF