#!/usr/bin/env bash # Periodic health checker for news-platform services. # Designed to run via systemd timer every 3 minutes. # # Environment (from /etc/news-platform/healthcheck.env): # NTFY_URL — Ntfy server topic URL (e.g. https://ntfy.example.com/news-alerts) # NTFY_TOKEN — (optional) Ntfy auth token # BRIDGE_URL — Bridge health endpoint (default: http://127.0.0.1:3200/health) # BRIDGE_DEEP_URL — Optional bridge deep check (e.g. …/health?checks=true — WordPress + Redis) # FRONTEND_URL — Frontend health endpoint (default: http://127.0.0.1:4321/health.json) # WP_URL — WordPress REST probe (default: http://127.0.0.1:8080/wp-json/wp/v2/) # PUBLIC_PAGE_URL — Optional customer-facing URL (HTTPS, follows redirects — e.g. /noticias path) # RESTART_THRESHOLD — Alert if NRestarts exceeds this (default: 10) # COOLDOWN_FILE — Path to cooldown state file (default: /tmp/news-healthcheck-cooldown) # COOLDOWN_SECS — Seconds between repeat alerts for the same failure (default: 900 = 15 min) set -euo pipefail # --- Configuration --- BRIDGE_URL="${BRIDGE_URL:-http://127.0.0.1:3200/health}" BRIDGE_DEEP_URL="${BRIDGE_DEEP_URL:-}" FRONTEND_URL="${FRONTEND_URL:-http://127.0.0.1:4321/health.json}" WP_URL="${WP_URL:-http://127.0.0.1:8080/wp-json/wp/v2/}" PUBLIC_PAGE_URL="${PUBLIC_PAGE_URL:-}" NTFY_URL="${NTFY_URL:-}" NTFY_TOKEN="${NTFY_TOKEN:-}" RESTART_THRESHOLD="${RESTART_THRESHOLD:-10}" COOLDOWN_FILE="${COOLDOWN_FILE:-/tmp/news-healthcheck-cooldown}" COOLDOWN_SECS="${COOLDOWN_SECS:-900}" HOSTNAME="${HOSTNAME:-$(hostname)}" FAILURES=() log() { echo "[healthcheck] $(date -u +%Y-%m-%dT%H:%M:%SZ) $*"; } # --- Cooldown logic (prevents alert storms) --- should_alert() { local key="$1" local now now=$(date +%s) if [[ -f "$COOLDOWN_FILE" ]]; then local last last=$(grep "^${key}=" "$COOLDOWN_FILE" 2>/dev/null | tail -1 | cut -d= -f2) || true if [[ -n "$last" ]] && (( now - last < COOLDOWN_SECS )); then return 1 # still in cooldown fi fi # Update cooldown timestamp if [[ -f "$COOLDOWN_FILE" ]]; then grep -v "^${key}=" "$COOLDOWN_FILE" > "${COOLDOWN_FILE}.tmp" 2>/dev/null || true mv "${COOLDOWN_FILE}.tmp" "$COOLDOWN_FILE" fi echo "${key}=${now}" >> "$COOLDOWN_FILE" return 0 } # --- HTTP health probe --- check_http() { local name="$1" url="$2" local status status=$(curl -fsS -o /dev/null -w '%{http_code}' --max-time 10 "$url" 2>/dev/null) || status="000" if [[ "$status" == "200" ]]; then log "OK: $name ($url) → HTTP $status" else log "FAIL: $name ($url) → HTTP $status" FAILURES+=("$name: HTTP $status ($url)") fi } # --- Public / customer-facing page (proxy, TLS, SSR path); follow redirects --- check_http_public() { local name="$1" url="$2" local status status=$(curl -fsSL -o /dev/null -w '%{http_code}' --max-time 20 "$url" 2>/dev/null) || status="000" if [[ "$status" == "200" ]]; then log "OK: $name ($url) → HTTP $status" else log "FAIL: $name ($url) → HTTP $status" FAILURES+=("$name: HTTP $status ($url)") fi } # --- systemd service status + restart count --- check_systemd() { local unit="$1" local active active=$(systemctl is-active "$unit" 2>/dev/null) || active="unknown" if [[ "$active" != "active" ]]; then log "FAIL: $unit is $active" FAILURES+=("systemd $unit: status=$active") else log "OK: $unit is active" fi # Check restart count (only for Type=simple services, not oneshot) local nrestarts nrestarts=$(systemctl show "$unit" -p NRestarts --value 2>/dev/null) || nrestarts="0" if [[ "$nrestarts" =~ ^[0-9]+$ ]] && (( nrestarts > RESTART_THRESHOLD )); then log "WARN: $unit has $nrestarts restarts (threshold: $RESTART_THRESHOLD)" FAILURES+=("systemd $unit: $nrestarts restarts (threshold: $RESTART_THRESHOLD)") fi } # --- Send alert via Ntfy --- send_alert() { local title="$1" body="$2" priority="${3:-high}" if [[ -z "$NTFY_URL" ]]; then log "ALERT (no NTFY_URL configured, logging only): $title — $body" return 0 fi local auth_header="" if [[ -n "$NTFY_TOKEN" ]]; then auth_header="-H \"Authorization: Bearer $NTFY_TOKEN\"" fi log "Sending alert to $NTFY_URL: $title" eval curl -fsS \ -H \"Title: "$title"\" \ -H \"Priority: "$priority"\" \ -H \"Tags: warning\" \ $auth_header \ -d \""$body"\" \ \""$NTFY_URL"\" >/dev/null 2>&1 || log "WARN: Failed to send Ntfy alert" } # --- Run checks --- log "Starting health checks on $HOSTNAME" check_http "bridge" "$BRIDGE_URL" if [[ -n "$BRIDGE_DEEP_URL" ]]; then check_http "bridge_deep" "$BRIDGE_DEEP_URL" fi check_http "frontend" "$FRONTEND_URL" check_http "wordpress" "$WP_URL" if [[ -n "$PUBLIC_PAGE_URL" ]]; then check_http_public "public_page" "$PUBLIC_PAGE_URL" fi check_systemd "news-bridge" check_systemd "news-frontend" check_systemd "news-wordpress" # --- Process failures --- if (( ${#FAILURES[@]} == 0 )); then log "All checks passed" # Clear cooldown file when everything is healthy rm -f "$COOLDOWN_FILE" exit 0 fi log "${#FAILURES[@]} failure(s) detected" # Build alert body ALERT_BODY="Host: $HOSTNAME Time: $(date -u +%Y-%m-%dT%H:%M:%SZ) Failures:" for f in "${FAILURES[@]}"; do ALERT_BODY="${ALERT_BODY} - $f" done # Alert once per unique failure set (use sorted failure names as key) ALERT_KEY=$(printf '%s\n' "${FAILURES[@]}" | sort | md5sum | cut -d' ' -f1) if should_alert "$ALERT_KEY"; then send_alert "News Platform: ${#FAILURES[@]} service failure(s)" "$ALERT_BODY" "high" else log "Alert suppressed (cooldown active for this failure set)" fi exit 1