From 8f1e3a2b3969866c68fc3f571d73eb0a3e3caa18 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8F=8D=E7=BC=96=E8=AF=91=E5=B7=A5=E4=BD=9C=E5=8C=BA?= Date: Fri, 1 May 2026 19:46:45 +0800 Subject: [PATCH] feat: add health-check script for all components Former-commit-id: 2e9a8f37462c5f71ac25a7e7fe707f935584167b --- scripts/test-env/health-check.sh | 53 ++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100755 scripts/test-env/health-check.sh diff --git a/scripts/test-env/health-check.sh b/scripts/test-env/health-check.sh new file mode 100755 index 00000000..7cf299b4 --- /dev/null +++ b/scripts/test-env/health-check.sh @@ -0,0 +1,53 @@ +#!/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/actuator/health" +check_http "elevator-v1" "http://127.0.0.1:$PORT_ELEVATOR_V1/actuator/health" +check_http "crk-std" "http://127.0.0.1:$PORT_CRK_MGMT/actuator/health" +check_http "alarm-app" "http://127.0.0.1:$PORT_ALARM_MGMT/actuator/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