#!/bin/bash
set -e

echo "[04] 验证安装结果 - 检查监控端口..."
echo "=========================================="

# 检查容器是否运行
if ! docker ps --format '{{.Names}}' | grep -q '^argus-metric-test-node$'; then
    echo "错误: 容器 argus-metric-test-node 未运行"
    exit 1
fi

ERRORS=0

# ==================== 检查监听端口 ====================
echo ""
echo "[1] 检查监听端口..."
echo "----------------------------------------"
CHECK_RESULT=$(docker exec argus-metric-test-node bash -c '
if command -v netstat >/dev/null 2>&1; then
    echo "使用 netstat 检查端口:"
    if netstat -tlnp 2>/dev/null | grep -E ":(9100|9400|2020)"; then
        echo "✓ 找到监控端口"
        exit 0
    else
        echo "✗ 未找到监控端口 (9100/9400/2020)"
        exit 1
    fi
elif command -v ss >/dev/null 2>&1; then
    echo "使用 ss 检查端口:"
    if ss -tlnp 2>/dev/null | grep -E ":(9100|9400|2020)"; then
        echo "✓ 找到监控端口"
        exit 0
    else
        echo "✗ 未找到监控端口 (9100/9400/2020)"
        exit 1
    fi
elif command -v lsof >/dev/null 2>&1; then
    echo "使用 lsof 检查端口:"
    if lsof -i :9100 -i :9400 -i :2020 2>/dev/null | grep LISTEN; then
        echo "✓ 找到监控端口"
        exit 0
    else
        echo "✗ 未找到监控端口 (9100/9400/2020)"
        exit 1
    fi
else
    echo "? 没有可用的端口检查工具 (netstat/ss/lsof)，跳过此检查"
    exit 0
fi
')
echo "$CHECK_RESULT"
# 只有在明确失败时才计入错误（exit 1），没有工具（exit 0）不算错误
if echo "$CHECK_RESULT" | grep -q "✗ 未找到监控端口"; then
    ERRORS=$((ERRORS + 1))
fi

# ==================== 测试端口连通性 ====================
echo ""
echo "[2] 测试端口连通性..."
echo "----------------------------------------"
docker exec argus-metric-test-node bash -c '
if command -v curl >/dev/null 2>&1; then
    FAILED=0
    for port in 9100 9400 2020; do
        echo -n "端口 $port: "
        if curl -s --connect-timeout 2 "http://localhost:$port/metrics" > /dev/null 2>&1; then
            echo "✓ 可访问 (/metrics)"
        elif curl -s --connect-timeout 2 "http://localhost:$port/" > /dev/null 2>&1; then
            echo "✓ 可访问 (根路径)"
        else
            echo "✗ 不可访问"
            FAILED=$((FAILED + 1))
        fi
    done
    exit $FAILED
else
    echo "? curl 不可用，跳过连通性测试"
    exit 0
fi
' || ERRORS=$((ERRORS + 1))

echo ""
echo "=========================================="
if [ $ERRORS -eq 0 ]; then
    echo "✓ [04] 验证完成 - 所有端口检查通过"
else
    echo "✗ [04] 验证失败 - 发现 $ERRORS 个问题"
    echo ""
    echo "调试建议："
    echo "  1. 进入容器检查: docker exec -it argus-metric-test-node bash"
    echo "  2. 查看进程: docker exec argus-metric-test-node ps aux"
    echo "  3. 查看日志: docker exec argus-metric-test-node cat /tmp/argus_install.log"
    exit 1
fi
echo "=========================================="
