Latest commit

History

History
357 lines (275 loc) · 12.4 KB

File metadata and controls

357 lines (275 loc) · 12.4 KB

ServerStatus 中文版

ServerStatus 是一个轻量的服务器探针和云监控面板,支持多节点在线状态、资源占用、三网延迟、服务监测、SSL 证书检查、Watchdog 告警、HTTP API 和 Web 配置管理。

在线演示:https://tz.cloudcpp.com

GoCILicenseVersion

ServerStatus WebUI

一、服务端

# Docker Compose,本地构建加:--build
ADMIN_TOKEN='your-strong-token' docker compose -f docker-compose-server.yml up -d 
# Docker Run
wget -qO ~/serverstatus-config.json \
--header='Accept: application/vnd.github.raw' \
'https://api.github.com/repos/cppla/ServerStatus/contents/server/config.json?ref=master'
mkdir -p ~/serverstatus-data
docker run -d --restart=always --name=serverstatus-server \
-e ADMIN_TOKEN='your-strong-token' \
-v ~/serverstatus-config.json:/app/config/config.json \
-v ~/serverstatus-data:/app/data \
-p 8080:80 -p 35601:35601 \
cppla/serverstatus:server

启动后访问:

ADMIN_TOKEN 不设置时,监控页面仍可读取,管理 API 返回 503,WebUI 的“配置”页不能修改数据。

二、客户端

# Docker Compose,本地构建加:--build
SERVER=127.0.0.1 USER=s01 PASSWORD=USER_DEFAULT_PASSWORD \
docker compose -f docker-compose-client.yml up -d --force-recreate
# Docker Run
docker run -d --restart=always --name=serverstatus-client \
--network=host --pid=host \
-e SERVER=127.0.0.1 \
-e USER=s01 \
-e PASSWORD=USER_DEFAULT_PASSWORD \
cppla/serverstatus:client
# Shell Run
wget -qO client-linux.py --header='Accept: application/vnd.github.raw' \
'https://api.github.com/repos/cppla/ServerStatus/contents/clients/client-linux.py?ref=master'
nohup python3 client-linux.py SERVER=127.0.0.1 USER=s01 PASSWORD=USER_DEFAULT_PASSWORD >/dev/null 2>&1&

USER 是常见的宿主机环境变量名。如果没有显式传递或传递方式错误,Compose 可能会把系统中的 $USER 解析成本机用户名,而不是默认的 s01。推荐优先级:

  1. 运行命令显式传递 USER=s01
  2. 修改 docker-compose-client.yml 中的 USER 默认值
  3. Docker 或系统环境中的 USER
变量默认值说明
SERVER127.0.0.1Go 服务端地址
USERs01客户端用户名,必须匹配服务端配置
PORT35601Agent TCP 上报端口
PASSWORDUSER_DEFAULT_PASSWORD客户端密码,必须匹配服务端配置
INTERVAL1状态上报间隔,单位秒
PROBEPORT80三网 TCP 探测端口
PROBE_PROTOCOL_PREFERipv4探测协议偏好,可选 ipv4ipv6
PING_PACKET_HISTORY_LEN100丢包历史窗口
CUcu.tz.cloudcpp.com联通探测地址
CTct.tz.cloudcpp.com电信探测地址
CMcm.tz.cloudcpp.com移动探测地址
CLIENTpsutil客户端实现,可选 psutillinux

服务端参数

Docker 镜像中的默认路径为:

环境变量默认值说明
CONFIG_PATH/app/config/config.json主配置文件
STATS_PATH/app/data/stats.json月流量与状态持久化文件;每 60 秒写入,关键操作与正常退出时立即写入
WEB_DIR/app/webWebUI 静态文件目录
HTTP_ADDR:80WebUI 与 HTTP API 监听地址
AGENT_ADDR:35601客户端 TCP 上报监听地址
ADMIN_TOKEN管理 API Bearer Token;为空时禁用管理接口
ADMIN_CORS_ORIGIN可选的 API CORS Origin
INSECURE_CALLBACK_TLSfalse是否允许 Watchdog/证书回调使用不可信 TLS 证书
VERBOSEfalse输出 Gin HTTP 请求日志
TZAsia/Shanghai容器时区

对应命令行参数:

--config, -c config.json 路径
--stats stats.json 路径
--web-dir, -d WebUI 目录
--http HTTP 监听地址
--agent Agent TCP 监听地址
--verbose, -v 详细请求日志
--version 输出构建版本

旧参数 --bind/-b--port/-p 仍可用于设置 Agent TCP 监听地址。

HTTP 管理 API

管理接口使用 Bearer Token:

Authorization: Bearer <ADMIN_TOKEN>

无需认证:

方法路径用途
GET/api/health进程、Agent TCP、版本和配置路径状态
GET/api/schema机器可读的端点与配置集合描述
GET/api/openapi.json可供 AI Agent 直接导入的 OpenAPI 3.1 文档
GET/json/stats.jsonWebUI 使用的实时状态快照

需要认证:

方法路径用途
GET/PUT/api/config读取或整体替换配置
GET/POST/api/servers查询或新增节点
PUT/DELETE/api/servers/{username}修改或删除节点
POST/api/servers/{username}/reset-traffic将当前流量设为本月基线
GET/POST/api/monitors查询或新增服务监测
PUT/DELETE/api/monitors/{index-or-name}修改或删除服务监测
GET/POST/api/sslcerts查询或新增证书监测
PUT/DELETE/api/sslcerts/{index-or-name}修改或删除证书监测
GET/POST/api/watchdog查询或新增 Watchdog
PUT/DELETE/api/watchdog/{index-or-name}修改或删除 Watchdog
POST/api/reload从磁盘重新读取配置
POST/api/restart在进程内重启采集运行时

配置修改采用“校验 → 备份 → 持久化 → 原子切换”的顺序。成功后现有 Agent 连接会被关闭,Python 客户端约 3 秒后自动重连并获取新的 monitors/api/restart 不退出 Go 进程,因此 Docker 和手动运行方式具有一致语义。

常用调用:

TOKEN='请替换为 ADMIN_TOKEN'
curl http://127.0.0.1:8080/api/health
curl -H "Authorization: Bearer ${TOKEN}" \
http://127.0.0.1:8080/api/config
curl -X POST http://127.0.0.1:8080/api/servers \
-H "Authorization: Bearer ${TOKEN}" \
-H 'Content-Type: application/json' \
-d '{"username":"s05","name":"node5","type":"kvm","host":"host5","location":"SG","password":"change-me","monthstart":1}'
curl -X DELETE \
-H "Authorization: Bearer ${TOKEN}" \
http://127.0.0.1:8080/api/servers/s05

请求体最大为 1 MiB。AI Agent 可直接导入 /api/openapi.json;轻量客户端也可以先读取 /api/schema,再根据返回的集合字段调用 CRUD 接口。

配置文件

{
"servers": [
{
"username": "s01",
"name": "node1",
"type": "kvm",
"host": "host1",
"location": "CN",
"password": "USER_DEFAULT_PASSWORD",
"monthstart": 1,
"disabled": false
}
],
"monitors": [
{
"name": "example",
"host": "https://example.com",
"interval": 600,
"type": "https"
}
],
"sslcerts": [
{
"name": "example",
"domain": "https://example.com",
"port": 443,
"interval": 7200,
"callback": "https://example.net/push?message="
}
],
"watchdog": [
{
"name": "offline warning",
"rule": "online4=0&online6=0",
"interval": 600,
"callback": "https://example.net/push?message="
}
]
}

约束:

  • servers.username 必须唯一。
  • monthstart 自动限制在 1-28
  • port 自动限制在 1-65535
  • interval 最小为 1 秒;Watchdog 中表示通知冷却时间,不是客户端采集间隔。
  • 配置写入前会创建 config.json.bak-*,最多保留 10 份。
  • Docker 单文件 bind mount 无法被 rename 覆盖时,服务端会在完成备份后安全地写回原 inode。

使用 Docker 单文件挂载时,配置备份位于容器 /app/config 的可写层;如需长期保留历史版本,建议同时在宿主机备份 server/config.json

Watchdog 表达式

rule 由 Go expr 引擎执行,并兼容旧版 Exprtk 的常用写法。Go 服务会把字符串外的单个操作符自动转换:

& -> &&
| -> ||
= -> ==

例如以下两种写法等价:

cpu>90&load_1>5&username!='s01'
cpu>90 && load_1>5 && username!='s01'

字符串值支持中文、Emoji 和其他 Unicode 字符,例如:

username='节点一号'&name='上海节点'&location='中国 🇨🇳'&type='云主机'

字段名必须使用系统定义的英文名称。可用字段包括:usernamenametypehostlocationload_1load_5load_15cpumemory_totalmemory_usedswap_totalswap_usedhdd_totalhdd_usednetwork_rxnetwork_txnetwork_innetwork_outlast_network_inlast_network_outping_10010ping_189ping_10086time_10010time_189time_10086tcp_countudp_countprocess_countthread_countio_readio_writeonline4online6

客户端断开 25 秒后仍未重连,服务端才计算离线规则,避免短暂网络波动触发告警。每个节点、每条规则分别记录冷却时间。

SSL 证书

证书检查使用 Go crypto/tls,不再调用外部 openssl。服务端记录到期时间、剩余天数和域名匹配状态,并保留原来的 7/3/1 天通知档位与冷却时间。

回调默认校验 HTTPS 证书。仅在必须兼容自签名回调服务时设置:

INSECURE_CALLBACK_TLS=true

源码编译和运行

需要 Go 1.25 或更高版本:

cd server
go mod download
go test ./...
go build -trimpath -ldflags='-s -w' -o serverstatus .

server/ 目录启动:

ADMIN_TOKEN='请替换为高强度随机字符串' \
./serverstatus \
--config=config.json \
--stats=../web/json/stats.json \
--web-dir=../web \
--http=:8080 \
--agent=:35601

访问 http://127.0.0.1:8080/。发送SIGHUP 可以重新读取配置:

kill -HUP "$(pgrep -x serverstatus)"

Systemd 示例位于 service/status-server.service。一键脚本 status.sh 也已切换到 Go 构建,但 Docker 仍是推荐部署方式。

构建和测试

Go 测试需要 Go 1.25+;WebUI 行为测试需要 Node.js 20+ 和 pnpm。

# Go 单元、协议、API、TLS 和回调测试cd server
go test ./...
go test -race ./...
go vet ./...
# Python 客户端指标、参数与平台识别测试cd ..
python3 -m unittest discover -s clients -p 'test_*.py'# WebUI Chromium 行为测试
pnpm install --frozen-lockfile
pnpm exec playwright install chromium
pnpm test:webui
# Docker 镜像
docker build -f Dockerfile.server -t cppla/serverstatus:server .
docker build -f Dockerfile.client -t cppla/serverstatus:client .# 服务端与客户端真实连接、认证和指标上报
SERVER_IMAGE=cppla/serverstatus:server \
CLIENT_IMAGE=cppla/serverstatus:client \
tests/docker-smoke.sh
# Compose 配置
docker compose -f docker-compose-server.yml config
docker compose -f docker-compose-client.yml config

Docker 联通测试在 Linux 上覆盖客户端的 host 网络和 pid 模式;Docker Desktop 未启用 Host Networking 时会明确提示并使用隔离 bridge 完成本地协议测试。

CI 还会运行 Chromium 行为测试、Docker 服务端/客户端联通测试,并验证客户端镜像可同时构建为 AMD64 和 ARM64。

从旧服务端迁移

  1. 备份原来的 config.jsonweb/json/stats.json
  2. 原配置结构和客户端账号无需转换。
  3. Docker 挂载目标改为 /app/config/config.json/app/data
  4. 删除旧的 nginx、manage_api.pysergate 启动或监督配置。
  5. 启动 Go 服务端后检查 /api/health,再观察客户端自动重连。

stats.json 会按节点的 name/type/host/location 恢复月流量基线。修改这些身份字段会被视为新节点。

致谢

, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Add copy buttons to all
 blocks\n(function() {\n function addCopyButtons() {\n document.querySelectorAll('pre code').forEach(function(codeBlock) {\n if (codeBlock.parentElement.hasAttribute('data-copy-added')) return;\n codeBlock.parentElement.setAttribute('data-copy-added', 'true');\n \n var btn = document.createElement('button');\n btn.textContent = 'Copy';\n btn.style.cssText = 'position:absolute;top:4px;right:4px;padding:2px 8px;font-size:11px;background:#4ecdc4;border:none;border-radius:4px;color:#1a1a2e;cursor:pointer;opacity:0.7;transition:opacity 0.2s;';\n btn.onmouseover = function() { this.style.opacity = '1'; };\n btn.onmouseout = function() { this.style.opacity = '0.7'; };\n btn.onclick = function() {\n navigator.clipboard.writeText(codeBlock.textContent).then(function() {\n btn.textContent = 'Copied!';\n setTimeout(function() { btn.textContent = 'Copy'; }, 1500);\n });\n };\n codeBlock.parentElement.style.position = 'relative';\n codeBlock.parentElement.appendChild(btn);\n });\n }\n \n addCopyButtons();\n \n // Re-run on dynamic content\n var observer = new MutationObserver(addCopyButtons);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Add Copy Buttons to Code Blocks");
}
} catch(__e) { console.warn('[Userscript:Add Copy Buttons to Code Blocks]', __e); }
})();
(function(){
try {
var __m = "github.com";
var __re = new RegExp('^' + "github\\.com" + '
Skip to content

Latest commit

History

History
357 lines (275 loc) · 12.4 KB

File metadata and controls

357 lines (275 loc) · 12.4 KB

ServerStatus 中文版

ServerStatus 是一个轻量的服务器探针和云监控面板,支持多节点在线状态、资源占用、三网延迟、服务监测、SSL 证书检查、Watchdog 告警、HTTP API 和 Web 配置管理。

在线演示:https://tz.cloudcpp.com

GoCILicenseVersion

ServerStatus WebUI

一、服务端

# Docker Compose,本地构建加:--build
ADMIN_TOKEN='your-strong-token' docker compose -f docker-compose-server.yml up -d 
# Docker Run
wget -qO ~/serverstatus-config.json \
--header='Accept: application/vnd.github.raw' \
'https://api.github.com/repos/cppla/ServerStatus/contents/server/config.json?ref=master'
mkdir -p ~/serverstatus-data
docker run -d --restart=always --name=serverstatus-server \
-e ADMIN_TOKEN='your-strong-token' \
-v ~/serverstatus-config.json:/app/config/config.json \
-v ~/serverstatus-data:/app/data \
-p 8080:80 -p 35601:35601 \
cppla/serverstatus:server

启动后访问:

ADMIN_TOKEN 不设置时,监控页面仍可读取,管理 API 返回 503,WebUI 的“配置”页不能修改数据。

二、客户端

# Docker Compose,本地构建加:--build
SERVER=127.0.0.1 USER=s01 PASSWORD=USER_DEFAULT_PASSWORD \
docker compose -f docker-compose-client.yml up -d --force-recreate
# Docker Run
docker run -d --restart=always --name=serverstatus-client \
--network=host --pid=host \
-e SERVER=127.0.0.1 \
-e USER=s01 \
-e PASSWORD=USER_DEFAULT_PASSWORD \
cppla/serverstatus:client
# Shell Run
wget -qO client-linux.py --header='Accept: application/vnd.github.raw' \
'https://api.github.com/repos/cppla/ServerStatus/contents/clients/client-linux.py?ref=master'
nohup python3 client-linux.py SERVER=127.0.0.1 USER=s01 PASSWORD=USER_DEFAULT_PASSWORD >/dev/null 2>&1&

USER 是常见的宿主机环境变量名。如果没有显式传递或传递方式错误,Compose 可能会把系统中的 $USER 解析成本机用户名,而不是默认的 s01。推荐优先级:

  1. 运行命令显式传递 USER=s01
  2. 修改 docker-compose-client.yml 中的 USER 默认值
  3. Docker 或系统环境中的 USER
变量默认值说明
SERVER127.0.0.1Go 服务端地址
USERs01客户端用户名,必须匹配服务端配置
PORT35601Agent TCP 上报端口
PASSWORDUSER_DEFAULT_PASSWORD客户端密码,必须匹配服务端配置
INTERVAL1状态上报间隔,单位秒
PROBEPORT80三网 TCP 探测端口
PROBE_PROTOCOL_PREFERipv4探测协议偏好,可选 ipv4ipv6
PING_PACKET_HISTORY_LEN100丢包历史窗口
CUcu.tz.cloudcpp.com联通探测地址
CTct.tz.cloudcpp.com电信探测地址
CMcm.tz.cloudcpp.com移动探测地址
CLIENTpsutil客户端实现,可选 psutillinux

服务端参数

Docker 镜像中的默认路径为:

环境变量默认值说明
CONFIG_PATH/app/config/config.json主配置文件
STATS_PATH/app/data/stats.json月流量与状态持久化文件;每 60 秒写入,关键操作与正常退出时立即写入
WEB_DIR/app/webWebUI 静态文件目录
HTTP_ADDR:80WebUI 与 HTTP API 监听地址
AGENT_ADDR:35601客户端 TCP 上报监听地址
ADMIN_TOKEN管理 API Bearer Token;为空时禁用管理接口
ADMIN_CORS_ORIGIN可选的 API CORS Origin
INSECURE_CALLBACK_TLSfalse是否允许 Watchdog/证书回调使用不可信 TLS 证书
VERBOSEfalse输出 Gin HTTP 请求日志
TZAsia/Shanghai容器时区

对应命令行参数:

--config, -c config.json 路径
--stats stats.json 路径
--web-dir, -d WebUI 目录
--http HTTP 监听地址
--agent Agent TCP 监听地址
--verbose, -v 详细请求日志
--version 输出构建版本

旧参数 --bind/-b--port/-p 仍可用于设置 Agent TCP 监听地址。

HTTP 管理 API

管理接口使用 Bearer Token:

Authorization: Bearer <ADMIN_TOKEN>

无需认证:

方法路径用途
GET/api/health进程、Agent TCP、版本和配置路径状态
GET/api/schema机器可读的端点与配置集合描述
GET/api/openapi.json可供 AI Agent 直接导入的 OpenAPI 3.1 文档
GET/json/stats.jsonWebUI 使用的实时状态快照

需要认证:

方法路径用途
GET/PUT/api/config读取或整体替换配置
GET/POST/api/servers查询或新增节点
PUT/DELETE/api/servers/{username}修改或删除节点
POST/api/servers/{username}/reset-traffic将当前流量设为本月基线
GET/POST/api/monitors查询或新增服务监测
PUT/DELETE/api/monitors/{index-or-name}修改或删除服务监测
GET/POST/api/sslcerts查询或新增证书监测
PUT/DELETE/api/sslcerts/{index-or-name}修改或删除证书监测
GET/POST/api/watchdog查询或新增 Watchdog
PUT/DELETE/api/watchdog/{index-or-name}修改或删除 Watchdog
POST/api/reload从磁盘重新读取配置
POST/api/restart在进程内重启采集运行时

配置修改采用“校验 → 备份 → 持久化 → 原子切换”的顺序。成功后现有 Agent 连接会被关闭,Python 客户端约 3 秒后自动重连并获取新的 monitors/api/restart 不退出 Go 进程,因此 Docker 和手动运行方式具有一致语义。

常用调用:

TOKEN='请替换为 ADMIN_TOKEN'
curl http://127.0.0.1:8080/api/health
curl -H "Authorization: Bearer ${TOKEN}" \
http://127.0.0.1:8080/api/config
curl -X POST http://127.0.0.1:8080/api/servers \
-H "Authorization: Bearer ${TOKEN}" \
-H 'Content-Type: application/json' \
-d '{"username":"s05","name":"node5","type":"kvm","host":"host5","location":"SG","password":"change-me","monthstart":1}'
curl -X DELETE \
-H "Authorization: Bearer ${TOKEN}" \
http://127.0.0.1:8080/api/servers/s05

请求体最大为 1 MiB。AI Agent 可直接导入 /api/openapi.json;轻量客户端也可以先读取 /api/schema,再根据返回的集合字段调用 CRUD 接口。

配置文件

{
"servers": [
{
"username": "s01",
"name": "node1",
"type": "kvm",
"host": "host1",
"location": "CN",
"password": "USER_DEFAULT_PASSWORD",
"monthstart": 1,
"disabled": false
}
],
"monitors": [
{
"name": "example",
"host": "https://example.com",
"interval": 600,
"type": "https"
}
],
"sslcerts": [
{
"name": "example",
"domain": "https://example.com",
"port": 443,
"interval": 7200,
"callback": "https://example.net/push?message="
}
],
"watchdog": [
{
"name": "offline warning",
"rule": "online4=0&online6=0",
"interval": 600,
"callback": "https://example.net/push?message="
}
]
}

约束:

  • servers.username 必须唯一。
  • monthstart 自动限制在 1-28
  • port 自动限制在 1-65535
  • interval 最小为 1 秒;Watchdog 中表示通知冷却时间,不是客户端采集间隔。
  • 配置写入前会创建 config.json.bak-*,最多保留 10 份。
  • Docker 单文件 bind mount 无法被 rename 覆盖时,服务端会在完成备份后安全地写回原 inode。

使用 Docker 单文件挂载时,配置备份位于容器 /app/config 的可写层;如需长期保留历史版本,建议同时在宿主机备份 server/config.json

Watchdog 表达式

rule 由 Go expr 引擎执行,并兼容旧版 Exprtk 的常用写法。Go 服务会把字符串外的单个操作符自动转换:

& -> &&
| -> ||
= -> ==

例如以下两种写法等价:

cpu>90&load_1>5&username!='s01'
cpu>90 && load_1>5 && username!='s01'

字符串值支持中文、Emoji 和其他 Unicode 字符,例如:

username='节点一号'&name='上海节点'&location='中国 🇨🇳'&type='云主机'

字段名必须使用系统定义的英文名称。可用字段包括:usernamenametypehostlocationload_1load_5load_15cpumemory_totalmemory_usedswap_totalswap_usedhdd_totalhdd_usednetwork_rxnetwork_txnetwork_innetwork_outlast_network_inlast_network_outping_10010ping_189ping_10086time_10010time_189time_10086tcp_countudp_countprocess_countthread_countio_readio_writeonline4online6

客户端断开 25 秒后仍未重连,服务端才计算离线规则,避免短暂网络波动触发告警。每个节点、每条规则分别记录冷却时间。

SSL 证书

证书检查使用 Go crypto/tls,不再调用外部 openssl。服务端记录到期时间、剩余天数和域名匹配状态,并保留原来的 7/3/1 天通知档位与冷却时间。

回调默认校验 HTTPS 证书。仅在必须兼容自签名回调服务时设置:

INSECURE_CALLBACK_TLS=true

源码编译和运行

需要 Go 1.25 或更高版本:

cd server
go mod download
go test ./...
go build -trimpath -ldflags='-s -w' -o serverstatus .

server/ 目录启动:

ADMIN_TOKEN='请替换为高强度随机字符串' \
./serverstatus \
--config=config.json \
--stats=../web/json/stats.json \
--web-dir=../web \
--http=:8080 \
--agent=:35601

访问 http://127.0.0.1:8080/。发送SIGHUP 可以重新读取配置:

kill -HUP "$(pgrep -x serverstatus)"

Systemd 示例位于 service/status-server.service。一键脚本 status.sh 也已切换到 Go 构建,但 Docker 仍是推荐部署方式。

构建和测试

Go 测试需要 Go 1.25+;WebUI 行为测试需要 Node.js 20+ 和 pnpm。

# Go 单元、协议、API、TLS 和回调测试cd server
go test ./...
go test -race ./...
go vet ./...
# Python 客户端指标、参数与平台识别测试cd ..
python3 -m unittest discover -s clients -p 'test_*.py'# WebUI Chromium 行为测试
pnpm install --frozen-lockfile
pnpm exec playwright install chromium
pnpm test:webui
# Docker 镜像
docker build -f Dockerfile.server -t cppla/serverstatus:server .
docker build -f Dockerfile.client -t cppla/serverstatus:client .# 服务端与客户端真实连接、认证和指标上报
SERVER_IMAGE=cppla/serverstatus:server \
CLIENT_IMAGE=cppla/serverstatus:client \
tests/docker-smoke.sh
# Compose 配置
docker compose -f docker-compose-server.yml config
docker compose -f docker-compose-client.yml config

Docker 联通测试在 Linux 上覆盖客户端的 host 网络和 pid 模式;Docker Desktop 未启用 Host Networking 时会明确提示并使用隔离 bridge 完成本地协议测试。

CI 还会运行 Chromium 行为测试、Docker 服务端/客户端联通测试,并验证客户端镜像可同时构建为 AMD64 和 ARM64。

从旧服务端迁移

  1. 备份原来的 config.jsonweb/json/stats.json
  2. 原配置结构和客户端账号无需转换。
  3. Docker 挂载目标改为 /app/config/config.json/app/data
  4. 删除旧的 nginx、manage_api.pysergate 启动或监督配置。
  5. 启动 Go 服务端后检查 /api/health,再观察客户端自动重连。

stats.json 会按节点的 name/type/host/location 恢复月流量基线。修改这些身份字段会被视为新节点。

致谢

, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Force GitHub README to respect dark mode\n(function() {\n var style = document.createElement('style');\n style.textContent = '\n .markdown-body {\n color-scheme: dark light;\n }\n .markdown-body pre { background: #161b22 !important; }\n .markdown-body code { background: rgba(110, 118, 129, 0.4) !important; }\n .markdown-body table th, .markdown-body table td { border-color: #30363d !important; }\n .markdown-body img { background: #0d1117; }\n .markdown-body blockquote { border-left-color: #8b949e; }\n .markdown-body hr { border-color: #30363d; }\n ';\n document.head.appendChild(style);\n})();", "GitHub Dark Mode README Fix"); } } catch(__e) { console.warn('[Userscript:GitHub Dark Mode README Fix]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Latest commit

History

History
357 lines (275 loc) · 12.4 KB

File metadata and controls

357 lines (275 loc) · 12.4 KB

ServerStatus 中文版

ServerStatus 是一个轻量的服务器探针和云监控面板,支持多节点在线状态、资源占用、三网延迟、服务监测、SSL 证书检查、Watchdog 告警、HTTP API 和 Web 配置管理。

在线演示:https://tz.cloudcpp.com

GoCILicenseVersion

ServerStatus WebUI

一、服务端

# Docker Compose,本地构建加:--build
ADMIN_TOKEN='your-strong-token' docker compose -f docker-compose-server.yml up -d 
# Docker Run
wget -qO ~/serverstatus-config.json \
--header='Accept: application/vnd.github.raw' \
'https://api.github.com/repos/cppla/ServerStatus/contents/server/config.json?ref=master'
mkdir -p ~/serverstatus-data
docker run -d --restart=always --name=serverstatus-server \
-e ADMIN_TOKEN='your-strong-token' \
-v ~/serverstatus-config.json:/app/config/config.json \
-v ~/serverstatus-data:/app/data \
-p 8080:80 -p 35601:35601 \
cppla/serverstatus:server

启动后访问:

ADMIN_TOKEN 不设置时,监控页面仍可读取,管理 API 返回 503,WebUI 的“配置”页不能修改数据。

二、客户端

# Docker Compose,本地构建加:--build
SERVER=127.0.0.1 USER=s01 PASSWORD=USER_DEFAULT_PASSWORD \
docker compose -f docker-compose-client.yml up -d --force-recreate
# Docker Run
docker run -d --restart=always --name=serverstatus-client \
--network=host --pid=host \
-e SERVER=127.0.0.1 \
-e USER=s01 \
-e PASSWORD=USER_DEFAULT_PASSWORD \
cppla/serverstatus:client
# Shell Run
wget -qO client-linux.py --header='Accept: application/vnd.github.raw' \
'https://api.github.com/repos/cppla/ServerStatus/contents/clients/client-linux.py?ref=master'
nohup python3 client-linux.py SERVER=127.0.0.1 USER=s01 PASSWORD=USER_DEFAULT_PASSWORD >/dev/null 2>&1&

USER 是常见的宿主机环境变量名。如果没有显式传递或传递方式错误,Compose 可能会把系统中的 $USER 解析成本机用户名,而不是默认的 s01。推荐优先级:

  1. 运行命令显式传递 USER=s01
  2. 修改 docker-compose-client.yml 中的 USER 默认值
  3. Docker 或系统环境中的 USER
变量默认值说明
SERVER127.0.0.1Go 服务端地址
USERs01客户端用户名,必须匹配服务端配置
PORT35601Agent TCP 上报端口
PASSWORDUSER_DEFAULT_PASSWORD客户端密码,必须匹配服务端配置
INTERVAL1状态上报间隔,单位秒
PROBEPORT80三网 TCP 探测端口
PROBE_PROTOCOL_PREFERipv4探测协议偏好,可选 ipv4ipv6
PING_PACKET_HISTORY_LEN100丢包历史窗口
CUcu.tz.cloudcpp.com联通探测地址
CTct.tz.cloudcpp.com电信探测地址
CMcm.tz.cloudcpp.com移动探测地址
CLIENTpsutil客户端实现,可选 psutillinux

服务端参数

Docker 镜像中的默认路径为:

环境变量默认值说明
CONFIG_PATH/app/config/config.json主配置文件
STATS_PATH/app/data/stats.json月流量与状态持久化文件;每 60 秒写入,关键操作与正常退出时立即写入
WEB_DIR/app/webWebUI 静态文件目录
HTTP_ADDR:80WebUI 与 HTTP API 监听地址
AGENT_ADDR:35601客户端 TCP 上报监听地址
ADMIN_TOKEN管理 API Bearer Token;为空时禁用管理接口
ADMIN_CORS_ORIGIN可选的 API CORS Origin
INSECURE_CALLBACK_TLSfalse是否允许 Watchdog/证书回调使用不可信 TLS 证书
VERBOSEfalse输出 Gin HTTP 请求日志
TZAsia/Shanghai容器时区

对应命令行参数:

--config, -c config.json 路径
--stats stats.json 路径
--web-dir, -d WebUI 目录
--http HTTP 监听地址
--agent Agent TCP 监听地址
--verbose, -v 详细请求日志
--version 输出构建版本

旧参数 --bind/-b--port/-p 仍可用于设置 Agent TCP 监听地址。

HTTP 管理 API

管理接口使用 Bearer Token:

Authorization: Bearer <ADMIN_TOKEN>

无需认证:

方法路径用途
GET/api/health进程、Agent TCP、版本和配置路径状态
GET/api/schema机器可读的端点与配置集合描述
GET/api/openapi.json可供 AI Agent 直接导入的 OpenAPI 3.1 文档
GET/json/stats.jsonWebUI 使用的实时状态快照

需要认证:

方法路径用途
GET/PUT/api/config读取或整体替换配置
GET/POST/api/servers查询或新增节点
PUT/DELETE/api/servers/{username}修改或删除节点
POST/api/servers/{username}/reset-traffic将当前流量设为本月基线
GET/POST/api/monitors查询或新增服务监测
PUT/DELETE/api/monitors/{index-or-name}修改或删除服务监测
GET/POST/api/sslcerts查询或新增证书监测
PUT/DELETE/api/sslcerts/{index-or-name}修改或删除证书监测
GET/POST/api/watchdog查询或新增 Watchdog
PUT/DELETE/api/watchdog/{index-or-name}修改或删除 Watchdog
POST/api/reload从磁盘重新读取配置
POST/api/restart在进程内重启采集运行时

配置修改采用“校验 → 备份 → 持久化 → 原子切换”的顺序。成功后现有 Agent 连接会被关闭,Python 客户端约 3 秒后自动重连并获取新的 monitors/api/restart 不退出 Go 进程,因此 Docker 和手动运行方式具有一致语义。

常用调用:

TOKEN='请替换为 ADMIN_TOKEN'
curl http://127.0.0.1:8080/api/health
curl -H "Authorization: Bearer ${TOKEN}" \
http://127.0.0.1:8080/api/config
curl -X POST http://127.0.0.1:8080/api/servers \
-H "Authorization: Bearer ${TOKEN}" \
-H 'Content-Type: application/json' \
-d '{"username":"s05","name":"node5","type":"kvm","host":"host5","location":"SG","password":"change-me","monthstart":1}'
curl -X DELETE \
-H "Authorization: Bearer ${TOKEN}" \
http://127.0.0.1:8080/api/servers/s05

请求体最大为 1 MiB。AI Agent 可直接导入 /api/openapi.json;轻量客户端也可以先读取 /api/schema,再根据返回的集合字段调用 CRUD 接口。

配置文件

{
"servers": [
{
"username": "s01",
"name": "node1",
"type": "kvm",
"host": "host1",
"location": "CN",
"password": "USER_DEFAULT_PASSWORD",
"monthstart": 1,
"disabled": false
}
],
"monitors": [
{
"name": "example",
"host": "https://example.com",
"interval": 600,
"type": "https"
}
],
"sslcerts": [
{
"name": "example",
"domain": "https://example.com",
"port": 443,
"interval": 7200,
"callback": "https://example.net/push?message="
}
],
"watchdog": [
{
"name": "offline warning",
"rule": "online4=0&online6=0",
"interval": 600,
"callback": "https://example.net/push?message="
}
]
}

约束:

  • servers.username 必须唯一。
  • monthstart 自动限制在 1-28
  • port 自动限制在 1-65535
  • interval 最小为 1 秒;Watchdog 中表示通知冷却时间,不是客户端采集间隔。
  • 配置写入前会创建 config.json.bak-*,最多保留 10 份。
  • Docker 单文件 bind mount 无法被 rename 覆盖时,服务端会在完成备份后安全地写回原 inode。

使用 Docker 单文件挂载时,配置备份位于容器 /app/config 的可写层;如需长期保留历史版本,建议同时在宿主机备份 server/config.json

Watchdog 表达式

rule 由 Go expr 引擎执行,并兼容旧版 Exprtk 的常用写法。Go 服务会把字符串外的单个操作符自动转换:

& -> &&
| -> ||
= -> ==

例如以下两种写法等价:

cpu>90&load_1>5&username!='s01'
cpu>90 && load_1>5 && username!='s01'

字符串值支持中文、Emoji 和其他 Unicode 字符,例如:

username='节点一号'&name='上海节点'&location='中国 🇨🇳'&type='云主机'

字段名必须使用系统定义的英文名称。可用字段包括:usernamenametypehostlocationload_1load_5load_15cpumemory_totalmemory_usedswap_totalswap_usedhdd_totalhdd_usednetwork_rxnetwork_txnetwork_innetwork_outlast_network_inlast_network_outping_10010ping_189ping_10086time_10010time_189time_10086tcp_countudp_countprocess_countthread_countio_readio_writeonline4online6

客户端断开 25 秒后仍未重连,服务端才计算离线规则,避免短暂网络波动触发告警。每个节点、每条规则分别记录冷却时间。

SSL 证书

证书检查使用 Go crypto/tls,不再调用外部 openssl。服务端记录到期时间、剩余天数和域名匹配状态,并保留原来的 7/3/1 天通知档位与冷却时间。

回调默认校验 HTTPS 证书。仅在必须兼容自签名回调服务时设置:

INSECURE_CALLBACK_TLS=true

源码编译和运行

需要 Go 1.25 或更高版本:

cd server
go mod download
go test ./...
go build -trimpath -ldflags='-s -w' -o serverstatus .

server/ 目录启动:

ADMIN_TOKEN='请替换为高强度随机字符串' \
./serverstatus \
--config=config.json \
--stats=../web/json/stats.json \
--web-dir=../web \
--http=:8080 \
--agent=:35601

访问 http://127.0.0.1:8080/。发送SIGHUP 可以重新读取配置:

kill -HUP "$(pgrep -x serverstatus)"

Systemd 示例位于 service/status-server.service。一键脚本 status.sh 也已切换到 Go 构建,但 Docker 仍是推荐部署方式。

构建和测试

Go 测试需要 Go 1.25+;WebUI 行为测试需要 Node.js 20+ 和 pnpm。

# Go 单元、协议、API、TLS 和回调测试cd server
go test ./...
go test -race ./...
go vet ./...
# Python 客户端指标、参数与平台识别测试cd ..
python3 -m unittest discover -s clients -p 'test_*.py'# WebUI Chromium 行为测试
pnpm install --frozen-lockfile
pnpm exec playwright install chromium
pnpm test:webui
# Docker 镜像
docker build -f Dockerfile.server -t cppla/serverstatus:server .
docker build -f Dockerfile.client -t cppla/serverstatus:client .# 服务端与客户端真实连接、认证和指标上报
SERVER_IMAGE=cppla/serverstatus:server \
CLIENT_IMAGE=cppla/serverstatus:client \
tests/docker-smoke.sh
# Compose 配置
docker compose -f docker-compose-server.yml config
docker compose -f docker-compose-client.yml config

Docker 联通测试在 Linux 上覆盖客户端的 host 网络和 pid 模式;Docker Desktop 未启用 Host Networking 时会明确提示并使用隔离 bridge 完成本地协议测试。

CI 还会运行 Chromium 行为测试、Docker 服务端/客户端联通测试,并验证客户端镜像可同时构建为 AMD64 和 ARM64。

从旧服务端迁移

  1. 备份原来的 config.jsonweb/json/stats.json
  2. 原配置结构和客户端账号无需转换。
  3. Docker 挂载目标改为 /app/config/config.json/app/data
  4. 删除旧的 nginx、manage_api.pysergate 启动或监督配置。
  5. 启动 Go 服务端后检查 /api/health,再观察客户端自动重连。

stats.json 会按节点的 name/type/host/location 恢复月流量基线。修改这些身份字段会被视为新节点。

致谢

, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Highlight search terms from Google/DuckDuckGo/Bing referrer\n(function() {\n var ref = document.referrer;\n var terms = [];\n \n if (ref.includes('google.com') || ref.includes('duckduckgo.com') || ref.includes('bing.com')) {\n var url = new URL(ref);\n var q = url.searchParams.get('q') || url.searchParams.get('p');\n if (q) {\n terms = q.split(/\\s+/).filter(function(t) { return t.length > 2; });\n }\n }\n \n if (terms.length === 0) return;\n \n var style = document.createElement('style');\n style.textContent = '.userscript-highlight { background: #fbbf24; color: #1a1a2e; padding: 1px 3px; border-radius: 2px; }';\n document.head.appendChild(style);\n \n function highlight(node) {\n if (node.nodeType === 3) { // text node\n var text = node.textContent;\n var found = false;\n terms.forEach(function(term) {\n var regex = new RegExp('(' + term.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\') + ')', 'gi');\n if (regex.test(text)) {\n found = true;\n var frag = document.createDocumentFragment();\n var parts = text.split(regex);\n parts.forEach(function(part, i) {\n if (i % 2 === 0) {\n frag.appendChild(document.createTextNode(part));\n } else {\n var span = document.createElement('span');\n span.className = 'userscript-highlight';\n span.textContent = part;\n frag.appendChild(span);\n }\n });\n node.parentNode.replaceChild(frag, node);\n }\n });\n } else if (node.nodeType === 1 && node.childNodes) { // element\n var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT'];\n if (!skipTags.includes(node.tagName)) {\n Array.from(node.childNodes).forEach(highlight);\n }\n }\n }\n \n highlight(document.body);\n \n // Re-highlight on dynamic content\n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1 || node.nodeType === 3) highlight(node);\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Highlight Search Terms"); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Latest commit

History

History
357 lines (275 loc) · 12.4 KB

File metadata and controls

357 lines (275 loc) · 12.4 KB

ServerStatus 中文版

ServerStatus 是一个轻量的服务器探针和云监控面板,支持多节点在线状态、资源占用、三网延迟、服务监测、SSL 证书检查、Watchdog 告警、HTTP API 和 Web 配置管理。

在线演示:https://tz.cloudcpp.com

GoCILicenseVersion

ServerStatus WebUI

一、服务端

# Docker Compose,本地构建加:--build
ADMIN_TOKEN='your-strong-token' docker compose -f docker-compose-server.yml up -d 
# Docker Run
wget -qO ~/serverstatus-config.json \
--header='Accept: application/vnd.github.raw' \
'https://api.github.com/repos/cppla/ServerStatus/contents/server/config.json?ref=master'
mkdir -p ~/serverstatus-data
docker run -d --restart=always --name=serverstatus-server \
-e ADMIN_TOKEN='your-strong-token' \
-v ~/serverstatus-config.json:/app/config/config.json \
-v ~/serverstatus-data:/app/data \
-p 8080:80 -p 35601:35601 \
cppla/serverstatus:server

启动后访问:

ADMIN_TOKEN 不设置时,监控页面仍可读取,管理 API 返回 503,WebUI 的“配置”页不能修改数据。

二、客户端

# Docker Compose,本地构建加:--build
SERVER=127.0.0.1 USER=s01 PASSWORD=USER_DEFAULT_PASSWORD \
docker compose -f docker-compose-client.yml up -d --force-recreate
# Docker Run
docker run -d --restart=always --name=serverstatus-client \
--network=host --pid=host \
-e SERVER=127.0.0.1 \
-e USER=s01 \
-e PASSWORD=USER_DEFAULT_PASSWORD \
cppla/serverstatus:client
# Shell Run
wget -qO client-linux.py --header='Accept: application/vnd.github.raw' \
'https://api.github.com/repos/cppla/ServerStatus/contents/clients/client-linux.py?ref=master'
nohup python3 client-linux.py SERVER=127.0.0.1 USER=s01 PASSWORD=USER_DEFAULT_PASSWORD >/dev/null 2>&1&

USER 是常见的宿主机环境变量名。如果没有显式传递或传递方式错误,Compose 可能会把系统中的 $USER 解析成本机用户名,而不是默认的 s01。推荐优先级:

  1. 运行命令显式传递 USER=s01
  2. 修改 docker-compose-client.yml 中的 USER 默认值
  3. Docker 或系统环境中的 USER
变量默认值说明
SERVER127.0.0.1Go 服务端地址
USERs01客户端用户名,必须匹配服务端配置
PORT35601Agent TCP 上报端口
PASSWORDUSER_DEFAULT_PASSWORD客户端密码,必须匹配服务端配置
INTERVAL1状态上报间隔,单位秒
PROBEPORT80三网 TCP 探测端口
PROBE_PROTOCOL_PREFERipv4探测协议偏好,可选 ipv4ipv6
PING_PACKET_HISTORY_LEN100丢包历史窗口
CUcu.tz.cloudcpp.com联通探测地址
CTct.tz.cloudcpp.com电信探测地址
CMcm.tz.cloudcpp.com移动探测地址
CLIENTpsutil客户端实现,可选 psutillinux

服务端参数

Docker 镜像中的默认路径为:

环境变量默认值说明
CONFIG_PATH/app/config/config.json主配置文件
STATS_PATH/app/data/stats.json月流量与状态持久化文件;每 60 秒写入,关键操作与正常退出时立即写入
WEB_DIR/app/webWebUI 静态文件目录
HTTP_ADDR:80WebUI 与 HTTP API 监听地址
AGENT_ADDR:35601客户端 TCP 上报监听地址
ADMIN_TOKEN管理 API Bearer Token;为空时禁用管理接口
ADMIN_CORS_ORIGIN可选的 API CORS Origin
INSECURE_CALLBACK_TLSfalse是否允许 Watchdog/证书回调使用不可信 TLS 证书
VERBOSEfalse输出 Gin HTTP 请求日志
TZAsia/Shanghai容器时区

对应命令行参数:

--config, -c config.json 路径
--stats stats.json 路径
--web-dir, -d WebUI 目录
--http HTTP 监听地址
--agent Agent TCP 监听地址
--verbose, -v 详细请求日志
--version 输出构建版本

旧参数 --bind/-b--port/-p 仍可用于设置 Agent TCP 监听地址。

HTTP 管理 API

管理接口使用 Bearer Token:

Authorization: Bearer <ADMIN_TOKEN>

无需认证:

方法路径用途
GET/api/health进程、Agent TCP、版本和配置路径状态
GET/api/schema机器可读的端点与配置集合描述
GET/api/openapi.json可供 AI Agent 直接导入的 OpenAPI 3.1 文档
GET/json/stats.jsonWebUI 使用的实时状态快照

需要认证:

方法路径用途
GET/PUT/api/config读取或整体替换配置
GET/POST/api/servers查询或新增节点
PUT/DELETE/api/servers/{username}修改或删除节点
POST/api/servers/{username}/reset-traffic将当前流量设为本月基线
GET/POST/api/monitors查询或新增服务监测
PUT/DELETE/api/monitors/{index-or-name}修改或删除服务监测
GET/POST/api/sslcerts查询或新增证书监测
PUT/DELETE/api/sslcerts/{index-or-name}修改或删除证书监测
GET/POST/api/watchdog查询或新增 Watchdog
PUT/DELETE/api/watchdog/{index-or-name}修改或删除 Watchdog
POST/api/reload从磁盘重新读取配置
POST/api/restart在进程内重启采集运行时

配置修改采用“校验 → 备份 → 持久化 → 原子切换”的顺序。成功后现有 Agent 连接会被关闭,Python 客户端约 3 秒后自动重连并获取新的 monitors/api/restart 不退出 Go 进程,因此 Docker 和手动运行方式具有一致语义。

常用调用:

TOKEN='请替换为 ADMIN_TOKEN'
curl http://127.0.0.1:8080/api/health
curl -H "Authorization: Bearer ${TOKEN}" \
http://127.0.0.1:8080/api/config
curl -X POST http://127.0.0.1:8080/api/servers \
-H "Authorization: Bearer ${TOKEN}" \
-H 'Content-Type: application/json' \
-d '{"username":"s05","name":"node5","type":"kvm","host":"host5","location":"SG","password":"change-me","monthstart":1}'
curl -X DELETE \
-H "Authorization: Bearer ${TOKEN}" \
http://127.0.0.1:8080/api/servers/s05

请求体最大为 1 MiB。AI Agent 可直接导入 /api/openapi.json;轻量客户端也可以先读取 /api/schema,再根据返回的集合字段调用 CRUD 接口。

配置文件

{
"servers": [
{
"username": "s01",
"name": "node1",
"type": "kvm",
"host": "host1",
"location": "CN",
"password": "USER_DEFAULT_PASSWORD",
"monthstart": 1,
"disabled": false
}
],
"monitors": [
{
"name": "example",
"host": "https://example.com",
"interval": 600,
"type": "https"
}
],
"sslcerts": [
{
"name": "example",
"domain": "https://example.com",
"port": 443,
"interval": 7200,
"callback": "https://example.net/push?message="
}
],
"watchdog": [
{
"name": "offline warning",
"rule": "online4=0&online6=0",
"interval": 600,
"callback": "https://example.net/push?message="
}
]
}

约束:

  • servers.username 必须唯一。
  • monthstart 自动限制在 1-28
  • port 自动限制在 1-65535
  • interval 最小为 1 秒;Watchdog 中表示通知冷却时间,不是客户端采集间隔。
  • 配置写入前会创建 config.json.bak-*,最多保留 10 份。
  • Docker 单文件 bind mount 无法被 rename 覆盖时,服务端会在完成备份后安全地写回原 inode。

使用 Docker 单文件挂载时,配置备份位于容器 /app/config 的可写层;如需长期保留历史版本,建议同时在宿主机备份 server/config.json

Watchdog 表达式

rule 由 Go expr 引擎执行,并兼容旧版 Exprtk 的常用写法。Go 服务会把字符串外的单个操作符自动转换:

& -> &&
| -> ||
= -> ==

例如以下两种写法等价:

cpu>90&load_1>5&username!='s01'
cpu>90 && load_1>5 && username!='s01'

字符串值支持中文、Emoji 和其他 Unicode 字符,例如:

username='节点一号'&name='上海节点'&location='中国 🇨🇳'&type='云主机'

字段名必须使用系统定义的英文名称。可用字段包括:usernamenametypehostlocationload_1load_5load_15cpumemory_totalmemory_usedswap_totalswap_usedhdd_totalhdd_usednetwork_rxnetwork_txnetwork_innetwork_outlast_network_inlast_network_outping_10010ping_189ping_10086time_10010time_189time_10086tcp_countudp_countprocess_countthread_countio_readio_writeonline4online6

客户端断开 25 秒后仍未重连,服务端才计算离线规则,避免短暂网络波动触发告警。每个节点、每条规则分别记录冷却时间。

SSL 证书

证书检查使用 Go crypto/tls,不再调用外部 openssl。服务端记录到期时间、剩余天数和域名匹配状态,并保留原来的 7/3/1 天通知档位与冷却时间。

回调默认校验 HTTPS 证书。仅在必须兼容自签名回调服务时设置:

INSECURE_CALLBACK_TLS=true

源码编译和运行

需要 Go 1.25 或更高版本:

cd server
go mod download
go test ./...
go build -trimpath -ldflags='-s -w' -o serverstatus .

server/ 目录启动:

ADMIN_TOKEN='请替换为高强度随机字符串' \
./serverstatus \
--config=config.json \
--stats=../web/json/stats.json \
--web-dir=../web \
--http=:8080 \
--agent=:35601

访问 http://127.0.0.1:8080/。发送SIGHUP 可以重新读取配置:

kill -HUP "$(pgrep -x serverstatus)"

Systemd 示例位于 service/status-server.service。一键脚本 status.sh 也已切换到 Go 构建,但 Docker 仍是推荐部署方式。

构建和测试

Go 测试需要 Go 1.25+;WebUI 行为测试需要 Node.js 20+ 和 pnpm。

# Go 单元、协议、API、TLS 和回调测试cd server
go test ./...
go test -race ./...
go vet ./...
# Python 客户端指标、参数与平台识别测试cd ..
python3 -m unittest discover -s clients -p 'test_*.py'# WebUI Chromium 行为测试
pnpm install --frozen-lockfile
pnpm exec playwright install chromium
pnpm test:webui
# Docker 镜像
docker build -f Dockerfile.server -t cppla/serverstatus:server .
docker build -f Dockerfile.client -t cppla/serverstatus:client .# 服务端与客户端真实连接、认证和指标上报
SERVER_IMAGE=cppla/serverstatus:server \
CLIENT_IMAGE=cppla/serverstatus:client \
tests/docker-smoke.sh
# Compose 配置
docker compose -f docker-compose-server.yml config
docker compose -f docker-compose-client.yml config

Docker 联通测试在 Linux 上覆盖客户端的 host 网络和 pid 模式;Docker Desktop 未启用 Host Networking 时会明确提示并使用隔离 bridge 完成本地协议测试。

CI 还会运行 Chromium 行为测试、Docker 服务端/客户端联通测试,并验证客户端镜像可同时构建为 AMD64 和 ARM64。

从旧服务端迁移

  1. 备份原来的 config.jsonweb/json/stats.json
  2. 原配置结构和客户端账号无需转换。
  3. Docker 挂载目标改为 /app/config/config.json/app/data
  4. 删除旧的 nginx、manage_api.pysergate 启动或监督配置。
  5. 启动 Go 服务端后检查 /api/health,再观察客户端自动重连。

stats.json 会按节点的 name/type/host/location 恢复月流量基线。修改这些身份字段会被视为新节点。

致谢

, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Strip utm_, fbclid, gclid, etc. from all links on page\n(function() {\n var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content',\n 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid',\n 'ref', 'ref_src', 'source', 'medium', 'campaign'];\n \n function cleanUrl(url) {\n try {\n var u = new URL(url, window.location.origin);\n var changed = false;\n trackingParams.forEach(function(p) {\n if (u.searchParams.has(p)) {\n u.searchParams.delete(p);\n changed = true;\n }\n });\n return changed ? u.toString() : url;\n } catch (e) {\n return url;\n }\n }\n \n function cleanLinks() {\n document.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n \n cleanLinks();\n \n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1) {\n if (node.tagName === 'A') cleanLinks();\n node.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Remove Tracking Parameters from Links"); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + '
Skip to content

Latest commit

History

History
357 lines (275 loc) · 12.4 KB

File metadata and controls

357 lines (275 loc) · 12.4 KB

ServerStatus 中文版

ServerStatus 是一个轻量的服务器探针和云监控面板,支持多节点在线状态、资源占用、三网延迟、服务监测、SSL 证书检查、Watchdog 告警、HTTP API 和 Web 配置管理。

在线演示:https://tz.cloudcpp.com

GoCILicenseVersion

ServerStatus WebUI

一、服务端

# Docker Compose,本地构建加:--build
ADMIN_TOKEN='your-strong-token' docker compose -f docker-compose-server.yml up -d 
# Docker Run
wget -qO ~/serverstatus-config.json \
--header='Accept: application/vnd.github.raw' \
'https://api.github.com/repos/cppla/ServerStatus/contents/server/config.json?ref=master'
mkdir -p ~/serverstatus-data
docker run -d --restart=always --name=serverstatus-server \
-e ADMIN_TOKEN='your-strong-token' \
-v ~/serverstatus-config.json:/app/config/config.json \
-v ~/serverstatus-data:/app/data \
-p 8080:80 -p 35601:35601 \
cppla/serverstatus:server

启动后访问:

ADMIN_TOKEN 不设置时,监控页面仍可读取,管理 API 返回 503,WebUI 的“配置”页不能修改数据。

二、客户端

# Docker Compose,本地构建加:--build
SERVER=127.0.0.1 USER=s01 PASSWORD=USER_DEFAULT_PASSWORD \
docker compose -f docker-compose-client.yml up -d --force-recreate
# Docker Run
docker run -d --restart=always --name=serverstatus-client \
--network=host --pid=host \
-e SERVER=127.0.0.1 \
-e USER=s01 \
-e PASSWORD=USER_DEFAULT_PASSWORD \
cppla/serverstatus:client
# Shell Run
wget -qO client-linux.py --header='Accept: application/vnd.github.raw' \
'https://api.github.com/repos/cppla/ServerStatus/contents/clients/client-linux.py?ref=master'
nohup python3 client-linux.py SERVER=127.0.0.1 USER=s01 PASSWORD=USER_DEFAULT_PASSWORD >/dev/null 2>&1&

USER 是常见的宿主机环境变量名。如果没有显式传递或传递方式错误,Compose 可能会把系统中的 $USER 解析成本机用户名,而不是默认的 s01。推荐优先级:

  1. 运行命令显式传递 USER=s01
  2. 修改 docker-compose-client.yml 中的 USER 默认值
  3. Docker 或系统环境中的 USER
变量默认值说明
SERVER127.0.0.1Go 服务端地址
USERs01客户端用户名,必须匹配服务端配置
PORT35601Agent TCP 上报端口
PASSWORDUSER_DEFAULT_PASSWORD客户端密码,必须匹配服务端配置
INTERVAL1状态上报间隔,单位秒
PROBEPORT80三网 TCP 探测端口
PROBE_PROTOCOL_PREFERipv4探测协议偏好,可选 ipv4ipv6
PING_PACKET_HISTORY_LEN100丢包历史窗口
CUcu.tz.cloudcpp.com联通探测地址
CTct.tz.cloudcpp.com电信探测地址
CMcm.tz.cloudcpp.com移动探测地址
CLIENTpsutil客户端实现,可选 psutillinux

服务端参数

Docker 镜像中的默认路径为:

环境变量默认值说明
CONFIG_PATH/app/config/config.json主配置文件
STATS_PATH/app/data/stats.json月流量与状态持久化文件;每 60 秒写入,关键操作与正常退出时立即写入
WEB_DIR/app/webWebUI 静态文件目录
HTTP_ADDR:80WebUI 与 HTTP API 监听地址
AGENT_ADDR:35601客户端 TCP 上报监听地址
ADMIN_TOKEN管理 API Bearer Token;为空时禁用管理接口
ADMIN_CORS_ORIGIN可选的 API CORS Origin
INSECURE_CALLBACK_TLSfalse是否允许 Watchdog/证书回调使用不可信 TLS 证书
VERBOSEfalse输出 Gin HTTP 请求日志
TZAsia/Shanghai容器时区

对应命令行参数:

--config, -c config.json 路径
--stats stats.json 路径
--web-dir, -d WebUI 目录
--http HTTP 监听地址
--agent Agent TCP 监听地址
--verbose, -v 详细请求日志
--version 输出构建版本

旧参数 --bind/-b--port/-p 仍可用于设置 Agent TCP 监听地址。

HTTP 管理 API

管理接口使用 Bearer Token:

Authorization: Bearer <ADMIN_TOKEN>

无需认证:

方法路径用途
GET/api/health进程、Agent TCP、版本和配置路径状态
GET/api/schema机器可读的端点与配置集合描述
GET/api/openapi.json可供 AI Agent 直接导入的 OpenAPI 3.1 文档
GET/json/stats.jsonWebUI 使用的实时状态快照

需要认证:

方法路径用途
GET/PUT/api/config读取或整体替换配置
GET/POST/api/servers查询或新增节点
PUT/DELETE/api/servers/{username}修改或删除节点
POST/api/servers/{username}/reset-traffic将当前流量设为本月基线
GET/POST/api/monitors查询或新增服务监测
PUT/DELETE/api/monitors/{index-or-name}修改或删除服务监测
GET/POST/api/sslcerts查询或新增证书监测
PUT/DELETE/api/sslcerts/{index-or-name}修改或删除证书监测
GET/POST/api/watchdog查询或新增 Watchdog
PUT/DELETE/api/watchdog/{index-or-name}修改或删除 Watchdog
POST/api/reload从磁盘重新读取配置
POST/api/restart在进程内重启采集运行时

配置修改采用“校验 → 备份 → 持久化 → 原子切换”的顺序。成功后现有 Agent 连接会被关闭,Python 客户端约 3 秒后自动重连并获取新的 monitors/api/restart 不退出 Go 进程,因此 Docker 和手动运行方式具有一致语义。

常用调用:

TOKEN='请替换为 ADMIN_TOKEN'
curl http://127.0.0.1:8080/api/health
curl -H "Authorization: Bearer ${TOKEN}" \
http://127.0.0.1:8080/api/config
curl -X POST http://127.0.0.1:8080/api/servers \
-H "Authorization: Bearer ${TOKEN}" \
-H 'Content-Type: application/json' \
-d '{"username":"s05","name":"node5","type":"kvm","host":"host5","location":"SG","password":"change-me","monthstart":1}'
curl -X DELETE \
-H "Authorization: Bearer ${TOKEN}" \
http://127.0.0.1:8080/api/servers/s05

请求体最大为 1 MiB。AI Agent 可直接导入 /api/openapi.json;轻量客户端也可以先读取 /api/schema,再根据返回的集合字段调用 CRUD 接口。

配置文件

{
"servers": [
{
"username": "s01",
"name": "node1",
"type": "kvm",
"host": "host1",
"location": "CN",
"password": "USER_DEFAULT_PASSWORD",
"monthstart": 1,
"disabled": false
}
],
"monitors": [
{
"name": "example",
"host": "https://example.com",
"interval": 600,
"type": "https"
}
],
"sslcerts": [
{
"name": "example",
"domain": "https://example.com",
"port": 443,
"interval": 7200,
"callback": "https://example.net/push?message="
}
],
"watchdog": [
{
"name": "offline warning",
"rule": "online4=0&online6=0",
"interval": 600,
"callback": "https://example.net/push?message="
}
]
}

约束:

  • servers.username 必须唯一。
  • monthstart 自动限制在 1-28
  • port 自动限制在 1-65535
  • interval 最小为 1 秒;Watchdog 中表示通知冷却时间,不是客户端采集间隔。
  • 配置写入前会创建 config.json.bak-*,最多保留 10 份。
  • Docker 单文件 bind mount 无法被 rename 覆盖时,服务端会在完成备份后安全地写回原 inode。

使用 Docker 单文件挂载时,配置备份位于容器 /app/config 的可写层;如需长期保留历史版本,建议同时在宿主机备份 server/config.json

Watchdog 表达式

rule 由 Go expr 引擎执行,并兼容旧版 Exprtk 的常用写法。Go 服务会把字符串外的单个操作符自动转换:

& -> &&
| -> ||
= -> ==

例如以下两种写法等价:

cpu>90&load_1>5&username!='s01'
cpu>90 && load_1>5 && username!='s01'

字符串值支持中文、Emoji 和其他 Unicode 字符,例如:

username='节点一号'&name='上海节点'&location='中国 🇨🇳'&type='云主机'

字段名必须使用系统定义的英文名称。可用字段包括:usernamenametypehostlocationload_1load_5load_15cpumemory_totalmemory_usedswap_totalswap_usedhdd_totalhdd_usednetwork_rxnetwork_txnetwork_innetwork_outlast_network_inlast_network_outping_10010ping_189ping_10086time_10010time_189time_10086tcp_countudp_countprocess_countthread_countio_readio_writeonline4online6

客户端断开 25 秒后仍未重连,服务端才计算离线规则,避免短暂网络波动触发告警。每个节点、每条规则分别记录冷却时间。

SSL 证书

证书检查使用 Go crypto/tls,不再调用外部 openssl。服务端记录到期时间、剩余天数和域名匹配状态,并保留原来的 7/3/1 天通知档位与冷却时间。

回调默认校验 HTTPS 证书。仅在必须兼容自签名回调服务时设置:

INSECURE_CALLBACK_TLS=true

源码编译和运行

需要 Go 1.25 或更高版本:

cd server
go mod download
go test ./...
go build -trimpath -ldflags='-s -w' -o serverstatus .

server/ 目录启动:

ADMIN_TOKEN='请替换为高强度随机字符串' \
./serverstatus \
--config=config.json \
--stats=../web/json/stats.json \
--web-dir=../web \
--http=:8080 \
--agent=:35601

访问 http://127.0.0.1:8080/。发送SIGHUP 可以重新读取配置:

kill -HUP "$(pgrep -x serverstatus)"

Systemd 示例位于 service/status-server.service。一键脚本 status.sh 也已切换到 Go 构建,但 Docker 仍是推荐部署方式。

构建和测试

Go 测试需要 Go 1.25+;WebUI 行为测试需要 Node.js 20+ 和 pnpm。

# Go 单元、协议、API、TLS 和回调测试cd server
go test ./...
go test -race ./...
go vet ./...
# Python 客户端指标、参数与平台识别测试cd ..
python3 -m unittest discover -s clients -p 'test_*.py'# WebUI Chromium 行为测试
pnpm install --frozen-lockfile
pnpm exec playwright install chromium
pnpm test:webui
# Docker 镜像
docker build -f Dockerfile.server -t cppla/serverstatus:server .
docker build -f Dockerfile.client -t cppla/serverstatus:client .# 服务端与客户端真实连接、认证和指标上报
SERVER_IMAGE=cppla/serverstatus:server \
CLIENT_IMAGE=cppla/serverstatus:client \
tests/docker-smoke.sh
# Compose 配置
docker compose -f docker-compose-server.yml config
docker compose -f docker-compose-client.yml config

Docker 联通测试在 Linux 上覆盖客户端的 host 网络和 pid 模式;Docker Desktop 未启用 Host Networking 时会明确提示并使用隔离 bridge 完成本地协议测试。

CI 还会运行 Chromium 行为测试、Docker 服务端/客户端联通测试,并验证客户端镜像可同时构建为 AMD64 和 ARM64。

从旧服务端迁移

  1. 备份原来的 config.jsonweb/json/stats.json
  2. 原配置结构和客户端账号无需转换。
  3. Docker 挂载目标改为 /app/config/config.json/app/data
  4. 删除旧的 nginx、manage_api.pysergate 启动或监督配置。
  5. 启动 Go 服务端后检查 /api/health,再观察客户端自动重连。

stats.json 会按节点的 name/type/host/location 恢复月流量基线。修改这些身份字段会被视为新节点。

致谢

, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Auto-enable theater mode on YouTube\n(function() {\n function tryTheater() {\n var btn = document.querySelector('button[aria-label=\"Theater mode\"], ytd-player #player button[title=\"Theater mode\"]');\n if (btn && !btn.classList.contains('activated')) {\n btn.click();\n }\n }\n \n // Try immediately\n tryTheater();\n \n // Try after navigation (SPA)\n var lastUrl = location.href;\n setInterval(function() {\n if (location.href !== lastUrl) {\n lastUrl = location.href;\n setTimeout(tryTheater, 500);\n }\n }, 1000);\n \n // Also try on player load\n var observer = new MutationObserver(tryTheater);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "YouTube Theater Mode Default"); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Latest commit

History

History
357 lines (275 loc) · 12.4 KB

File metadata and controls

357 lines (275 loc) · 12.4 KB

ServerStatus 中文版

ServerStatus 是一个轻量的服务器探针和云监控面板,支持多节点在线状态、资源占用、三网延迟、服务监测、SSL 证书检查、Watchdog 告警、HTTP API 和 Web 配置管理。

在线演示:https://tz.cloudcpp.com

GoCILicenseVersion

ServerStatus WebUI

一、服务端

# Docker Compose,本地构建加:--build
ADMIN_TOKEN='your-strong-token' docker compose -f docker-compose-server.yml up -d 
# Docker Run
wget -qO ~/serverstatus-config.json \
--header='Accept: application/vnd.github.raw' \
'https://api.github.com/repos/cppla/ServerStatus/contents/server/config.json?ref=master'
mkdir -p ~/serverstatus-data
docker run -d --restart=always --name=serverstatus-server \
-e ADMIN_TOKEN='your-strong-token' \
-v ~/serverstatus-config.json:/app/config/config.json \
-v ~/serverstatus-data:/app/data \
-p 8080:80 -p 35601:35601 \
cppla/serverstatus:server

启动后访问:

ADMIN_TOKEN 不设置时,监控页面仍可读取,管理 API 返回 503,WebUI 的“配置”页不能修改数据。

二、客户端

# Docker Compose,本地构建加:--build
SERVER=127.0.0.1 USER=s01 PASSWORD=USER_DEFAULT_PASSWORD \
docker compose -f docker-compose-client.yml up -d --force-recreate
# Docker Run
docker run -d --restart=always --name=serverstatus-client \
--network=host --pid=host \
-e SERVER=127.0.0.1 \
-e USER=s01 \
-e PASSWORD=USER_DEFAULT_PASSWORD \
cppla/serverstatus:client
# Shell Run
wget -qO client-linux.py --header='Accept: application/vnd.github.raw' \
'https://api.github.com/repos/cppla/ServerStatus/contents/clients/client-linux.py?ref=master'
nohup python3 client-linux.py SERVER=127.0.0.1 USER=s01 PASSWORD=USER_DEFAULT_PASSWORD >/dev/null 2>&1&

USER 是常见的宿主机环境变量名。如果没有显式传递或传递方式错误,Compose 可能会把系统中的 $USER 解析成本机用户名,而不是默认的 s01。推荐优先级:

  1. 运行命令显式传递 USER=s01
  2. 修改 docker-compose-client.yml 中的 USER 默认值
  3. Docker 或系统环境中的 USER
变量默认值说明
SERVER127.0.0.1Go 服务端地址
USERs01客户端用户名,必须匹配服务端配置
PORT35601Agent TCP 上报端口
PASSWORDUSER_DEFAULT_PASSWORD客户端密码,必须匹配服务端配置
INTERVAL1状态上报间隔,单位秒
PROBEPORT80三网 TCP 探测端口
PROBE_PROTOCOL_PREFERipv4探测协议偏好,可选 ipv4ipv6
PING_PACKET_HISTORY_LEN100丢包历史窗口
CUcu.tz.cloudcpp.com联通探测地址
CTct.tz.cloudcpp.com电信探测地址
CMcm.tz.cloudcpp.com移动探测地址
CLIENTpsutil客户端实现,可选 psutillinux

服务端参数

Docker 镜像中的默认路径为:

环境变量默认值说明
CONFIG_PATH/app/config/config.json主配置文件
STATS_PATH/app/data/stats.json月流量与状态持久化文件;每 60 秒写入,关键操作与正常退出时立即写入
WEB_DIR/app/webWebUI 静态文件目录
HTTP_ADDR:80WebUI 与 HTTP API 监听地址
AGENT_ADDR:35601客户端 TCP 上报监听地址
ADMIN_TOKEN管理 API Bearer Token;为空时禁用管理接口
ADMIN_CORS_ORIGIN可选的 API CORS Origin
INSECURE_CALLBACK_TLSfalse是否允许 Watchdog/证书回调使用不可信 TLS 证书
VERBOSEfalse输出 Gin HTTP 请求日志
TZAsia/Shanghai容器时区

对应命令行参数:

--config, -c config.json 路径
--stats stats.json 路径
--web-dir, -d WebUI 目录
--http HTTP 监听地址
--agent Agent TCP 监听地址
--verbose, -v 详细请求日志
--version 输出构建版本

旧参数 --bind/-b--port/-p 仍可用于设置 Agent TCP 监听地址。

HTTP 管理 API

管理接口使用 Bearer Token:

Authorization: Bearer <ADMIN_TOKEN>

无需认证:

方法路径用途
GET/api/health进程、Agent TCP、版本和配置路径状态
GET/api/schema机器可读的端点与配置集合描述
GET/api/openapi.json可供 AI Agent 直接导入的 OpenAPI 3.1 文档
GET/json/stats.jsonWebUI 使用的实时状态快照

需要认证:

方法路径用途
GET/PUT/api/config读取或整体替换配置
GET/POST/api/servers查询或新增节点
PUT/DELETE/api/servers/{username}修改或删除节点
POST/api/servers/{username}/reset-traffic将当前流量设为本月基线
GET/POST/api/monitors查询或新增服务监测
PUT/DELETE/api/monitors/{index-or-name}修改或删除服务监测
GET/POST/api/sslcerts查询或新增证书监测
PUT/DELETE/api/sslcerts/{index-or-name}修改或删除证书监测
GET/POST/api/watchdog查询或新增 Watchdog
PUT/DELETE/api/watchdog/{index-or-name}修改或删除 Watchdog
POST/api/reload从磁盘重新读取配置
POST/api/restart在进程内重启采集运行时

配置修改采用“校验 → 备份 → 持久化 → 原子切换”的顺序。成功后现有 Agent 连接会被关闭,Python 客户端约 3 秒后自动重连并获取新的 monitors/api/restart 不退出 Go 进程,因此 Docker 和手动运行方式具有一致语义。

常用调用:

TOKEN='请替换为 ADMIN_TOKEN'
curl http://127.0.0.1:8080/api/health
curl -H "Authorization: Bearer ${TOKEN}" \
http://127.0.0.1:8080/api/config
curl -X POST http://127.0.0.1:8080/api/servers \
-H "Authorization: Bearer ${TOKEN}" \
-H 'Content-Type: application/json' \
-d '{"username":"s05","name":"node5","type":"kvm","host":"host5","location":"SG","password":"change-me","monthstart":1}'
curl -X DELETE \
-H "Authorization: Bearer ${TOKEN}" \
http://127.0.0.1:8080/api/servers/s05

请求体最大为 1 MiB。AI Agent 可直接导入 /api/openapi.json;轻量客户端也可以先读取 /api/schema,再根据返回的集合字段调用 CRUD 接口。

配置文件

{
"servers": [
{
"username": "s01",
"name": "node1",
"type": "kvm",
"host": "host1",
"location": "CN",
"password": "USER_DEFAULT_PASSWORD",
"monthstart": 1,
"disabled": false
}
],
"monitors": [
{
"name": "example",
"host": "https://example.com",
"interval": 600,
"type": "https"
}
],
"sslcerts": [
{
"name": "example",
"domain": "https://example.com",
"port": 443,
"interval": 7200,
"callback": "https://example.net/push?message="
}
],
"watchdog": [
{
"name": "offline warning",
"rule": "online4=0&online6=0",
"interval": 600,
"callback": "https://example.net/push?message="
}
]
}

约束:

  • servers.username 必须唯一。
  • monthstart 自动限制在 1-28
  • port 自动限制在 1-65535
  • interval 最小为 1 秒;Watchdog 中表示通知冷却时间,不是客户端采集间隔。
  • 配置写入前会创建 config.json.bak-*,最多保留 10 份。
  • Docker 单文件 bind mount 无法被 rename 覆盖时,服务端会在完成备份后安全地写回原 inode。

使用 Docker 单文件挂载时,配置备份位于容器 /app/config 的可写层;如需长期保留历史版本,建议同时在宿主机备份 server/config.json

Watchdog 表达式

rule 由 Go expr 引擎执行,并兼容旧版 Exprtk 的常用写法。Go 服务会把字符串外的单个操作符自动转换:

& -> &&
| -> ||
= -> ==

例如以下两种写法等价:

cpu>90&load_1>5&username!='s01'
cpu>90 && load_1>5 && username!='s01'

字符串值支持中文、Emoji 和其他 Unicode 字符,例如:

username='节点一号'&name='上海节点'&location='中国 🇨🇳'&type='云主机'

字段名必须使用系统定义的英文名称。可用字段包括:usernamenametypehostlocationload_1load_5load_15cpumemory_totalmemory_usedswap_totalswap_usedhdd_totalhdd_usednetwork_rxnetwork_txnetwork_innetwork_outlast_network_inlast_network_outping_10010ping_189ping_10086time_10010time_189time_10086tcp_countudp_countprocess_countthread_countio_readio_writeonline4online6

客户端断开 25 秒后仍未重连,服务端才计算离线规则,避免短暂网络波动触发告警。每个节点、每条规则分别记录冷却时间。

SSL 证书

证书检查使用 Go crypto/tls,不再调用外部 openssl。服务端记录到期时间、剩余天数和域名匹配状态,并保留原来的 7/3/1 天通知档位与冷却时间。

回调默认校验 HTTPS 证书。仅在必须兼容自签名回调服务时设置:

INSECURE_CALLBACK_TLS=true

源码编译和运行

需要 Go 1.25 或更高版本:

cd server
go mod download
go test ./...
go build -trimpath -ldflags='-s -w' -o serverstatus .

server/ 目录启动:

ADMIN_TOKEN='请替换为高强度随机字符串' \
./serverstatus \
--config=config.json \
--stats=../web/json/stats.json \
--web-dir=../web \
--http=:8080 \
--agent=:35601

访问 http://127.0.0.1:8080/。发送SIGHUP 可以重新读取配置:

kill -HUP "$(pgrep -x serverstatus)"

Systemd 示例位于 service/status-server.service。一键脚本 status.sh 也已切换到 Go 构建,但 Docker 仍是推荐部署方式。

构建和测试

Go 测试需要 Go 1.25+;WebUI 行为测试需要 Node.js 20+ 和 pnpm。

# Go 单元、协议、API、TLS 和回调测试cd server
go test ./...
go test -race ./...
go vet ./...
# Python 客户端指标、参数与平台识别测试cd ..
python3 -m unittest discover -s clients -p 'test_*.py'# WebUI Chromium 行为测试
pnpm install --frozen-lockfile
pnpm exec playwright install chromium
pnpm test:webui
# Docker 镜像
docker build -f Dockerfile.server -t cppla/serverstatus:server .
docker build -f Dockerfile.client -t cppla/serverstatus:client .# 服务端与客户端真实连接、认证和指标上报
SERVER_IMAGE=cppla/serverstatus:server \
CLIENT_IMAGE=cppla/serverstatus:client \
tests/docker-smoke.sh
# Compose 配置
docker compose -f docker-compose-server.yml config
docker compose -f docker-compose-client.yml config

Docker 联通测试在 Linux 上覆盖客户端的 host 网络和 pid 模式;Docker Desktop 未启用 Host Networking 时会明确提示并使用隔离 bridge 完成本地协议测试。

CI 还会运行 Chromium 行为测试、Docker 服务端/客户端联通测试,并验证客户端镜像可同时构建为 AMD64 和 ARM64。

从旧服务端迁移

  1. 备份原来的 config.jsonweb/json/stats.json
  2. 原配置结构和客户端账号无需转换。
  3. Docker 挂载目标改为 /app/config/config.json/app/data
  4. 删除旧的 nginx、manage_api.pysergate 启动或监督配置。
  5. 启动 Go 服务端后检查 /api/health,再观察客户端自动重连。

stats.json 会按节点的 name/type/host/location 恢复月流量基线。修改这些身份字段会被视为新节点。

致谢

, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Remove or un-stick sticky/fixed headers that block content\n(function() {\n function unstick() {\n document.querySelectorAll('header, nav, [role=\"banner\"], .header, .navbar, .sticky, .fixed-top, [style*=\"position: fixed\"], [style*=\"position:sticky\"]').forEach(function(el) {\n if (el.style.position === 'fixed' || el.style.position === 'sticky' || \n getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') {\n el.style.position = 'static';\n el.style.top = 'auto';\n el.style.zIndex = 'auto';\n }\n });\n }\n \n unstick();\n \n var observer = new MutationObserver(unstick);\n observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] });\n})();", "Kill Sticky Headers"); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Latest commit

History

History
357 lines (275 loc) · 12.4 KB

File metadata and controls

357 lines (275 loc) · 12.4 KB

ServerStatus 中文版

ServerStatus 是一个轻量的服务器探针和云监控面板,支持多节点在线状态、资源占用、三网延迟、服务监测、SSL 证书检查、Watchdog 告警、HTTP API 和 Web 配置管理。

在线演示:https://tz.cloudcpp.com

GoCILicenseVersion

ServerStatus WebUI

一、服务端

# Docker Compose,本地构建加:--build
ADMIN_TOKEN='your-strong-token' docker compose -f docker-compose-server.yml up -d 
# Docker Run
wget -qO ~/serverstatus-config.json \
--header='Accept: application/vnd.github.raw' \
'https://api.github.com/repos/cppla/ServerStatus/contents/server/config.json?ref=master'
mkdir -p ~/serverstatus-data
docker run -d --restart=always --name=serverstatus-server \
-e ADMIN_TOKEN='your-strong-token' \
-v ~/serverstatus-config.json:/app/config/config.json \
-v ~/serverstatus-data:/app/data \
-p 8080:80 -p 35601:35601 \
cppla/serverstatus:server

启动后访问:

ADMIN_TOKEN 不设置时,监控页面仍可读取,管理 API 返回 503,WebUI 的“配置”页不能修改数据。

二、客户端

# Docker Compose,本地构建加:--build
SERVER=127.0.0.1 USER=s01 PASSWORD=USER_DEFAULT_PASSWORD \
docker compose -f docker-compose-client.yml up -d --force-recreate
# Docker Run
docker run -d --restart=always --name=serverstatus-client \
--network=host --pid=host \
-e SERVER=127.0.0.1 \
-e USER=s01 \
-e PASSWORD=USER_DEFAULT_PASSWORD \
cppla/serverstatus:client
# Shell Run
wget -qO client-linux.py --header='Accept: application/vnd.github.raw' \
'https://api.github.com/repos/cppla/ServerStatus/contents/clients/client-linux.py?ref=master'
nohup python3 client-linux.py SERVER=127.0.0.1 USER=s01 PASSWORD=USER_DEFAULT_PASSWORD >/dev/null 2>&1&

USER 是常见的宿主机环境变量名。如果没有显式传递或传递方式错误,Compose 可能会把系统中的 $USER 解析成本机用户名,而不是默认的 s01。推荐优先级:

  1. 运行命令显式传递 USER=s01
  2. 修改 docker-compose-client.yml 中的 USER 默认值
  3. Docker 或系统环境中的 USER
变量默认值说明
SERVER127.0.0.1Go 服务端地址
USERs01客户端用户名,必须匹配服务端配置
PORT35601Agent TCP 上报端口
PASSWORDUSER_DEFAULT_PASSWORD客户端密码,必须匹配服务端配置
INTERVAL1状态上报间隔,单位秒
PROBEPORT80三网 TCP 探测端口
PROBE_PROTOCOL_PREFERipv4探测协议偏好,可选 ipv4ipv6
PING_PACKET_HISTORY_LEN100丢包历史窗口
CUcu.tz.cloudcpp.com联通探测地址
CTct.tz.cloudcpp.com电信探测地址
CMcm.tz.cloudcpp.com移动探测地址
CLIENTpsutil客户端实现,可选 psutillinux

服务端参数

Docker 镜像中的默认路径为:

环境变量默认值说明
CONFIG_PATH/app/config/config.json主配置文件
STATS_PATH/app/data/stats.json月流量与状态持久化文件;每 60 秒写入,关键操作与正常退出时立即写入
WEB_DIR/app/webWebUI 静态文件目录
HTTP_ADDR:80WebUI 与 HTTP API 监听地址
AGENT_ADDR:35601客户端 TCP 上报监听地址
ADMIN_TOKEN管理 API Bearer Token;为空时禁用管理接口
ADMIN_CORS_ORIGIN可选的 API CORS Origin
INSECURE_CALLBACK_TLSfalse是否允许 Watchdog/证书回调使用不可信 TLS 证书
VERBOSEfalse输出 Gin HTTP 请求日志
TZAsia/Shanghai容器时区

对应命令行参数:

--config, -c config.json 路径
--stats stats.json 路径
--web-dir, -d WebUI 目录
--http HTTP 监听地址
--agent Agent TCP 监听地址
--verbose, -v 详细请求日志
--version 输出构建版本

旧参数 --bind/-b--port/-p 仍可用于设置 Agent TCP 监听地址。

HTTP 管理 API

管理接口使用 Bearer Token:

Authorization: Bearer <ADMIN_TOKEN>

无需认证:

方法路径用途
GET/api/health进程、Agent TCP、版本和配置路径状态
GET/api/schema机器可读的端点与配置集合描述
GET/api/openapi.json可供 AI Agent 直接导入的 OpenAPI 3.1 文档
GET/json/stats.jsonWebUI 使用的实时状态快照

需要认证:

方法路径用途
GET/PUT/api/config读取或整体替换配置
GET/POST/api/servers查询或新增节点
PUT/DELETE/api/servers/{username}修改或删除节点
POST/api/servers/{username}/reset-traffic将当前流量设为本月基线
GET/POST/api/monitors查询或新增服务监测
PUT/DELETE/api/monitors/{index-or-name}修改或删除服务监测
GET/POST/api/sslcerts查询或新增证书监测
PUT/DELETE/api/sslcerts/{index-or-name}修改或删除证书监测
GET/POST/api/watchdog查询或新增 Watchdog
PUT/DELETE/api/watchdog/{index-or-name}修改或删除 Watchdog
POST/api/reload从磁盘重新读取配置
POST/api/restart在进程内重启采集运行时

配置修改采用“校验 → 备份 → 持久化 → 原子切换”的顺序。成功后现有 Agent 连接会被关闭,Python 客户端约 3 秒后自动重连并获取新的 monitors/api/restart 不退出 Go 进程,因此 Docker 和手动运行方式具有一致语义。

常用调用:

TOKEN='请替换为 ADMIN_TOKEN'
curl http://127.0.0.1:8080/api/health
curl -H "Authorization: Bearer ${TOKEN}" \
http://127.0.0.1:8080/api/config
curl -X POST http://127.0.0.1:8080/api/servers \
-H "Authorization: Bearer ${TOKEN}" \
-H 'Content-Type: application/json' \
-d '{"username":"s05","name":"node5","type":"kvm","host":"host5","location":"SG","password":"change-me","monthstart":1}'
curl -X DELETE \
-H "Authorization: Bearer ${TOKEN}" \
http://127.0.0.1:8080/api/servers/s05

请求体最大为 1 MiB。AI Agent 可直接导入 /api/openapi.json;轻量客户端也可以先读取 /api/schema,再根据返回的集合字段调用 CRUD 接口。

配置文件

{
"servers": [
{
"username": "s01",
"name": "node1",
"type": "kvm",
"host": "host1",
"location": "CN",
"password": "USER_DEFAULT_PASSWORD",
"monthstart": 1,
"disabled": false
}
],
"monitors": [
{
"name": "example",
"host": "https://example.com",
"interval": 600,
"type": "https"
}
],
"sslcerts": [
{
"name": "example",
"domain": "https://example.com",
"port": 443,
"interval": 7200,
"callback": "https://example.net/push?message="
}
],
"watchdog": [
{
"name": "offline warning",
"rule": "online4=0&online6=0",
"interval": 600,
"callback": "https://example.net/push?message="
}
]
}

约束:

  • servers.username 必须唯一。
  • monthstart 自动限制在 1-28
  • port 自动限制在 1-65535
  • interval 最小为 1 秒;Watchdog 中表示通知冷却时间,不是客户端采集间隔。
  • 配置写入前会创建 config.json.bak-*,最多保留 10 份。
  • Docker 单文件 bind mount 无法被 rename 覆盖时,服务端会在完成备份后安全地写回原 inode。

使用 Docker 单文件挂载时,配置备份位于容器 /app/config 的可写层;如需长期保留历史版本,建议同时在宿主机备份 server/config.json

Watchdog 表达式

rule 由 Go expr 引擎执行,并兼容旧版 Exprtk 的常用写法。Go 服务会把字符串外的单个操作符自动转换:

& -> &&
| -> ||
= -> ==

例如以下两种写法等价:

cpu>90&load_1>5&username!='s01'
cpu>90 && load_1>5 && username!='s01'

字符串值支持中文、Emoji 和其他 Unicode 字符,例如:

username='节点一号'&name='上海节点'&location='中国 🇨🇳'&type='云主机'

字段名必须使用系统定义的英文名称。可用字段包括:usernamenametypehostlocationload_1load_5load_15cpumemory_totalmemory_usedswap_totalswap_usedhdd_totalhdd_usednetwork_rxnetwork_txnetwork_innetwork_outlast_network_inlast_network_outping_10010ping_189ping_10086time_10010time_189time_10086tcp_countudp_countprocess_countthread_countio_readio_writeonline4online6

客户端断开 25 秒后仍未重连,服务端才计算离线规则,避免短暂网络波动触发告警。每个节点、每条规则分别记录冷却时间。

SSL 证书

证书检查使用 Go crypto/tls,不再调用外部 openssl。服务端记录到期时间、剩余天数和域名匹配状态,并保留原来的 7/3/1 天通知档位与冷却时间。

回调默认校验 HTTPS 证书。仅在必须兼容自签名回调服务时设置:

INSECURE_CALLBACK_TLS=true

源码编译和运行

需要 Go 1.25 或更高版本:

cd server
go mod download
go test ./...
go build -trimpath -ldflags='-s -w' -o serverstatus .

server/ 目录启动:

ADMIN_TOKEN='请替换为高强度随机字符串' \
./serverstatus \
--config=config.json \
--stats=../web/json/stats.json \
--web-dir=../web \
--http=:8080 \
--agent=:35601

访问 http://127.0.0.1:8080/。发送SIGHUP 可以重新读取配置:

kill -HUP "$(pgrep -x serverstatus)"

Systemd 示例位于 service/status-server.service。一键脚本 status.sh 也已切换到 Go 构建,但 Docker 仍是推荐部署方式。

构建和测试

Go 测试需要 Go 1.25+;WebUI 行为测试需要 Node.js 20+ 和 pnpm。

# Go 单元、协议、API、TLS 和回调测试cd server
go test ./...
go test -race ./...
go vet ./...
# Python 客户端指标、参数与平台识别测试cd ..
python3 -m unittest discover -s clients -p 'test_*.py'# WebUI Chromium 行为测试
pnpm install --frozen-lockfile
pnpm exec playwright install chromium
pnpm test:webui
# Docker 镜像
docker build -f Dockerfile.server -t cppla/serverstatus:server .
docker build -f Dockerfile.client -t cppla/serverstatus:client .# 服务端与客户端真实连接、认证和指标上报
SERVER_IMAGE=cppla/serverstatus:server \
CLIENT_IMAGE=cppla/serverstatus:client \
tests/docker-smoke.sh
# Compose 配置
docker compose -f docker-compose-server.yml config
docker compose -f docker-compose-client.yml config

Docker 联通测试在 Linux 上覆盖客户端的 host 网络和 pid 模式;Docker Desktop 未启用 Host Networking 时会明确提示并使用隔离 bridge 完成本地协议测试。

CI 还会运行 Chromium 行为测试、Docker 服务端/客户端联通测试,并验证客户端镜像可同时构建为 AMD64 和 ARM64。

从旧服务端迁移

  1. 备份原来的 config.jsonweb/json/stats.json
  2. 原配置结构和客户端账号无需转换。
  3. Docker 挂载目标改为 /app/config/config.json/app/data
  4. 删除旧的 nginx、manage_api.pysergate 启动或监督配置。
  5. 启动 Go 服务端后检查 /api/health,再观察客户端自动重连。

stats.json 会按节点的 name/type/host/location 恢复月流量基线。修改这些身份字段会被视为新节点。

致谢

, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Universal Dark Mode - works on any site\n(function() {\n var enabled = true;\n \n function applyDarkMode() {\n if (!enabled) return;\n \n // Create style element if it doesn't exist\n var style = document.getElementById('universal-dark-mode-style');\n if (!style) {\n style = document.createElement('style');\n style.id = 'universal-dark-mode-style';\n document.head.appendChild(style);\n }\n \n // Dark mode CSS - inverts colors but preserves images/video\n style.textContent = '\n /* Invert everything except media */\n html {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #1a1a2e !important;\n }\n \n /* Restore images, videos, iframes, canvas */\n img, video, iframe, canvas, svg, picture, [style*=\"background-image\"] {\n filter: invert(1) hue-rotate(180deg) !important;\n }\n \n /* Preserve specific elements that should not be inverted */\n .no-dark-mode, .no-dark-mode *,\n [data-theme=\"light\"], [data-theme=\"light\"],\n .ace_editor, .ace_editor *,\n .CodeMirror, .CodeMirror *,\n .monaco-editor, .monaco-editor *,\n .markdown-body pre, .markdown-body pre *,\n .highlight, .highlight *,\n pre code, pre code * {\n filter: none !important;\n }\n \n /* Fix common UI elements */\n .modal, .popup, .dropdown-menu, .tooltip, .popover {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #2d2d44 !important;\n border-color: #444 !important;\n }\n \n /* Scrollbars */\n ::-webkit-scrollbar { background: #1a1a2e !important; }\n ::-webkit-scrollbar-thumb { background: #444 !important; }\n ::-webkit-scrollbar-thumb:hover { background: #555 !important; }\n \n /* Selection */\n ::selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ::-moz-selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ';\n }\n \n function removeDarkMode() {\n var style = document.getElementById('universal-dark-mode-style');\n if (style) style.remove();\n }\n \n // Toggle with Alt+Shift+D\n document.addEventListener('keydown', function(e) {\n if (e.altKey && e.shiftKey && e.key === 'D') {\n e.preventDefault();\n enabled = !enabled;\n if (enabled) {\n applyDarkMode();\n console.log('[Universal Dark Mode] Enabled');\n } else {\n removeDarkMode();\n console.log('[Universal Dark Mode] Disabled');\n }\n }\n });\n \n // Apply on load\n applyDarkMode();\n \n // Re-apply on dynamic content\n var observer = new MutationObserver(function(mutations) {\n if (enabled && !document.getElementById('universal-dark-mode-style')) {\n applyDarkMode();\n }\n });\n observer.observe(document.head, { childList: true });\n \n console.log('[Universal Dark Mode] Loaded - Press Alt+Shift+D to toggle');\n})();", "Universal Dark Mode"); } } catch(__e) { console.warn('[Userscript:Universal Dark Mode]', __e); } })(); })();
Skip to content

Latest commit

History

History
357 lines (275 loc) · 12.4 KB

File metadata and controls

357 lines (275 loc) · 12.4 KB

ServerStatus 中文版

ServerStatus 是一个轻量的服务器探针和云监控面板,支持多节点在线状态、资源占用、三网延迟、服务监测、SSL 证书检查、Watchdog 告警、HTTP API 和 Web 配置管理。

在线演示:https://tz.cloudcpp.com

GoCILicenseVersion

ServerStatus WebUI

一、服务端

# Docker Compose,本地构建加:--build
ADMIN_TOKEN='your-strong-token' docker compose -f docker-compose-server.yml up -d 
# Docker Run
wget -qO ~/serverstatus-config.json \
--header='Accept: application/vnd.github.raw' \
'https://api.github.com/repos/cppla/ServerStatus/contents/server/config.json?ref=master'
mkdir -p ~/serverstatus-data
docker run -d --restart=always --name=serverstatus-server \
-e ADMIN_TOKEN='your-strong-token' \
-v ~/serverstatus-config.json:/app/config/config.json \
-v ~/serverstatus-data:/app/data \
-p 8080:80 -p 35601:35601 \
cppla/serverstatus:server

启动后访问:

ADMIN_TOKEN 不设置时,监控页面仍可读取,管理 API 返回 503,WebUI 的“配置”页不能修改数据。

二、客户端

# Docker Compose,本地构建加:--build
SERVER=127.0.0.1 USER=s01 PASSWORD=USER_DEFAULT_PASSWORD \
docker compose -f docker-compose-client.yml up -d --force-recreate
# Docker Run
docker run -d --restart=always --name=serverstatus-client \
--network=host --pid=host \
-e SERVER=127.0.0.1 \
-e USER=s01 \
-e PASSWORD=USER_DEFAULT_PASSWORD \
cppla/serverstatus:client
# Shell Run
wget -qO client-linux.py --header='Accept: application/vnd.github.raw' \
'https://api.github.com/repos/cppla/ServerStatus/contents/clients/client-linux.py?ref=master'
nohup python3 client-linux.py SERVER=127.0.0.1 USER=s01 PASSWORD=USER_DEFAULT_PASSWORD >/dev/null 2>&1&

USER 是常见的宿主机环境变量名。如果没有显式传递或传递方式错误,Compose 可能会把系统中的 $USER 解析成本机用户名,而不是默认的 s01。推荐优先级:

  1. 运行命令显式传递 USER=s01
  2. 修改 docker-compose-client.yml 中的 USER 默认值
  3. Docker 或系统环境中的 USER
变量默认值说明
SERVER127.0.0.1Go 服务端地址
USERs01客户端用户名,必须匹配服务端配置
PORT35601Agent TCP 上报端口
PASSWORDUSER_DEFAULT_PASSWORD客户端密码,必须匹配服务端配置
INTERVAL1状态上报间隔,单位秒
PROBEPORT80三网 TCP 探测端口
PROBE_PROTOCOL_PREFERipv4探测协议偏好,可选 ipv4ipv6
PING_PACKET_HISTORY_LEN100丢包历史窗口
CUcu.tz.cloudcpp.com联通探测地址
CTct.tz.cloudcpp.com电信探测地址
CMcm.tz.cloudcpp.com移动探测地址
CLIENTpsutil客户端实现,可选 psutillinux

服务端参数

Docker 镜像中的默认路径为:

环境变量默认值说明
CONFIG_PATH/app/config/config.json主配置文件
STATS_PATH/app/data/stats.json月流量与状态持久化文件;每 60 秒写入,关键操作与正常退出时立即写入
WEB_DIR/app/webWebUI 静态文件目录
HTTP_ADDR:80WebUI 与 HTTP API 监听地址
AGENT_ADDR:35601客户端 TCP 上报监听地址
ADMIN_TOKEN管理 API Bearer Token;为空时禁用管理接口
ADMIN_CORS_ORIGIN可选的 API CORS Origin
INSECURE_CALLBACK_TLSfalse是否允许 Watchdog/证书回调使用不可信 TLS 证书
VERBOSEfalse输出 Gin HTTP 请求日志
TZAsia/Shanghai容器时区

对应命令行参数:

--config, -c config.json 路径
--stats stats.json 路径
--web-dir, -d WebUI 目录
--http HTTP 监听地址
--agent Agent TCP 监听地址
--verbose, -v 详细请求日志
--version 输出构建版本

旧参数 --bind/-b--port/-p 仍可用于设置 Agent TCP 监听地址。

HTTP 管理 API

管理接口使用 Bearer Token:

Authorization: Bearer <ADMIN_TOKEN>

无需认证:

方法路径用途
GET/api/health进程、Agent TCP、版本和配置路径状态
GET/api/schema机器可读的端点与配置集合描述
GET/api/openapi.json可供 AI Agent 直接导入的 OpenAPI 3.1 文档
GET/json/stats.jsonWebUI 使用的实时状态快照

需要认证:

方法路径用途
GET/PUT/api/config读取或整体替换配置
GET/POST/api/servers查询或新增节点
PUT/DELETE/api/servers/{username}修改或删除节点
POST/api/servers/{username}/reset-traffic将当前流量设为本月基线
GET/POST/api/monitors查询或新增服务监测
PUT/DELETE/api/monitors/{index-or-name}修改或删除服务监测
GET/POST/api/sslcerts查询或新增证书监测
PUT/DELETE/api/sslcerts/{index-or-name}修改或删除证书监测
GET/POST/api/watchdog查询或新增 Watchdog
PUT/DELETE/api/watchdog/{index-or-name}修改或删除 Watchdog
POST/api/reload从磁盘重新读取配置
POST/api/restart在进程内重启采集运行时

配置修改采用“校验 → 备份 → 持久化 → 原子切换”的顺序。成功后现有 Agent 连接会被关闭,Python 客户端约 3 秒后自动重连并获取新的 monitors/api/restart 不退出 Go 进程,因此 Docker 和手动运行方式具有一致语义。

常用调用:

TOKEN='请替换为 ADMIN_TOKEN'
curl http://127.0.0.1:8080/api/health
curl -H "Authorization: Bearer ${TOKEN}" \
http://127.0.0.1:8080/api/config
curl -X POST http://127.0.0.1:8080/api/servers \
-H "Authorization: Bearer ${TOKEN}" \
-H 'Content-Type: application/json' \
-d '{"username":"s05","name":"node5","type":"kvm","host":"host5","location":"SG","password":"change-me","monthstart":1}'
curl -X DELETE \
-H "Authorization: Bearer ${TOKEN}" \
http://127.0.0.1:8080/api/servers/s05

请求体最大为 1 MiB。AI Agent 可直接导入 /api/openapi.json;轻量客户端也可以先读取 /api/schema,再根据返回的集合字段调用 CRUD 接口。

配置文件

{
"servers": [
{
"username": "s01",
"name": "node1",
"type": "kvm",
"host": "host1",
"location": "CN",
"password": "USER_DEFAULT_PASSWORD",
"monthstart": 1,
"disabled": false
}
],
"monitors": [
{
"name": "example",
"host": "https://example.com",
"interval": 600,
"type": "https"
}
],
"sslcerts": [
{
"name": "example",
"domain": "https://example.com",
"port": 443,
"interval": 7200,
"callback": "https://example.net/push?message="
}
],
"watchdog": [
{
"name": "offline warning",
"rule": "online4=0&online6=0",
"interval": 600,
"callback": "https://example.net/push?message="
}
]
}

约束:

  • servers.username 必须唯一。
  • monthstart 自动限制在 1-28
  • port 自动限制在 1-65535
  • interval 最小为 1 秒;Watchdog 中表示通知冷却时间,不是客户端采集间隔。
  • 配置写入前会创建 config.json.bak-*,最多保留 10 份。
  • Docker 单文件 bind mount 无法被 rename 覆盖时,服务端会在完成备份后安全地写回原 inode。

使用 Docker 单文件挂载时,配置备份位于容器 /app/config 的可写层;如需长期保留历史版本,建议同时在宿主机备份 server/config.json

Watchdog 表达式

rule 由 Go expr 引擎执行,并兼容旧版 Exprtk 的常用写法。Go 服务会把字符串外的单个操作符自动转换:

& -> &&
| -> ||
= -> ==

例如以下两种写法等价:

cpu>90&load_1>5&username!='s01'
cpu>90 && load_1>5 && username!='s01'

字符串值支持中文、Emoji 和其他 Unicode 字符,例如:

username='节点一号'&name='上海节点'&location='中国 🇨🇳'&type='云主机'

字段名必须使用系统定义的英文名称。可用字段包括:usernamenametypehostlocationload_1load_5load_15cpumemory_totalmemory_usedswap_totalswap_usedhdd_totalhdd_usednetwork_rxnetwork_txnetwork_innetwork_outlast_network_inlast_network_outping_10010ping_189ping_10086time_10010time_189time_10086tcp_countudp_countprocess_countthread_countio_readio_writeonline4online6

客户端断开 25 秒后仍未重连,服务端才计算离线规则,避免短暂网络波动触发告警。每个节点、每条规则分别记录冷却时间。

SSL 证书

证书检查使用 Go crypto/tls,不再调用外部 openssl。服务端记录到期时间、剩余天数和域名匹配状态,并保留原来的 7/3/1 天通知档位与冷却时间。

回调默认校验 HTTPS 证书。仅在必须兼容自签名回调服务时设置:

INSECURE_CALLBACK_TLS=true

源码编译和运行

需要 Go 1.25 或更高版本:

cd server
go mod download
go test ./...
go build -trimpath -ldflags='-s -w' -o serverstatus .

server/ 目录启动:

ADMIN_TOKEN='请替换为高强度随机字符串' \
./serverstatus \
--config=config.json \
--stats=../web/json/stats.json \
--web-dir=../web \
--http=:8080 \
--agent=:35601

访问 http://127.0.0.1:8080/。发送SIGHUP 可以重新读取配置:

kill -HUP "$(pgrep -x serverstatus)"

Systemd 示例位于 service/status-server.service。一键脚本 status.sh 也已切换到 Go 构建,但 Docker 仍是推荐部署方式。

构建和测试

Go 测试需要 Go 1.25+;WebUI 行为测试需要 Node.js 20+ 和 pnpm。

# Go 单元、协议、API、TLS 和回调测试cd server
go test ./...
go test -race ./...
go vet ./...
# Python 客户端指标、参数与平台识别测试cd ..
python3 -m unittest discover -s clients -p 'test_*.py'# WebUI Chromium 行为测试
pnpm install --frozen-lockfile
pnpm exec playwright install chromium
pnpm test:webui
# Docker 镜像
docker build -f Dockerfile.server -t cppla/serverstatus:server .
docker build -f Dockerfile.client -t cppla/serverstatus:client .# 服务端与客户端真实连接、认证和指标上报
SERVER_IMAGE=cppla/serverstatus:server \
CLIENT_IMAGE=cppla/serverstatus:client \
tests/docker-smoke.sh
# Compose 配置
docker compose -f docker-compose-server.yml config
docker compose -f docker-compose-client.yml config

Docker 联通测试在 Linux 上覆盖客户端的 host 网络和 pid 模式;Docker Desktop 未启用 Host Networking 时会明确提示并使用隔离 bridge 完成本地协议测试。

CI 还会运行 Chromium 行为测试、Docker 服务端/客户端联通测试,并验证客户端镜像可同时构建为 AMD64 和 ARM64。

从旧服务端迁移

  1. 备份原来的 config.jsonweb/json/stats.json
  2. 原配置结构和客户端账号无需转换。
  3. Docker 挂载目标改为 /app/config/config.json/app/data
  4. 删除旧的 nginx、manage_api.pysergate 启动或监督配置。
  5. 启动 Go 服务端后检查 /api/health,再观察客户端自动重连。

stats.json 会按节点的 name/type/host/location 恢复月流量基线。修改这些身份字段会被视为新节点。

致谢