#!/bin/bash
# Linux服务器监控系统一键部署脚本

set -e

echo "========================================"
echo "Linux服务器监控系统一键部署脚本"
echo "========================================"
echo "功能特点："
echo "  1. 每天9点发送一次报告（如果达到阈值）"
echo "  2. 只有达到阈值时才发送"
echo "  3. 系统自启动"
echo "========================================"

# 检查是否为root用户
if [ "$EUID" -ne 0 ]; then 
    echo "请使用root权限运行此脚本！"
    echo "使用: sudo ./deploy_linux_monitor.sh"
    exit 1
fi

# 检查Python3
if ! command -v python3 &> /dev/null; then
    echo "安装Python3..."
    apt update && apt install -y python3 python3-pip
fi

# 创建程序目录
PROGRAM_DIR="/opt/server-monitor"
echo "创建程序目录: $PROGRAM_DIR"
mkdir -p "$PROGRAM_DIR"

# 复制程序文件
echo "复制程序文件..."
cp linux_main_fixed.py "$PROGRAM_DIR/main.py"
cp linux_config.py "$PROGRAM_DIR/config.py"
cp linux_monitor.py "$PROGRAM_DIR/monitor.py"
cp linux_notifier.py "$PROGRAM_DIR/notifier.py"
cp linux_logger.py "$PROGRAM_DIR/logger.py"

# 设置权限
chmod 755 "$PROGRAM_DIR/main.py"
chmod 644 "$PROGRAM_DIR/config.py"
chmod 644 "$PROGRAM_DIR/monitor.py"
chmod 644 "$PROGRAM_DIR/notifier.py"
chmod 644 "$PROGRAM_DIR/logger.py"

# 安装Python依赖
echo "安装Python依赖..."
pip3 install psutil requests schedule > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo "✅ Python依赖安装成功"
else
    echo "⚠️  Python依赖安装可能失败，请手动检查"
    echo "   手动安装命令: pip3 install psutil requests schedule"
fi

# 创建日志目录
LOG_DIR="/var/log/server_monitor"
echo "创建日志目录: $LOG_DIR"
mkdir -p "$LOG_DIR"
chmod 755 "$LOG_DIR"

# 更新配置文件中的日志路径
echo "更新配置文件..."
sed -i "s|LOG_FILE = \".*\"|LOG_FILE = \"$LOG_DIR/monitor.log\"|" "$PROGRAM_DIR/config.py"

# 安装系统服务
echo "安装系统服务..."
cp server-monitor.service /etc/systemd/system/
systemctl daemon-reload
systemctl enable server-monitor.service

echo "✅ 系统服务安装完成"

# 启动服务
echo "启动监控服务..."
systemctl start server-monitor.service

# 等待服务启动
sleep 3

echo ""
echo "========================================"
echo "部署完成！"
echo "========================================"
echo ""
echo "✅ 监控系统已部署并启动"
echo ""
echo "📊 服务状态:"
systemctl status server-monitor.service --no-pager | head -20
echo ""
echo "📋 服务管理命令:"
echo "  查看状态: systemctl status server-monitor"
echo "  启动服务: systemctl start server-monitor"
echo "  停止服务: systemctl stop server-monitor"
echo "  重启服务: systemctl restart server-monitor"
echo "  查看日志: journalctl -u server-monitor -f"
echo ""
echo "📁 程序目录: $PROGRAM_DIR"
echo "📄 配置文件: $PROGRAM_DIR/config.py"
echo "📝 日志文件: $LOG_DIR/monitor.log"
echo ""
echo "🔧 修改配置:"
echo "  编辑 $PROGRAM_DIR/config.py 修改监控设置"
echo "  修改后重启服务: systemctl restart server-monitor"
echo ""
echo "🧪 测试功能:"
echo "  1. 测试监控: cd $PROGRAM_DIR && python3 monitor.py"
echo "  2. 测试通知: cd $PROGRAM_DIR && python3 notifier.py"
echo "  3. 测试完整流程: cd $PROGRAM_DIR && python3 main.py --test"
echo "  4. 强制发送测试: cd $PROGRAM_DIR && python3 main.py --force"
echo ""
echo "⏰ 定时报告规则:"
echo "  - 每天9点检查一次"
echo "  - 只有达到阈值时才发送报告"
echo "  - 未达到阈值时不发送任何消息"
echo ""
echo "🔔 阈值配置（在config.py中修改）:"
echo "  - CPU阈值: 80%"
echo "  - 内存阈值: 80%"
echo "  - 磁盘阈值: 80%"
echo "  - 磁盘警告阈值: 90%"
echo ""
echo "📞 技术支持:"
echo "  查看详细日志: journalctl -u server-monitor -n 50"
echo "  重新部署: 重新运行此脚本"
echo ""
echo "========================================"
echo "✅ Linux服务器监控系统部署完成！"
echo "========================================"