#!/bin/bash # 端到端验证 Bark 告警:临时加一个必然失败的监控 → 触发 DOWN → 看推送是否送达 → 清理 # 2026-09-13 DB=/app/data/kuma.db Q() { sudo docker exec uptime-kuma sqlite3 "$DB" "$1"; } KCONF=/data/uptime-kuma/kuma.db TS=$(date +%Y%m%d%H%M%S) sudo cp "$KCONF" "/tmp/kuma.db.backup.$TS" echo "=== 1. 新增临时监控(指向必然连不上的端口) ===" Q "INSERT INTO monitor (name, active, user_id, type, url, interval, retry_interval, maxretries, weight, accepted_statuscodes_json, description) VALUES ('自检 · 临时(验证后自动删除)', 1, 1, 'http', 'http://127.0.0.1:9/', 20, 20, 0, 9999, '[\"200-399\"]', 'Bark 通道端到端验证');" TMPID=$(Q "SELECT id FROM monitor WHERE name='自检 · 临时(验证后自动删除)' LIMIT 1;") NID=$(Q "SELECT id FROM notification WHERE name='Bark 手机推送' LIMIT 1;") echo "temp_monitor_id=$TMPID notification_id=$NID" Q "INSERT INTO monitor_notification (monitor_id, notification_id) VALUES ($TMPID, $NID);" echo "" echo "=== 2. 重启 Kuma 让它加载临时监控并开始探测 ===" sudo docker restart uptime-kuma > /dev/null sleep 20 sudo docker ps --filter name=uptime-kuma --format '{{.Names}} | {{.Status}}' echo "" echo "=== 3. 等待探测与告警推送(约 60 秒) ===" for i in 1 2 3 4 5 6; do sleep 15 ST=$(Q "SELECT status FROM heartbeat WHERE monitor_id=$TMPID ORDER BY id DESC LIMIT 1;" 2>/dev/null) echo " [${i}] 第 $((i*15))s:heartbeat status=${ST:-无}" if [ "$ST" = "0" ]; then break; fi done echo "" echo "=== 4. 该临时监控的心跳记录(0=DOWN) ===" Q "SELECT id, status, msg, time FROM heartbeat WHERE monitor_id=$TMPID ORDER BY id DESC LIMIT 5;" echo "" echo "=== 5. 通知发送历史 ===" Q "SELECT name FROM sqlite_master WHERE type='table' AND name LIKE '%notification%';" Q "SELECT * FROM notification_sent_history ORDER BY id DESC LIMIT 5;" 2>/dev/null || echo "(无该表或为空)" echo "" echo "=== 6. Bark 服务端日志(能看到实际收到的推送) ===" sudo docker logs --tail 25 bark 2>&1 | tail -25 echo "" echo "=== 7. Kuma 日志中的通知相关行 ===" sudo docker logs --tail 60 uptime-kuma 2>&1 | grep -i -E "bark|notification|error" | tail -15 || echo "(无)" echo "" echo "=== 8. 清理临时监控 ===" Q "DELETE FROM heartbeat WHERE monitor_id=$TMPID;" Q "DELETE FROM monitor_notification WHERE monitor_id=$TMPID;" Q "DELETE FROM monitor WHERE id=$TMPID;" echo "--- 清理后剩余监控 ---" Q "SELECT id, name FROM monitor ORDER BY id;" echo "--- 清理后关联数(应为 9) ---" Q "SELECT COUNT(*) FROM monitor_notification;" echo "" echo "=== 9. 重启并复核 ===" sudo docker restart uptime-kuma > /dev/null sleep 15 sudo docker ps --filter name=uptime-kuma --format '{{.Names}} | {{.Status}}' curl -s -o /dev/null -m 10 -w "panel_http=%{http_code}\n" http://127.0.0.1:3001/ echo "DONE"