#!/bin/bash
################################################################################
# 应用日志分析模块
# 功能: 检测Java、Python应用日志中的错误和异常
# 作者: 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

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

# 检测应用日志错误
check_app_log_errors() {
    local log_dirs=("/data/services/api/*/log" "/data/services/api/python*/log")
    local total_errors=0

    for log_dir in $log_dirs; do
        if [ -d "$log_dir" ]; then
            local error_count
            error_count=$(find "$log_dir" -name "*.log" -mtime -1 -exec grep -lE "ERROR|Exception" {} \; 2>/dev/null | wc -l)
            total_errors=$((total_errors + error_count))
        fi
    done

    output_result "APP_LOG_ERRORS_24H" "$total_errors"

    if [ "$total_errors" -gt 100 ]; then
        output_result "APP_LOG_LEVEL" "严重"
    elif [ "$total_errors" -gt 10 ]; then
        output_result "APP_LOG_LEVEL" "警告"
    else
        output_result "APP_LOG_LEVEL" "正常"
    fi
}

# 检测Docker容器日志错误
check_container_log_errors() {
    if ! command -v docker &> /dev/null; then
        return
    fi

    local total_container_errors=0
    local running_containers

    running_containers=$(docker ps --format "{{.Names}}" 2>/dev/null | head -20)

    for container in $running_containers; do
        local error_count
        error_count=$(docker logs --tail 200 "$container" 2>&1 | grep -ciE "ERROR|Exception|FATAL" || echo "0")
        error_count=$(echo "$error_count" | tail -1)

        if [ -n "$error_count" ] && [ "$error_count" -gt 0 ]; then
            output_result "CONTAINER_LOG_ERRORS_${container}" "$error_count"
            total_container_errors=$((total_container_errors + error_count))
        fi
    done

    output_result "CONTAINER_LOG_ERRORS_TOTAL" "$total_container_errors"

    if [ "$total_container_errors" -gt 200 ]; then
        output_result "CONTAINER_LOG_LEVEL" "严重"
    elif [ "$total_container_errors" -gt 50 ]; then
        output_result "CONTAINER_LOG_LEVEL" "警告"
    else
        output_result "CONTAINER_LOG_LEVEL" "正常"
    fi
}

# 检测系统日志错误
check_system_log_errors() {
    local sys_errors=0

    # 检查 /var/log/messages 或 /var/log/syslog
    if [ -f "/var/log/messages" ]; then
        sys_errors=$(grep -cE "error|ERROR|critical|CRITICAL" /var/log/messages 2>/dev/null || echo "0")
    elif [ -f "/var/log/syslog" ]; then
        sys_errors=$(grep -cE "error|ERROR|critical|CRITICAL" /var/log/syslog 2>/dev/null || echo "0")
    fi

    if [ "$sys_errors" -gt 0 ]; then
        output_result "SYSLOG_ERRORS" "$sys_errors"
    fi
}

# ==================== 主检测流程 ====================
main() {
    log_info "开始应用日志分析..."

    check_app_log_errors
    check_container_log_errors
    check_system_log_errors

    log_info "应用日志分析完成"
}

main
