/home/techb158/cosmic-risk.abdallabala.com/scripts
Edit: /home/techb158/cosmic-risk.abdallabala.com/scripts/monitor-health.sh (1485B)
#!/usr/bin/env bash
# Health monitor script — checks app, database, Redis; logs to syslog
# Usage: ./scripts/monitor-health.sh [url]
# Can be run from cron every 5 minutes
URL="${1:-http://localhost:8092}"
LOG_TAG="cosmic-monitor"
TIMEOUT=10
log() { logger -t "$LOG_TAG" "$1"; echo "$(date -uIseconds) $1"; }
fail() { log "ALERT: $1"; exit 1; }
# 1. Health endpoint
http_code=$(curl -s -o /dev/null -w "%{http_code}" --max-time "$TIMEOUT" "$URL/api/health" 2>/dev/null)
[ "$http_code" = "200" ] || fail "Health check failed (HTTP $http_code) at $URL/api/health"
# 2. Readiness (DB check via API)
ready_code=$(curl -s -o /dev/null -w "%{http_code}" --max-time "$TIMEOUT" "$URL/api/ready" 2>/dev/null)
[ "$ready_code" = "200" ] || fail "Readiness check failed (HTTP $ready_code) — DB may be down"
# 3. Login smoke test
login_resp=$(curl -s --max-time "$TIMEOUT" \
-X POST "$URL/api/auth/login" \
-H "Content-Type: application/json" \
-d '{"email":"super-owner@cosmic.local","password":"cosmic123"}' 2>/dev/null)
echo "$login_resp" | grep -q '"user"' || fail "Login smoke test failed"
# 4. Docker container check (if running inside Docker or on host with Docker)
if command -v docker &>/dev/null; then
for svc in app postgres redis; do
status=$(docker inspect --format='{{.State.Status}}' "saas-app-$svc-1" 2>/dev/null)
[ "$status" = "running" ] || log "WARN: container saas-app-$svc-1 is not running (status=$status)"
done
fi
log "All health checks passed"