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:
1 parent
1664227c3b
commit
88354ec078
7 files changed
+492
-5
No files matched your search
+16
-5
@@ -8,6 +8,7 @@ import type { Bus, InstanceManager, Logger, WorkspaceStore } from "../core/inter
|
||||
import { ERROR_CODES, errorBody } from "../protocol/errors.js";
|
||||
import { PROTOCOL_VERSION, type CommandRequest } from "../protocol/types.js";
|
||||
import type { Config } from "../config.js";
|
||||
import type { SysinfoSampler } from "../core/sysinfo.js";
|
||||
import { networkBases } from "../core/network.js";
|
||||
import { encodeQr, fitsQr } from "../core/qr.js";
|
||||
import type { AuthStore, DevicePublic } from "./auth.js";
|
||||
@@ -39,6 +40,7 @@ export interface AppDeps {
|
||||
startedAt?: number;
|
||||
now?: () => number;
|
||||
metrics?: () => DaemonMetrics;
|
||||
sysinfo?: SysinfoSampler;
|
||||
/** Redeem budget; injectable so tests can advance the clock. */
|
||||
pairLimiter?: PairRateLimiter;
|
||||
}
|
||||
@@ -191,8 +193,9 @@ export function createApp(deps: AppDeps): Hono<Env> {
|
||||
await next();
|
||||
});
|
||||
|
||||
app.get("/api/v1/info", (c) =>
|
||||
c.json({
|
||||
app.get("/api/v1/info", async (c) => {
|
||||
const system = await deps.sysinfo?.sample();
|
||||
return c.json({
|
||||
v: PROTOCOL_VERSION,
|
||||
daemonId: deps.daemonId,
|
||||
machineId: deps.machineId ?? deps.daemonId,
|
||||
@@ -207,8 +210,13 @@ export function createApp(deps: AppDeps): Hono<Env> {
|
||||
limits: config.limits,
|
||||
host: config.host,
|
||||
publicUrl: config.publicUrl,
|
||||
}),
|
||||
);
|
||||
cpuCores: system?.cpuCores,
|
||||
loadAvg: system?.loadAvg ?? null,
|
||||
...(typeof system?.cpuPercent === "number" ? { cpu: system.cpuPercent } : {}),
|
||||
...(typeof system?.memPercent === "number" ? { mem: system.memPercent } : {}),
|
||||
...(typeof system?.diskPercent === "number" ? { disk: system.diskPercent } : {}),
|
||||
});
|
||||
});
|
||||
|
||||
/**
|
||||
* Local-browser login handshake: loopback + the local CLI token only.
|
||||
@@ -324,7 +332,10 @@ export function createApp(deps: AppDeps): Hono<Env> {
|
||||
return c.body(null, 204);
|
||||
});
|
||||
|
||||
app.get("/api/v1/resources", async (c) => c.json(await instances.resources()));
|
||||
app.get("/api/v1/resources", async (c) => {
|
||||
const [snapshot, system] = await Promise.all([instances.resources(), deps.sysinfo?.sample()]);
|
||||
return c.json(system === undefined ? snapshot : { ...snapshot, system });
|
||||
});
|
||||
|
||||
app.get("/api/v1/diagnostics", (c) => {
|
||||
const metrics = deps.metrics?.() ?? { connections: 0 };
|
||||
|
||||
@@ -102,6 +102,7 @@ export async function startServer(config: Config, deps: ServerDeps): Promise<Sta
|
||||
startedAt: deps.startedAt,
|
||||
now: deps.now,
|
||||
metrics: () => ({ connections: ws.connections(), eventsSent: ws.eventsSent() }),
|
||||
sysinfo: deps.sysinfo,
|
||||
});
|
||||
|
||||
let closing: Promise<void> | undefined;
|
||||
|
||||
Reference in new issue
Block a user