#!/bin/bash # ============================================ # 丹阳公益婚恋服务平台 — 健康检查与监控脚本 # ============================================ # 用法: # ./scripts/health-check.sh # 检查所有服务 # ./scripts/health-check.sh --detail # 详细输出 # ============================================ set -euo pipefail COMPOSE_FILE="${COMPOSE_FILE:-../infrastructure/docker-compose.prod.yml}" DETAIL="${2:-}" EXIT_CODE=0 check_service() { local name="$1" local check="$2" local result result=$(eval "$check" 2>/dev/null || echo "FAIL") if [ "${result}" = "OK" ]; then echo " ✅ ${name}: ${result}" else echo " ❌ ${name}: ${result}" EXIT_CODE=1 fi } echo "============================================" echo " 丹阳公益婚恋服务平台 — 健康检查" echo " $(date '+%Y-%m-%d %H:%M:%S')" echo "============================================" # 容器状态 echo "" echo "【容器状态】" docker compose -f "${COMPOSE_FILE}" ps --format "table {{.Name}}\t{{.Status}}\t{{.Ports}}" 2>/dev/null | head -10 # 服务检查 echo "" echo "【服务检查】" check_service "PostgreSQL" \ "pg_isready -h postgres -U ${DB_USER:-danyang} -d ${DB_NAME:-danyang_match} 2>/dev/null && echo OK || echo FAIL" check_service "Redis" \ "redis-cli -h redis${REDIS_PASSWORD:+ -a ${REDIS_PASSWORD}} ping 2>/dev/null | grep -q PONG && echo OK || echo FAIL" check_service "SeaweedFS" \ "curl -s -o /dev/null -w '%{http_code}' http://seaweedfs:8333/ 2>/dev/null | grep -qE '^(200|403)$' && echo OK || echo FAIL" check_service "Backend API" \ "curl -sf http://localhost:8080/api/v1/health/live 2>/dev/null | grep -q success && echo OK || echo FAIL" check_service "Backend Ready" \ "curl -sf http://localhost:8080/api/v1/health/ready 2>/dev/null | grep -q success && echo OK || echo FAIL" check_service "Nginx" \ "curl -sf http://localhost/nginx-health 2>/dev/null && echo OK || echo FAIL" # 详细输出 if [ "${DETAIL}" = "--detail" ]; then echo "" echo "【磁盘使用】" docker system df 2>/dev/null || true echo "" echo "【内存使用】" docker stats --no-stream --format "table {{.Name}}\t{{.CPUPerc}}\t{{.MemUsage}}" 2>/dev/null || true fi echo "" if [ ${EXIT_CODE} -eq 0 ]; then echo "✅ 所有服务正常" else echo "❌ 存在异常服务,请检查上方输出" fi exit ${EXIT_CODE}