Host health from the Linux agent, agent self-update, and disk forecast in /metrics
- Linux agent v4: failed systemd services, SMART disk health, ZFS pool state, pacman updates; SMART/ZFS and self-update run in the ExecStartPre=-+ step outside the sandbox - Server: hosts' health stored (servers.host_health, migration 0029), alerts under a new "Failing services" kind plus disk/ZFS alerts, Host health card, Prometheus series - Agent self-update (Linux v4, Windows v3): admin switch on the Servers page, checksum-verified download, atomic replace; GET /api/agent/report/update - homelab_server_disk_days_to_full in /metrics - Tests (server, real PowerShell against a fake server) and docs Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
540ab704b7
commit
5dc6cea69c
38 files changed
+3884
-73
No files matched your search
@@ -4,6 +4,8 @@ import { db } from "../db/client.js";
|
||||
import { hashToken } from "../services/tokens.js";
|
||||
import { syncServerTasks } from "../services/taskSync.js";
|
||||
import { asyncHandler } from "../utils/asyncHandler.js";
|
||||
import { getSettings } from "../services/settingsStore.js";
|
||||
import { latestAgentSha256, latestAgentVersions } from "../services/agentVersions.js";
|
||||
|
||||
export const agentReportRouter = Router();
|
||||
|
||||
@@ -31,7 +33,33 @@ const updatesSchema = z.object({
|
||||
reboot_required: z.boolean().optional(),
|
||||
});
|
||||
|
||||
// Failed services, disk health, ZFS pools. Each part is checked on its own and a bad one dropped, like the rest: it must never cost the report.
|
||||
const healthSchema = z.object({
|
||||
failed_units: z
|
||||
.array(z.object({ unit: text(200), description: text(300).optional() }))
|
||||
.transform(cap(100))
|
||||
.optional()
|
||||
.catch(undefined),
|
||||
smart: z
|
||||
.array(z.object({ device: text(100), model: text(200).optional(), healthy: z.boolean().nullable().optional(), temperature_c: z.number().nullable().optional(), wear_percent: z.number().nullable().optional() }))
|
||||
.transform(cap(100))
|
||||
.optional()
|
||||
.catch(undefined),
|
||||
zfs: z
|
||||
.array(z.object({ name: text(200), health: text(40), capacity_percent: z.number().nullable().optional() }))
|
||||
.transform(cap(100))
|
||||
.optional()
|
||||
.catch(undefined),
|
||||
});
|
||||
|
||||
const systemSchema = z.object({
|
||||
health: z
|
||||
.unknown()
|
||||
.optional()
|
||||
.transform((h) => {
|
||||
const parsed = healthSchema.safeParse(h);
|
||||
return parsed.success && Object.values(parsed.data).some((v) => v !== undefined) ? parsed.data : undefined;
|
||||
}),
|
||||
updates: z
|
||||
.unknown()
|
||||
.optional()
|
||||
@@ -105,6 +133,23 @@ async function authenticateAgent(req: Request, res: Response, next: NextFunction
|
||||
next();
|
||||
}
|
||||
|
||||
/**
|
||||
* "Should I replace myself?" — asked by an agent that has been told to keep itself up to date, before it reports. Says yes only when an
|
||||
* administrator has switched automatic updates on and this agent is behind the script this app serves, and gives the script's SHA-256 so
|
||||
* the agent can check what it downloads. The agent says which version it is; failing that, the last one it reported counts.
|
||||
*/
|
||||
agentReportRouter.get("/update", asyncHandler(authenticateAgent), asyncHandler(async (req, res) => {
|
||||
const server = res.locals.agentServer as { osType: string; agentVersion: number | null };
|
||||
const os = server.osType === "windows" ? "windows" : "linux";
|
||||
const claimed = Number(req.query.version);
|
||||
const running = Number.isInteger(claimed) && claimed >= 1 && claimed <= 100_000 ? claimed : (server.agentVersion ?? 1);
|
||||
const latest = latestAgentVersions()[os];
|
||||
const sha256 = latestAgentSha256(os);
|
||||
const { agents } = await getSettings();
|
||||
const update = agents.autoUpdate && latest !== null && sha256 !== null && running < latest;
|
||||
res.json({ update, version: update ? latest : null, sha256: update ? sha256 : null });
|
||||
}));
|
||||
|
||||
agentReportRouter.post("/", asyncHandler(authenticateAgent), express.json({ limit: AGENT_BODY_LIMIT }), asyncHandler(async (req, res) => {
|
||||
const server = res.locals.agentServer as { id: number };
|
||||
|
||||
|
||||
Reference in new issue
Block a user