Dependencies beyond servers, web push, inventory auto-fill with QR labels, Windows agent health

- Dependencies: servers and integrations can depend on each other; alerts are folded under the root cause, and a failure explained by something down isn't announced
- Web push: hand-written VAPID and RFC 8291 encryption, per-device subscriptions, a fifth notification channel, service worker handlers
- Inventory: maker/model/serial from the agents fill blank fields, "Refresh from agent", QR label printing and ?asset= deep links
- Windows agent (v4) reports failed services, disk health, Storage Spaces pools and hardware identity; Linux agent (v5) reports hardware identity

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
bobbanandClaude Sonnet 5.5 committed 2026-10-18 18:53:30 +02:00
1 parent 5cc51c57de
commit ec9c2f73f0
55 files changed
+12553 -65

No files matched your search

+14
View File
@@ -52,7 +52,21 @@ const healthSchema = z.object({
.catch(undefined),
});
// What the machine is. Each part is optional and a malformed one is dropped, like the rest.
const hardwareSchema = z.object({
manufacturer: text(100).optional().catch(undefined),
model: text(150).optional().catch(undefined),
serial: text(100).optional().catch(undefined),
});
const systemSchema = z.object({
hardware: z
.unknown()
.optional()
.transform((h) => {
const parsed = hardwareSchema.safeParse(h);
return parsed.success && Object.values(parsed.data).some((v) => typeof v === "string" && v.trim()) ? parsed.data : undefined;
}),
health: z
.unknown()
.optional()
+80
View File
@@ -0,0 +1,80 @@
import { Router } from "express";
import { eq } from "drizzle-orm";
import { z } from "zod";
import { db } from "../db/client.js";
import { dependencies } from "../db/schema.js";
import { requireAuth, requireRole } from "../auth/middleware.js";
import { recordAudit } from "../services/audit.js";
import { invalidateAlertsCache } from "../services/alerts.js";
import { loadGraph, nodeKey, nodeTypes, wouldLoop } from "../services/dependencies.js";
import { asyncHandler } from "../utils/asyncHandler.js";
export const dependenciesRouter = Router();
dependenciesRouter.use(requireAuth);
const edgeSchema = z.object({
fromType: z.enum(nodeTypes),
fromId: z.number().int().positive(),
toType: z.enum(nodeTypes),
toId: z.number().int().positive(),
});
// Everything signed in people may see: what needs what, with the "runs on" links of servers alongside (those are set on the server itself).
dependenciesRouter.get("/", asyncHandler(async (_req, res) => {
const graph = await loadGraph();
const written = new Set(graph.rows.map((r) => `${nodeKey(r.fromType, r.fromId)}>${nodeKey(r.toType, r.toId)}`));
const runsOn: { from: string; to: string }[] = [];
for (const [from, tos] of graph.edges) for (const to of tos) if (!written.has(`${from}>${to}`)) runsOn.push({ from, to });
const nodes = [...graph.names.entries()]
.map(([key, name]) => {
const [type, id] = key.split(":");
return { key, type, id: Number(id), name };
})
.sort((a, b) => a.type.localeCompare(b.type) || a.name.localeCompare(b.name));
res.json({
dependencies: graph.rows
.filter((r) => graph.names.has(nodeKey(r.fromType, r.fromId)) && graph.names.has(nodeKey(r.toType, r.toId)))
.map((r) => ({ id: r.id, from: { type: r.fromType, id: r.fromId, name: graph.names.get(nodeKey(r.fromType, r.fromId))! }, to: { type: r.toType, id: r.toId, name: graph.names.get(nodeKey(r.toType, r.toId))! }, createdAt: r.createdAt })),
runsOn: runsOn.map((r) => ({ from: { key: r.from, name: graph.names.get(r.from) ?? r.from }, to: { key: r.to, name: graph.names.get(r.to) ?? r.to } })),
nodes,
});
}));
dependenciesRouter.post("/", requireRole("operator"), asyncHandler(async (req, res) => {
const parsed = edgeSchema.safeParse(req.body);
if (!parsed.success) return res.status(400).json({ error: "invalid_body", message: "Say what needs what: a server or an integration each.", details: parsed.error.flatten() });
const d = parsed.data;
const graph = await loadGraph();
const from = nodeKey(d.fromType, d.fromId);
const to = nodeKey(d.toType, d.toId);
if (!graph.names.has(from) || !graph.names.has(to)) return res.status(400).json({ error: "unknown_node", message: "One of those doesn't exist." });
if (from === to) return res.status(400).json({ error: "self", message: "Something can't depend on itself." });
if (graph.edges.get(from)?.includes(to)) return res.status(409).json({ error: "duplicate", message: "That is already so." });
if (wouldLoop(graph.edges, from, to)) {
return res.status(409).json({ error: "loop", message: `${graph.names.get(to)} already depends on ${graph.names.get(from)}, so this would make them wait for each other.` });
}
const [row] = await db.insert(dependencies).values(d).returning();
await recordAudit({ actor: req.currentUser!, category: "dependency", action: "create", targetType: "dependency", targetId: row.id, detail: { from: graph.names.get(from), to: graph.names.get(to) } });
invalidateAlertsCache();
res.status(201).json({ id: row.id });
}));
dependenciesRouter.delete("/:id", requireRole("operator"), asyncHandler(async (req, res) => {
const id = /^\d+$/.test(req.params.id) ? Number(req.params.id) : NaN;
if (!Number.isInteger(id)) return res.status(400).json({ error: "invalid_id" });
const [before] = await db.select().from(dependencies).where(eq(dependencies.id, id)).limit(1);
if (!before) return res.status(404).json({ error: "not_found" });
const graph = await loadGraph();
await db.delete(dependencies).where(eq(dependencies.id, id));
await recordAudit({
actor: req.currentUser!,
category: "dependency",
action: "delete",
targetType: "dependency",
targetId: id,
detail: { from: graph.names.get(nodeKey(before.fromType as "server", before.fromId)) ?? `${before.fromType} #${before.fromId}`, to: graph.names.get(nodeKey(before.toType as "server", before.toId)) ?? `${before.toType} #${before.toId}` },
});
invalidateAlertsCache();
res.status(204).end();
}));
+42 -8
View File
@@ -6,6 +6,7 @@ import { assetKinds, assets, servers } from "../db/schema.js";
import { requireAuth, requireRole } from "../auth/middleware.js";
import { recordAudit } from "../services/audit.js";
import { isRealDate } from "../services/inventory.js";
import { blanksToFill, differences, hardwareOf } from "../services/inventoryAutofill.js";
import { getSettings, updateSettings } from "../services/settingsStore.js";
import { asyncHandler } from "../utils/asyncHandler.js";
@@ -53,16 +54,32 @@ function values(d: z.infer<typeof assetSchema>) {
};
}
/** A linked entry's blank maker, model and serial, from what that server's agent says. */
async function fillFromAgent(v: ReturnType<typeof values>) {
if (v.serverId === null) return {};
const [server] = await db.select({ hwManufacturer: servers.hwManufacturer, hwModel: servers.hwModel, hwSerial: servers.hwSerial }).from(servers).where(eq(servers.id, v.serverId)).limit(1);
return blanksToFill(v, hardwareOf(server));
}
const idOf = (v: string) => (/^\d+$/.test(v) ? Number(v) : NaN);
// Everything signed-in people may see: the hardware, and the price of power the running cost is worked out with.
inventoryRouter.get("/", asyncHandler(async (_req, res) => {
const [rows, serverRows, settings] = await Promise.all([db.select().from(assets).orderBy(assets.name), db.select({ id: servers.id, name: servers.name }).from(servers), getSettings()]);
const serverName = new Map(serverRows.map((s) => [s.id, s.name]));
const [rows, serverRows, settings] = await Promise.all([db.select().from(assets).orderBy(assets.name), db.select().from(servers), getSettings()]);
const byId = new Map(serverRows.map((s) => [s.id, s]));
res.json({
assets: rows.map((a) => ({ ...a, server: a.serverId !== null && serverName.has(a.serverId) ? { id: a.serverId, name: serverName.get(a.serverId)! } : null })),
assets: rows.map((a) => {
const s = a.serverId !== null ? byId.get(a.serverId) : undefined;
const hw = hardwareOf(s);
return {
...a,
server: s ? { id: s.id, name: s.name } : null,
// What the machine's agent says it is, and what that would change here (for the "refresh from agent" button).
agent: hw ? { ...hw, differs: differences(a, hw) } : null,
};
}),
config: settings.inventory,
servers: serverRows.sort((a, b) => a.name.localeCompare(b.name)),
servers: serverRows.map((s) => ({ id: s.id, name: s.name })).sort((a, b) => a.name.localeCompare(b.name)),
});
}));
@@ -89,8 +106,9 @@ inventoryRouter.post("/", requireRole("operator"), asyncHandler(async (req, res)
return res.status(400).json({ error: "unknown_server", message: "That server doesn't exist." });
}
const now = new Date().toISOString();
const [row] = await db.insert(assets).values({ ...v, createdAt: now, updatedAt: now }).returning();
await recordAudit({ actor: req.currentUser!, category: "inventory", action: "create", targetType: "asset", targetId: row.id, detail: { name: row.name, kind: row.kind } });
const filled = await fillFromAgent(v);
const [row] = await db.insert(assets).values({ ...v, ...filled, createdAt: now, updatedAt: now }).returning();
await recordAudit({ actor: req.currentUser!, category: "inventory", action: "create", targetType: "asset", targetId: row.id, detail: { name: row.name, kind: row.kind, ...(Object.keys(filled).length ? { filledFromAgent: Object.keys(filled) } : {}) } });
res.status(201).json({ asset: row });
}));
@@ -104,11 +122,27 @@ inventoryRouter.put("/:id", requireRole("operator"), asyncHandler(async (req, re
if (v.serverId !== null && (await db.select({ id: servers.id }).from(servers).where(eq(servers.id, v.serverId))).length === 0) {
return res.status(400).json({ error: "unknown_server", message: "That server doesn't exist." });
}
const [row] = await db.update(assets).set({ ...v, updatedAt: new Date().toISOString() }).where(eq(assets.id, id)).returning();
await recordAudit({ actor: req.currentUser!, category: "inventory", action: "update", targetType: "asset", targetId: id, detail: { name: row.name } });
const filled = await fillFromAgent(v);
const [row] = await db.update(assets).set({ ...v, ...filled, updatedAt: new Date().toISOString() }).where(eq(assets.id, id)).returning();
await recordAudit({ actor: req.currentUser!, category: "inventory", action: "update", targetType: "asset", targetId: id, detail: { name: row.name, ...(Object.keys(filled).length ? { filledFromAgent: Object.keys(filled) } : {}) } });
res.json({ asset: row });
}));
// Takes what the agent says over what is written here: the one deliberate overwrite (the entry's own fields are otherwise only ever filled when blank).
inventoryRouter.post("/:id/refresh-from-agent", requireRole("operator"), asyncHandler(async (req, res) => {
const id = idOf(req.params.id);
if (!Number.isInteger(id)) return res.status(400).json({ error: "invalid_id" });
const [before] = await db.select().from(assets).where(eq(assets.id, id)).limit(1);
if (!before) return res.status(404).json({ error: "not_found" });
if (before.serverId === null) return res.status(400).json({ error: "not_linked", message: "Link the entry to a server first." });
const [server] = await db.select({ hwManufacturer: servers.hwManufacturer, hwModel: servers.hwModel, hwSerial: servers.hwSerial }).from(servers).where(eq(servers.id, before.serverId)).limit(1);
const changes = differences(before, hardwareOf(server));
if (Object.keys(changes).length === 0) return res.json({ asset: before, changed: [] });
const [row] = await db.update(assets).set({ ...changes, updatedAt: new Date().toISOString() }).where(eq(assets.id, id)).returning();
await recordAudit({ actor: req.currentUser!, category: "inventory", action: "refresh_from_agent", targetType: "asset", targetId: id, detail: { name: row.name, from: Object.fromEntries(Object.keys(changes).map((k) => [k, (before as any)[k]])), to: changes } });
res.json({ asset: row, changed: Object.keys(changes) });
}));
inventoryRouter.delete("/:id", requireRole("operator"), asyncHandler(async (req, res) => {
const id = idOf(req.params.id);
if (!Number.isInteger(id)) return res.status(400).json({ error: "invalid_id" });
+1
View File
@@ -63,6 +63,7 @@ privacyRouter.get("/", asyncHandler(async (req, res) => {
ntfy: channel(settings.ntfy.enabled, hostOf(settings.ntfy.url)),
smtp: channel(settings.smtp.enabled, settings.smtp.host || null),
webhook: channel(settings.webhook.enabled, hostOf(settings.webhook.url)),
webPush: channel(settings.webPush.enabled, "the push services of your browsers (Google, Mozilla, Apple …)"),
},
},
});
+97
View File
@@ -0,0 +1,97 @@
import { Router } from "express";
import { and, eq } from "drizzle-orm";
import { z } from "zod";
import { db } from "../db/client.js";
import { pushSubscriptions } from "../db/schema.js";
import { requireAuth } from "../auth/middleware.js";
import { recordAudit } from "../services/audit.js";
import { getSettings } from "../services/settingsStore.js";
import { checkEndpoint, getVapidKeys, sendPush } from "../services/webPush.js";
import { asyncHandler } from "../utils/asyncHandler.js";
export const pushRouter = Router();
pushRouter.use(requireAuth);
/** At most this many browsers per person: a phone, a laptop, a tablet and the odd one. */
const MAX_PER_USER = 10;
const subscribeSchema = z.object({
endpoint: z.string().url().max(2000),
keys: z.object({ p256dh: z.string().min(80).max(100), auth: z.string().min(20).max(30) }),
label: z.string().trim().max(80).optional(),
});
const hostOf = (endpoint: string) => {
try {
return new URL(endpoint).host;
} catch {
return "";
}
};
const off = { error: "push_disabled", message: "Push notifications are switched off. An administrator turns them on under Settings, Notifications." };
// The key a browser subscribes with. Anyone signed in may ask (their own device is theirs to enrol), once push is on.
pushRouter.get("/key", asyncHandler(async (_req, res) => {
const { webPush } = await getSettings();
if (!webPush.enabled) return res.status(409).json(off);
res.json({ publicKey: (await getVapidKeys()).publicKey });
}));
// This person's own browsers.
pushRouter.get("/mine", asyncHandler(async (req, res) => {
const rows = await db.select().from(pushSubscriptions).where(eq(pushSubscriptions.userId, req.currentUser!.id)).orderBy(pushSubscriptions.id);
const { webPush } = await getSettings();
res.json({
enabled: webPush.enabled,
subscriptions: rows.map((r) => ({ id: r.id, label: r.label, host: hostOf(r.endpoint), endpoint: r.endpoint, createdAt: r.createdAt, lastSuccessAt: r.lastSuccessAt })),
});
}));
pushRouter.post("/subscribe", asyncHandler(async (req, res) => {
const { webPush } = await getSettings();
if (!webPush.enabled) return res.status(409).json(off);
const parsed = subscribeSchema.safeParse(req.body);
if (!parsed.success) return res.status(400).json({ error: "invalid_body", message: "That isn't a push subscription.", details: parsed.error.flatten() });
const { endpoint, keys, label } = parsed.data;
try {
await checkEndpoint(endpoint);
} catch (err) {
return res.status(400).json({ error: "bad_endpoint", message: err instanceof Error ? err.message : "That push address can't be used." });
}
const me = req.currentUser!;
const mine = await db.select({ id: pushSubscriptions.id, endpoint: pushSubscriptions.endpoint }).from(pushSubscriptions).where(eq(pushSubscriptions.userId, me.id));
const existing = mine.find((r) => r.endpoint === endpoint);
if (!existing && mine.length >= MAX_PER_USER) return res.status(409).json({ error: "too_many", message: `You already have ${MAX_PER_USER} browsers subscribed. Remove one first.` });
// The same browser subscribing again (or after someone else used it) is the same row, now this person's.
const [row] = await db
.insert(pushSubscriptions)
.values({ userId: me.id, endpoint, p256dh: keys.p256dh, auth: keys.auth, label: label || null })
.onConflictDoUpdate({ target: pushSubscriptions.endpoint, set: { userId: me.id, p256dh: keys.p256dh, auth: keys.auth, label: label || null } })
.returning({ id: pushSubscriptions.id });
if (!existing) await recordAudit({ actor: me, category: "push", action: "subscribe", targetType: "push_subscription", targetId: row.id, detail: { host: hostOf(endpoint), label: label || null } });
res.status(201).json({ id: row.id });
}));
// Stops a browser being written to. A person removes their own; an administrator may remove anyone's.
pushRouter.delete("/:id", asyncHandler(async (req, res) => {
const id = /^\d+$/.test(req.params.id) ? Number(req.params.id) : NaN;
if (!Number.isInteger(id)) return res.status(400).json({ error: "invalid_id" });
const me = req.currentUser!;
const [row] = await db.select().from(pushSubscriptions).where(eq(pushSubscriptions.id, id)).limit(1);
if (!row || (row.userId !== me.id && me.role !== "admin")) return res.status(404).json({ error: "not_found" });
await db.delete(pushSubscriptions).where(and(eq(pushSubscriptions.id, id)));
await recordAudit({ actor: me, category: "push", action: "unsubscribe", targetType: "push_subscription", targetId: id, detail: { host: hostOf(row.endpoint), label: row.label } });
res.status(204).end();
}));
// A message to this person's own browsers, to see that it works.
pushRouter.post("/test", asyncHandler(async (req, res) => {
const { webPush } = await getSettings();
if (!webPush.enabled) return res.status(409).json(off);
const mine = await db.select().from(pushSubscriptions).where(eq(pushSubscriptions.userId, req.currentUser!.id));
if (mine.length === 0) return res.status(400).json({ error: "none", message: "This browser isn't subscribed yet." });
const outcome = await sendPush({ title: "Homelab Manager — Test", body: "Push notifications are working.", url: "/settings/notifications", tag: "test" }, mine);
res.json(outcome);
}));
+1
View File
@@ -64,6 +64,7 @@ const updateSchema = z.object({
.partial()
.optional(),
webhook: z.object({ enabled: z.boolean(), url: z.string(), secret: z.string() }).partial().optional(),
webPush: z.object({ enabled: z.boolean(), contact: z.string().trim().max(200) }).partial().optional(),
notifications: z
.object({
dnsAdd: z.boolean(),