提交 3c03d21b authored 作者: 陈泽健's avatar 陈泽健

feat(service): 添加服务自检脚本历史记录清理功能

- 实现了自动清理超过14天的历史记录功能
- 添加_get_cutoff_date函数用于计算截止日期
- 添加_cleanup_old_files函数用于清理指定目录旧文件
- 添加cleanup_history_records主函数执行清理流程
- 在自检报告中增加历史记录清理摘要章节
- 集成清理结果显示在报告和日志中
- 支持GNU和BSD系统的日期兼容性处理
- 实现错误处理和状态记录机制
上级 cdb97800
...@@ -161,6 +161,67 @@ run_issue_handler() { ...@@ -161,6 +161,67 @@ run_issue_handler() {
fi fi
} }
# ------------------------------
# 历史记录清理辅助函数
# ------------------------------
# 获取截止日期(昨天往前推14天)
_get_cutoff_date() {
local cutoff_date
# Linux 兼容方式(GNU date 和 BSD date)
if date --version >/dev/null 2>&1; then
# GNU date (Linux)
cutoff_date=$(date -d "yesterday -14 days" '+%Y%m%d')
else
# BSD date (macOS)
cutoff_date=$(date -v-15d '+%Y%m%d')
fi
echo "$cutoff_date"
}
# 清理指定目录下超过截止日期的文件
_cleanup_old_files() {
local dir_path="$1" # 目录路径
local file_prefix="$2" # 文件前缀
local cutoff_date="$3" # 截止日期 (YYYYMMDD格式)
local deleted_count=0
local total_count=0
local errors=0
if [[ ! -d "$dir_path" ]]; then
log WARN "[清理] 目录不存在: $dir_path"
echo "0|0|0"
return
fi
while IFS= read -r -d '' file; do
((total_count++))
local filename
filename=$(basename "$file")
# 提取日期 (格式: health_report_192.168.5.44_20260421_095831.md)
local file_date
file_date=$(echo "$filename" | grep -oP '\d{8}' | head -1)
if [[ -z "$file_date" ]]; then
log DEBUG "[清理] 无法提取日期: $filename"
continue
fi
if [[ "$file_date" < "$cutoff_date" ]]; then
if rm -f "$file"; then
((deleted_count++))
log INFO "[清理] 已删除: $filename"
else
((errors++))
log ERROR "[清理] 删除失败: $filename"
fi
fi
done < <(find "$dir_path" -maxdepth 1 -type f -name "${file_prefix}*" -print0 2>/dev/null)
echo "${deleted_count}|${total_count}|${errors}"
}
# ------------------------------ # ------------------------------
# 1) 平台识别 # 1) 平台识别
# ------------------------------ # ------------------------------
...@@ -2798,6 +2859,16 @@ write_report() { ...@@ -2798,6 +2859,16 @@ write_report() {
w "- LogExport: \`$(report_kv_get "log_export.status")\` count=\`$(report_kv_get "log_export.count")\` dir=\`$(report_kv_get "log_export.dir")\`" w "- LogExport: \`$(report_kv_get "log_export.status")\` count=\`$(report_kv_get "log_export.count")\` dir=\`$(report_kv_get "log_export.dir")\`"
w "" w ""
# ✅ 新增:历史记录清理摘要
w "## 历史记录清理"
w ""
w "- 报告清理: 已删除 \`$(report_kv_get "clean.report.deleted")\`/总计\`$(report_kv_get "clean.report.total")\`"
w "- 日志清理: 已删除 \`$(report_kv_get "clean.log.deleted")\`/总计\`$(report_kv_get "clean.log.total")\`"
w "- 清理时间: \`$(report_kv_get "clean.time")\`"
w "- 截止日期: \`$(report_kv_get "clean.cutoff_date")\`"
w "- 状态: \`$(report_kv_get "clean.status")\`"
w ""
# ✅ 原有:REPORT_LINES 写入(过程摘要) # ✅ 原有:REPORT_LINES 写入(过程摘要)
if [[ "${#REPORT_LINES[@]}" -gt 0 ]]; then if [[ "${#REPORT_LINES[@]}" -gt 0 ]]; then
w "## 过程摘要(REPORT_LINES)" w "## 过程摘要(REPORT_LINES)"
...@@ -3117,6 +3188,50 @@ write_report() { ...@@ -3117,6 +3188,50 @@ write_report() {
w "- 详细过程日志见:\`${LOG_FILE}\`" w "- 详细过程日志见:\`${LOG_FILE}\`"
} }
# ------------------------------
# 历史记录清理(主函数)
# ------------------------------
cleanup_history_records() {
section "历史记录清理"
local cutoff_date
cutoff_date=$(_get_cutoff_date)
log INFO "[清理] 截止日期: $cutoff_date (保留此日期之后的记录)"
local report_deleted report_total report_errors
local log_deleted log_total log_errors
local result
local overall_status="成功"
# 清理报告
result=$(_cleanup_old_files "$REPORT_DIR" "health_report_" "$cutoff_date")
IFS='|' read -r report_deleted report_total report_errors <<< "$result"
# 清理日志
result=$(_cleanup_old_files "$LOG_DIR" "health_check_" "$cutoff_date")
IFS='|' read -r log_deleted log_total log_errors <<< "$result"
# 记录结果到报告KV
report_kv_set "clean.report.deleted" "$report_deleted"
report_kv_set "clean.report.total" "$report_total"
report_kv_set "clean.log.deleted" "$log_deleted"
report_kv_set "clean.log.total" "$log_total"
report_kv_set "clean.time" "$(date '+%Y-%m-%d %H:%M:%S')"
report_kv_set "clean.cutoff_date" "$cutoff_date"
# 错误处理
if [[ "$report_errors" -gt 0 || "$log_errors" -gt 0 ]]; then
overall_status="部分成功(有删除失败)"
log ERROR "[清理] 发现删除错误,已记录"
fi
report_kv_set "clean.status" "$overall_status"
log INFO "[清理] 完成 - 报告: 删除${report_deleted}/${report_total}, 日志: 删除${log_deleted}/${log_total}, 状态: $overall_status"
return 0
}
# ------------------------------ # ------------------------------
# 主流程 # 主流程
# ------------------------------ # ------------------------------
...@@ -3320,6 +3435,9 @@ main() { ...@@ -3320,6 +3435,9 @@ main() {
write_report "$server_ip" "$platform" "$sys_kv" "$report_file" write_report "$server_ip" "$platform" "$sys_kv" "$report_file"
log SUCCESS "Markdown 报告已生成: $report_file" log SUCCESS "Markdown 报告已生成: $report_file"
echo "[REPORT_FILE] $report_file" echo "[REPORT_FILE] $report_file"
# 12) 历史记录清理
cleanup_history_records
} }
# ✅ 确保文件末尾仍然调用 main # ✅ 确保文件末尾仍然调用 main
......
# _PRD_服务自检需求文档_自运行版本优化
> 版本:V1.0
> 更新日期:2026-04-20
> 代码路径:AuxiliaryTool/ScriptTool/ServiceSelfInspection/check_server_health_auto.sh
## 1. 背景与目标
### 1.1 背景
- 当前版本脚本缺少了清理历史记录功能,导致历史记录无法删除,会累积占用磁盘空间,影响服务器性能。
### 1.2 目标
- 添加清理历史记录功能,提升服务自检效率。
- 清理时机:每次执行自检完成后开始检查是否有历史记录。
- 清理周期:自检报告、自检日志以及中间件日志只保留14天。从昨天往前推算14天的需删除
- 清理范围:
- 自检报告路径:当前脚本所在目录的Reports/ 报告命名例如:health_report_192.168.5.44_20260421_095831.md:
- 自检日志路径:当前脚本所在目录的logs/ 日志命名例如:health_check_20260421_095831.log
- 日志记录:需记录清理操作的时间和结果,整合进自检日志文件与自检报告中。
- 错误处理:需记录错误信息,并记录处理结果后中断清理操作。
---
## 需求规范
- 代码规范: `Docs/PRD/01规范文档/_PRD_规范文档_代码规范.md`
- 问题总结: `Docs/PRD/01规范文档/_PRD_问题总结_记录文档.md`
- 方法总结: `Docs/PRD/01规范文档/_PRD_方法总结_记录文档.md`
- 文档规范: `Docs/PRD/01规范文档/_PRD_规范文档_文档规范.md`
- 测试规范: `Docs/PRD/01规范文档/_PRD_规范文档_测试规范.md`
\ No newline at end of file
# _PRD_服务自检需求文档_自运行版本优化清理历史记录_计划执行
> 版本:V1.0
> 更新日期:2026-04-21
> 代码路径:AuxiliaryTool/ScriptTool/ServiceSelfInspection/check_server_health_auto.sh
## 1. 需求回顾
### 1.1 背景
当前版本脚本缺少清理历史记录功能,导致历史记录累积占用磁盘空间。
### 1.2 目标
- 添加清理历史记录功能
- 清理时机:每次执行自检完成后执行
- 清理周期:保留今天 + 昨天及往前14天(共15天),删除第15天以前的记录
- 清理范围:
- 自检报告路径:`当前脚本所在目录/Reports/`(文件名格式:`health_report_*.md`
- 自检日志路径:`当前脚本所在目录/logs/`(文件名格式:`health_check_*.log`
- 日志记录:整合进自检日志文件与自检报告
- 错误处理:记录错误信息后中断清理操作
---
## 2. 实现方案
### 2.1 文件修改位置
- **文件**`AuxiliaryTool/ScriptTool/ServiceSelfInspection/check_server_health_auto.sh`
- **修改位置**:在 `main()` 函数中,生成报告之后(第3322行)添加清理历史记录调用
### 2.2 新增函数设计
#### 2.2.1 `cleanup_history_records()` 函数
清理历史记录的主函数,负责清理超过15天的自检报告和日志文件。
**函数签名**
```bash
cleanup_history_records() {
# 返回值:0=成功,1=失败
}
```
**实现逻辑**
1. 计算截止日期(昨天往前推14天)
2. 清理Reports目录下的旧报告
3. 清理logs目录下的旧日志
4. 汇总清理结果并记录到报告KV
5. 记录清理操作到日志
#### 2.2.2 `_cleanup_old_files()` 辅助函数
按日期阈值清理指定目录下的文件。
**函数签名**
```bash
_cleanup_old_files() {
local dir_path="$1" # 目录路径
local file_prefix="$2" # 文件前缀
local cutoff_date="$3" # 截止日期 (YYYYMMDD格式)
# 返回值:清理的文件数量
}
```
### 2.3 日期计算逻辑
**截止日期计算**
```bash
# 获取昨天的日期,再往前推14天
cutoff_date=$(date -d "yesterday -14 days" '+%Y%m%d' 2>/dev/null || \
date -v-15d '+%Y%m%d' 2>/dev/null || \
awk 'BEGIN{print strftime("%Y%m%d", systime()-15*86400)}')
```
**文件日期提取**
从文件名中提取日期部分(格式:`YYYYMMDD_HHMMSS``YYYYMMDD`
### 2.4 报告集成
`write_report()` 函数中新增"历史记录清理"摘要章节:
```markdown
## 历史记录清理
- 报告清理: `clean.report.deleted=5` `clean.report.total=100`
- 日志清理: `clean.log.deleted=3` `clean.log.total=50`
- 清理时间: `2026-04-21 10:00:00`
- 状态: `成功/部分成功/失败`
```
---
## 3. 详细实现步骤
### 3.1 步骤一:添加日期计算辅助函数
```bash
# 获取截止日期(昨天往前推14天)
_get_cutoff_date() {
local cutoff_date
# Linux 兼容方式(GNU date 和 BSD date)
if date --version >/dev/null 2>&1; then
# GNU date (Linux)
cutoff_date=$(date -d "yesterday -14 days" '+%Y%m%d')
else
# BSD date (macOS)
cutoff_date=$(date -v-15d '+%Y%m%d')
fi
echo "$cutoff_date"
}
```
### 3.2 步骤二:添加文件清理辅助函数
```bash
# 清理指定目录下超过截止日期的文件
_cleanup_old_files() {
local dir_path="$1"
local file_prefix="$2"
local cutoff_date="$3"
local deleted_count=0
local total_count=0
local errors=0
if [[ ! -d "$dir_path" ]]; then
log WARN "[清理] 目录不存在: $dir_path"
echo "0|0|0"
return
fi
while IFS= read -r -d '' file; do
((total_count++))
local filename=$(basename "$file")
# 提取日期 (格式: health_report_192.168.5.44_20260421_095831.md)
local file_date=$(echo "$filename" | grep -oP '\d{8}' | head -1)
if [[ -z "$file_date" ]]; then
log DEBUG "[清理] 无法提取日期: $filename"
continue
fi
if [[ "$file_date" < "$cutoff_date" ]]; then
if rm -f "$file"; then
((deleted_count++))
log INFO "[清理] 已删除: $filename"
else
((errors++))
log ERROR "[清理] 删除失败: $filename"
fi
fi
done < <(find "$dir_path" -maxdepth 1 -type f -name "${file_prefix}*" -print0 2>/dev/null)
echo "${deleted_count}|${total_count}|${errors}"
}
```
### 3.3 步骤三:添加主清理函数
```bash
# 清理历史记录
cleanup_history_records() {
section "历史记录清理"
local cutoff_date=$(_get_cutoff_date)
log INFO "[清理] 截止日期: $cutoff_date (保留此日期之后的记录)"
local report_deleted report_total report_errors
local log_deleted log_total log_errors
local overall_status="成功"
# 清理报告
local result=$(_cleanup_old_files "$REPORT_DIR" "health_report_" "$cutoff_date")
IFS='|' read -r report_deleted report_total report_errors <<< "$result"
# 清理日志
result=$(_cleanup_old_files "$LOG_DIR" "health_check_" "$cutoff_date")
IFS='|' read -r log_deleted log_total log_errors <<< "$result"
# 记录结果到报告KV
report_kv_set "clean.report.deleted" "$report_deleted"
report_kv_set "clean.report.total" "$report_total"
report_kv_set "clean.log.deleted" "$log_deleted"
report_kv_set "clean.log.total" "$log_total"
report_kv_set "clean.time" "$(date '+%Y-%m-%d %H:%M:%S')"
report_kv_set "clean.cutoff_date" "$cutoff_date"
# 错误处理
if [[ "$report_errors" -gt 0 || "$log_errors" -gt 0 ]]; then
overall_status="部分成功(有删除失败)"
log ERROR "[清理] 发现删除错误,已记录"
fi
report_kv_set "clean.status" "$overall_status"
log INFO "[清理] 完成 - 报告: 删除${report_deleted}/${report_total}, 日志: 删除${log_deleted}/${log_total}, 状态: $overall_status"
return 0
}
```
### 3.4 步骤四:修改main()函数
在第3322行(`log SUCCESS "Markdown 报告已生成: $report_file"`)之后添加:
```bash
# 12) 历史记录清理
cleanup_history_records
```
### 3.5 步骤六:修改write_report()函数
在"备份/日志导出摘要"章节后添加"历史记录清理"章节:
```bash
# 新增:历史记录清理摘要
w "## 历史记录清理"
w ""
w "- 报告清理: 已删除 \`$(report_kv_get "clean.report.deleted")\`/总计\`$(report_kv_get "clean.report.total")\`"
w "- 日志清理: 已删除 \`$(report_kv_get "clean.log.deleted")\`/总计\`$(report_kv_get "clean.log.total")\`"
w "- 清理时间: \`$(report_kv_get "clean.time")\`"
w "- 截止日期: \`$(report_kv_get "clean.cutoff_date")\`"
w "- 状态: \`$(report_kv_get "clean.status")\`"
w ""
```
---
## 4. 测试计划
### 4.1 单元测试
- [ ] 测试 `_get_cutoff_date()` 函数在不同系统(GNU/BSD date)下的输出
- [ ] 测试日期提取逻辑(从文件名中提取YYYYMMDD)
- [ ] 测试 `_cleanup_old_files()` 函数的边界情况:
- 目录不存在
- 目录为空
- 没有符合条件的文件
- 文件名格式异常
### 4.2 集成测试
- [ ] 创建测试用的旧报告/日志文件
- [ ] 执行完整自检流程,验证清理功能
- [ ] 检查报告中的清理摘要是否正确
- [ ] 检查日志中的清理记录是否完整
### 4.3 边界测试
| 测试场景 | 预期结果 |
|---------|---------|
| 目录不存在 | 跳过清理,记录警告 |
| 文件名无日期 | 跳过该文件 |
| 刚好15天的文件 | 保留 |
| 16天的文件 | 删除 |
| 删除失败 | 记录错误,继续处理其他文件 |
---
## 5. 实现检查清单
- [ ] 添加 `_get_cutoff_date()` 函数
- [ ] 添加 `_cleanup_old_files()` 函数
- [ ] 添加 `cleanup_history_records()` 函数
- [ ]`main()` 函数中添加清理调用
- [ ]`write_report()` 函数中添加清理摘要
- [ ] 添加完整的中文注释
- [ ] 本地测试验证
---
## 6. 优化功能回填
### 待补充
-
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论