#!/bin/bash # 批量添加 Uptime Kuma 监控(2026-09-13 第一批) # 说明:Uptime Kuma 官方 REST API 不支持创建监控,这里直接写入 SQLite(字段均有默认值), # 写完后重启容器,服务端会从 monitor 表重新加载并开始探测。 set -e DB=/data/uptime-kuma/kuma.db echo "===== insert monitors =====" sudo python3 - <<'PY' import sqlite3, shutil, datetime DB = "/data/uptime-kuma/kuma.db" USER_ID = 1 # 先备份数据库(含 WAL),便于回滚 stamp = datetime.datetime.now().strftime("%Y%m%d%H%M") for ext in ("", "-wal", "-shm"): try: shutil.copy(DB + ext, "/tmp/kuma.db.backup.%s%s" % (stamp, ext)) except Exception: pass print("backup -> /tmp/kuma.db.backup.%s" % stamp) # (名称, URL, 探测间隔秒, 最大重试, 排序权重(越小越前), 描述) MONITORS = [ ("管理后台 · admin.xpcool.com", "https://admin.xpcool.com", 60, 3, 1000, "Vue 管理后台前端"), ("后端服务 · service.xpcool.com", "https://service.xpcool.com", 60, 3, 1000, "Go 后端服务"), ("代码托管 · git.xpcool.com", "https://git.xpcool.com", 60, 3, 1000, "Gitea 代码托管"), ("文件下载 · file.xpcool.com", "https://file.xpcool.com", 120, 2, 2000, "FileBrowser Quantum 只读下载"), ("阅读 · read.xpcool.com", "https://read.xpcool.com", 120, 2, 2000, "legado 自托管阅读"), ("工具站 · tool.xpcool.com", "https://tool.xpcool.com", 120, 2, 2000, "前端工具站"), ("sub2api · sub2api.xpcool.com", "https://sub2api.xpcool.com", 120, 2, 2000, "sub2api 服务"), ("网页 IDE · code.xpcool.com", "https://code.xpcool.com", 300, 2, 3000, "code-server 网页 IDE"), ("监控面板 · status.xpcool.com", "https://status.xpcool.com", 300, 2, 3000, "Uptime Kuma 自身可用性"), ] con = sqlite3.connect(DB, timeout=30) cur = con.cursor() existing = {r[0] for r in cur.execute("select name from monitor")} added = 0 for name, url, interval, retries, weight, desc in MONITORS: if name in existing: print("SKIP exists:", name) continue cur.execute( "INSERT INTO monitor (" " name, active, user_id, interval, retry_interval, maxretries, url, type, method," " weight, description, accepted_statuscodes_json, expiry_notification," " domain_expiry_notification, upside_down, maxredirects, invert_keyword" ") VALUES (?, 1, ?, ?, ?, ?, ?, 'http', 'GET', ?, ?, ?, 1, 0, 0, 10, 0)", (name, USER_ID, interval, interval, retries, url, weight, desc, '["200-399"]'), ) print("ADD:", name, "-> id", cur.lastrowid) added += 1 con.commit() print("added =", added, " total =", cur.execute("select count(*) from monitor").fetchone()[0]) con.close() PY echo "===== restart uptime-kuma (reload monitors) =====" sudo docker restart uptime-kuma >/dev/null sleep 50 echo "===== verify =====" sudo python3 - <<'PY' import sqlite3 con = sqlite3.connect("file:/data/uptime-kuma/kuma.db?mode=ro", uri=True) print("monitors:") for row in con.execute("select id, name, type, interval, active from monitor order by weight, id"): print(" ", row) print("heartbeat rows:", con.execute("select count(*) from heartbeat").fetchone()[0]) print("latest beats (monitor_id, status, time):") for row in con.execute("select monitor_id, status, time from heartbeat order by id desc limit 15"): print(" ", row) PY echo "===== container =====" sudo docker ps --filter name=uptime-kuma --format '{{.Names}} {{.Status}}' echo DONE