Compare commits
6 Commits
ccfa49bc7d
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| e136f1b290 | |||
| 758d3d1c59 | |||
| 65f521dce0 | |||
| 6e60fae559 | |||
| dc05fa01d1 | |||
| 360743ba6b |
84
README.md
84
README.md
@@ -19,7 +19,7 @@
|
|||||||
|
|
||||||
客户端调用:
|
客户端调用:
|
||||||
|
|
||||||
- `POST /monitor/server/heartbeat-v2`
|
- `POST /monitor/server/heartbeat`
|
||||||
- Header: `X-API-Key`
|
- Header: `X-API-Key`
|
||||||
|
|
||||||
## 项目结构
|
## 项目结构
|
||||||
@@ -29,7 +29,9 @@ HarborForge.Monitor/
|
|||||||
├── cmd/harborforge-monitor/ # 程序入口
|
├── cmd/harborforge-monitor/ # 程序入口
|
||||||
├── internal/config/ # 配置加载
|
├── internal/config/ # 配置加载
|
||||||
├── internal/telemetry/ # 指标采集与上报
|
├── internal/telemetry/ # 指标采集与上报
|
||||||
|
├── internal/bridge/ # MONITOR_PORT 本地桥接服务
|
||||||
├── Dockerfile # 容器化运行
|
├── Dockerfile # 容器化运行
|
||||||
|
├── docker-compose.yml # Docker Compose 配置
|
||||||
├── config.example.json
|
├── config.example.json
|
||||||
└── README.md
|
└── README.md
|
||||||
```
|
```
|
||||||
@@ -46,7 +48,8 @@ HarborForge.Monitor/
|
|||||||
"identifier": "vps-nginx-01",
|
"identifier": "vps-nginx-01",
|
||||||
"apiKey": "your-api-key",
|
"apiKey": "your-api-key",
|
||||||
"reportIntervalSec": 30,
|
"reportIntervalSec": 30,
|
||||||
"logLevel": "info"
|
"logLevel": "info",
|
||||||
|
"monitorPort": 9100
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -61,6 +64,35 @@ HarborForge.Monitor/
|
|||||||
|
|
||||||
同时也兼容旧的/正确拼写的 `HF_MONITOR_*` 变量名。
|
同时也兼容旧的/正确拼写的 `HF_MONITOR_*` 变量名。
|
||||||
|
|
||||||
|
### MONITOR_PORT — 插件桥接端口
|
||||||
|
|
||||||
|
当 `MONITOR_PORT` 设置为大于 0 的值时,Monitor 会在 `127.0.0.1:<MONITOR_PORT>` 上启动一个本地 HTTP 服务,供 HarborForge OpenClaw 插件查询遥测数据。
|
||||||
|
|
||||||
|
支持的端点:
|
||||||
|
|
||||||
|
| 端点 | 说明 |
|
||||||
|
|------|------|
|
||||||
|
| `GET /health` | 健康检查,返回 Monitor 版本和标识符 |
|
||||||
|
| `GET /telemetry` | 返回最新的遥测数据快照 |
|
||||||
|
| `POST /openclaw` | 接收 OpenClaw 插件推送的元数据(版本、代理等) |
|
||||||
|
|
||||||
|
### OpenClaw 元数据 enrichment
|
||||||
|
|
||||||
|
当 OpenClaw 插件通过 `POST /openclaw` 推送元数据后,Monitor 会在后续的心跳上报中自动将这些信息附加到遥测数据中:
|
||||||
|
|
||||||
|
- `openclaw_version` — OpenClaw 运行时版本
|
||||||
|
- `plugin_version` — 插件版本
|
||||||
|
- `agents` — 代理列表
|
||||||
|
|
||||||
|
如果插件从未推送过元数据,这些字段会被省略,心跳上报完全不受影响。
|
||||||
|
|
||||||
|
**重要**:桥接端口是可选的。如果 `MONITOR_PORT` 为 0 或未设置,桥接服务不会启动,Monitor 的心跳上报功能完全不受影响。即使桥接服务启动失败,心跳上报也会继续正常工作。
|
||||||
|
|
||||||
|
环境变量:
|
||||||
|
|
||||||
|
- `MONITOR_PORT` — 首选
|
||||||
|
- `HF_MONITOR_PORT` — 备选
|
||||||
|
|
||||||
## 本地开发
|
## 本地开发
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
@@ -77,34 +109,46 @@ go build ./cmd/harborforge-monitor
|
|||||||
docker build -t harborforge-monitor .
|
docker build -t harborforge-monitor .
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### 使用 Docker Compose
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# 设置环境变量
|
||||||
|
export HF_IDENTIFIER=my-server
|
||||||
|
export HF_API_KEY=your-api-key
|
||||||
|
export MONITOR_PORT=9100
|
||||||
|
|
||||||
|
# 启动
|
||||||
|
docker compose up -d
|
||||||
|
```
|
||||||
|
|
||||||
|
### 手动 Docker 运行
|
||||||
|
|
||||||
推荐以**宿主机 rootfs 只读挂载**方式运行,这样容器里采集到的是宿主机信息而不是容器自身:
|
推荐以**宿主机 rootfs 只读挂载**方式运行,这样容器里采集到的是宿主机信息而不是容器自身:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
docker run -d \
|
docker run -d \
|
||||||
--name harborforge-monitor \
|
--name harborforge-monitor \
|
||||||
--restart unless-stopped \
|
--restart unless-stopped \
|
||||||
-e HF_MONITER_BACKEND_URL=https://monitor.hangman-lab.top \
|
--network host \
|
||||||
-e HF_MONITER_IDENTIFIER=vps-nginx-01 \
|
|
||||||
-e HF_MONITER_API_KEY=your-api-key \
|
|
||||||
-e HF_MONITER_REPORT_INTERVAL=30 \
|
|
||||||
-e HF_MONITER_ROOTFS=/host \
|
|
||||||
-v /:/host:ro \
|
-v /:/host:ro \
|
||||||
|
-e HF_MONITER_BACKEND_URL=https://monitor.hangman-lab.top \
|
||||||
|
-e HF_MONITER_IDENTIFIER=my-server \
|
||||||
|
-e HF_MONITER_API_KEY=your-api-key \
|
||||||
|
-e HF_MONITER_ROOTFS=/host \
|
||||||
|
-e MONITOR_PORT=9100 \
|
||||||
harborforge-monitor
|
harborforge-monitor
|
||||||
```
|
```
|
||||||
|
|
||||||
`Dockerfile` 里已经预置了这些环境变量:
|
## systemd
|
||||||
|
|
||||||
- `HF_MONITER_BACKEND_URL`
|
也可以直接用 systemd 运行编译好的二进制:
|
||||||
- `HF_MONITER_IDENTIFIER`
|
|
||||||
- `HF_MONITER_API_KEY`
|
|
||||||
- `HF_MONITER_REPORT_INTERVAL`
|
|
||||||
- `HF_MONITER_LOG_LEVEL`
|
|
||||||
- `HF_MONITER_ROOTFS`
|
|
||||||
|
|
||||||
## 注意
|
```bash
|
||||||
|
# 编译
|
||||||
|
go build -o /usr/local/bin/harborforge-monitor ./cmd/harborforge-monitor
|
||||||
|
|
||||||
- Docker 模式下,建议挂载 `-v /:/host:ro` 并设置 `HF_MONITER_ROOTFS=/host`
|
# 复制 systemd unit (见 systemd/ 目录)
|
||||||
- 这样 CPU/MEM/LOAD/UPTIME 会通过 host proc/sys 视角采集,磁盘和 nginx 配置也会走宿主机路径
|
cp systemd/harborforge-monitor.service /etc/systemd/system/
|
||||||
- 当前 Nginx site 列表读取的是 `${ROOTFS}/etc/nginx/sites-enabled`
|
systemctl daemon-reload
|
||||||
- 如果机器没有安装 Nginx,会回报 `nginx_installed = false`
|
systemctl enable --now harborforge-monitor
|
||||||
- 该客户端不会尝试读取 OpenClaw 信息,`agents` 默认为空,`openclaw_version` 不上报
|
```
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ import (
|
|||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"git.hangman-lab.top/zhi/HarborForge.Monitor/internal/bridge"
|
||||||
"git.hangman-lab.top/zhi/HarborForge.Monitor/internal/config"
|
"git.hangman-lab.top/zhi/HarborForge.Monitor/internal/config"
|
||||||
"git.hangman-lab.top/zhi/HarborForge.Monitor/internal/telemetry"
|
"git.hangman-lab.top/zhi/HarborForge.Monitor/internal/telemetry"
|
||||||
)
|
)
|
||||||
@@ -23,12 +24,26 @@ func main() {
|
|||||||
printPayload bool
|
printPayload bool
|
||||||
dryRun bool
|
dryRun bool
|
||||||
showVersion bool
|
showVersion bool
|
||||||
|
backendURL string
|
||||||
|
identifier string
|
||||||
|
apiKey string
|
||||||
|
reportInt int
|
||||||
|
logLevel string
|
||||||
|
rootFS string
|
||||||
|
monitorPort int
|
||||||
)
|
)
|
||||||
flag.StringVar(&configPath, "config", "/etc/harborforge-monitor/config.json", "Path to config file")
|
flag.StringVar(&configPath, "config", "/etc/harborforge-monitor/config.json", "Path to config file")
|
||||||
flag.BoolVar(&runOnce, "once", false, "Collect and send telemetry once, then exit")
|
flag.BoolVar(&runOnce, "once", false, "Collect and send telemetry once, then exit")
|
||||||
flag.BoolVar(&printPayload, "print-payload", false, "Print payload JSON before sending")
|
flag.BoolVar(&printPayload, "print-payload", false, "Print payload JSON before sending")
|
||||||
flag.BoolVar(&dryRun, "dry-run", false, "Collect telemetry but do not send it")
|
flag.BoolVar(&dryRun, "dry-run", false, "Collect telemetry but do not send it")
|
||||||
flag.BoolVar(&showVersion, "version", false, "Print version and exit")
|
flag.BoolVar(&showVersion, "version", false, "Print version and exit")
|
||||||
|
flag.StringVar(&backendURL, "backend-url", "", "Override backend URL")
|
||||||
|
flag.StringVar(&identifier, "identifier", "", "Override identifier")
|
||||||
|
flag.StringVar(&apiKey, "api-key", "", "Override API key")
|
||||||
|
flag.IntVar(&reportInt, "report-interval", 0, "Override report interval in seconds")
|
||||||
|
flag.StringVar(&logLevel, "log-level", "", "Override log level")
|
||||||
|
flag.StringVar(&rootFS, "rootfs", "", "Override root filesystem path")
|
||||||
|
flag.IntVar(&monitorPort, "monitor-port", 0, "Override monitor bridge port")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
if showVersion {
|
if showVersion {
|
||||||
@@ -36,7 +51,15 @@ func main() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
cfg, err := config.Load(configPath)
|
cfg, err := config.LoadWithOverrides(configPath, config.Overrides{
|
||||||
|
BackendURL: backendURL,
|
||||||
|
Identifier: identifier,
|
||||||
|
APIKey: apiKey,
|
||||||
|
ReportIntervalSec: reportInt,
|
||||||
|
LogLevel: logLevel,
|
||||||
|
RootFS: rootFS,
|
||||||
|
MonitorPort: monitorPort,
|
||||||
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("load config: %v", err)
|
log.Fatalf("load config: %v", err)
|
||||||
}
|
}
|
||||||
@@ -50,11 +73,43 @@ func main() {
|
|||||||
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
|
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
|
||||||
defer stop()
|
defer stop()
|
||||||
|
|
||||||
|
// Start the bridge server if MONITOR_PORT is configured.
|
||||||
|
// The bridge is independent of heartbeat — if it fails to start,
|
||||||
|
// heartbeat continues normally.
|
||||||
|
var bridgeSrv *bridge.Server
|
||||||
|
if cfg.MonitorPort > 0 {
|
||||||
|
bridgeSrv = bridge.New(cfg, logger)
|
||||||
|
go func() {
|
||||||
|
if err := bridgeSrv.Start(ctx); err != nil {
|
||||||
|
logger.Printf("bridge error (non-fatal): %v", err)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
|
||||||
sendOnce := func() error {
|
sendOnce := func() error {
|
||||||
payload, err := telemetry.BuildPayload(ctx, cfg)
|
payload, err := telemetry.BuildPayload(ctx, cfg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Update bridge with latest telemetry
|
||||||
|
if bridgeSrv != nil {
|
||||||
|
bridgeSrv.UpdatePayload(payload)
|
||||||
|
|
||||||
|
// Enrich payload with OpenClaw metadata if available
|
||||||
|
if meta := bridgeSrv.GetOpenClawMeta(); meta != nil {
|
||||||
|
if meta.Version != "" {
|
||||||
|
payload.OpenClawVersion = meta.Version
|
||||||
|
}
|
||||||
|
if meta.PluginVersion != "" {
|
||||||
|
payload.PluginVersion = meta.PluginVersion
|
||||||
|
}
|
||||||
|
if len(meta.Agents) > 0 {
|
||||||
|
payload.Agents = meta.Agents
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if printPayload || dryRun {
|
if printPayload || dryRun {
|
||||||
buf, _ := json.MarshalIndent(payload, "", " ")
|
buf, _ := json.MarshalIndent(payload, "", " ")
|
||||||
fmt.Println(string(buf))
|
fmt.Println(string(buf))
|
||||||
|
|||||||
@@ -5,5 +5,5 @@
|
|||||||
"reportIntervalSec": 30,
|
"reportIntervalSec": 30,
|
||||||
"logLevel": "info",
|
"logLevel": "info",
|
||||||
"rootFs": "/host",
|
"rootFs": "/host",
|
||||||
"monitorPort": 0
|
"monitorPort": 9100
|
||||||
}
|
}
|
||||||
|
|||||||
22
docker-compose.yml
Normal file
22
docker-compose.yml
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
version: "3.8"
|
||||||
|
|
||||||
|
services:
|
||||||
|
harborforge-monitor:
|
||||||
|
build: .
|
||||||
|
container_name: harborforge-monitor
|
||||||
|
restart: unless-stopped
|
||||||
|
environment:
|
||||||
|
- HF_MONITER_BACKEND_URL=https://monitor.hangman-lab.top
|
||||||
|
- HF_MONITER_IDENTIFIER=${HF_IDENTIFIER:-}
|
||||||
|
- HF_MONITER_API_KEY=${HF_API_KEY:-}
|
||||||
|
- HF_MONITER_REPORT_INTERVAL=${HF_REPORT_INTERVAL:-30}
|
||||||
|
- HF_MONITER_LOG_LEVEL=${HF_LOG_LEVEL:-info}
|
||||||
|
- HF_MONITER_ROOTFS=/host
|
||||||
|
- MONITOR_PORT=${MONITOR_PORT:-0}
|
||||||
|
volumes:
|
||||||
|
- /:/host:ro
|
||||||
|
# network_mode: host shares the host network namespace, so the bridge
|
||||||
|
# server (if MONITOR_PORT > 0) listens directly on the host's
|
||||||
|
# 127.0.0.1:<MONITOR_PORT>. `ports:` is ignored under network_mode:
|
||||||
|
# host, so it is intentionally omitted.
|
||||||
|
network_mode: host
|
||||||
187
internal/bridge/bridge.go
Normal file
187
internal/bridge/bridge.go
Normal file
@@ -0,0 +1,187 @@
|
|||||||
|
// Package bridge provides a local HTTP server on MONITOR_PORT for
|
||||||
|
// communication between the HarborForge OpenClaw plugin and Monitor.
|
||||||
|
//
|
||||||
|
// The bridge serves two purposes:
|
||||||
|
// 1. Expose hardware telemetry to the plugin via GET /telemetry
|
||||||
|
// 2. Receive OpenClaw metadata from the plugin via POST /openclaw
|
||||||
|
//
|
||||||
|
// The bridge is optional: if monitorPort is 0 or not set, the bridge
|
||||||
|
// is not started and Monitor operates normally.
|
||||||
|
package bridge
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"log"
|
||||||
|
"net"
|
||||||
|
"net/http"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"git.hangman-lab.top/zhi/HarborForge.Monitor/internal/config"
|
||||||
|
"git.hangman-lab.top/zhi/HarborForge.Monitor/internal/telemetry"
|
||||||
|
)
|
||||||
|
|
||||||
|
// OpenClawMeta holds metadata received from the OpenClaw plugin.
|
||||||
|
// This data is optional enrichment for heartbeat uploads.
|
||||||
|
type OpenClawMeta struct {
|
||||||
|
Version string `json:"version"`
|
||||||
|
PluginVersion string `json:"plugin_version"`
|
||||||
|
Agents []any `json:"agents,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// Server is the local bridge HTTP server.
|
||||||
|
type Server struct {
|
||||||
|
cfg config.Config
|
||||||
|
logger *log.Logger
|
||||||
|
srv *http.Server
|
||||||
|
|
||||||
|
mu sync.RWMutex
|
||||||
|
lastPayload *telemetry.Payload
|
||||||
|
lastUpdated time.Time
|
||||||
|
|
||||||
|
openclawMeta *OpenClawMeta
|
||||||
|
openclawUpdated time.Time
|
||||||
|
}
|
||||||
|
|
||||||
|
// New creates a bridge server. It does not start listening.
|
||||||
|
func New(cfg config.Config, logger *log.Logger) *Server {
|
||||||
|
return &Server{
|
||||||
|
cfg: cfg,
|
||||||
|
logger: logger,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// UpdatePayload stores the latest telemetry payload so the bridge can
|
||||||
|
// serve it to plugin queries without re-collecting.
|
||||||
|
func (s *Server) UpdatePayload(p telemetry.Payload) {
|
||||||
|
s.mu.Lock()
|
||||||
|
defer s.mu.Unlock()
|
||||||
|
s.lastPayload = &p
|
||||||
|
s.lastUpdated = time.Now()
|
||||||
|
}
|
||||||
|
|
||||||
|
// bridgeResponse is the JSON structure served to the plugin.
|
||||||
|
type bridgeResponse struct {
|
||||||
|
Status string `json:"status"`
|
||||||
|
MonitorVer string `json:"monitor_version"`
|
||||||
|
Identifier string `json:"identifier"`
|
||||||
|
Telemetry *telemetry.Payload `json:"telemetry,omitempty"`
|
||||||
|
LastUpdated *time.Time `json:"last_updated,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetOpenClawMeta returns the latest OpenClaw metadata received from
|
||||||
|
// the plugin, or nil if no metadata has been received.
|
||||||
|
func (s *Server) GetOpenClawMeta() *OpenClawMeta {
|
||||||
|
s.mu.RLock()
|
||||||
|
defer s.mu.RUnlock()
|
||||||
|
return s.openclawMeta
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Server) handler() http.Handler {
|
||||||
|
mux := http.NewServeMux()
|
||||||
|
|
||||||
|
// Health / discovery endpoint
|
||||||
|
mux.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
json.NewEncoder(w).Encode(map[string]string{
|
||||||
|
"status": "ok",
|
||||||
|
"monitor_version": telemetry.Version,
|
||||||
|
"identifier": s.cfg.Identifier,
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
// Telemetry endpoint — returns the latest cached payload
|
||||||
|
mux.HandleFunc("/telemetry", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
s.mu.RLock()
|
||||||
|
payload := s.lastPayload
|
||||||
|
updated := s.lastUpdated
|
||||||
|
s.mu.RUnlock()
|
||||||
|
|
||||||
|
resp := bridgeResponse{
|
||||||
|
Status: "ok",
|
||||||
|
MonitorVer: telemetry.Version,
|
||||||
|
Identifier: s.cfg.Identifier,
|
||||||
|
}
|
||||||
|
if payload != nil {
|
||||||
|
resp.Telemetry = payload
|
||||||
|
resp.LastUpdated = &updated
|
||||||
|
}
|
||||||
|
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
json.NewEncoder(w).Encode(resp)
|
||||||
|
})
|
||||||
|
|
||||||
|
// OpenClaw metadata endpoint — plugin POSTs its metadata here
|
||||||
|
mux.HandleFunc("/openclaw", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if r.Method != http.MethodPost {
|
||||||
|
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
body, err := io.ReadAll(io.LimitReader(r.Body, 64*1024))
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, "read error", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer r.Body.Close()
|
||||||
|
|
||||||
|
var meta OpenClawMeta
|
||||||
|
if err := json.Unmarshal(body, &meta); err != nil {
|
||||||
|
http.Error(w, "invalid json", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
s.mu.Lock()
|
||||||
|
s.openclawMeta = &meta
|
||||||
|
s.openclawUpdated = time.Now()
|
||||||
|
s.mu.Unlock()
|
||||||
|
|
||||||
|
s.logger.Printf("received OpenClaw metadata: version=%s plugin=%s agents=%d",
|
||||||
|
meta.Version, meta.PluginVersion, len(meta.Agents))
|
||||||
|
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
json.NewEncoder(w).Encode(map[string]string{
|
||||||
|
"status": "ok",
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
return mux
|
||||||
|
}
|
||||||
|
|
||||||
|
// Start begins listening on 127.0.0.1:<monitorPort>. It blocks until
|
||||||
|
// the context is cancelled or an error occurs.
|
||||||
|
func (s *Server) Start(ctx context.Context) error {
|
||||||
|
if s.cfg.MonitorPort <= 0 {
|
||||||
|
return nil // bridge disabled
|
||||||
|
}
|
||||||
|
|
||||||
|
addr := fmt.Sprintf("127.0.0.1:%d", s.cfg.MonitorPort)
|
||||||
|
listener, err := net.Listen("tcp", addr)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("bridge listen on %s: %w", addr, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
s.srv = &http.Server{
|
||||||
|
Handler: s.handler(),
|
||||||
|
ReadTimeout: 5 * time.Second,
|
||||||
|
WriteTimeout: 5 * time.Second,
|
||||||
|
IdleTimeout: 30 * time.Second,
|
||||||
|
}
|
||||||
|
|
||||||
|
s.logger.Printf("bridge listening on %s", addr)
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
<-ctx.Done()
|
||||||
|
shutCtx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
||||||
|
defer cancel()
|
||||||
|
s.srv.Shutdown(shutCtx)
|
||||||
|
}()
|
||||||
|
|
||||||
|
if err := s.srv.Serve(listener); err != nil && err != http.ErrServerClosed {
|
||||||
|
return fmt.Errorf("bridge serve: %w", err)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
@@ -5,6 +5,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
@@ -17,10 +18,34 @@ type Config struct {
|
|||||||
MonitorPort int `json:"monitorPort"`
|
MonitorPort int `json:"monitorPort"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type Overrides struct {
|
||||||
|
BackendURL string
|
||||||
|
Identifier string
|
||||||
|
APIKey string
|
||||||
|
ReportIntervalSec int
|
||||||
|
LogLevel string
|
||||||
|
RootFS string
|
||||||
|
MonitorPort int
|
||||||
|
}
|
||||||
|
|
||||||
func Load(path string) (Config, error) {
|
func Load(path string) (Config, error) {
|
||||||
|
return LoadWithOverrides(path, Overrides{})
|
||||||
|
}
|
||||||
|
|
||||||
|
func LoadWithOverrides(path string, overrides Overrides) (Config, error) {
|
||||||
|
// If running inside a container with the host FS bind-mounted, prefer
|
||||||
|
// the host's /etc/hostname for the default identifier. The container's
|
||||||
|
// own os.Hostname() is a docker-assigned random string under
|
||||||
|
// network_mode: host (UTS namespace is not shared).
|
||||||
|
rootFSEarly := getenvAny([]string{"HF_MONITER_ROOTFS", "HF_MONITOR_ROOTFS"}, "")
|
||||||
|
defaultIdentifier := hostHostname(rootFSEarly)
|
||||||
|
if defaultIdentifier == "" {
|
||||||
|
defaultIdentifier = hostnameOr("unknown-host")
|
||||||
|
}
|
||||||
|
|
||||||
cfg := Config{
|
cfg := Config{
|
||||||
BackendURL: getenvAny([]string{"HF_MONITER_BACKEND_URL", "HF_MONITOR_BACKEND_URL"}, "https://monitor.hangman-lab.top"),
|
BackendURL: getenvAny([]string{"HF_MONITER_BACKEND_URL", "HF_MONITOR_BACKEND_URL"}, "https://monitor.hangman-lab.top"),
|
||||||
Identifier: getenvAny([]string{"HF_MONITER_IDENTIFIER", "HF_MONITOR_IDENTIFIER"}, hostnameOr("unknown-host")),
|
Identifier: getenvAny([]string{"HF_MONITER_IDENTIFIER", "HF_MONITOR_IDENTIFIER"}, defaultIdentifier),
|
||||||
APIKey: getenvAny([]string{"HF_MONITER_API_KEY", "HF_MONITOR_API_KEY"}, ""),
|
APIKey: getenvAny([]string{"HF_MONITER_API_KEY", "HF_MONITOR_API_KEY"}, ""),
|
||||||
ReportIntervalSec: getenvIntAny([]string{"HF_MONITER_REPORT_INTERVAL", "HF_MONITOR_REPORT_INTERVAL"}, 30),
|
ReportIntervalSec: getenvIntAny([]string{"HF_MONITER_REPORT_INTERVAL", "HF_MONITOR_REPORT_INTERVAL"}, 30),
|
||||||
LogLevel: getenvAny([]string{"HF_MONITER_LOG_LEVEL", "HF_MONITOR_LOG_LEVEL"}, "info"),
|
LogLevel: getenvAny([]string{"HF_MONITER_LOG_LEVEL", "HF_MONITOR_LOG_LEVEL"}, "info"),
|
||||||
@@ -46,6 +71,28 @@ func Load(path string) (Config, error) {
|
|||||||
cfg.RootFS = getenvAny([]string{"HF_MONITER_ROOTFS", "HF_MONITOR_ROOTFS"}, cfg.RootFS)
|
cfg.RootFS = getenvAny([]string{"HF_MONITER_ROOTFS", "HF_MONITOR_ROOTFS"}, cfg.RootFS)
|
||||||
cfg.MonitorPort = getenvIntAny([]string{"MONITOR_PORT", "HF_MONITOR_PORT"}, cfg.MonitorPort)
|
cfg.MonitorPort = getenvIntAny([]string{"MONITOR_PORT", "HF_MONITOR_PORT"}, cfg.MonitorPort)
|
||||||
|
|
||||||
|
if overrides.BackendURL != "" {
|
||||||
|
cfg.BackendURL = overrides.BackendURL
|
||||||
|
}
|
||||||
|
if overrides.Identifier != "" {
|
||||||
|
cfg.Identifier = overrides.Identifier
|
||||||
|
}
|
||||||
|
if overrides.APIKey != "" {
|
||||||
|
cfg.APIKey = overrides.APIKey
|
||||||
|
}
|
||||||
|
if overrides.ReportIntervalSec > 0 {
|
||||||
|
cfg.ReportIntervalSec = overrides.ReportIntervalSec
|
||||||
|
}
|
||||||
|
if overrides.LogLevel != "" {
|
||||||
|
cfg.LogLevel = overrides.LogLevel
|
||||||
|
}
|
||||||
|
if overrides.RootFS != "" {
|
||||||
|
cfg.RootFS = overrides.RootFS
|
||||||
|
}
|
||||||
|
if overrides.MonitorPort > 0 {
|
||||||
|
cfg.MonitorPort = overrides.MonitorPort
|
||||||
|
}
|
||||||
|
|
||||||
if cfg.BackendURL == "" {
|
if cfg.BackendURL == "" {
|
||||||
return cfg, fmt.Errorf("backendUrl is required")
|
return cfg, fmt.Errorf("backendUrl is required")
|
||||||
}
|
}
|
||||||
@@ -117,11 +164,25 @@ func getenvIntAny(keys []string, fallback int) int {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func hostnameOr(fallback string) string {
|
func hostnameOr(fallback string) string {
|
||||||
name, err := os.Hostname()
|
if name, err := os.Hostname(); err == nil && name != "" {
|
||||||
if err != nil || name == "" {
|
return name
|
||||||
return fallback
|
|
||||||
}
|
}
|
||||||
return name
|
return fallback
|
||||||
|
}
|
||||||
|
|
||||||
|
// hostHostname reads the hostname from <rootFS>/etc/hostname. Used when
|
||||||
|
// Monitor runs inside a container and wants the host's hostname rather
|
||||||
|
// than the container's UTS namespace hostname (which docker randomizes
|
||||||
|
// unless hostname: is set).
|
||||||
|
func hostHostname(rootFS string) string {
|
||||||
|
if rootFS == "" {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
buf, err := os.ReadFile(filepath.Join(rootFS, "etc", "hostname"))
|
||||||
|
if err != nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
return strings.TrimSpace(string(buf))
|
||||||
}
|
}
|
||||||
|
|
||||||
func applyHostFSEnv(rootFS string) {
|
func applyHostFSEnv(rootFS string) {
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
@@ -35,6 +36,10 @@ type Payload struct {
|
|||||||
SwapPct float64 `json:"swap_pct,omitempty"`
|
SwapPct float64 `json:"swap_pct,omitempty"`
|
||||||
LoadAvg []float64 `json:"load_avg,omitempty"`
|
LoadAvg []float64 `json:"load_avg,omitempty"`
|
||||||
UptimeSeconds uint64 `json:"uptime_seconds,omitempty"`
|
UptimeSeconds uint64 `json:"uptime_seconds,omitempty"`
|
||||||
|
|
||||||
|
// Optional OpenClaw metadata, enriched from plugin bridge.
|
||||||
|
// These fields are omitted if no plugin data is available.
|
||||||
|
OpenClawVersion string `json:"openclaw_version,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func BuildPayload(ctx context.Context, cfg config.Config) (Payload, error) {
|
func BuildPayload(ctx context.Context, cfg config.Config) (Payload, error) {
|
||||||
@@ -46,12 +51,15 @@ func BuildPayload(ctx context.Context, cfg config.Config) (Payload, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
cpuPct, err := cpu.PercentWithContext(ctx, time.Second, false)
|
cpuPct, err := cpu.PercentWithContext(ctx, time.Second, false)
|
||||||
if err == nil && len(cpuPct) > 0 {
|
if err != nil {
|
||||||
|
log.Printf("telemetry: cpu.Percent failed: %v", err)
|
||||||
|
} else if len(cpuPct) > 0 {
|
||||||
payload.CPUPct = round1(cpuPct[0])
|
payload.CPUPct = round1(cpuPct[0])
|
||||||
}
|
}
|
||||||
|
|
||||||
vm, err := mem.VirtualMemoryWithContext(ctx)
|
if vm, err := mem.VirtualMemoryWithContext(ctx); err != nil {
|
||||||
if err == nil {
|
log.Printf("telemetry: mem.VirtualMemory failed: %v", err)
|
||||||
|
} else {
|
||||||
payload.MemPct = round1(vm.UsedPercent)
|
payload.MemPct = round1(vm.UsedPercent)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -59,28 +67,33 @@ func BuildPayload(ctx context.Context, cfg config.Config) (Payload, error) {
|
|||||||
if diskPath == "" {
|
if diskPath == "" {
|
||||||
diskPath = "/"
|
diskPath = "/"
|
||||||
}
|
}
|
||||||
diskUsage, err := disk.UsageWithContext(ctx, diskPath)
|
if diskUsage, err := disk.UsageWithContext(ctx, diskPath); err != nil {
|
||||||
if err == nil {
|
log.Printf("telemetry: disk.Usage(%s) failed: %v", diskPath, err)
|
||||||
|
} else {
|
||||||
payload.DiskPct = round1(diskUsage.UsedPercent)
|
payload.DiskPct = round1(diskUsage.UsedPercent)
|
||||||
}
|
}
|
||||||
|
|
||||||
swapUsage, err := mem.SwapMemoryWithContext(ctx)
|
if swapUsage, err := mem.SwapMemoryWithContext(ctx); err != nil {
|
||||||
if err == nil {
|
log.Printf("telemetry: mem.SwapMemory failed: %v", err)
|
||||||
|
} else {
|
||||||
payload.SwapPct = round1(swapUsage.UsedPercent)
|
payload.SwapPct = round1(swapUsage.UsedPercent)
|
||||||
}
|
}
|
||||||
|
|
||||||
avg, err := gopsload.AvgWithContext(ctx)
|
if avg, err := gopsload.AvgWithContext(ctx); err != nil {
|
||||||
if err == nil {
|
log.Printf("telemetry: load.Avg failed: %v", err)
|
||||||
|
} else {
|
||||||
payload.LoadAvg = []float64{round2(avg.Load1), round2(avg.Load5), round2(avg.Load15)}
|
payload.LoadAvg = []float64{round2(avg.Load1), round2(avg.Load5), round2(avg.Load15)}
|
||||||
}
|
}
|
||||||
|
|
||||||
hostInfo, err := host.InfoWithContext(ctx)
|
if hostInfo, err := host.InfoWithContext(ctx); err != nil {
|
||||||
if err == nil {
|
log.Printf("telemetry: host.Info failed: %v", err)
|
||||||
|
} else {
|
||||||
payload.UptimeSeconds = hostInfo.Uptime
|
payload.UptimeSeconds = hostInfo.Uptime
|
||||||
}
|
}
|
||||||
|
|
||||||
nginxInstalled, nginxSites, err := detectNginx(cfg.RootFS)
|
if nginxInstalled, nginxSites, err := detectNginx(cfg.RootFS); err != nil {
|
||||||
if err == nil {
|
log.Printf("telemetry: detectNginx failed: %v", err)
|
||||||
|
} else {
|
||||||
payload.NginxInstalled = nginxInstalled
|
payload.NginxInstalled = nginxInstalled
|
||||||
payload.NginxSites = nginxSites
|
payload.NginxSites = nginxSites
|
||||||
}
|
}
|
||||||
@@ -94,7 +107,7 @@ func Send(ctx context.Context, client *http.Client, cfg config.Config, payload P
|
|||||||
return fmt.Errorf("marshal payload: %w", err)
|
return fmt.Errorf("marshal payload: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, strings.TrimRight(cfg.BackendURL, "/")+"/monitor/server/heartbeat-v2", strings.NewReader(string(body)))
|
req, err := http.NewRequestWithContext(ctx, http.MethodPost, strings.TrimRight(cfg.BackendURL, "/")+"/monitor/server/heartbeat", strings.NewReader(string(body)))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("build request: %w", err)
|
return fmt.Errorf("build request: %w", err)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user