Add address aliases for the Uptime Kuma maintenance sync, and ignore Kuma's "null"
Uptime Kuma often watches a host by another address than the one it was added with here (a Proxmox host monitored over Tailscale, say). Address aliases on the Maintenance page say that an address belongs to a server, integration or DNS provider, so the sync puts the window there; matching stays exact. Migration 0018 adds maintenance_aliases. Also fixes monitors whose empty hostname/URL/port arrive from Kuma's metrics as the text "null": they were taken as an address, hiding the real URL. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
3e6404371b
commit
44cc9d1cf2
12 files changed
+2239
-14
No files matched your search
@@ -1,8 +1,9 @@
|
||||
import { Router } from "express";
|
||||
import { eq } from "drizzle-orm";
|
||||
import { and, eq } from "drizzle-orm";
|
||||
import { z } from "zod";
|
||||
import { db } from "../db/client.js";
|
||||
import { dnsProviders, integrations, maintenanceTargetTypes, maintenanceWindows, servers } from "../db/schema.js";
|
||||
import { dnsProviders, integrations, maintenanceAliases, maintenanceTargetTypes, maintenanceWindows, servers } from "../db/schema.js";
|
||||
import { hostOfAddress } from "../services/kumaTargets.js";
|
||||
import { requireAuth, requireRole } from "../auth/middleware.js";
|
||||
import { recordAudit } from "../services/audit.js";
|
||||
import { describeTarget, listActiveWindows, listUpcomingWindows, startOrExtendWindow } from "../services/maintenance.js";
|
||||
@@ -38,7 +39,13 @@ maintenanceRouter.get("/", asyncHandler(async (_req, res) => {
|
||||
db.select({ id: integrations.id, name: integrations.name, type: integrations.type }).from(integrations).orderBy(integrations.name),
|
||||
db.select({ id: dnsProviders.id, name: dnsProviders.name, providerType: dnsProviders.providerType }).from(dnsProviders).orderBy(dnsProviders.name),
|
||||
]);
|
||||
res.json({ windows, upcoming, targets: { servers: serverRows, integrations: integrationRows, dnsProviders: providerRows } });
|
||||
const aliases = [];
|
||||
for (const a of await db.select().from(maintenanceAliases).orderBy(maintenanceAliases.address)) {
|
||||
const target = await describeTarget(a.targetType, a.targetId);
|
||||
if (!target) continue; // target was deleted — the alias has nothing left to point at
|
||||
aliases.push({ id: a.id, address: a.address, targetType: a.targetType, targetId: a.targetId, targetName: target.name, targetKind: target.kind });
|
||||
}
|
||||
res.json({ windows, upcoming, aliases, targets: { servers: serverRows, integrations: integrationRows, dnsProviders: providerRows } });
|
||||
}));
|
||||
|
||||
const startSchema = z.object({
|
||||
@@ -145,6 +152,59 @@ maintenanceRouter.post("/import/uptimekuma", requireRole("operator"), asyncHandl
|
||||
res.json({ imported, unmatched, monitorsInMaintenance: inMaintenance.length, monitorsChecked: monitors.length });
|
||||
}));
|
||||
|
||||
const aliasSchema = z.object({
|
||||
address: z.string().min(1).max(253),
|
||||
targetType: z.enum(maintenanceTargetTypes),
|
||||
targetId: z.number().int().positive(),
|
||||
});
|
||||
|
||||
maintenanceRouter.post("/aliases", requireRole("operator"), asyncHandler(async (req, res) => {
|
||||
const parsed = aliasSchema.safeParse(req.body);
|
||||
if (!parsed.success) {
|
||||
return res.status(400).json({ error: "invalid_body", message: "Give an address and say what it belongs to.", details: parsed.error.flatten() });
|
||||
}
|
||||
const host = hostOfAddress(parsed.data.address);
|
||||
if (!host) {
|
||||
return res.status(400).json({ error: "invalid_address", message: "That isn't an address — use a host name or IP, like 100.64.0.7 or pve.example.lan." });
|
||||
}
|
||||
const target = await describeTarget(parsed.data.targetType, parsed.data.targetId);
|
||||
if (!target) return res.status(404).json({ error: "not_found", message: "That target doesn't exist." });
|
||||
|
||||
const [existing] = await db
|
||||
.select()
|
||||
.from(maintenanceAliases)
|
||||
.where(and(eq(maintenanceAliases.address, host), eq(maintenanceAliases.targetType, parsed.data.targetType), eq(maintenanceAliases.targetId, parsed.data.targetId)))
|
||||
.limit(1);
|
||||
if (existing) return res.status(409).json({ error: "exists", message: `${host} is already an alias of ${target.name}.` });
|
||||
|
||||
const [created] = await db.insert(maintenanceAliases).values({ address: host, targetType: parsed.data.targetType, targetId: parsed.data.targetId }).returning();
|
||||
await recordAudit({
|
||||
actor: req.currentUser!,
|
||||
category: "maintenance",
|
||||
action: "alias_add",
|
||||
targetType: parsed.data.targetType,
|
||||
targetId: parsed.data.targetId,
|
||||
detail: { name: target.name, address: host },
|
||||
});
|
||||
res.status(201).json({ alias: { ...created, targetName: target.name, targetKind: target.kind } });
|
||||
}));
|
||||
|
||||
maintenanceRouter.delete("/aliases/:id", requireRole("operator"), asyncHandler(async (req, res) => {
|
||||
const id = Number(req.params.id);
|
||||
const [existing] = await db.select().from(maintenanceAliases).where(eq(maintenanceAliases.id, id)).limit(1);
|
||||
if (!existing) return res.status(404).json({ error: "not_found" });
|
||||
await db.delete(maintenanceAliases).where(eq(maintenanceAliases.id, id));
|
||||
await recordAudit({
|
||||
actor: req.currentUser!,
|
||||
category: "maintenance",
|
||||
action: "alias_remove",
|
||||
targetType: existing.targetType,
|
||||
targetId: existing.targetId,
|
||||
detail: { address: existing.address },
|
||||
});
|
||||
res.status(204).end();
|
||||
}));
|
||||
|
||||
const syncUptimeKumaSchema = z.object({ integrationId: z.number().int().positive() });
|
||||
|
||||
/** Reads Uptime Kuma's maintenance schedule now and brings the mirrored windows in line with it (the same pass that runs every 15 minutes when switched on). */
|
||||
|
||||
Reference in new issue
Block a user