Files
aitsc/docs/development/监控和日志系统配置总结.md

312 lines
7.5 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# Flask 提示词大师 - 监控和日志系统配置总结
## 概述
本文档总结了为 Flask 提示词大师项目配置的监控和日志系统,包括服务监控、日志管理、性能监控等功能。
## 系统架构
### 1. 监控系统
#### 1.1 简化监控脚本 (`simple_monitor.py`)
- **功能**: 基础服务健康检查、响应时间监控、日志文件检查
- **特点**: 不依赖外部库,轻量级实现
- **监控项目**:
- 应用健康状态 (`/health` 端点)
- 响应时间统计
- 错误日志检测
- 监控报告生成
#### 1.2 监控管理脚本 (`monitor_manager.py`)
- **功能**: 统一管理监控和日志功能
- **特点**: 支持交互式操作和命令行操作
- **管理功能**:
- 启动/停止监控
- 服务状态检查
- 日志管理
- 报告生成
- 监控仪表板
### 2. 日志管理系统
#### 2.1 日志管理脚本 (`log_manager.py`)
- **功能**: 日志轮转、清理、压缩
- **特点**: 自动化日志维护
- **管理功能**:
- 日志文件轮转超过10MB自动轮转
- 旧日志清理保留30天
- 日志文件压缩gzip格式
- 日志统计信息
## 文件结构
```
aitsc/
├── simple_monitor.py # 简化监控脚本
├── log_manager.py # 日志管理脚本
├── monitor_manager.py # 监控管理脚本
├── logs/ # 日志目录
│ ├── app.log # 应用日志
│ ├── simple_service.log # 服务日志
│ ├── monitor.log # 监控日志
│ ├── log_manager.log # 日志管理日志
│ ├── monitor_report.json # 监控报告
│ └── archive/ # 归档目录
│ └── *.log.gz # 压缩的归档日志
```
## 使用方法
### 1. 监控系统使用
#### 1.1 启动监控
```bash
# 交互式模式
python monitor_manager.py
# 直接启动监控
python monitor_manager.py start
# 直接运行监控脚本
python simple_monitor.py
```
#### 1.2 检查服务状态
```bash
# 使用监控管理器
python monitor_manager.py status
# 直接使用监控脚本
python simple_monitor.py check
```
#### 1.3 生成监控报告
```bash
# 使用监控管理器
python monitor_manager.py report
# 直接使用监控脚本
python simple_monitor.py report
```
#### 1.4 显示监控仪表板
```bash
python monitor_manager.py dashboard
```
### 2. 日志管理系统使用
#### 2.1 查看日志统计
```bash
# 使用监控管理器
python monitor_manager.py logs stats
# 直接使用日志管理器
python log_manager.py stats
```
#### 2.2 日志轮转
```bash
# 使用监控管理器
python monitor_manager.py logs rotate
# 直接使用日志管理器
python log_manager.py rotate
```
#### 2.3 清理旧日志
```bash
# 使用监控管理器
python monitor_manager.py logs cleanup
# 直接使用日志管理器
python log_manager.py cleanup
```
#### 2.4 完整日志维护
```bash
# 轮转和清理
python log_manager.py
```
## 配置参数
### 1. 监控配置
#### 1.1 监控间隔
- **默认值**: 30秒
- **位置**: `simple_monitor.py` 中的 `monitor_interval`
- **说明**: 健康检查的执行间隔
#### 1.2 响应时间阈值
- **默认值**: 2.0秒
- **位置**: `simple_monitor.py` 中的阈值检查
- **说明**: 超过此时间会发出警告
### 2. 日志配置
#### 2.1 日志轮转阈值
- **默认值**: 10MB
- **位置**: `log_manager.py` 中的 `max_file_size`
- **说明**: 日志文件超过此大小会自动轮转
#### 2.2 日志保留时间
- **默认值**: 30天
- **位置**: `log_manager.py` 中的 `retention_days`
- **说明**: 归档日志的保留时间
#### 2.3 日志压缩
- **默认值**: 启用
- **位置**: `log_manager.py` 中的 `compress_files`
- **说明**: 是否压缩归档的日志文件
## 监控指标
### 1. 服务健康指标
- **状态**: 运行/停止
- **环境**: development/production
- **响应时间**: 平均响应时间
- **成功率**: 健康检查成功率
### 2. 性能指标
- **总请求数**: 监控期间的总请求数
- **成功请求数**: 成功的健康检查次数
- **失败请求数**: 失败的健康检查次数
- **平均响应时间**: 健康检查的平均响应时间
### 3. 日志指标
- **日志文件数量**: 当前日志文件总数
- **日志总大小**: 所有日志文件的总大小
- **错误日志**: 最近发现的错误日志数量
## 报告生成
### 1. 监控报告 (`monitor_report.json`)
```json
{
"timestamp": "2025-08-17T21:58:19.822324",
"uptime": "0:00:00",
"total_requests": 0,
"successful_requests": 0,
"failed_requests": 0,
"success_rate": "0.00%",
"avg_response_time": "0.00s",
"last_check": null
}
```
### 2. 日志统计报告
- 当前日志文件列表及大小
- 归档日志文件列表及大小
- 总文件数和总大小
## 自动化建议
### 1. 定时任务配置
#### 1.1 Windows 任务计划程序
```batch
# 创建定时监控任务
schtasks /create /tn "FlaskMonitor" /tr "python D:\wxxcx\aitsc\simple_monitor.py" /sc daily /st 09:00
# 创建定时日志维护任务
schtasks /create /tn "FlaskLogMaintenance" /tr "python D:\wxxcx\aitsc\log_manager.py" /sc daily /st 02:00
```
#### 1.2 Linux Cron 任务
```bash
# 编辑 crontab
crontab -e
# 添加定时任务
# 每天上午9点运行监控
0 9 * * * cd /path/to/aitsc && python simple_monitor.py
# 每天凌晨2点维护日志
0 2 * * * cd /path/to/aitsc && python log_manager.py
```
### 2. 服务集成
#### 2.1 与 Windows 服务集成
- 将监控脚本集成到现有的 Windows 服务中
- 在服务启动时自动启动监控
- 在服务停止时自动停止监控
#### 2.2 与 Docker 集成
- 将监控脚本添加到 Docker 容器中
- 使用 Docker 的健康检查机制
- 配置日志卷挂载
## 故障排除
### 1. 常见问题
#### 1.1 编码问题
- **问题**: Unicode 字符显示异常
- **解决**: 已移除所有 Unicode 表情符号,使用纯文本
#### 1.2 权限问题
- **问题**: 无法访问日志文件
- **解决**: 确保脚本有读写权限
#### 1.3 服务连接问题
- **问题**: 无法连接到应用服务
- **解决**: 检查服务是否运行,端口是否正确
### 2. 调试方法
#### 2.1 查看监控日志
```bash
tail -f logs/monitor.log
```
#### 2.2 查看日志管理日志
```bash
tail -f logs/log_manager.log
```
#### 2.3 手动测试健康检查
```bash
curl http://localhost:5000/health
```
## 扩展建议
### 1. 监控扩展
- 添加数据库连接监控
- 添加磁盘空间监控
- 添加网络连接监控
- 添加邮件/短信告警
### 2. 日志扩展
- 添加日志分析功能
- 添加日志搜索功能
- 添加日志可视化
- 集成 ELK 栈
### 3. 性能扩展
- 添加性能指标收集
- 添加慢查询监控
- 添加内存使用监控
- 添加 CPU 使用监控
## 总结
通过配置这套监控和日志系统,我们实现了:
1. **自动化监控**: 服务健康状态自动检查
2. **日志管理**: 自动轮转、清理、压缩日志文件
3. **统一管理**: 通过监控管理器统一管理所有功能
4. **报告生成**: 自动生成监控报告和统计信息
5. **故障排除**: 提供完整的故障排除工具
这套系统为 Flask 提示词大师项目提供了完整的运维支持,确保服务的稳定运行和问题的及时发现。
## 下一步建议
1. **配置告警系统**: 添加邮件或短信告警功能
2. **集成监控面板**: 集成 Grafana 等可视化监控面板
3. **性能优化**: 根据监控数据优化应用性能
4. **自动化部署**: 将监控系统集成到 CI/CD 流程中