Files
oa-monitor 20fa48fb34 fix: check-alert v3 修复钉钉推送断档 P0 假阳性
根因:sqlite_exec 吞掉 stderr(2>/dev/null),查询失败返回空串,
0 默认为 0 导致误报

修复:业务层取数显式调用 sqlite3 + 检查退出码 + 正则校验,
查询失败跳过检测并 warn 日志
2026-11-04 19:41:18 +08:00

37 lines
1.4 KiB
Bash
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.
#!/usr/bin/env bash
# ============================================================================
# OA Monitor · 数据清理(每天 03:00)
# 保留最近 7 天指标,alerts 和 alert_cooldown 保留 30 天
# ============================================================================
set -u
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
source "${SCRIPT_DIR}/../lib/common.sh"
NOW=$(date +%s)
RETENTION_7D=$(( NOW - 7 * 86400 ))
RETENTION_30D=$(( NOW - 30 * 86400 ))
log_info "[CLEANUP] 开始清理,阈值 ts < ${RETENTION_7D}(7天),alerts < ${RETENTION_30D}(30天)"
# 各层指标表
for table in db_metrics http_metrics business_metrics infra_metrics db_processlist; do
BEFORE=$(sqlite_exec "SELECT COUNT(*) FROM ${table};")
sqlite_exec "DELETE FROM ${table} WHERE ts < ${RETENTION_7D};"
AFTER=$(sqlite_exec "SELECT COUNT(*) FROM ${table};")
log_info "[CLEANUP] ${table}: ${BEFORE} → ${AFTER}(删了 $((BEFORE - AFTER)) 条)"
done
# alerts 保留 30 天
BEFORE=$(sqlite_exec "SELECT COUNT(*) FROM alerts;")
sqlite_exec "DELETE FROM alerts WHERE ts < ${RETENTION_30D};"
AFTER=$(sqlite_exec "SELECT COUNT(*) FROM alerts;")
log_info "[CLEANUP] alerts: ${BEFORE} → ${AFTER}(删了 $((BEFORE - AFTER)) 条)"
# VACUUM 压缩(可选,不频繁)
if (( NOW % (86400 * 7) < 3600 )); then
log_info "[CLEANUP] 每周 VACUUM SQLite..."
sqlite_exec "VACUUM;"
fi
log_info "[CLEANUP] 完成"