#!/bin/bash
################################################################################
# Nginx基础检测模块
# 功能: 检测Nginx容器状态、配置、连接数等
# 作者: 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

# Nginx容器名称
NGINX_CONTAINER="${CONTAINERS[nginx]}"

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

nginx_exec() {
    docker exec "$NGINX_CONTAINER" $@ 2>/dev/null
}

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

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

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

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

# 检测Nginx版本
check_nginx_version() {
    local nginx_version
    nginx_version=$(nginx_exec sh -c "nginx -v 2>&1" | grep "nginx version" | sed 's/.*nginx\///')
    [ -n "$nginx_version" ] && output_result "NGINX_VERSION" "$nginx_version"
}

# 检测Nginx配置
check_nginx_config() {
    local nginx_conf
    nginx_conf=$(nginx_exec nginx -t 2>&1 | grep "successful")
    if [ -n "$nginx_conf" ]; then
        output_result "NGINX_CONFIG" "正常"
    else
        output_result "NGINX_CONFIG" "异常"
    fi
}

# 检测Nginx状态页
check_nginx_status() {
    local nginx_status
    nginx_status=$(nginx_exec curl -s localhost/nginx_status 2>/dev/null | head -1)
    [ -n "$nginx_status" ] && output_result "NGINX_STATUS" "$nginx_status"
}

# 检测Nginx容器资源
check_nginx_resources() {
    local mem_usage
    mem_usage=$(docker stats --no-stream --format "{{.MemUsage}}" "$NGINX_CONTAINER" 2>/dev/null)
    [ -n "$mem_usage" ] && output_result "NGINX_MEMORY" "$mem_usage"
}

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

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

    check_nginx_version
    check_nginx_config
    check_nginx_status
    check_nginx_resources

    log_info "Nginx基础检测完成"
}

main
