Files
oa-monitor/run-long-check.sh
T
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

68 lines
2.2 KiB
Bash
Raw 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 · 长查询检测 dry-run KILL(每 10 分钟)
# 默认 DRY_RUN=1(只报告,不真 KILL)。--execute 才真的 KILL
# ============================================================================
set -u
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
source "${SCRIPT_DIR}/../lib/common.sh"
DRY_RUN=1
for arg in "$@"; do
[[ "${arg}" == "--execute" ]] && DRY_RUN=0
[[ "${arg}" == "--dry-run" ]] && DRY_RUN=1
done
NOW=$(date +%s)
db_exec() {
local sql="$1"
MYSQL_PWD="${DB_PASS}" timeout 30 mysql -h39.100.72.102 -P3306 -uecology ecology -e "${sql}" 2>/dev/null
}
log_info "[LONG-CHECK] 开始(DRY_RUN=${DRY_RUN},阈值 ${ALERT_LONG_QUERY_SEC}s)"
RESULT=$(db_exec "SHOW FULL PROCESSLIST;" | awk -F'\t' -v t="${ALERT_LONG_QUERY_SEC}" '
NR==1 { next }
NF < 10 { next }
{
pid=$1; user=$2; state=$6; time=$7; info=$9
if (time+0 >= t+0 && time+0 > 0) {
gsub(/\t/, " ", info)
printf "%d\t%d\t%s\t%s\t%s\n", pid, time, user, state, substr(info,1,300)
}
}
')
if [[ -z "${RESULT}" ]]; then
log_info "[LONG-CHECK] ✅ 无长查询"
exit 0
fi
COUNT=$(echo "${RESULT}" | wc -l | tr -d ' ')
log_warn "[LONG-CHECK] ⚠️ ${COUNT} 条长查询"
DETAIL=""
while IFS=$'\t' read -r pid time user state info; do
[[ -z "${pid}" ]] && continue
DETAIL="${DETAIL}\n- PID \`${pid}\` **${time}s** \`${user}\` ${state}"
DETAIL="${DETAIL}\n \`\`\`\n ${info}\n \`\`\`"
done <<< "${RESULT}"
MODE_LABEL=$([[ ${DRY_RUN} -eq 1 ]] && echo "🔒 DRY-RUN" || echo "🚨 执行 KILL")
BODY="**${COUNT} 条长查询**(${ALERT_LONG_QUERY_SEC}s 阈值)· ${MODE_LABEL}\n\n${DETAIL}"
dingtalk_alert "P0" "长查询阻塞需处理" "${BODY}"
sqlite_exec "INSERT INTO alerts (ts, level, category, title, detail) VALUES (${NOW}, 'P0', 'db', '长查询', '${COUNT} 条, dry_run=${DRY_RUN}');"
if [[ ${DRY_RUN} -eq 0 ]]; then
log_warn "[LONG-CHECK] 执行 KILL!"
while IFS=$'\t' read -r pid time user state info; do
[[ -z "${pid}" ]] && continue
db_exec "KILL ${pid};"
log_warn "[LONG-CHECK] KILL ${pid} (${time}s, ${user})"
done <<< "${RESULT}"
else
log_info "[LONG-CHECK] DRY-RUN,不执行 KILL"
fi