#!/bin/bash # health-check.sh — 全组件探活检查 source "$(dirname "${BASH_SOURCE[0]}")/config/env.sh" log_info "=== Health Check ===" FAILED=0 PASSED=0 check_http() { local name="$1"; local url="$2" if curl -sf --max-time 5 "$url" &>/dev/null; then log_ok " $name ($url)" ((PASSED++)) else log_error " $name ($url) FAILED" ((FAILED++)) fi } check_tcp() { local name="$1"; local host="$2"; local port="$3" if nc -z -w3 "$host" "$port" &>/dev/null; then log_ok " $name ($host:$port)" ((PASSED++)) else log_error " $name ($host:$port) FAILED" ((FAILED++)) fi } log_info "--- Infrastructure ---" check_http "Consul" "http://$CONSUL_HOST:$CONSUL_PORT/v1/status/leader" check_tcp "Redis" "$REDIS_HOST" "$REDIS_PORT" check_tcp "Kafka" "$KAFKA_HOST" "$KAFKA_PORT" check_http "Nginx" "http://$CONSUL_HOST:$PORT_NGINX" log_info "--- Application Services ---" check_http "elevator-v2" "http://127.0.0.1:$PORT_ELEVATOR_V2/health" check_http "elevator-v1" "http://127.0.0.1:$PORT_ELEVATOR_V1/health" check_http "crk-std" "http://127.0.0.1:$PORT_CRK_MGMT/health" check_http "alarm-app" "http://127.0.0.1:$PORT_ALARM_MGMT/health" log_info "--- Databases ---" check_tcp "MySQL" "$MYSQL_HOST" "$MYSQL_PORT" log_info "--- Consul Registration ---" CONSUL_SVCS=$(curl -sf "http://$CONSUL_HOST:$CONSUL_PORT/v1/agent/services" 2>/dev/null | python3 -c "import sys,json; print(len(json.load(sys.stdin)))" 2>/dev/null || echo "0") log_info " Registered services: $CONSUL_SVCS" echo "" log_info "=== Result: $PASSED passed, $FAILED failed ===" [[ $FAILED -eq 0 ]] && exit 0 || exit 1