Name ports after the Docker containers that publish them

The health pass keeps the host ports Dockhand says running containers
publish (new table server_container_ports, migration 0080), matched to a
server by its Docker host's name. They name the port on the server's Ports
card and the network Ports table, ahead of a port preset and behind a
label somebody wrote, and in the new-port alert ("8096/tcp jellyfin
(container)"). Never written onto port notes, so a container's port is
still a new port; an unreadable host keeps its names, a day-old name is
dropped. Dockhand is now read on the health pass even with container
alerts off.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
bobbanandClaude Sonnet 5.5 committed 2026-10-31 22:45:50 +01:00
1 parent 75f060049f
commit 9ca83994fb
18 files changed
+9026 -29

No files matched your search

+18
View File
@@ -689,6 +689,24 @@ export const serverPorts = sqliteTable(
(t) => [uniqueIndex("server_ports_unique").on(t.serverId, t.port, t.protocol)],
);
// The host ports the containers on a server publish, as Dockhand last said (services/containerPorts.ts): one row per port, replaced at every look at that server's Docker host. They name
// the ports on the Ports pages and in the "new port" alert (never written onto server_ports, so a note somebody wrote is never touched, and they do not count as "expected").
export const serverContainerPorts = sqliteTable(
"server_container_ports",
{
id: integer("id").primaryKey({ autoIncrement: true }),
serverId: integer("server_id")
.notNull()
.references(() => servers.id, { onDelete: "cascade" }),
port: integer("port").notNull(),
protocol: text("protocol").$type<"tcp" | "udp">().notNull().default("tcp"),
container: text("container").notNull(),
image: text("image").notNull().default(""),
updatedAt: text("updated_at").notNull(), // ISO
},
(t) => [uniqueIndex("server_container_ports_unique").on(t.serverId, t.port, t.protocol)],
);
// A manually-recorded port opening on something this app doesn't monitor directly — a router's port forward, an
// edge firewall rule, a cloud provider's security group, etc. Distinct from serverPorts (which is what a server
// itself, or a scan of it, reports): this is what someone tells the app is open further out on the network path,
+5
View File
@@ -7,6 +7,7 @@ import { requireAuth, requireRole } from "../auth/middleware.js";
import { recordAudit } from "../services/audit.js";
import { type AgentPort, groupAgentPorts, parseJson, splitPortKey } from "../services/agentPorts.js";
import { listPresets, serviceName, serviceNames } from "../services/portPresets.js";
import { containerPortName, containerPortNames, type ContainerPortName } from "../services/containerPorts.js";
import { asyncHandler } from "../utils/asyncHandler.js";
export const portsRouter = Router();
@@ -26,6 +27,8 @@ export interface AgentPortRow {
localOnly: boolean;
/** What a port preset calls it ("RDP"), when one does. */
service: string | null;
/** The container that publishes it, as Dockhand says, when one does. */
container: ContainerPortName | null;
lastSeenAt: string | null;
}
@@ -35,6 +38,7 @@ portsRouter.get("/agent", asyncHandler(async (_req, res) => {
.from(servers);
const names = serviceNames(await listPresets());
const containers = await containerPortNames();
const out: AgentPortRow[] = [];
for (const s of rows) {
const raw = parseJson<AgentPort[] | null>(s.listeningPorts, null);
@@ -51,6 +55,7 @@ portsRouter.get("/agent", asyncHandler(async (_req, res) => {
process: grouped.process,
localOnly: grouped.localOnly,
service: serviceName(names, protocol, port),
container: containerPortName(containers, s.id, protocol, port),
lastSeenAt: s.lastSeenAt,
});
}
+6
View File
@@ -8,6 +8,7 @@ import { recordAudit } from "../services/audit.js";
import { beginScan, endScan, MAX_SCAN_SPAN, resolveScanTarget, scanPorts, toRanges } from "../services/portScan.js";
import { type AgentPort, groupAgentPorts, parseJson } from "../services/agentPorts.js";
import { listPresets, serviceName, serviceNames } from "../services/portPresets.js";
import { containerPortName, containerPortNames, type ContainerPortName } from "../services/containerPorts.js";
import { recordPortScan } from "../services/portScanRecord.js";
import { newListeningPorts, parseBaseline, portKey } from "../services/newPorts.js";
import { getSettings } from "../services/settingsStore.js";
@@ -36,6 +37,8 @@ export interface PortEntry {
comment: string | null;
/** What a port preset calls this port ("RDP"), when one does; shown where there is no label of its own. */
service: string | null;
/** The container that publishes it, as Dockhand says ("jellyfin"), when one does; shown where there is no label of its own, ahead of the preset's name. */
container: ContainerPortName | null;
/** The last scan from this app connected to it. */
scanOpen: boolean;
lastSeenOpenAt: string | null;
@@ -60,6 +63,7 @@ async function buildPortList(serverId: number) {
const fresh = new Set(alerting ? newListeningPorts(agentRaw, parseBaseline(server.portBaseline), expected).map((p) => portKey(p.protocol, p.port)) : []);
const names = serviceNames(await listPresets());
const containers = await containerPortNames(serverId);
const entries = new Map<string, PortEntry>();
for (const row of rows) {
const key = `${row.protocol}:${row.port}`;
@@ -70,6 +74,7 @@ async function buildPortList(serverId: number) {
label: row.label,
comment: row.comment,
service: serviceName(names, row.protocol, row.port),
container: containerPortName(containers, serverId, row.protocol, row.port),
scanOpen: row.open,
lastSeenOpenAt: row.lastSeenOpenAt,
agent: agent.get(key) ?? null,
@@ -87,6 +92,7 @@ async function buildPortList(serverId: number) {
label: null,
comment: null,
service: serviceName(names, protocol, Number(port)),
container: containerPortName(containers, serverId, protocol, Number(port)),
scanOpen: false,
lastSeenOpenAt: null,
agent: info,
+112
View File
@@ -0,0 +1,112 @@
/**
* The ports the containers on a server publish, said by name: "8096 → jellyfin". Dockhand knows which containers run on each Docker host and which host ports they publish; the host an
* environment is (its name there) is matched to a server the way Uptime Kuma targets and Dockhand-recognised apps are (services/uptimeKumaMatch.ts), and what it publishes is kept per server
* (server_container_ports) so the Ports pages and the "new port" alert can name a port without asking Dockhand each time.
*
* It is only ever a name shown beside the port. Nothing is written onto the server's own port notes, so a label somebody wrote wins, and a container's port does not count as "expected" —
* a container that starts publishing something new is still news, and the alert now says which container.
*
* Read on the health pass from the container list it already takes. A host Dockhand could not read keeps what it had; what has not been refreshed for a day is dropped.
*/
import { eq, inArray, lt } from "drizzle-orm";
import { db } from "../db/client.js";
import { serverContainerPorts, servers } from "../db/schema.js";
import { matchServerForTarget, toMatchableServers, type MatchableServer } from "./uptimeKumaMatch.js";
/** What Dockhand's container list is read as here; the adapter's own types satisfy it. */
export interface ContainerPortRead {
containers: { name: string; image: string; state: string; environmentId: number; ports: { publicPort: number | null; protocol: string }[] }[];
environments: { id: number; name: string }[];
unreachable: { environmentId: number }[];
}
export interface ContainerPortRow {
port: number;
protocol: "tcp" | "udp";
container: string;
image: string;
}
/** A refresh older than this is not trusted any more: the Docker host stopped being read, or the integration is gone. */
export const CONTAINER_PORTS_STALE_MS = 24 * 3_600_000;
export interface ContainerPortPlan {
/** For each server whose Docker host was read: what its running containers publish. A server with a host but nothing published has an empty list, so old rows are cleared. */
rows: Map<number, ContainerPortRow[]>;
}
/**
* Pure: which published port belongs to which server. Only running containers count, only host ports (a port with no host side is not on the server), only TCP and UDP. A host Dockhand
* could not read, or one that matches no server, is left out altogether. The same host port twice (IPv4 and IPv6 bindings, usually) is one row; if two containers claim it, the first by name.
*/
export function planContainerPorts(read: ContainerPortRead, servers: MatchableServer[]): ContainerPortPlan {
const unreachable = new Set(read.unreachable.map((u) => u.environmentId));
const serverOfEnv = new Map<number, number>();
const rows = new Map<number, ContainerPortRow[]>();
for (const env of read.environments) {
if (unreachable.has(env.id)) continue;
const server = matchServerForTarget(env.name, servers);
if (!server) continue;
serverOfEnv.set(env.id, server.id);
if (!rows.has(server.id)) rows.set(server.id, []);
}
const seen = new Set<string>();
for (const c of [...read.containers].sort((a, b) => a.name.localeCompare(b.name))) {
const serverId = serverOfEnv.get(c.environmentId);
if (serverId === undefined || c.state !== "running") continue;
for (const p of c.ports) {
const protocol = p.protocol === "tcp" || p.protocol === "udp" ? p.protocol : null;
if (p.publicPort === null || protocol === null) continue;
const key = `${serverId}:${protocol}:${p.publicPort}`;
if (seen.has(key)) continue;
seen.add(key);
rows.get(serverId)!.push({ port: p.publicPort, protocol, container: c.name.replace(/^\//, ""), image: c.image });
}
}
for (const list of rows.values()) list.sort((a, b) => a.port - b.port || a.protocol.localeCompare(b.protocol));
return { rows };
}
/** Takes in what a Dockhand read says, replacing what was kept for each server it covers. */
export async function refreshContainerPorts(read: ContainerPortRead, now = new Date()): Promise<void> {
const matchable = toMatchableServers((await db.select({ id: servers.id, name: servers.name, hostname: servers.hostname, ips: servers.ipAddresses }).from(servers)));
const { rows } = planContainerPorts(read, matchable);
if (rows.size === 0) return;
const updatedAt = now.toISOString();
await db.transaction(async (tx) => {
await tx.delete(serverContainerPorts).where(inArray(serverContainerPorts.serverId, [...rows.keys()]));
for (const [serverId, list] of rows) {
if (list.length > 0) await tx.insert(serverContainerPorts).values(list.map((r) => ({ serverId, updatedAt, ...r })));
}
});
}
/** Drops what has not been refreshed for a day. */
export async function purgeStaleContainerPorts(nowMs = Date.now()): Promise<void> {
await db.delete(serverContainerPorts).where(lt(serverContainerPorts.updatedAt, new Date(nowMs - CONTAINER_PORTS_STALE_MS).toISOString()));
}
export interface ContainerPortName {
container: string;
image: string;
}
/** The kept names, by server and then "tcp:8096"; nothing older than a day. Pass a server to read only that one's. */
export async function containerPortNames(serverId?: number, nowMs = Date.now()): Promise<Map<number, Map<string, ContainerPortName>>> {
const fresh = new Date(nowMs - CONTAINER_PORTS_STALE_MS).toISOString();
const rows = await db.select().from(serverContainerPorts).where(serverId === undefined ? undefined : eq(serverContainerPorts.serverId, serverId));
const out = new Map<number, Map<string, ContainerPortName>>();
for (const r of rows) {
if (r.updatedAt < fresh) continue;
const byKey = out.get(r.serverId) ?? new Map<string, ContainerPortName>();
byKey.set(`${r.protocol}:${r.port}`, { container: r.container, image: r.image });
out.set(r.serverId, byKey);
}
return out;
}
/** How a container reads in the new-port alert: "jellyfin (container)". */
export const containerLabel = (name: ContainerPortName | null): string | null => (name ? `${name.container} (container)` : null);
export const containerPortName =(names: Map<number, Map<string, ContainerPortName>>, serverId: number, protocol: string, port: number): ContainerPortName | null => names.get(serverId)?.get(`${protocol}:${port}`) ?? null;
+35 -19
View File
@@ -14,6 +14,7 @@ import { getFailingSources } from "./integrationHealthMonitor.js";
import { hostHealthConditions, parseHostHealth, withoutIgnoredServices, type HostHealth } from "./hostHealth.js";
import { newListeningPorts, newPortMessage, parseBaseline, portKey, type NewPort } from "./newPorts.js";
import { listPresets, serviceName, serviceNames } from "./portPresets.js";
import { containerLabel, containerPortName, containerPortNames, purgeStaleContainerPorts, refreshContainerPorts } from "./containerPorts.js";
import { parseJson, type AgentPort } from "./agentPorts.js";
import { diskLimit, parseThresholds, type ServerThresholds } from "./serverThresholds.js";
import { computeRuleFindings, findingKey, listRules, type RuleFinding } from "./alertRules.js";
@@ -488,6 +489,39 @@ export async function collectSnapshot(): Promise<{ snapshot: HealthSnapshot; hel
const snapshot: HealthSnapshot = { servers: [], proxmox: [], synology: [] };
const { healthChecks: checks } = await getSettings();
// Docker containers, from Dockhand: what is broken, what stopped unexpectedly. A Dockhand that cannot be read holds its containers' alerts rather than clearing them. Read first, and
// whether or not the container alerts are on, because the ports the containers publish are kept from this same read (services/containerPorts.ts) to name them in the new-port alert below.
if (checks.containerAlerts) {
snapshot.dockerIgnore = checks.containerIgnore;
snapshot.dockerMemory = await loadContainerMemory();
}
try {
await purgeStaleContainerPorts();
} catch (err) {
console.error("[health] couldn't clear old container ports:", err instanceof Error ? err.message : err);
}
for (const row of await db.select({ id: integrations.id, name: integrations.name }).from(integrations).where(and(eq(integrations.type, "dockhand"), eq(integrations.enabled, true)))) {
try {
const loaded = await loadIntegrationConfig(row.id);
if (!loaded) continue;
const read = await createDockhandAdapter(loaded.config as any).listContainersDetailed();
if (checks.containerAlerts) {
(snapshot.docker ??= []).push({ integrationId: row.id, integrationName: row.name, containers: read.containers, unreachable: read.unreachable });
for (const u of read.unreachable) held.add(`dockhand:${row.id}:${u.environmentId}`);
}
try {
await refreshContainerPorts(read);
} catch (err) {
console.error(`[health] couldn't keep the ports of Docker integration ${row.id}:`, err instanceof Error ? err.message : err);
}
} catch (err) {
console.error(`[health] couldn't read Docker integration ${row.id}:`, err instanceof Error ? err.message : err);
if (checks.containerAlerts) held.add(`dockhand:${row.id}`);
}
}
const containerNames = await containerPortNames();
// The ports somebody has said are expected (a label or a comment on the Ports page); a row that only records an open scan says nothing.
const portNames = serviceNames(await listPresets());
const expectedByServer = new Map<number, Set<string>>();
@@ -506,7 +540,7 @@ export async function collectSnapshot(): Promise<{ snapshot: HealthSnapshot; hel
} catch {
// a malformed report shouldn't stop every other server being checked
}
const newPorts = checks.newPortAlerts ? newListeningPorts(parseJson<AgentPort[] | null>(s.listeningPorts, null), parseBaseline(s.portBaseline), expectedByServer.get(s.id) ?? new Set()).map((p) => ({ ...p, service: serviceName(portNames, "tcp", p.port) })) : [];
const newPorts = checks.newPortAlerts ? newListeningPorts(parseJson<AgentPort[] | null>(s.listeningPorts, null), parseBaseline(s.portBaseline), expectedByServer.get(s.id) ?? new Set()).map((p) => ({ ...p, service: containerLabel(containerPortName(containerNames, s.id, "tcp", p.port)) ?? serviceName(portNames, "tcp", p.port) })) : [];
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), newPorts });
}
@@ -801,24 +835,6 @@ export async function collectSnapshot(): Promise<{ snapshot: HealthSnapshot; hel
held.add("duplicati");
}
// Docker containers, from Dockhand: what is broken, what stopped unexpectedly. A Dockhand that cannot be read holds its containers' alerts rather than clearing them.
if (checks.containerAlerts) {
snapshot.dockerIgnore = checks.containerIgnore;
snapshot.dockerMemory = await loadContainerMemory();
for (const row of await db.select({ id: integrations.id, name: integrations.name }).from(integrations).where(and(eq(integrations.type, "dockhand"), eq(integrations.enabled, true)))) {
try {
const loaded = await loadIntegrationConfig(row.id);
if (!loaded) continue;
const read = await createDockhandAdapter(loaded.config as any).listContainersDetailed();
(snapshot.docker ??= []).push({ integrationId: row.id, integrationName: row.name, containers: read.containers, unreachable: read.unreachable });
for (const u of read.unreachable) held.add(`dockhand:${row.id}:${u.environmentId}`);
} catch (err) {
console.error(`[health] couldn't read Docker integration ${row.id}:`, err instanceof Error ? err.message : err);
held.add(`dockhand:${row.id}`);
}
}
}
// Vulnerabilities Dockhand's scans found, as the sync pass stored them (only critical ones are raised). Switched off with the setting; a reading gone stale is held, not cleared.
if (checks.vulnerabilityAlerts) {
try {