fix: correct telemetry identifier and visibility when containerized

Three related fixes for running Monitor inside a container with
/:/host:ro bind-mounted and network_mode: host.

* config: when HF_MONITER_ROOTFS is set, read the default identifier
  from <rootFS>/etc/hostname instead of os.Hostname(). Under
  network_mode: host the UTS namespace is not shared, so os.Hostname()
  returns a random docker-assigned string that changes across
  recreations, causing the backend to treat each restart as a new
  server.

* telemetry: log gopsutil errors from BuildPayload instead of silently
  swallowing them. Previously a missing /host mount would send a
  payload full of zeroed fields with no indication of failure.

* docker-compose: drop the 'ports:' block. It is silently ignored
  under network_mode: host (the bridge server binds directly on the
  host's 127.0.0.1:MONITOR_PORT).
This commit is contained in:
zhi
2026-04-15 23:02:44 +00:00
parent 758d3d1c59
commit e136f1b290
3 changed files with 56 additions and 22 deletions

View File

@@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"os/exec"
@@ -50,12 +51,15 @@ func BuildPayload(ctx context.Context, cfg config.Config) (Payload, error) {
}
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])
}
vm, err := mem.VirtualMemoryWithContext(ctx)
if err == nil {
if vm, err := mem.VirtualMemoryWithContext(ctx); err != nil {
log.Printf("telemetry: mem.VirtualMemory failed: %v", err)
} else {
payload.MemPct = round1(vm.UsedPercent)
}
@@ -63,28 +67,33 @@ func BuildPayload(ctx context.Context, cfg config.Config) (Payload, error) {
if diskPath == "" {
diskPath = "/"
}
diskUsage, err := disk.UsageWithContext(ctx, diskPath)
if err == nil {
if diskUsage, err := disk.UsageWithContext(ctx, diskPath); err != nil {
log.Printf("telemetry: disk.Usage(%s) failed: %v", diskPath, err)
} else {
payload.DiskPct = round1(diskUsage.UsedPercent)
}
swapUsage, err := mem.SwapMemoryWithContext(ctx)
if err == nil {
if swapUsage, err := mem.SwapMemoryWithContext(ctx); err != nil {
log.Printf("telemetry: mem.SwapMemory failed: %v", err)
} else {
payload.SwapPct = round1(swapUsage.UsedPercent)
}
avg, err := gopsload.AvgWithContext(ctx)
if err == nil {
if avg, err := gopsload.AvgWithContext(ctx); err != nil {
log.Printf("telemetry: load.Avg failed: %v", err)
} else {
payload.LoadAvg = []float64{round2(avg.Load1), round2(avg.Load5), round2(avg.Load15)}
}
hostInfo, err := host.InfoWithContext(ctx)
if err == nil {
if hostInfo, err := host.InfoWithContext(ctx); err != nil {
log.Printf("telemetry: host.Info failed: %v", err)
} else {
payload.UptimeSeconds = hostInfo.Uptime
}
nginxInstalled, nginxSites, err := detectNginx(cfg.RootFS)
if err == nil {
if nginxInstalled, nginxSites, err := detectNginx(cfg.RootFS); err != nil {
log.Printf("telemetry: detectNginx failed: %v", err)
} else {
payload.NginxInstalled = nginxInstalled
payload.NginxSites = nginxSites
}