workbuddy.xpcool.com/.workbuddy/tmp/fix-service-monitor.sh

44 lines
1.6 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.

#!/bin/bash
# 修正 service.xpcool.com 监控的判定规则
# 背景:该后端为纯 POST 接口,任何 GET 都返回 404404 说明 nginx+GoFrame 存活,
# 真正故障(后端进程挂掉)时 nginx 会返回 502故把 404 纳入可接受状态码。
set -e
echo "===== update monitor #2 ====="
sudo python3 - <<'PY'
import sqlite3
con = sqlite3.connect("/data/uptime-kuma/kuma.db", timeout=30)
cur = con.cursor()
cur.execute(
"update monitor set accepted_statuscodes_json = ?, description = ? where id = 2",
('["200-399","404"]', "Go 后端服务无根路由404 即存活502/超时才算故障)"),
)
con.commit()
row = cur.execute("select id, name, accepted_statuscodes_json from monitor where id = 2").fetchone()
print("updated:", row[0], row[2])
con.close()
PY
echo "===== restart to reload ====="
sudo docker restart uptime-kuma >/dev/null
sleep 55
echo "===== final status ====="
sudo python3 - <<'PY'
import sqlite3
con = sqlite3.connect("file:/data/uptime-kuma/kuma.db?mode=ro", uri=True)
label = {0: "DOWN", 1: "UP", 2: "PENDING", 3: "MAINTENANCE"}
q = """
select m.id, m.name, m.interval, m.accepted_statuscodes_json, h.status, h.msg
from monitor m
left join heartbeat h on h.id = (select max(id) from heartbeat where monitor_id = m.id)
order by m.weight, m.id
"""
for mid, name, interval, codes, status, msg in con.execute(q):
print(" id=%s %-4ss [%-11s] %-34s %s" % (mid, interval, label.get(status, status), name[:34], (msg or "")[:40]))
print()
print("heartbeat total:", con.execute("select count(*) from heartbeat").fetchone()[0])
PY
sudo docker ps --filter name=uptime-kuma --format '{{.Names}} {{.Status}}'
echo DONE