#!/bin/bash
################################################################################
# Nacos基础检测模块
# 功能: 检测Nacos容器状态、服务实例数等
# 作者: Claude Code
# 日期: 2026-07-20
################################################################################

# 获取脚本所在目录并加载依赖
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
LIB_DIR="${LIB_DIR:-/tmp/check_modules}"

# 加载配置文件和通用函数库
if [ -f "$LIB_DIR/lib/config.sh" ]; then
    source "$LIB_DIR/lib/config.sh"
else
    echo "ERROR: 配置文件不存在: $LIB_DIR/lib/config.sh"
    exit 1
fi

if [ -f "$LIB_DIR/lib/common.sh" ]; then
    source "$LIB_DIR/lib/common.sh"
else
    echo "ERROR: 通用函数库不存在: $LIB_DIR/lib/common.sh"
    exit 1
fi

# Nacos容器名称
NACOS_CONTAINER="${CONTAINERS[nacos]}"

# ==================== 辅助函数 ====================

nacos_exec() {
    docker exec "$NACOS_CONTAINER" $@ 2>/dev/null
}

nacos_api() {
    local endpoint=$1
    nacos_exec curl -s "http://localhost:8848/nacos/$endpoint" 2>/dev/null
}

# ==================== 检测函数 ====================

# 检测Nacos容器状态
check_nacos_container() {
    local matched_container=""
    if command -v docker &> /dev/null; then
        matched_container=$(docker ps --format "{{.Names}}" | grep -x "${NACOS_CONTAINER}" | head -1)

        if [ -z "$matched_container" ] && [ -n "$NACOS_CONTAINER" ]; then
            matched_container=$(docker ps --format "{{.Names}}" | grep -i "${NACOS_CONTAINER}" | head -1)
        fi
    fi

    if [ -n "$matched_container" ]; then
        eval "NACOS_CONTAINER='${matched_container}'"
        output_result "NACOS_CONTAINER_STATUS" "运行中 (${NACOS_CONTAINER})"
        output_result "NACOS_CONTAINER_LEVEL" "正常"
        return 0
    else
        matched_container=$(docker ps -a --format "{{.Names}}" | grep -i "${NACOS_CONTAINER}" | head -1)
        if [ -n "$matched_container" ]; then
            output_result "NACOS_CONTAINER_STATUS" "已停止 (${matched_container})"
            output_result "NACOS_CONTAINER_LEVEL" "警告"
        else
            output_result "NACOS_CONTAINER_STATUS" "未运行"
            output_result "NACOS_CONTAINER_LEVEL" "严重"
        fi
        return 1
    fi
}

# 检测Nacos健康状态
check_nacos_health() {
    local nacos_health
    nacos_health=$(nacos_api "v1/console/health/readiness" 2>/dev/null)
    [ -n "$nacos_health" ] && output_result "NACOS_HEALTH" "$nacos_health"
}

# 检测Nacos容器资源
check_nacos_resources() {
    local nacos_mem
    nacos_mem=$(docker stats --no-stream --format "{{.MemUsage}}" "$NACOS_CONTAINER" 2>/dev/null)
    [ -n "$nacos_mem" ] && output_result "NACOS_MEMORY" "$nacos_mem"
}

# 检测Nacos服务数量
check_nacos_services() {
    local services
    services=$(nacos_api "v1/ns/service/list?pageNo=1&pageSize=100&namespaceId=" 2>/dev/null)

    if [ -n "$services" ]; then
        local service_count
        service_count=$(echo "$services" | grep -o '"count":[0-9]*' | cut -d: -f2 | head -1)
        [ -n "$service_count" ] && output_result "NACOS_SERVICES_COUNT" "$service_count"
    fi
}

# ==================== 主检测流程 ====================
main() {
    log_info "开始Nacos基础检测..."

    if ! check_nacos_container; then
        log_warn "Nacos容器未运行，跳过其他检测"
        return
    fi

    check_nacos_health
    check_nacos_resources
    check_nacos_services

    log_info "Nacos基础检测完成"
}

main
