/** * Which server runs on which: a virtual machine on its Proxmox host, a container on a Docker box. When the host stops reporting, the * things on it stop too, and seven alerts about seven guests only hide the one that matters — so those alerts are folded under the * host's. The link is either set by hand ("runs on") or worked out: a server linked to a Proxmox guest runs on the server named after * that guest's Proxmox node, when there is exactly one such server. An admin can also say a server runs on nothing, which stops the guess. */ import { db } from "../db/client.js"; import { servers } from "../db/schema.js"; export interface ParentRow { id: number; name: string; hostname: string | null; parentServerId: number | null; proxmoxNode: string | null; /** Said by hand to run on nothing: no link, and no guessing from the Proxmox node. */ noParent?: boolean; } export interface ParentLink { parentId: number; /** Worked out from the Proxmox node rather than set by hand. */ auto: boolean; } const lc = (s: string | null | undefined) => (s ?? "").trim().toLowerCase(); /** Whether a server is the machine a Proxmox node name refers to: by its name, its hostname, or the first part of a fully qualified hostname. */ function isNode(row: ParentRow, node: string): boolean { const n = lc(node); if (!n) return false; const host = lc(row.hostname); return lc(row.name) === n || host === n || host.split(".")[0] === n; } /** Each server's parent, where it has one. A link set by hand wins; otherwise the unique server that is its Proxmox node. */ export function resolveParents(rows: ParentRow[]): Map { const byId = new Map(rows.map((r) => [r.id, r])); const out = new Map(); for (const row of rows) { if (row.noParent) continue; if (row.parentServerId !== null && row.parentServerId !== row.id && byId.has(row.parentServerId)) { out.set(row.id, { parentId: row.parentServerId, auto: false }); continue; } if (row.proxmoxNode) { const candidates = rows.filter((r) => r.id !== row.id && isNode(r, row.proxmoxNode!)); if (candidates.length === 1) out.set(row.id, { parentId: candidates[0].id, auto: true }); } } return out; } /** * Of the servers that are down, those whose cause is another server that is down too: each maps to the topmost ancestor that is down — * the root cause. A chain (guest → VM host → the box it hangs off) is followed all the way up; a loop in the links covers nothing. */ export function coveredByParent(offline: Set, parents: Map): Map { const covered = new Map(); for (const id of offline) { const seen = new Set([id]); let root: number | null = null; let looped = false; let link = parents.get(id); while (link && !seen.has(link.parentId)) { if (offline.has(link.parentId)) root = link.parentId; seen.add(link.parentId); link = parents.get(link.parentId); } if (link && seen.has(link.parentId)) looped = true; // A loop explains nothing (each would be waiting for the other), so its members stand alone and are announced. if (root !== null && !looped) covered.set(id, root); } return covered; } /** Would making `parentId` the parent of `id` close a loop? True if `id` is `parentId` or already sits above it. */ export function wouldLoop(rows: ParentRow[], id: number, parentId: number): boolean { const parents = resolveParents(rows); const seen = new Set(); let cur: number | undefined = parentId; while (cur !== undefined && !seen.has(cur)) { if (cur === id) return true; seen.add(cur); cur = parents.get(cur)?.parentId; } return false; } export async function loadParentRows(): Promise { return db .select({ id: servers.id, name: servers.name, hostname: servers.hostname, parentServerId: servers.parentServerId, proxmoxNode: servers.proxmoxNode, noParent: servers.noParent }) .from(servers); }