#!/bin/bash
################################################################################
# 系统基础信息检测模块
# 功能: 检测主机名、操作系统、内核版本、运行时间、负载等基础信息
# 作者: Claude Code
# 日期: 2026-05-09
################################################################################

# 获取脚本所在目录并加载依赖
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_hostname() {
    local hostname
    hostname=$(hostname -f 2>/dev/null || hostname)
    output_result "HOSTNAME" "$hostname"
}

# 检测操作系统版本
check_os_version() {
    local os_version=""

    if [ -f /etc/os-release ]; then
        source /etc/os-release
        os_version="$PRETTY_NAME"
    elif [ -f /etc/redhat-release ]; then
        os_version=$(cat /etc/redhat-release)
    elif [ -f /etc/lsb-release ]; then
        source /etc/lsb-release
        os_version="$DISTRIB_DESCRIPTION"
    else
        os_version=$(uname -s)
    fi

    output_result "OS_VERSION" "$os_version"
}

# 检测内核版本
check_kernel_version() {
    local kernel
    kernel=$(uname -r)
    output_result "KERNEL_VERSION" "$kernel"
}

# 检测运行时间（天数）
check_uptime_days() {
    local uptime_days
    uptime_days=$(get_uptime_days)
    output_result "UPTIME_DAYS" "$uptime_days"
}

# 检测系统负载
check_loadavg() {
    local load1 load5 load15

    # 从 /proc/loadavg 读取负载
    read load1 load5 load15 rest < /proc/loadavg

    output_result "LOAD_1MIN" "$load1"
    output_result "LOAD_5MIN" "$load5"
    output_result "LOAD_15MIN" "$load15"
}

# 检测CPU核心数
check_cpu_cores() {
    local cores
    cores=$(nproc 2>/dev/null || echo "未知")
    output_result "CPU_CORES" "$cores"
}

# 检测总内存
check_memory_total() {
    local mem_total_bytes mem_total_gb

    mem_total_bytes=$(free -b | grep Mem | awk '{print $2}')

    if [ -n "$mem_total_bytes" ] && [ "$mem_total_bytes" -gt 0 ]; then
        mem_total_gb=$(awk "BEGIN {printf \"%.2f\", $mem_total_bytes/1024/1024/1024}")
        output_result "MEMORY_TOTAL" "${mem_total_gb}GB"
    else
        output_result "MEMORY_TOTAL" "未知"
    fi
}

# 检测系统启动时间
check_boot_time() {
    local boot_time
    boot_time=$(uptime -s 2>/dev/null || echo "未知")
    output_result "BOOT_TIME" "$boot_time"
}

# 检测架构
check_architecture() {
    local arch
    arch=$(uname -m)
    output_result "ARCHITECTURE" "$arch"
}

# 检测系统资源限制
check_ulimit() {
    local ulimit_info=""
    if command -v ulimit &>/dev/null; then
        ulimit_info=$(ulimit -a 2>/dev/null | head -20 | tr '\n' ',' | sed 's/,$//')
    fi
    if [ -n "$ulimit_info" ]; then
        output_result "ULIMIT_INFO" "$ulimit_info"
    fi
    # 无法获取时不输出，让 parser 忽略
}

# 检测内核启动参数
check_kernel_cmdline() {
    local cmdline=""
    if [ -f /proc/cmdline ]; then
        cmdline=$(cat /proc/cmdline 2>/dev/null | tr ' ' ',')
        # 截取前200个字符避免过长
        cmdline=${cmdline:0:200}
        if [ ${#cmdline} -eq 200 ]; then
            cmdline="${cmdline}..."
        fi
    fi
    if [ -n "$cmdline" ]; then
        output_result "KERNEL_CMDLINE" "$cmdline"
    else
        output_result "KERNEL_CMDLINE" "无法读取"
    fi
}

# 检测内核关键参数
check_kernel_params() {
    # 文件描述符限制
    local file_max=""
    if [ -f /proc/sys/fs/file-max ]; then
        file_max=$(cat /proc/sys/fs/file-max 2>/dev/null)
    fi
    [ -n "$file_max" ] && output_result "FS_FILE_MAX" "$file_max"

    # inotify监控限制
    local inotify_max_watches=""
    if [ -f /proc/sys/fs/inotify/max_user_watches ]; then
        inotify_max_watches=$(cat /proc/sys/fs/inotify/max_user_watches 2>/dev/null)
    fi
    [ -n "$inotify_max_watches" ] && output_result "INOTIFY_MAX_WATCHES" "$inotify_max_watches"

    # TCP连接队列
    local somaxconn=""
    if [ -f /proc/sys/net/core/somaxconn ]; then
        somaxconn=$(cat /proc/sys/net/core/somaxconn 2>/dev/null)
    fi
    [ -n "$somaxconn" ] && output_result "NET_SOMAXCONN" "$somaxconn"

    # TCP TIME_WAIT超时
    local tcp_tw_timeout=""
    if [ -f /proc/sys/net/ipv4/tcp_fin_timeout ]; then
        tcp_tw_timeout=$(cat /proc/sys/net/ipv4/tcp_fin_timeout 2>/dev/null)
    fi
    [ -n "$tcp_tw_timeout" ] && output_result "TCP_FIN_TIMEOUT" "$tcp_tw_timeout"

    # TCP保活时间
    local tcp_keepalive_time=""
    if [ -f /proc/sys/net/ipv4/tcp_keepalive_time ]; then
        tcp_keepalive_time=$(cat /proc/sys/net/ipv4/tcp_keepalive_time 2>/dev/null)
    fi
    [ -n "$tcp_keepalive_time" ] && output_result "TCP_KEEPALIVE_TIME" "$tcp_keepalive_time"
}

# ==================== 主检测流程 ====================
main() {
    log_info "开始系统基础信息检测..."

    # 执行各项检测
    check_hostname
    check_os_version
    check_kernel_version
    check_uptime_days
    check_loadavg
    check_cpu_cores
    check_memory_total
    check_boot_time
    check_architecture
    check_ulimit
    check_kernel_cmdline
    check_kernel_params

    log_info "系统基础信息检测完成"
}

# 执行主函数
main
