feat(daemon): sample host cpu, memory and disk for /info and /resources

The status page had no host figures: `/api/v1/info` only carried identity and
`/api/v1/resources` only carried instance RSS, so the CPU/mem/disk tiles fell
back to "—" and the memory tile faked its percentage from RSS over physical
memory.

src/core/sysinfo.ts is a zero-dependency sampler: CPU from the /proc/stat delta
(null on the first read, since it needs two samples), load average from
os.loadavg(), memory from os.freemem/os.totalmem, and disk via statfs on the
daemon's state dir. One sample is cached for ~1 s and shared between /info and
/resources, so polling both does not double the syscalls. /info gains additive
`cpu`, `mem`, `disk` percentages plus `cpuCores` and `loadAvg`; /resources gains
a `system` object with the byte counts, the disk path and `sampledAt`.

Every field is optional on the client side: an older daemon simply omits them.
This commit is contained in:
yuez committed 2026-10-28 08:13:58 +08:00
1 parent 1664227c3b
commit 88354ec078
7 files changed
+492 -5

No files matched your search

+2
View File
@@ -16,6 +16,7 @@ import { join } from "node:path";
import { loadConfig, type Config, type ConfigEnv } from "../config.js";
import { dataDir, daemonSocketPath, daemonTokenPath, runtimeDir, stateDir } from "../core/paths.js";
import type { Logger } from "../core/interfaces.js";
import { createSysinfo } from "../core/sysinfo.js";
import { AuthStore } from "../daemon/auth.js";
import { InMemoryBus } from "../daemon/hub.js";
import { startServer, type ServerDeps, type StartedServer } from "../daemon/server.js";
@@ -144,6 +145,7 @@ export async function wireDaemon(options: WireOptions = {}): Promise<WiredDaemon
machineId: options.machineId ?? machineIdFor(env.HOSTNAME ?? process.env.HOSTNAME ?? "localhost"),
startedAt,
now,
sysinfo: createSysinfo({ now }),
...(options.server?.heartbeatMs === undefined ? {} : { heartbeatMs: options.server.heartbeatMs }),
...(options.server?.ws === undefined ? {} : { ws: options.server.ws }),
};