● Zion Boggan
repos/Drophawk/scripts/healthcheck.sh
zionboggan.com ↗
121 lines · bash
History for this file →
1
 
2
set -uo pipefail
3
 
4
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
5
REPO_ROOT="$(cd "${SCRIPT_DIR}/.." && pwd)"
6
 
7
if [[ -f "${REPO_ROOT}/.env" ]]; then
8
    set -o allexport
9
    source "${REPO_ROOT}/.env"
10
    set +o allexport
11
fi
12
 
13
 
14
GREEN="\033[0;32m"
15
RED="\033[0;31m"
16
YELLOW="\033[0;33m"
17
RESET="\033[0m"
18
 
19
 
20
PASS=0
21
FAIL=0
22
 
23
check() {
24
    local name="$1"
25
    local url="$2"
26
    local expected_pattern="${3:-}"
27
    local timeout="${4:-5}"
28
 
29
    local http_code
30
    local body
31
 
32
    body=$(curl -sf --max-time "${timeout}" -w "\n%{http_code}" "${url}" 2>/dev/null) || true
33
    http_code=$(echo "${body}" | tail -1)
34
    body=$(echo "${body}" | head -n -1)
35
 
36
    local ok=false
37
    if [[ "${http_code}" =~ ^2 ]]; then
38
        if [[ -z "${expected_pattern}" ]] || echo "${body}" | grep -q "${expected_pattern}"; then
39
            ok=true
40
        fi
41
    fi
42
 
43
    if "${ok}"; then
44
        printf "  ${GREEN}[OK]${RESET}   %-30s %s\n" "${name}" "${url}"
45
        PASS=$((PASS + 1))
46
    else
47
        printf "  ${RED}[FAIL]${RESET} %-30s %s (HTTP ${http_code:-timeout})\n" "${name}" "${url}"
48
        FAIL=$((FAIL + 1))
49
    fi
50
}
51
 
52
check_docker() {
53
    local name="$1"
54
    local service="$2"
55
 
56
    local status
57
    status=$(docker compose --env-file "${REPO_ROOT}/.env" \
58
        -f "${REPO_ROOT}/docker-compose.yml" \
59
        ps --format json "${service}" 2>/dev/null \
60
        | python3 -c "import sys,json; d=json.load(sys.stdin); print(d.get('Health','unknown'))" 2>/dev/null \
61
        || echo "unknown")
62
 
63
    if [[ "${status}" == "healthy" ]]; then
64
        printf "  ${GREEN}[OK]${RESET}   %-30s docker:%s (%s)\n" "${name}" "${service}" "${status}"
65
        PASS=$((PASS + 1))
66
    else
67
        printf "  ${RED}[FAIL]${RESET} %-30s docker:%s (%s)\n" "${name}" "${service}" "${status}"
68
        FAIL=$((FAIL + 1))
69
    fi
70
}
71
 
72
 
73
if curl -sf --max-time 2 "http://localhost:80/" >/dev/null 2>&1; then
74
    BASE_HOST="localhost"
75
else
76
    BASE_HOST="localhost"
77
fi
78
 
79
echo ""
80
echo "DropHawk Service Health Check - $(date -u '+%Y-%m-%dT%H:%M:%SZ')"
81
echo "─────────────────────────────────────────────────────"
82
 
83
 
84
check "Caddy (edge)"          "http://${BASE_HOST}:80/"       ""       5
85
 
86
check "Backend /api/health"   "http://${BASE_HOST}:80/api/health"   "ok"  5
87
 
88
check "Backend /api/ready"    "http://${BASE_HOST}:80/api/ready"    "ok"  5
89
 
90
check "ntfy (via Caddy)"      "http://${BASE_HOST}:80/ntfy/v1/health"  "ok"  5
91
 
92
check "Prometheus /-/healthy" "http://${BASE_HOST}:9090/-/healthy"   "Healthy"  5
93
 
94
check "Grafana /api/health"   "http://${BASE_HOST}:3000/api/health"  "ok"  5
95
 
96
check "Loki /ready"           "http://${BASE_HOST}:3100/ready"        "ready"  5
97
 
98
 
99
echo ""
100
echo "Docker container health states:"
101
check_docker "postgres"    "postgres"
102
check_docker "redis"       "redis"
103
check_docker "nats"        "nats"
104
check_docker "ntfy"        "ntfy"
105
check_docker "backend"     "backend"
106
check_docker "frontend"    "frontend"
107
check_docker "caddy"       "caddy"
108
check_docker "prometheus"  "prometheus"
109
check_docker "grafana"     "grafana"
110
check_docker "loki"        "loki"
111
 
112
 
113
echo ""
114
echo "─────────────────────────────────────────────────────"
115
if [[ "${FAIL}" -eq 0 ]]; then
116
    printf "${GREEN}All ${PASS} checks passed.${RESET}\n\n"
117
    exit 0
118
else
119
    printf "${RED}${FAIL} check(s) failed${RESET}, ${PASS} passed.\n\n"
120
    exit 1
121
fi