# servermonitor **Repository Path**: zyx777/servermonitor ## Basic Information - **Project Name**: servermonitor - **Description**: 服务器监控和日志统一管理方案 - **Primary Language**: Python - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2026-03-19 - **Last Updated**: 2026-03-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # ServerMonitor 一个功能完善的服务器监控和日志收集系统,提供 Web 端可视化查看。 ## 功能特性 - **集中式日志管理**:从多个来源(SSH、文件、Agent)收集日志 - **实时仪表盘**:监控系统状态和日志统计信息 - **高级搜索**:支持全文搜索,可按级别、来源、主机名和时间范围筛选 - **Agent 采集**:轻量级 Agent 实现实时日志监控 - **RESTful API**:提供完整的 API 用于与其他工具集成 - **Docker 支持**:使用 Docker Compose 轻松部署 ## 技术栈 - **后端**:FastAPI + SQLAlchemy + PostgreSQL - **前端**:Vue 3 + Element-UI + ECharts - **Agent**:Python + watchdog 文件监控 ## 快速开始 ### 使用 Docker Compose(推荐) ```bash # 克隆仓库 git clone cd servermonitor # 启动所有服务 docker-compose up -d # 初始化数据库(创建默认管理员账户) docker-compose exec backend python scripts/init_db.py # 访问应用 # 前端界面:http://localhost:3000 # API 文档:http://localhost:8000/docs # 默认账号:admin / admin123 ``` ### 手动安装 #### 前置要求 - Python 3.10+ - Node.js 18+ - PostgreSQL 15+ #### 后端设置 ```bash cd backend # 创建虚拟环境 python -m venv venv source venv/bin/activate # Windows: venv\Scripts\activate # 安装依赖 pip install -r requirements.txt # 设置环境变量 export DATABASE_URL="postgresql://servermonitor:servermonitor@localhost:5432/servermonitor" # 运行数据库初始化 python scripts/init_db.py # 启动服务器 uvicorn src.main:app --reload ``` #### 前端设置 ```bash cd frontend # 安装依赖 npm install # 启动开发服务器 npm run dev # 构建生产版本 npm run build ``` #### Agent 设置 ```bash cd agent # 安装依赖 pip install -r requirements.txt # 配置 Agent(编辑 config/agent_config.yaml) # 然后运行 Agent python agent.py --config ../config/agent_config.yaml ``` ## 使用指南 1. **登录**:访问 `http://localhost:3000` 进入 Web 界面 - 默认账号:`admin` / `admin123` 2. **添加日志源**:进入「日志源管理」→「添加日志源」 - 配置 SSH 连接以采集远程日志 - 或部署 Agent 进行本地监控 3. **查看日志**:进入「日志列表」页面浏览已收集的日志 - 使用筛选器过滤结果 - 点击任意日志条目查看详情 4. **监控仪表盘**:查看仪表盘了解系统概况 - 日志统计和趋势 - Agent 状态 - 日志源信息 ## 配置说明 ### 后端配置 编辑 `config/settings.yaml`: ```yaml database: url: "postgresql://user:pass@host:5432/dbname" jwt: secret_key: "your-secret-key" agent: api_key: "your-agent-api-key" ``` ### Agent 配置 编辑 `config/agent_config.yaml`: ```yaml server_url: "http://your-server:8000" agent_key: "your-agent-api-key" agent_name: "agent-01" log_files: - path: "/var/log/syslog" source_id: 1 format: "syslog" ``` ## API 文档 启动后端服务后,访问 `http://localhost:8000/docs` 查看交互式 API 文档。 ### 主要 API 接口 | 接口 | 方法 | 说明 | |------|------|------| | `/api/auth/login` | POST | 用户登录 | | `/api/logs/` | GET | 查询日志 | | `/api/sources/` | GET/POST | 管理日志源 | | `/api/agents/` | GET | 列出 Agent | | `/api/monitor/status` | GET | 系统状态 | ## 系统架构 ``` ┌─────────────────────────────────────────────────────────────┐ │ 前端 (Vue 3) │ │ 登录 | 仪表盘 | 日志 | 日志源 | Agent 管理 │ └─────────────────────────────────────────────────────────────┘ ↓ HTTP ┌─────────────────────────────────────────────────────────────┐ │ 后端 (FastAPI) │ │ 认证 | 日志 API | 日志源 API | 监控 API │ └─────────────────────────────────────────────────────────────┘ ↓ ┌──────────────────────┬──────────────────────────────────────┐ │ PostgreSQL (日志) │ 文件系统 (原始日志) │ └──────────────────────┴──────────────────────────────────────┘ ↓ ┌─────────────────────────────────────────────────────────────┐ │ 日志采集层 │ │ SSH 采集器 | Agent 采集器 | 文件监控器 │ └─────────────────────────────────────────────────────────────┘ ``` ## 开发指南 ### 运行测试 ```bash # 后端测试 cd backend pytest tests/ # 前端测试(待实现) cd frontend npm test ``` ### 数据库迁移 ```bash cd backend python scripts/migrate.py ``` ## 生产部署 1. 更新 `config/settings.yaml` 中的密钥 2. 配置正确的数据库连接 3. 为前端配置反向代理(nginx) 4. 使用 systemd/supervisor 管理后端进程 5. 配置日志轮转 详细的部署文档请参阅 [docs/DEPLOY.md](docs/DEPLOY.md) ## 故障排查 ### 数据库连接问题 ```bash # 检查 PostgreSQL 是否运行 sudo systemctl status postgresql # 测试连接 psql -h localhost -U servermonitor -d servermonitor ``` ### Agent 无法连接 - 验证 Agent 配置中的 `server_url` - 检查防火墙设置 - 确保 Agent API 密钥与后端配置匹配 ### 日志未显示 - 检查日志源是否激活 - 验证文件路径是否正确 - 测试远程日志源的 SSH 连接 - 检查 Agent 心跳状态 ## 许可证 MIT License ## 贡献 1. Fork 本仓库 2. 创建特性分支 3. 提交更改 4. 推送到分支 5. 创建 Pull Request