Match Uptime Kuma monitors to integrations and DNS providers as well as servers

A monitor whose address is the host of an integration's or DNS provider's own
address now gets a maintenance window there, alongside the existing match
against servers. Matching stays exact (no inference through DNS), and monitors
that match nothing are listed with their address in the sync result.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
bobbanandClaude Sonnet 5.5 committed 2026-10-12 00:13:04 +02:00
1 parent 27a191b54c
commit 3e6404371b
7 files changed
+217 -53

No files matched your search

+72 -9
View File
@@ -23,11 +23,12 @@ process.env.CREDENTIALS_ENCRYPTION_KEY = "ab".repeat(32);
const express = (await import("express")).default;
const { Server: IOServer } = await import("socket.io");
const { db } = await import("../src/db/client.js");
const { users, servers, integrations, maintenanceWindows } = await import("../src/db/schema.js");
const { users, servers, integrations, dnsProviders, maintenanceWindows } = await import("../src/db/schema.js");
const { runMigrations } = await import("../src/db/migrate.js");
const { maintenanceRouter } = await import("../src/routes/maintenance.js");
const { kumaInstant, normalizeMaintenanceList, monitorIdsFromReply } = await import("../src/integrations/uptimekuma/maintenanceFeed.js");
const { planWindowSync, desiredWindows, MANUAL_WINDOW_MS } = await import("../src/services/kumaMaintenanceSync.js");
const { hostOfAddress, targetsForHost } = await import("../src/services/kumaTargets.js");
const { listActiveWindows, listUpcomingWindows } = await import("../src/services/maintenance.js");
await runMigrations();
@@ -77,11 +78,47 @@ describe("reading Kuma's lists", () => {
});
});
describe("which things a monitor's address belongs to", () => {
const serverList = [{ id: 1, name: "elsa", hostname: "elsa.lab.example", ips: ["10.0.0.5", "fd00::5"] }];
const addressed = [
{ type: "integration" as const, id: 2, name: "Proxmox", host: "pve.lab.example" },
{ type: "integration" as const, id: 3, name: "NAS", host: "10.0.0.20" },
{ type: "dns_provider" as const, id: 4, name: "Technitium", host: "fd00:0:0:0:0:0:0:53" },
];
const names = (host: string | null) => targetsForHost(host, serverList, addressed).map((t) => `${t.type}:${t.name}`);
test("the address a monitor watches is matched to a server, an integration or a DNS provider", () => {
assert.deepEqual(names("10.0.0.5"), ["server:elsa"]);
assert.deepEqual(names("pve.lab.example"), ["integration:Proxmox"]);
assert.deepEqual(names("PVE.lab.example."), ["integration:Proxmox"]);
assert.deepEqual(names("10.0.0.20"), ["integration:NAS"]);
});
test("an IPv6 address matches however it is written", () => {
assert.deepEqual(names("fd00::53"), ["dns_provider:Technitium"]);
});
test("nothing is inferred: a name that is merely near, or no address, matches nothing", () => {
assert.deepEqual(names("www.lab.example"), []);
assert.deepEqual(names("10.0.0.21"), []);
assert.deepEqual(names(null), []);
assert.deepEqual(names(""), []);
});
test("the host is read out of an address as typed", () => {
assert.equal(hostOfAddress("https://PVE.lab.example:8006/"), "pve.lab.example");
assert.equal(hostOfAddress("10.0.0.5:8006"), "10.0.0.5");
assert.equal(hostOfAddress("http://[fd00::1]:5380"), "fd00::1");
assert.equal(hostOfAddress(""), null);
assert.equal(hostOfAddress(null), null);
});
});
describe("planWindowSync", () => {
const now = new Date("2026-10-10T12:00:00.000Z");
const S = "kuma:1:5";
const want = (start: string | null, end: string, serverId = 7) => ({ source: S, serverId, start, end, reason: "r" });
const row = (id: number, startedAt: string, endsAt: string, targetId = 7) => ({ id, source: S, targetId, startedAt, endsAt });
const want = (start: string | null, end: string, targetId = 7) => ({ source: S, targetType: "server" as const, targetId, start, end, reason: "r" });
const row = (id: number, startedAt: string, endsAt: string, targetId = 7) => ({ id, source: S, targetType: "server" as const, targetId, startedAt, endsAt });
test("a slot Kuma reports that isn't here yet is added", () => {
const plan = planWindowSync([want("2026-10-11T00:00:00.000Z", "2026-10-11T01:00:00.000Z")], [], now);
@@ -142,7 +179,8 @@ describe("planWindowSync", () => {
describe("desiredWindows", () => {
const now = new Date("2026-10-10T12:00:00.000Z");
const server = (id: string) => (id === "1" ? { id: 10, name: "elsa" } : id === "2" ? { id: 20, name: "anna" } : null);
const server = (id: string) =>
id === "1" ? [{ type: "server" as const, id: 10, name: "elsa" }] : id === "2" ? [{ type: "server" as const, id: 20, name: "anna" }] : [];
const label = (id: string) => `monitor ${id}`;
const base = { id: 5, title: "Nightly", strategy: "recurring-interval", active: true, status: "scheduled", timezone: "UTC", timeslots: [] as { start: string; end: string }[], monitorIds: ["1", "2", "3"] };
const run = (m: Partial<typeof base>) => desiredWindows(1, "Kuma", [{ ...base, ...m }], server, label, now);
@@ -150,8 +188,8 @@ describe("desiredWindows", () => {
test("each slot on each matching server; monitors that match no server are reported", () => {
const { desired, summary } = run({ timeslots: [{ start: iso(now.getTime() + 2 * HOUR), end: iso(now.getTime() + 3 * HOUR) }] });
assert.equal(desired.length, 2);
assert.deepEqual(desired.map((d) => d.serverId).sort(), [10, 20]);
assert.deepEqual(summary[0].servers, ["anna", "elsa"]);
assert.deepEqual(desired.map((d) => d.targetId).sort(), [10, 20]);
assert.deepEqual(summary[0].targets.map((t) => t.name), ["anna", "elsa"]);
assert.deepEqual(summary[0].unmatched, ["monitor 3"]);
assert.equal(summary[0].note, null);
});
@@ -211,7 +249,7 @@ describe("syncing from a stand-in Uptime Kuma", () => {
before(async () => {
httpServer = http.createServer((req, res) => {
if (req.url === "/metrics") {
res.end([metric(1, "elsa.lab.example"), metric(2, "anna.lab.example"), metric(3, "10.9.9.9")].join("\n"));
res.end([metric(1, "elsa.lab.example"), metric(2, "anna.lab.example"), metric(3, "10.9.9.9"), metric(4, "pve.lab.example"), metric(5, "10.0.0.53")].join("\n"));
} else {
res.statusCode = 404;
res.end();
@@ -248,6 +286,10 @@ describe("syncing from a stand-in Uptime Kuma", () => {
.returning()
)[0].id;
// Things that are not servers: a Proxmox integration reached by name, and a DNS provider reached by IP.
await db.insert(integrations).values({ type: "proxmox", name: "Proxmox Felicia", baseUrl: "https://pve.lab.example:8006", config: "{}" });
await db.insert(dnsProviders).values({ providerType: "technitium", name: "Technitium Ginny", config: JSON.stringify({ url: "http://10.0.0.53:5380" }) });
const a = express();
a.use(express.json());
a.use((req, _res, next) => {
@@ -298,8 +340,8 @@ describe("syncing from a stand-in Uptime Kuma", () => {
const { status, body } = await sync();
assert.equal(status, 200);
assert.equal(body.started, 2);
assert.deepEqual(body.maintenances[0].servers, ["anna", "elsa"]);
assert.deepEqual(body.maintenances[0].unmatched, ["m3"]);
assert.deepEqual(body.maintenances[0].targets.map((t: { name: string }) => t.name), ["anna", "elsa"]);
assert.deepEqual(body.maintenances[0].unmatched, ["m3 (10.9.9.9)"]);
assert.equal((await listActiveWindows()).length, 0, "it hasn't begun, so it silences nothing yet");
const upcoming = await listUpcomingWindows();
@@ -308,6 +350,27 @@ describe("syncing from a stand-in Uptime Kuma", () => {
assert.match(upcoming[0].reason ?? "", /Nightly patching/);
});
test("monitors on an integration's or a DNS provider's own address get windows there too", async () => {
await db.delete(maintenanceWindows);
const start = Date.now() + 4 * HOUR;
kuma.list = { "6": daily(start, start + HOUR, { id: 6, title: "Sunday" }) };
kuma.monitorsOf = { "6": [4, 5] };
const { body } = await sync();
assert.equal(body.started, 2);
assert.deepEqual(
body.maintenances[0].targets.map((t: { type: string; name: string }) => `${t.type}:${t.name}`).sort(),
["dns_provider:Technitium Ginny", "integration:Proxmox Felicia"],
);
const types = (await listUpcomingWindows()).map((w) => w.targetType).sort();
assert.deepEqual(types, ["dns_provider", "integration"]);
// Back to the first scenario, for the tests that follow.
kuma.list = { "5": daily(start, start + HOUR) };
kuma.monitorsOf = { "5": [1, 2, 3] };
await db.delete(maintenanceWindows);
await sync();
});
test("syncing again adds nothing", async () => {
const { body } = await sync();
assert.deepEqual([body.started, body.updated, body.removed], [0, 0, 0]);