xiaowei-system/scripts/db-monitor.sh

75 lines
2.0 KiB
Bash
Executable File
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.

#!/bin/bash
# DB 完整性 + 内存监控
# 每 30 分钟跑一次
# 异常时飞书报警,恢复时静默
set -e
HERMES=/home/muc/.hermes
LOG=$HERMES/logs/db-monitor.log
ALERT_FILE=$HERMES/logs/db-monitor.alert
mkdir -p "$HERMES/logs"
# 1. 内存检查
MEM_FREE=$(free -m | awk '/^Mem:/{print $7}')
SWAP_USED=$(free -m | awk '/^Swap:/{print $3}')
SWAP_TOTAL=$(free -m | awk '/^Swap:/{print $2}')
MEM_ALERT=""
if [ "$MEM_FREE" -lt 200 ]; then
MEM_ALERT="🚨 内存 free ${MEM_FREE}M (< 200M) | swap ${SWAP_USED}/${SWAP_TOTAL}M"
fi
# 2. DB 完整性检查(关键 5 个 DB
DB_ALERT=""
for db in \
"$HERMES/state.db" \
"$HERMES/kanban.db" \
"$HERMES/cron.db" \
"$HERMES/graph.db" \
"$HERMES/profiles/prof-b/state.db"
do
if [ ! -f "$db" ]; then
DB_ALERT="${DB_ALERT}❌ 缺失: $db\n"
continue
fi
size=$(stat -c %s "$db")
if [ "$size" = "0" ]; then
DB_ALERT="${DB_ALERT}❌ 0字节: $db\n"
continue
fi
# 跳过 >100MB 的大文件(检查太慢)
if [ "$size" -gt 104857600 ]; then
# 大文件只查快速检查
result=$(timeout 5 sqlite3 "$db" "PRAGMA quick_check;" 2>&1 | head -1)
else
result=$(timeout 10 sqlite3 "$db" "PRAGMA integrity_check;" 2>&1 | head -1)
fi
if [ "$result" != "ok" ]; then
DB_ALERT="${DB_ALERT}$db: $result\n"
fi
done
# 3. 写入日志
TS=$(date '+%Y-%m-%d %H:%M:%S')
echo "[$TS] mem=${MEM_FREE}M swap=${SWAP_USED}/${SWAP_TOTAL}M db=$(echo -e "$DB_ALERT" | wc -l) issues" >> "$LOG"
# 4. 报警逻辑(异常时飞书,正常时静默)
ALERT_TEXT="${MEM_ALERT}${DB_ALERT}"
if [ -n "$ALERT_TEXT" ]; then
# 写新告警
echo "[$TS] $ALERT_TEXT" > "$ALERT_FILE"
# 30 分钟去重
PREV=$(cat "$ALERT_FILE.prev" 2>/dev/null || echo "")
if [ "$PREV" != "$ALERT_TEXT" ]; then
echo "$ALERT_TEXT" > "$ALERT_FILE.prev"
# 调用飞书
/home/muc/.hermes/scripts/feishu-alert.sh "🔴 DB/内存异常\n$ALERT_TEXT"
fi
else
# 健康时清空告警
[ -f "$ALERT_FILE" ] && rm "$ALERT_FILE"
[ -f "$ALERT_FILE.prev" ] && rm "$ALERT_FILE.prev"
fi