Agent diagnostics ("what the agent couldn't collect") and services to ignore per server

- Agents (Linux 6, Windows 5) note each check they couldn't make and why; the server page turns that into tips and fixes, including when the agent is behind
- Linux --prepare leaves its notes for the report; a missing prepare step is itself noted
- Services to ignore are set per server from the Host health card (names or * patterns), applied on the server so alerts and /metrics drop them at once; saving alert limits keeps the list

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
bobbanandClaude Sonnet 5.5 committed 2026-10-18 22:16:31 +02:00
1 parent b5384c1e03
commit c3bc6bbf72
26 files changed
+4118 -45

No files matched your search

+3
View File
@@ -341,6 +341,9 @@ export const servers = sqliteTable("servers", {
// What the agent says about the machine's own health, as JSON: {failedUnits, smart, zfs, reportedAt} — see services/hostHealth.ts. Null until it says.
hostHealth: text("host_health"),
// What the agent says it could not collect, as JSON: {items: [{check, status, reason?, detail?}], reportedAt} — see services/agentDiagnostics.ts. Null until an agent that can say so has.
agentDiagnostics: text("agent_diagnostics"),
// What the machine is, as its firmware says (agents report it): maker, model and serial number. Used to fill in the inventory.
hwManufacturer: text("hw_manufacturer"),
hwModel: text("hw_model"),
+16
View File
@@ -59,7 +59,23 @@ const hardwareSchema = z.object({
serial: text(100).optional().catch(undefined),
});
// What the agent could not collect: leniently read, each entry on its own, so one odd one never costs the report.
const diagnosticsSchema = z.unknown().transform((v) => {
if (!Array.isArray(v)) return undefined;
const out: { check: string; status: "unavailable" | "failed"; reason?: string; detail?: string }[] = [];
for (const i of v.slice(0, 20)) {
if (!i || typeof i !== "object") continue;
const e = i as Record<string, unknown>;
const clean = (x: unknown, max: number) => (typeof x === "string" ? x.replace(/[\u0000-\u001f]/g, " ").trim().slice(0, max) : "");
const check = clean(e.check, 40);
if (!check || (e.status !== "unavailable" && e.status !== "failed")) continue;
out.push({ check, status: e.status, ...(clean(e.reason, 40) ? { reason: clean(e.reason, 40) } : {}), ...(clean(e.detail, 160) ? { detail: clean(e.detail, 160) } : {}) });
}
return out;
});
const systemSchema = z.object({
diagnostics: diagnosticsSchema.optional(),
hardware: z
.unknown()
.optional()
+6 -4
View File
@@ -8,7 +8,8 @@ import { generateApiToken } from "../services/tokens.js";
import { recordAudit } from "../services/audit.js";
import { loadParentRows, resolveParents, wouldLoop } from "../services/serverParents.js";
import { parsePendingUpdates } from "../services/pendingUpdates.js";
import { parseHostHealth } from "../services/hostHealth.js";
import { markIgnoredServices, parseHostHealth } from "../services/hostHealth.js";
import { describeDiagnostics, parseDiagnostics } from "../services/agentDiagnostics.js";
import { checkThresholds, parseThresholds } from "../services/serverThresholds.js";
import { latestAgentVersions } from "../services/agentVersions.js";
import { getServerMetrics, RETENTION_DAYS } from "../services/serverMetrics.js";
@@ -27,8 +28,8 @@ import { getSettings, updateSettings } from "../services/settingsStore.js";
export const serversRouter = Router();
/** A server row as the API returns it: no token hash, and tags as an array rather than the stored JSON. */
function publicServer<T extends { apiTokenHash: string; tags: string | null; pendingUpdates: string | null; hostHealth: string | null; thresholds: string | null }>(row: T) {
const { apiTokenHash: _hash, tags, pendingUpdates, hostHealth, thresholds, ...rest } = row;
function publicServer<T extends { apiTokenHash: string; tags: string | null; pendingUpdates: string | null; hostHealth: string | null; thresholds: string | null; agentDiagnostics?: string | null }>(row: T) {
const { apiTokenHash: _hash, tags, pendingUpdates, hostHealth, thresholds, agentDiagnostics: _diagnostics, ...rest } = row;
return { ...rest, tags: parseTags(tags), pendingUpdates: parsePendingUpdates(pendingUpdates), hostHealth: parseHostHealth(hostHealth), thresholds: parseThresholds(thresholds) };
}
@@ -382,6 +383,7 @@ serversRouter.get("/:id/detail", asyncHandler(async (req, res) => {
pendingUpdates: rawUpdates,
hostHealth: rawHealth,
thresholds: rawThresholds,
agentDiagnostics: rawDiagnostics,
...serverOut
} = server;
@@ -395,7 +397,7 @@ serversRouter.get("/:id/detail", asyncHandler(async (req, res) => {
children: [...links2.entries()].filter(([, l]) => l.parentId === id).map(([childId, l]) => ({ id: childId, name: nameOf.get(childId) ?? "", auto: l.auto })).sort((a, b) => a.name.localeCompare(b.name)),
};
res.json({ server: { ...serverOut, tags: parseTags(rawTags), pendingUpdates: parsePendingUpdates(rawUpdates), hostHealth: parseHostHealth(rawHealth), thresholds: parseThresholds(rawThresholds) }, thresholdDefaults: (await getSettings()).healthChecks, ipAddresses, hardware, dnsMatches, links, relations, agentLatest: latestAgentVersions() });
res.json({ server: { ...serverOut, tags: parseTags(rawTags), pendingUpdates: parsePendingUpdates(rawUpdates), hostHealth: markIgnoredServices(parseHostHealth(rawHealth), parseThresholds(rawThresholds)?.ignoreServices), thresholds: parseThresholds(rawThresholds) }, diagnostics: describeDiagnostics(parseDiagnostics(rawDiagnostics), { osType: server.osType, lastSeenAt: server.lastSeenAt, agentVersion: server.agentVersion, latest: latestAgentVersions() }), thresholdDefaults: (await getSettings()).healthChecks, ipAddresses, hardware, dnsMatches, links, relations, agentLatest: latestAgentVersions() });
}));
const tagsSchema = z.object({ tags: z.array(z.string().max(100)).max(50) });
+130
View File
@@ -0,0 +1,130 @@
/**
* What an agent could not collect, and what to do about it. Agents (Linux version 6, Windows version 5) note each check they couldn't make —
* with whether nothing was there to read it with ("unavailable") or it tried and couldn't ("failed"), why in a word and at most a line of
* what the tool said. They say only problems; a check not mentioned went fine. The words and the fixes live here, not in the agents, so they
* can be improved without installing anything again.
*/
import { isAgentOutdated, type LatestAgentVersions } from "./agentVersions.js";
export interface AgentDiagnostic {
check: string;
status: "unavailable" | "failed";
reason?: string;
detail?: string;
}
export interface StoredDiagnostics {
items: AgentDiagnostic[];
/** ISO — the report these came from. */
reportedAt: string;
}
export interface DiagnosticNote {
check: string;
/** "warning": something to look at. "info": a tip, or something that is simply how this machine is. */
level: "warning" | "info";
title: string;
fix: string | null;
/** What the agent said about it, when it said anything. */
detail: string | null;
}
const clean = (v: unknown, max: number) => (typeof v === "string" ? v.replace(/[\u0000-\u001f]/g, " ").trim().slice(0, max) : "");
/** Reads what is stored, leniently. Null when the agent has never said (an agent that predates this, or one that has nothing to report on). */
export function parseDiagnostics(stored: string | null | undefined): StoredDiagnostics | null {
if (!stored) return null;
try {
const v = JSON.parse(stored);
if (!v || typeof v !== "object" || !Array.isArray(v.items)) return null;
const items: AgentDiagnostic[] = [];
for (const i of v.items.slice(0, 20)) {
if (!i || typeof i !== "object") continue;
const check = clean(i.check, 40);
if (!check || (i.status !== "unavailable" && i.status !== "failed")) continue;
const item: AgentDiagnostic = { check, status: i.status };
const reason = clean(i.reason, 40);
const detail = clean(i.detail, 160);
if (reason) item.reason = reason;
if (detail) item.detail = detail;
items.push(item);
}
return { items, reportedAt: typeof v.reportedAt === "string" ? v.reportedAt : "" };
} catch {
return null;
}
}
/** What to store after a report that carried notes (an empty list is an answer: nothing was missed). */
export function nextDiagnostics(incoming: AgentDiagnostic[], now: string): string {
return JSON.stringify({ items: incoming, reportedAt: now } satisfies StoredDiagnostics);
}
type Os = "linux" | "windows";
/** One note for what the agent said, or null when it is not worth a person's attention (no ZFS on a machine that has none). */
function describe(d: AgentDiagnostic, os: Os): Omit<DiagnosticNote, "detail"> | null {
const linux = os === "linux";
const key = `${d.check}/${d.status}${d.reason ? `/${d.reason}` : ""}`;
switch (key) {
case "prepare/unavailable/step_missing":
return {
check: d.check,
level: "warning",
title: "The agent's step outside its sandbox didn't run",
fix: "Disk health and ZFS pools are only read in that step. Install the agent again (Servers → Manage servers has the command) so its systemd unit has it; if it is installed, look at `journalctl -u homelab-manager-agent` on the host.",
};
case "updates/unavailable/no_package_manager":
return { check: d.check, level: "info", title: "Pending updates can't be read here", fix: "The agent understands apt, dnf, zypper and pacman, and Windows Update. This machine has none of them." };
case "failed_units/unavailable/no_systemd":
return { check: d.check, level: "info", title: "Failed services can't be read: this machine doesn't run systemd", fix: null };
case "smart/unavailable/tool_missing":
return { check: d.check, level: "info", title: "Disk health isn't reported: smartctl isn't installed", fix: "Install smartmontools on the host (apt install smartmontools, or dnf install smartmontools); the next report includes it." };
case "smart/unavailable/not_root":
return { check: d.check, level: "warning", title: "Disk health needs the agent to run as root", fix: "The install script's systemd unit runs it as root. A copy started by hand as another user can't read the disks." };
case "zfs/unavailable/tool_missing":
return null; // most machines have no ZFS, and that is not a problem
case "hardware/unavailable/not_found":
return { check: d.check, level: "info", title: "No maker, model or serial number found", fix: "Many virtual machines and single-board computers don't have them. Type them into the Inventory instead." };
}
if (d.status === "failed") {
const what: Record<string, string> = { updates: "pending updates", failed_units: "services", smart: "disk health", zfs: "storage pools", host_health: "the machine's health", hardware: "what the machine is" };
const fixes: Record<string, string> = linux
? {
updates: "Run `sudo /usr/local/bin/homelab-manager-agent.sh --check-updates` on the host to see what the package manager said (dnf and zypper answer from their caches: `sudo dnf makecache`).",
failed_units: "`systemctl list-units --state=failed` didn't answer in time on that host.",
zfs: "`zpool list` didn't answer; check that the ZFS kernel module is loaded.",
}
: {
updates: "Windows Update didn't answer (the search is tried again after an hour). Check the Windows Update service and that the machine can reach Microsoft.",
failed_units: "Reading services through WMI failed. Check that the Windows Management Instrumentation service is running.",
smart: "Get-PhysicalDisk failed. In an elevated PowerShell, run `Get-PhysicalDisk` to see why.",
zfs: "Get-StoragePool failed. In an elevated PowerShell, run `Get-StoragePool` to see why.",
host_health: "Reading the machine's health failed; the detail says why.",
hardware: "Reading the system's own details through WMI failed; the detail says why.",
};
return { check: d.check, level: "warning", title: `Reading ${what[d.check] ?? d.check} failed`, fix: fixes[d.check] ?? null };
}
return { check: d.check, level: "info", title: `${d.check.replace(/_/g, " ")} isn't available${d.reason ? ` (${d.reason.replace(/_/g, " ")})` : ""}`, fix: null };
}
/** What to tell a person about one server's agent: what it couldn't collect (with what to do) and, when it is behind, that. Warnings first. */
export function describeDiagnostics(stored: StoredDiagnostics | null, context: { osType: string; lastSeenAt: string | null; agentVersion: number | null; latest: LatestAgentVersions }): DiagnosticNote[] {
const os: Os = context.osType === "windows" ? "windows" : "linux";
const notes: DiagnosticNote[] = [];
for (const d of stored?.items ?? []) {
const note = describe(d, os);
if (note) notes.push({ ...note, detail: d.status === "failed" ? (d.detail ?? null) : null }); // what the agent said is only worth showing for a failure
}
if (isAgentOutdated({ osType: context.osType, lastSeenAt: context.lastSeenAt, agentVersion: context.agentVersion }, context.latest)) {
const current = os === "windows" ? context.latest.windows : context.latest.linux;
notes.push({
check: "agent_behind",
level: "info",
title: `This agent is version ${context.agentVersion ?? 1}; ${current} is current`,
fix: "A newer agent reports more and says more about what it couldn't read. Update it from the Updates page, or switch on automatic updates for agents in Settings.",
detail: null,
});
}
return notes.sort((a, b) => Number(b.level === "warning") - Number(a.level === "warning"));
}
+1
View File
@@ -11,6 +11,7 @@
* 4 — Linux: also reports failed services, disk health (SMART) and ZFS pools, reads pacman, and can replace itself too.
* Windows: also reports failed services, disk health and Storage Spaces pools, and what the machine is (maker, model, serial)
* 5 — Linux: also says what the machine is (maker, model, serial), for the inventory
* 6 — Linux (and Windows 5): also says what it could not collect and why, so the server can say what to fix
*/
import { createHash } from "node:crypto";
import { readFileSync } from "node:fs";
+2 -2
View File
@@ -10,7 +10,7 @@ import { getInternalFlag, getSettings, setInternalFlag } from "./settingsStore.j
import { activeSubjects, subjectOfConditionKey } from "./maintenance.js";
import { dependencyCover, HEALTH_STATE_FLAG } from "./dependencies.js";
import { getFailingSources } from "./integrationHealthMonitor.js";
import { hostHealthConditions, parseHostHealth, type HostHealth } from "./hostHealth.js";
import { hostHealthConditions, parseHostHealth, withoutIgnoredServices, type HostHealth } from "./hostHealth.js";
import { diskLimit, parseThresholds, type ServerThresholds } from "./serverThresholds.js";
const STATE_FLAG = HEALTH_STATE_FLAG;
@@ -306,7 +306,7 @@ export async function collectSnapshot(): Promise<{ snapshot: HealthSnapshot; hel
} catch {
// a malformed report shouldn't stop every other server being checked
}
snapshot.servers.push({ id: s.id, name: s.name, lastSeenAt: s.lastSeenAt, disks, health: parseHostHealth(s.hostHealth), thresholds: parseThresholds(s.thresholds) });
snapshot.servers.push({ id: s.id, name: s.name, lastSeenAt: s.lastSeenAt, disks, health: withoutIgnoredServices(parseHostHealth(s.hostHealth), parseThresholds(s.thresholds)?.ignoreServices), thresholds: parseThresholds(s.thresholds) });
}
const rows = await db
+27
View File
@@ -8,6 +8,8 @@
export interface FailedUnit {
unit: string;
description: string;
/** Set where it is shown, for a unit this server has been told to ignore. It never counts as failed. */
ignored?: boolean;
}
export interface SmartDisk {
device: string;
@@ -82,6 +84,31 @@ export function nextHostHealth(previous: string | null, incoming: IncomingHostHe
return any ? JSON.stringify(next) : previous;
}
const globToRegex = (pattern: string) => new RegExp(`^${pattern.trim().toLowerCase().replace(/[.+?^${}()|[\]\\]/g, "\\$&").replace(/\*/g, ".*")}$`);
/** Whether a unit is one this server is told to ignore: a name, or a pattern with * ("gupdate*"); case doesn't matter and ".service" may be left off. */
export function isServiceIgnored(unit: string, patterns: string[] | undefined): boolean {
if (!patterns || patterns.length === 0) return false;
const u = unit.toLowerCase();
const bare = u.replace(/\.service$/, "");
return patterns.some((p) => {
const re = globToRegex(p);
return re.test(u) || re.test(bare);
});
}
/** The health with the ignored services taken out: what alerts and figures are worked from. */
export function withoutIgnoredServices(health: HostHealth | null, patterns: string[] | undefined): HostHealth | null {
if (!health?.failedUnits || !patterns || patterns.length === 0) return health;
return { ...health, failedUnits: health.failedUnits.filter((u) => !isServiceIgnored(u.unit, patterns)) };
}
/** The health with the ignored services marked, not removed: what the server's page shows, so they can be seen and taken off the list. */
export function markIgnoredServices(health: HostHealth | null, patterns: string[] | undefined): HostHealth | null {
if (!health?.failedUnits || !patterns || patterns.length === 0) return health;
return { ...health, failedUnits: health.failedUnits.map((u) => (isServiceIgnored(u.unit, patterns) ? { ...u, ignored: true } : u)) };
}
/** A pool in any state but ONLINE has lost something (DEGRADED has lost redundancy, the rest are worse). */
export const poolIsFine = (p: ZfsPool) => p.health.toUpperCase() === "ONLINE";
+3 -2
View File
@@ -10,7 +10,8 @@ import { getAlerts } from "./alerts.js";
import { isAgentOutdated, latestAgentVersions } from "./agentVersions.js";
import { getSourceStatus } from "./integrationHealthMonitor.js";
import { parsePendingUpdates } from "./pendingUpdates.js";
import { parseHostHealth, poolIsFine } from "./hostHealth.js";
import { parseHostHealth, poolIsFine, withoutIgnoredServices } from "./hostHealth.js";
import { parseThresholds as parseServerThresholds } from "./serverThresholds.js";
import { parseThresholds } from "./serverThresholds.js";
import { getCachedBackupCoverage } from "./backupCoverage.js";
import { lastAttempt as lastSelfBackup, listSnapshots } from "./selfBackup.js";
@@ -88,7 +89,7 @@ export async function buildMetrics(now: Date = new Date()): Promise<string> {
if (updates.security !== null) out.add("homelab_server_pending_security_updates", "gauge", "Of those, the ones that are security fixes.", updates.security, { server });
out.add("homelab_server_reboot_required", "gauge", "1 if a restart is due.", updates.rebootRequired ? 1 : 0, { server });
}
const health = parseHostHealth(s.hostHealth);
const health = withoutIgnoredServices(parseHostHealth(s.hostHealth), parseServerThresholds(s.thresholds)?.ignoreServices);
if (health?.failedUnits) out.add("homelab_server_failed_units", "gauge", "Services (systemd units) that have failed on the server.", health.failedUnits.length, { server });
for (const d of health?.smart ?? []) {
if (d.healthy !== null) out.add("homelab_server_disk_healthy", "gauge", "1 if the disk's own SMART self-assessment passes, 0 if it fails.", d.healthy ? 1 : 0, { server, device: d.device });
+23
View File
@@ -21,6 +21,8 @@ export interface ServerThresholds {
/** 0 turns the stale-backup alert off for this server's guest. */
backupStaleHours?: number;
mounts?: Record<string, MountThreshold>;
/** Services (units) never to count as failed on this server: names or patterns like "gupdate*" (case does not matter, ".service" is optional). */
ignoreServices?: string[];
}
/** The global values these override. */
@@ -38,6 +40,8 @@ export const LIMITS = {
backupStaleHours: { min: 0, max: 2160 },
} as const;
export const MAX_MOUNT_RULES = 50;
export const MAX_IGNORED_SERVICES = 30;
const MAX_SERVICE_PATTERN = 100;
const inRange = (v: unknown, key: keyof typeof LIMITS): v is number => typeof v === "number" && Number.isInteger(v) && v >= LIMITS[key].min && v <= LIMITS[key].max;
@@ -61,6 +65,11 @@ export function parseThresholds(stored: string | null | undefined): ServerThresh
}
if (Object.keys(mounts).length > 0) out.mounts = mounts;
}
if (Array.isArray(v.ignoreServices)) {
const strings: string[] = (v.ignoreServices as unknown[]).filter((p): p is string => typeof p === "string");
const list = [...new Set(strings.map((p) => p.trim()).filter((p) => p.length > 0 && p.length <= MAX_SERVICE_PATTERN))].slice(0, MAX_IGNORED_SERVICES);
if (list.length > 0) out.ignoreServices = list;
}
return Object.keys(out).length > 0 ? out : null;
} catch {
return null;
@@ -103,6 +112,20 @@ export function checkThresholds(input: unknown): { ok: true; value: ServerThresh
}
if (Object.keys(mounts).length > 0) out.mounts = mounts;
}
if (v.ignoreServices !== undefined && v.ignoreServices !== null) {
if (!Array.isArray(v.ignoreServices)) return { ok: false, message: "The services to ignore should be a list." };
const list: string[] = [];
for (const raw of v.ignoreServices) {
if (typeof raw !== "string") return { ok: false, message: "Each service to ignore should be a name or a pattern." };
const p = raw.trim();
if (p.length === 0) continue;
// eslint-disable-next-line no-control-regex
if (p.length > MAX_SERVICE_PATTERN || /[\u0000-\u001f]/.test(p)) return { ok: false, message: `"${p.slice(0, 30)}…" isn't a service name or pattern (up to ${MAX_SERVICE_PATTERN} characters, no line breaks).` };
if (!list.includes(p)) list.push(p);
}
if (list.length > MAX_IGNORED_SERVICES) return { ok: false, message: `At most ${MAX_IGNORED_SERVICES} services can be ignored on one server.` };
if (list.length > 0) out.ignoreServices = list;
}
return { ok: true, value: Object.keys(out).length > 0 ? out : null };
}
+4
View File
@@ -6,6 +6,7 @@ import { nextUpdateState, type IncomingUpdates } from "./pendingUpdates.js";
import { nextHostHealth, type IncomingHostHealth } from "./hostHealth.js";
import { recordAudit } from "./audit.js";
import { fillInventoryFromServer } from "./inventoryAutofill.js";
import { nextDiagnostics, type AgentDiagnostic } from "./agentDiagnostics.js";
export interface IncomingTask {
scheduleType: "cron" | "systemd_timer" | "windows_task";
@@ -30,6 +31,8 @@ export interface IncomingSystemInfo {
health?: IncomingHostHealth;
/** What the machine is, from its firmware: each part present only if it says anything. */
hardware?: { manufacturer?: string; model?: string; serial?: string };
/** What the agent could not collect; an empty list says nothing was missed. */
diagnostics?: AgentDiagnostic[];
}
export interface AgentReport {
@@ -118,6 +121,7 @@ export async function syncServerTasks(serverId: number, report: AgentReport) {
// Only stored when the agent reported it, so an agent that predates this (or couldn't tell this time) doesn't wipe what it said before.
...(system?.updates && before ? nextUpdateState(before, system.updates, now) : {}),
...(system?.health && before ? { hostHealth: nextHostHealth(before.hostHealth, system.health, now) } : {}),
...(system?.diagnostics ? { agentDiagnostics: nextDiagnostics(system.diagnostics, now) } : {}),
...(system?.hardware?.manufacturer ? { hwManufacturer: system.hardware.manufacturer } : {}),
...(system?.hardware?.model ? { hwModel: system.hardware.model } : {}),
...(system?.hardware?.serial ? { hwSerial: system.hardware.serial } : {}),