- Export CSV for the small tables that had none (reads the table as shown) - Servers page shows a skeleton while loading instead of "No servers registered yet" - Recurring maintenance schedules can be edited (PUT /schedules/:id) - A server can be set to run on nothing (servers.no_parent, migration 0028) - Linux agent v3: package-manager question runs in ExecStartPre=-+ outside the sandbox, with --prepare-updates and --check-updates modes - Docs: README, DATABASE, AGENT_UPDATE Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
95 lines
4.0 KiB
TypeScript
95 lines
4.0 KiB
TypeScript
/**
|
|
* 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<number, ParentLink> {
|
|
const byId = new Map(rows.map((r) => [r.id, r]));
|
|
const out = new Map<number, ParentLink>();
|
|
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<number>, parents: Map<number, ParentLink>): Map<number, number> {
|
|
const covered = new Map<number, number>();
|
|
for (const id of offline) {
|
|
const seen = new Set<number>([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<number>();
|
|
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<ParentRow[]> {
|
|
return db
|
|
.select({ id: servers.id, name: servers.name, hostname: servers.hostname, parentServerId: servers.parentServerId, proxmoxNode: servers.proxmoxNode, noParent: servers.noParent })
|
|
.from(servers);
|
|
}
|