Add runbooks, backup coverage, inventory, roles from groups, Prometheus metrics and event webhooks
- Runbooks: notes per server/integration, shown next to matching alerts - Backup coverage page across Proxmox and PBS, with a digest section - Hardware inventory with warranty, power and running cost - Authentik group to role mapping with preview and safeguards - /metrics (bearer token) and signed outgoing event webhooks - Docs: README, DATABASE, NOTIFICATIONS, ROLES and new API.md Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
c36e2092c5
commit
c17e9aa140
55 files changed
+7177
-27
No files matched your search
@@ -10,6 +10,11 @@ import { authRouter } from "./auth/router.js";
|
||||
import { meRouter } from "./routes/me.js";
|
||||
import { uptimeRouter } from "./routes/uptime.js";
|
||||
import { shareLinksRouter } from "./routes/shareLinks.js";
|
||||
import { runbooksRouter } from "./routes/runbooks.js";
|
||||
import { inventoryRouter } from "./routes/inventory.js";
|
||||
import { backupsRouter } from "./routes/backups.js";
|
||||
import { metricsRouter, metricsTokensRouter } from "./routes/metrics.js";
|
||||
import { eventWebhooksRouter } from "./routes/eventWebhooks.js";
|
||||
import { publicWallRouter } from "./routes/publicWall.js";
|
||||
import { usersRouter } from "./routes/users.js";
|
||||
import { auditLogRouter } from "./routes/auditLog.js";
|
||||
@@ -92,6 +97,13 @@ export function createApp() {
|
||||
app.use("/api/me", meRouter);
|
||||
app.use("/api/uptime", uptimeRouter);
|
||||
app.use("/api/share-links", shareLinksRouter);
|
||||
app.use("/api/runbooks", runbooksRouter);
|
||||
app.use("/api/inventory", inventoryRouter);
|
||||
app.use("/api/backups", backupsRouter);
|
||||
app.use("/api/metrics-tokens", metricsTokensRouter);
|
||||
app.use("/api/event-webhooks", eventWebhooksRouter);
|
||||
// Prometheus scrapes this with a bearer token, not a session.
|
||||
app.use("/metrics", metricsRouter);
|
||||
app.use("/api/public/wall", publicWallRouter);
|
||||
app.use("/api/users", usersRouter);
|
||||
app.use("/api/audit-log", auditLogRouter);
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
/**
|
||||
* Roles from the groups the identity provider says a person is in. An administrator names the groups that mean "admin" and "operator";
|
||||
* at each sign-in the person's role follows from them. Kept free of the database and the sign-in flow so every rule can be tested exactly.
|
||||
*/
|
||||
import type { UserRole } from "../db/schema.js";
|
||||
|
||||
export interface SignInRoleSettings {
|
||||
/** Roles follow the groups. Off, roles are set by hand on the Users page, as they always were. */
|
||||
roleFromGroups: boolean;
|
||||
/** The name of the claim that carries the groups ("groups" in Authentik). */
|
||||
groupsClaim: string;
|
||||
adminGroups: string[];
|
||||
operatorGroups: string[];
|
||||
/** What someone in none of the named groups gets: viewer, or whatever they already have. */
|
||||
otherwise: "viewer" | "keep";
|
||||
}
|
||||
|
||||
const MAX_GROUPS = 200;
|
||||
const MAX_GROUP_LENGTH = 200;
|
||||
|
||||
/**
|
||||
* The groups named in a claim, from every place the provider might have put it (the ID token, the user-info answer). Returns null when
|
||||
* none of them has the claim at all — which is "we weren't told", not "in no groups", and must never be mistaken for it.
|
||||
*/
|
||||
export function extractGroups(sources: Record<string, unknown>[], claim: string): string[] | null {
|
||||
let found = false;
|
||||
const groups = new Set<string>();
|
||||
for (const source of sources) {
|
||||
if (!source || !Object.prototype.hasOwnProperty.call(source, claim)) continue;
|
||||
const value = source[claim];
|
||||
if (value === null || value === undefined) continue;
|
||||
found = true;
|
||||
const list = Array.isArray(value) ? value : typeof value === "string" ? [value] : [];
|
||||
for (const g of list) {
|
||||
if (typeof g !== "string") continue;
|
||||
const name = g.trim().slice(0, MAX_GROUP_LENGTH);
|
||||
if (name) groups.add(name);
|
||||
}
|
||||
}
|
||||
return found ? [...groups].slice(0, MAX_GROUPS) : null;
|
||||
}
|
||||
|
||||
const norm = (s: string) => s.trim().toLowerCase();
|
||||
|
||||
/** The role these groups give: admin if any is an admin group, else operator if any is an operator group, else what `otherwise` says. */
|
||||
export function roleFromGroups(settings: SignInRoleSettings, groups: string[], current: UserRole): UserRole {
|
||||
const have = new Set(groups.map(norm));
|
||||
if (settings.adminGroups.some((g) => have.has(norm(g)))) return "admin";
|
||||
if (settings.operatorGroups.some((g) => have.has(norm(g)))) return "operator";
|
||||
return settings.otherwise === "keep" ? current : "viewer";
|
||||
}
|
||||
@@ -3,6 +3,8 @@ import * as client from "openid-client";
|
||||
import { eq } from "drizzle-orm";
|
||||
import { getOidcConfig } from "./oidc.js";
|
||||
import { upsertUserFromLogin } from "./users.js";
|
||||
import { extractGroups } from "./groups.js";
|
||||
import { getSettings } from "../services/settingsStore.js";
|
||||
import { env } from "../env.js";
|
||||
import { db } from "../db/client.js";
|
||||
import { users } from "../db/schema.js";
|
||||
@@ -110,15 +112,35 @@ authRouter.get("/callback", signInLimiter, async (req, res, next) => {
|
||||
|
||||
let email: string | undefined = typeof claims.email === "string" ? claims.email : undefined;
|
||||
let name: string | undefined = typeof claims.name === "string" ? claims.name : undefined;
|
||||
const claimSources: Record<string, unknown>[] = [claims as Record<string, unknown>];
|
||||
try {
|
||||
const userinfo = await client.fetchUserInfo(config, tokens.access_token, claims.sub);
|
||||
email = userinfo.email ?? email;
|
||||
name = userinfo.name ?? userinfo.preferred_username ?? name;
|
||||
claimSources.push(userinfo as Record<string, unknown>);
|
||||
} catch {
|
||||
// fall back to ID token claims already captured above
|
||||
}
|
||||
|
||||
const { user, created } = await upsertUserFromLogin({ sub: claims.sub, email, name });
|
||||
// The groups, from wherever the provider put them. Read (and kept for the Users page) whether or not roles follow them, so the
|
||||
// names can be seen before they are relied on.
|
||||
const { signIn } = await getSettings();
|
||||
const groups = extractGroups(claimSources, signIn.groupsClaim);
|
||||
if (signIn.roleFromGroups && groups === null) {
|
||||
console.warn(`[auth] roles follow groups, but the sign-in carried no "${signIn.groupsClaim}" claim — roles left as they are`);
|
||||
}
|
||||
|
||||
const { user, created, roleChange } = await upsertUserFromLogin({ sub: claims.sub, email, name, groups });
|
||||
if (roleChange) {
|
||||
await recordAudit({
|
||||
actor: user,
|
||||
category: "user",
|
||||
action: roleChange.blocked ? "role_from_groups_blocked" : "role_from_groups",
|
||||
targetType: "user",
|
||||
targetId: user.id,
|
||||
detail: { from: roleChange.from, to: roleChange.to, groups: roleChange.groups, ...(roleChange.blocked ? { reason: "the only administrator" } : {}) },
|
||||
});
|
||||
}
|
||||
|
||||
// Access to the app is itself something worth being able to look back on: who got an account (and the very first
|
||||
// one becomes admin), and every sign-in with where it came from.
|
||||
|
||||
@@ -1,35 +1,72 @@
|
||||
import { eq } from "drizzle-orm";
|
||||
import { and, eq, ne } from "drizzle-orm";
|
||||
import { db } from "../db/client.js";
|
||||
import { users } from "../db/schema.js";
|
||||
import { users, type UserRole } from "../db/schema.js";
|
||||
import { getSettings } from "../services/settingsStore.js";
|
||||
import { roleFromGroups } from "./groups.js";
|
||||
|
||||
export interface RoleChange {
|
||||
from: UserRole;
|
||||
to: UserRole;
|
||||
groups: string[];
|
||||
/** The group rules would have taken the last administrator's rights away, so the role was left alone. */
|
||||
blocked?: boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
* Called on every successful OIDC login. The very first user ever to sign in
|
||||
* becomes admin; everyone after defaults to viewer until an admin promotes
|
||||
* them from the Users page.
|
||||
* them from the Users page — or, when roles follow groups (Settings → Sign-in),
|
||||
* until the groups the identity provider reports decide it.
|
||||
*
|
||||
* `groups` is what the provider said, or null/undefined when it said nothing about groups at all. That is not "in no groups": with no
|
||||
* information the role is left exactly as it is, so a wrong claim name can't quietly demote everyone. And the last administrator is
|
||||
* never demoted by a group rule, whatever the groups say.
|
||||
*/
|
||||
export async function upsertUserFromLogin(params: {
|
||||
sub: string;
|
||||
email?: string;
|
||||
name?: string;
|
||||
}): Promise<{ user: typeof users.$inferSelect; created: boolean }> {
|
||||
groups?: string[] | null;
|
||||
}): Promise<{ user: typeof users.$inferSelect; created: boolean; roleChange?: RoleChange }> {
|
||||
const [existing] = await db.select().from(users).where(eq(users.oidcSub, params.sub)).limit(1);
|
||||
const now = new Date().toISOString();
|
||||
const { signIn } = await getSettings();
|
||||
const mapping = signIn.roleFromGroups && params.groups !== null && params.groups !== undefined;
|
||||
const groupsJson = params.groups ? JSON.stringify(params.groups) : undefined;
|
||||
|
||||
if (existing) {
|
||||
let role = existing.role;
|
||||
let roleChange: RoleChange | undefined;
|
||||
if (mapping) {
|
||||
const wanted = roleFromGroups(signIn, params.groups!, existing.role);
|
||||
if (wanted !== existing.role) {
|
||||
const demotesAdmin = existing.role === "admin" && wanted !== "admin";
|
||||
const otherAdmin = demotesAdmin ? await db.select({ id: users.id }).from(users).where(and(eq(users.role, "admin"), ne(users.id, existing.id))).limit(1) : [];
|
||||
if (demotesAdmin && otherAdmin.length === 0) {
|
||||
roleChange = { from: existing.role, to: wanted, groups: params.groups!, blocked: true };
|
||||
} else {
|
||||
role = wanted;
|
||||
roleChange = { from: existing.role, to: wanted, groups: params.groups! };
|
||||
}
|
||||
}
|
||||
}
|
||||
const [updated] = await db
|
||||
.update(users)
|
||||
.set({
|
||||
email: params.email ?? existing.email,
|
||||
name: params.name ?? existing.name,
|
||||
lastLoginAt: now,
|
||||
role,
|
||||
...(groupsJson !== undefined ? { groups: groupsJson } : {}),
|
||||
})
|
||||
.where(eq(users.id, existing.id))
|
||||
.returning();
|
||||
return { user: updated, created: false };
|
||||
return { user: updated, created: false, roleChange };
|
||||
}
|
||||
|
||||
const anyUser = await db.select({ id: users.id }).from(users).limit(1);
|
||||
const role = anyUser.length === 0 ? ("admin" as const) : ("viewer" as const);
|
||||
// The first person ever is the administrator, so there is always one; after that the groups decide, when they are used, else viewer.
|
||||
const role: UserRole = anyUser.length === 0 ? "admin" : mapping ? roleFromGroups({ ...signIn, otherwise: "viewer" }, params.groups!, "viewer") : "viewer";
|
||||
|
||||
const [created] = await db
|
||||
.insert(users)
|
||||
@@ -39,6 +76,7 @@ export async function upsertUserFromLogin(params: {
|
||||
name: params.name,
|
||||
role,
|
||||
lastLoginAt: now,
|
||||
...(groupsJson !== undefined ? { groups: groupsJson } : {}),
|
||||
})
|
||||
.returning();
|
||||
return { user: created, created: true };
|
||||
|
||||
@@ -20,6 +20,9 @@ export const users = sqliteTable("users", {
|
||||
favorites: text("favorites"),
|
||||
// When this person last pressed "Got it" on the dashboard summary of what changed; the next summary starts from here (ISO).
|
||||
lastLookedAt: text("last_looked_at"),
|
||||
// The groups the identity provider said this person was in at their last sign-in, as a JSON array — shown on the Users page so the group
|
||||
// names can be matched to roles (Settings → Sign-in). Null until they have signed in with groups available.
|
||||
groups: text("groups"),
|
||||
// How this person arranged the dashboard: a JSON object with the cards they hid ("hidden": ["Gitea"]) and the order of the others
|
||||
// ("order": {"DNS": 0, "Secrets": 1}). Follows them to any browser; null is the default layout.
|
||||
dashboardLayout: text("dashboard_layout"),
|
||||
@@ -539,3 +542,85 @@ export const maintenanceSchedules = sqliteTable("maintenance_schedules", {
|
||||
.notNull()
|
||||
.default(sql`(current_timestamp)`),
|
||||
});
|
||||
|
||||
// ─── Runbooks: what to do when something is wrong ───────────────────────────
|
||||
|
||||
// Notes attached to a server, integration or DNS provider — "if this is down, check the UPS first" — shown on its page and next to alerts
|
||||
// about it. A runbook can be for one kind of alert only (`category`, an alert category such as "disk"), or for everything about the thing.
|
||||
export const runbooks = sqliteTable(
|
||||
"runbooks",
|
||||
{
|
||||
id: integer("id").primaryKey({ autoIncrement: true }),
|
||||
targetType: text("target_type").$type<MaintenanceTargetType>().notNull(),
|
||||
targetId: integer("target_id").notNull(), // polymorphic, like maintenance_windows — no FK; one whose target was deleted is simply not shown
|
||||
title: text("title").notNull(),
|
||||
body: text("body").notNull(),
|
||||
category: text("category"), // null = any alert about the target
|
||||
updatedBy: text("updated_by"),
|
||||
createdAt: text("created_at").notNull(), // ISO
|
||||
updatedAt: text("updated_at").notNull(), // ISO
|
||||
},
|
||||
(t) => [index("runbooks_target_idx").on(t.targetType, t.targetId)],
|
||||
);
|
||||
|
||||
// ─── Inventory: the hardware behind it all ──────────────────────────────────
|
||||
|
||||
export const assetKinds = ["server", "storage", "network", "power", "client", "other"] as const;
|
||||
export type AssetKind = (typeof assetKinds)[number];
|
||||
|
||||
// A piece of hardware: what it is, when it was bought, how long it is covered, where it sits and what it draws. Optionally the server it is.
|
||||
export const assets = sqliteTable("assets", {
|
||||
id: integer("id").primaryKey({ autoIncrement: true }),
|
||||
name: text("name").notNull(),
|
||||
kind: text("kind").$type<AssetKind>().notNull().default("other"),
|
||||
manufacturer: text("manufacturer"),
|
||||
model: text("model"),
|
||||
serial: text("serial"),
|
||||
purchaseDate: text("purchase_date"), // YYYY-MM-DD
|
||||
purchasePrice: real("purchase_price"), // in the currency set under Settings → Inventory
|
||||
warrantyUntil: text("warranty_until"), // YYYY-MM-DD
|
||||
location: text("location"), // room, rack, shelf
|
||||
rackUnit: text("rack_unit"), // e.g. "U12" or "U12-13"
|
||||
powerWatts: integer("power_watts"), // typical draw, for the running cost
|
||||
serverId: integer("server_id"), // the server this is, if it is one — a plain reference (no FK); one whose server is gone shows as unlinked
|
||||
notes: text("notes"),
|
||||
createdAt: text("created_at").notNull(), // ISO
|
||||
updatedAt: text("updated_at").notNull(), // ISO
|
||||
});
|
||||
|
||||
// ─── Metrics tokens: read-only access to /metrics for Prometheus ────────────
|
||||
|
||||
// Only a hash of the secret is kept (like an agent token or a share link), so it can't be read back out of the database or a backup.
|
||||
export const metricsTokens = sqliteTable("metrics_tokens", {
|
||||
id: integer("id").primaryKey({ autoIncrement: true }),
|
||||
label: text("label").notNull(), // e.g. "Prometheus"
|
||||
tokenHash: text("token_hash").notNull().unique(),
|
||||
tokenPrefix: text("token_prefix").notNull(),
|
||||
createdBy: text("created_by"),
|
||||
createdAt: text("created_at")
|
||||
.notNull()
|
||||
.default(sql`(current_timestamp)`),
|
||||
lastUsedAt: text("last_used_at"), // ISO
|
||||
});
|
||||
|
||||
// ─── Event webhooks: tell another system when something happens ─────────────
|
||||
|
||||
export const webhookEvents = ["alert.opened", "alert.resolved", "maintenance.started", "maintenance.ended"] as const;
|
||||
export type WebhookEvent = (typeof webhookEvents)[number];
|
||||
|
||||
// A URL that gets a signed JSON message for the events it subscribes to — for Home Assistant, n8n, a status board. Separate from the
|
||||
// chat-style webhook under Settings → Notifications, which carries the same text a person would read.
|
||||
export const eventWebhooks = sqliteTable("event_webhooks", {
|
||||
id: integer("id").primaryKey({ autoIncrement: true }),
|
||||
label: text("label").notNull(),
|
||||
url: text("url").notNull(),
|
||||
secret: text("secret"), // encrypted at rest; signs each message (HMAC-SHA256 in X-HLM-Signature)
|
||||
events: text("events").notNull(), // JSON: WebhookEvent[]
|
||||
enabled: integer("enabled", { mode: "boolean" }).notNull().default(true),
|
||||
createdBy: text("created_by"),
|
||||
createdAt: text("created_at")
|
||||
.notNull()
|
||||
.default(sql`(current_timestamp)`),
|
||||
lastAttemptAt: text("last_attempt_at"), // ISO
|
||||
lastResult: text("last_result"), // "200", or what went wrong
|
||||
});
|
||||
@@ -11,6 +11,7 @@ import { initProxmoxBackupScheduler } from "./services/proxmoxBackupScheduler.js
|
||||
import { initPbsVerificationScheduler } from "./services/pbsVerificationScheduler.js";
|
||||
import { initQuietHoursScheduler } from "./services/quietHoursScheduler.js";
|
||||
import { initWeeklyDigestScheduler } from "./services/weeklyDigestScheduler.js";
|
||||
import { initMaintenanceEvents } from "./services/maintenanceEvents.js";
|
||||
import { initHealthScheduler } from "./services/healthScheduler.js";
|
||||
import { initAlertTicketScheduler } from "./services/alertTicketScheduler.js";
|
||||
import { initKumaMaintenanceScheduler } from "./services/kumaMaintenanceScheduler.js";
|
||||
@@ -42,6 +43,7 @@ await initProxmoxBackupScheduler();
|
||||
await initPbsVerificationScheduler();
|
||||
await initQuietHoursScheduler();
|
||||
await initWeeklyDigestScheduler();
|
||||
initMaintenanceEvents();
|
||||
await initHealthScheduler();
|
||||
await initAlertTicketScheduler();
|
||||
await initKumaMaintenanceScheduler();
|
||||
|
||||
@@ -43,6 +43,16 @@ export interface PbsFailedSnapshot {
|
||||
backupTime: number;
|
||||
}
|
||||
|
||||
/** The newest snapshot of one guest (or host) in a datastore — what "when was this last backed up" is answered from. */
|
||||
export interface PbsBackupGroup {
|
||||
backupType: string; // "vm" | "ct" | "host"
|
||||
backupId: string; // the guest's vmid, for vm and ct
|
||||
/** Unix seconds. */
|
||||
backupTime: number;
|
||||
/** What the last verify job found for this snapshot; null when it has not been checked. */
|
||||
verification: "ok" | "failed" | null;
|
||||
}
|
||||
|
||||
export interface PbsDatastore {
|
||||
name: string;
|
||||
comment: string | null;
|
||||
@@ -60,6 +70,8 @@ export interface PbsDatastore {
|
||||
latestSnapshotAt: number | null;
|
||||
/** Up to 20 of the most recent verification failures, newest first. */
|
||||
recentFailures: PbsFailedSnapshot[];
|
||||
/** The newest snapshot of every guest in the datastore. */
|
||||
groups: PbsBackupGroup[];
|
||||
}
|
||||
|
||||
export interface PbsNodeStatus {
|
||||
@@ -158,12 +170,20 @@ export function createPbsAdapter(config: PbsConfig): PbsAdapter {
|
||||
let unverifiedCount = 0;
|
||||
let latestSnapshotAt: number | null = null;
|
||||
const failures: PbsFailedSnapshot[] = [];
|
||||
const newest = new Map<string, PbsBackupGroup>();
|
||||
|
||||
for (const s of snapshots) {
|
||||
const backupTime = num(s?.["backup-time"]);
|
||||
if (backupTime !== null && (latestSnapshotAt === null || backupTime > latestSnapshotAt)) latestSnapshotAt = backupTime;
|
||||
|
||||
const state = str(s?.verification?.state)?.toLowerCase() ?? null;
|
||||
const groupType = str(s?.["backup-type"]);
|
||||
const groupId = str(s?.["backup-id"]);
|
||||
if (groupType && groupId && backupTime !== null) {
|
||||
const key = `${groupType}/${groupId}`;
|
||||
const seen = newest.get(key);
|
||||
if (!seen || backupTime > seen.backupTime) newest.set(key, { backupType: groupType, backupId: groupId, backupTime, verification: state === "ok" ? "ok" : state === "failed" ? "failed" : null });
|
||||
}
|
||||
if (state === "failed") {
|
||||
failedCount++;
|
||||
const backupType = str(s?.["backup-type"]);
|
||||
@@ -175,7 +195,7 @@ export function createPbsAdapter(config: PbsConfig): PbsAdapter {
|
||||
}
|
||||
|
||||
failures.sort((a, b) => b.backupTime - a.backupTime);
|
||||
return { snapshotCount: snapshots.length, failedCount, unverifiedCount, latestSnapshotAt, recentFailures: failures.slice(0, 20) };
|
||||
return { snapshotCount: snapshots.length, failedCount, unverifiedCount, latestSnapshotAt, recentFailures: failures.slice(0, 20), groups: [...newest.values()] };
|
||||
}
|
||||
|
||||
async function listDatastores(): Promise<PbsDatastore[]> {
|
||||
@@ -206,6 +226,7 @@ export function createPbsAdapter(config: PbsConfig): PbsAdapter {
|
||||
unverifiedCount: 0,
|
||||
latestSnapshotAt: null,
|
||||
recentFailures: [],
|
||||
groups: [],
|
||||
};
|
||||
}
|
||||
}),
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
import { Router } from "express";
|
||||
import { requireAuth } from "../auth/middleware.js";
|
||||
import { DEFAULT_STALE_HOURS, getBackupCoverage } from "../services/backupCoverage.js";
|
||||
import { asyncHandler } from "../utils/asyncHandler.js";
|
||||
|
||||
export const backupsRouter = Router();
|
||||
|
||||
// Everyone signed in can see what is and isn't backed up, like the Proxmox and Backup Server pages it is drawn from.
|
||||
backupsRouter.use(requireAuth);
|
||||
|
||||
// `staleDays`: how old the newest backup may be before a guest counts as stale (default 2). `refresh=1` asks Proxmox again now.
|
||||
backupsRouter.get("/coverage", asyncHandler(async (req, res) => {
|
||||
const days = Number(req.query.staleDays);
|
||||
const staleHours = Number.isFinite(days) && days >= 1 && days <= 60 ? Math.round(days * 24) : DEFAULT_STALE_HOURS;
|
||||
res.json(await getBackupCoverage({ staleHours, refresh: req.query.refresh === "1" }));
|
||||
}));
|
||||
@@ -0,0 +1,97 @@
|
||||
import { Router } from "express";
|
||||
import { eq } from "drizzle-orm";
|
||||
import { z } from "zod";
|
||||
import { db } from "../db/client.js";
|
||||
import { eventWebhooks, webhookEvents } from "../db/schema.js";
|
||||
import { requireAuth, requireRole } from "../auth/middleware.js";
|
||||
import { recordAudit } from "../services/audit.js";
|
||||
import { deliver, parseEvents, sealSecret, type EventWebhookRow } from "../services/eventWebhooks.js";
|
||||
import { asyncHandler } from "../utils/asyncHandler.js";
|
||||
|
||||
export const eventWebhooksRouter = Router();
|
||||
|
||||
// A URL that is sent a message whenever something happens: an administrator's to set up.
|
||||
eventWebhooksRouter.use(requireAuth, requireRole("admin"));
|
||||
|
||||
const MAX_WEBHOOKS = 20;
|
||||
|
||||
const url = z
|
||||
.string()
|
||||
.trim()
|
||||
.max(500)
|
||||
.refine((v) => {
|
||||
try {
|
||||
const u = new URL(v);
|
||||
return (u.protocol === "http:" || u.protocol === "https:") && !u.username && !u.password;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}, "Use an http:// or https:// address, without a user name or password in it");
|
||||
|
||||
const createSchema = z.object({
|
||||
label: z.string().trim().min(1).max(60),
|
||||
url,
|
||||
secret: z.string().max(200).optional(),
|
||||
events: z.array(z.enum(webhookEvents)).min(1).max(webhookEvents.length),
|
||||
});
|
||||
const patchSchema = z.object({ enabled: z.boolean() });
|
||||
|
||||
/** A webhook as the list shows it: never the secret, only whether there is one. */
|
||||
function present(row: EventWebhookRow) {
|
||||
return { id: row.id, label: row.label, url: row.url, hasSecret: !!row.secret, events: parseEvents(row.events), enabled: row.enabled, createdBy: row.createdBy, createdAt: row.createdAt, lastAttemptAt: row.lastAttemptAt, lastResult: row.lastResult };
|
||||
}
|
||||
|
||||
const idOf = (v: string) => (/^\d+$/.test(v) ? Number(v) : NaN);
|
||||
|
||||
eventWebhooksRouter.get("/", asyncHandler(async (_req, res) => {
|
||||
const rows = await db.select().from(eventWebhooks).orderBy(eventWebhooks.label);
|
||||
res.json({ webhooks: rows.map(present), events: webhookEvents });
|
||||
}));
|
||||
|
||||
eventWebhooksRouter.post("/", asyncHandler(async (req, res) => {
|
||||
const parsed = createSchema.safeParse(req.body);
|
||||
if (!parsed.success) {
|
||||
return res.status(400).json({ error: "invalid_body", message: "Give it a name, an http(s) address and at least one event.", details: parsed.error.flatten() });
|
||||
}
|
||||
if ((await db.select({ id: eventWebhooks.id }).from(eventWebhooks)).length >= MAX_WEBHOOKS) {
|
||||
return res.status(400).json({ error: "too_many", message: `There are already ${MAX_WEBHOOKS} webhooks. Remove one you don't use first.` });
|
||||
}
|
||||
const actor = req.currentUser!;
|
||||
const [row] = await db
|
||||
.insert(eventWebhooks)
|
||||
.values({ label: parsed.data.label, url: parsed.data.url, secret: parsed.data.secret ? sealSecret(parsed.data.secret) : null, events: JSON.stringify([...new Set(parsed.data.events)]), createdBy: actor.name ?? actor.email ?? actor.oidcSub })
|
||||
.returning();
|
||||
// The address can carry a token of its own, so the audit log gets the name and the events, not the address.
|
||||
await recordAudit({ actor, category: "webhook", action: "create", targetType: "event_webhook", targetId: row.id, detail: { label: row.label, events: parseEvents(row.events), signed: !!row.secret } });
|
||||
res.status(201).json({ webhook: present(row) });
|
||||
}));
|
||||
|
||||
eventWebhooksRouter.patch("/:id", asyncHandler(async (req, res) => {
|
||||
const id = idOf(req.params.id);
|
||||
const parsed = patchSchema.safeParse(req.body);
|
||||
if (!Number.isInteger(id) || !parsed.success) return res.status(400).json({ error: "invalid_body" });
|
||||
const [row] = await db.update(eventWebhooks).set({ enabled: parsed.data.enabled }).where(eq(eventWebhooks.id, id)).returning();
|
||||
if (!row) return res.status(404).json({ error: "not_found" });
|
||||
await recordAudit({ actor: req.currentUser!, category: "webhook", action: parsed.data.enabled ? "enable" : "disable", targetType: "event_webhook", targetId: id, detail: { label: row.label } });
|
||||
res.json({ webhook: present(row) });
|
||||
}));
|
||||
|
||||
eventWebhooksRouter.delete("/:id", asyncHandler(async (req, res) => {
|
||||
const id = idOf(req.params.id);
|
||||
if (!Number.isInteger(id)) return res.status(400).json({ error: "invalid_id" });
|
||||
const [row] = await db.select().from(eventWebhooks).where(eq(eventWebhooks.id, id)).limit(1);
|
||||
if (!row) return res.status(404).json({ error: "not_found" });
|
||||
await db.delete(eventWebhooks).where(eq(eventWebhooks.id, id));
|
||||
await recordAudit({ actor: req.currentUser!, category: "webhook", action: "delete", targetType: "event_webhook", targetId: id, detail: { label: row.label } });
|
||||
res.status(204).end();
|
||||
}));
|
||||
|
||||
// Sends one test message, signed like the real ones, and says how it went — whether or not the webhook is switched on or wants the event.
|
||||
eventWebhooksRouter.post("/:id/test", asyncHandler(async (req, res) => {
|
||||
const id = idOf(req.params.id);
|
||||
if (!Number.isInteger(id)) return res.status(400).json({ error: "invalid_id" });
|
||||
const [row] = await db.select().from(eventWebhooks).where(eq(eventWebhooks.id, id)).limit(1);
|
||||
if (!row) return res.status(404).json({ error: "not_found" });
|
||||
const outcome = await deliver(row, "test", { message: "This is a test from Homelab Manager." });
|
||||
res.json(outcome);
|
||||
}));
|
||||
@@ -0,0 +1,120 @@
|
||||
import { Router } from "express";
|
||||
import { eq } from "drizzle-orm";
|
||||
import { z } from "zod";
|
||||
import { db } from "../db/client.js";
|
||||
import { assetKinds, assets, servers } from "../db/schema.js";
|
||||
import { requireAuth, requireRole } from "../auth/middleware.js";
|
||||
import { recordAudit } from "../services/audit.js";
|
||||
import { isRealDate } from "../services/inventory.js";
|
||||
import { getSettings, updateSettings } from "../services/settingsStore.js";
|
||||
import { asyncHandler } from "../utils/asyncHandler.js";
|
||||
|
||||
export const inventoryRouter = Router();
|
||||
|
||||
inventoryRouter.use(requireAuth);
|
||||
|
||||
const text = (max: number) => z.string().trim().max(max).nullable().optional();
|
||||
const date = z.string().refine(isRealDate, "Use a date like 2026-10-17").nullable().optional();
|
||||
|
||||
const assetSchema = z.object({
|
||||
name: z.string().trim().min(1).max(120),
|
||||
kind: z.enum(assetKinds),
|
||||
manufacturer: text(100),
|
||||
model: text(100),
|
||||
serial: text(100),
|
||||
purchaseDate: date,
|
||||
purchasePrice: z.number().min(0).max(1_000_000_000).nullable().optional(),
|
||||
warrantyUntil: date,
|
||||
location: text(120),
|
||||
rackUnit: text(30),
|
||||
powerWatts: z.number().int().min(0).max(100_000).nullable().optional(),
|
||||
serverId: z.number().int().positive().nullable().optional(),
|
||||
notes: text(2000),
|
||||
});
|
||||
|
||||
/** A blank field is no value, not an empty string. */
|
||||
const clean = (v: string | null | undefined) => (v === undefined || v === null || v.trim() === "" ? null : v.trim());
|
||||
|
||||
function values(d: z.infer<typeof assetSchema>) {
|
||||
return {
|
||||
name: d.name,
|
||||
kind: d.kind,
|
||||
manufacturer: clean(d.manufacturer),
|
||||
model: clean(d.model),
|
||||
serial: clean(d.serial),
|
||||
purchaseDate: clean(d.purchaseDate),
|
||||
purchasePrice: d.purchasePrice ?? null,
|
||||
warrantyUntil: clean(d.warrantyUntil),
|
||||
location: clean(d.location),
|
||||
rackUnit: clean(d.rackUnit),
|
||||
powerWatts: d.powerWatts ?? null,
|
||||
serverId: d.serverId ?? null,
|
||||
notes: clean(d.notes),
|
||||
};
|
||||
}
|
||||
|
||||
const idOf = (v: string) => (/^\d+$/.test(v) ? Number(v) : NaN);
|
||||
|
||||
// Everything signed-in people may see: the hardware, and the price of power the running cost is worked out with.
|
||||
inventoryRouter.get("/", asyncHandler(async (_req, res) => {
|
||||
const [rows, serverRows, settings] = await Promise.all([db.select().from(assets).orderBy(assets.name), db.select({ id: servers.id, name: servers.name }).from(servers), getSettings()]);
|
||||
const serverName = new Map(serverRows.map((s) => [s.id, s.name]));
|
||||
res.json({
|
||||
assets: rows.map((a) => ({ ...a, server: a.serverId !== null && serverName.has(a.serverId) ? { id: a.serverId, name: serverName.get(a.serverId)! } : null })),
|
||||
config: settings.inventory,
|
||||
servers: serverRows.sort((a, b) => a.name.localeCompare(b.name)),
|
||||
});
|
||||
}));
|
||||
|
||||
const configSchema = z.object({
|
||||
currency: z.string().trim().max(8),
|
||||
pricePerKwh: z.number().min(0).max(1000).nullable(),
|
||||
});
|
||||
|
||||
// The price of power, for the running cost. An administrator's to set.
|
||||
inventoryRouter.put("/config", requireRole("admin"), asyncHandler(async (req, res) => {
|
||||
const parsed = configSchema.safeParse(req.body);
|
||||
if (!parsed.success) return res.status(400).json({ error: "invalid_body", message: "A price per kWh (or none) and a short currency name.", details: parsed.error.flatten() });
|
||||
const before = (await getSettings()).inventory;
|
||||
const updated = await updateSettings({ inventory: parsed.data });
|
||||
await recordAudit({ actor: req.currentUser!, category: "inventory", action: "update_config", targetType: "settings", detail: { from: before, to: parsed.data } });
|
||||
res.json({ config: updated.inventory });
|
||||
}));
|
||||
|
||||
inventoryRouter.post("/", requireRole("operator"), asyncHandler(async (req, res) => {
|
||||
const parsed = assetSchema.safeParse(req.body);
|
||||
if (!parsed.success) return res.status(400).json({ error: "invalid_body", message: "Give it a name and a kind; dates look like 2026-10-17.", details: parsed.error.flatten() });
|
||||
const v = values(parsed.data);
|
||||
if (v.serverId !== null && (await db.select({ id: servers.id }).from(servers).where(eq(servers.id, v.serverId))).length === 0) {
|
||||
return res.status(400).json({ error: "unknown_server", message: "That server doesn't exist." });
|
||||
}
|
||||
const now = new Date().toISOString();
|
||||
const [row] = await db.insert(assets).values({ ...v, createdAt: now, updatedAt: now }).returning();
|
||||
await recordAudit({ actor: req.currentUser!, category: "inventory", action: "create", targetType: "asset", targetId: row.id, detail: { name: row.name, kind: row.kind } });
|
||||
res.status(201).json({ asset: row });
|
||||
}));
|
||||
|
||||
inventoryRouter.put("/:id", requireRole("operator"), asyncHandler(async (req, res) => {
|
||||
const id = idOf(req.params.id);
|
||||
const parsed = assetSchema.safeParse(req.body);
|
||||
if (!Number.isInteger(id) || !parsed.success) return res.status(400).json({ error: "invalid_body", details: parsed.success ? undefined : parsed.error.flatten() });
|
||||
const [before] = await db.select().from(assets).where(eq(assets.id, id)).limit(1);
|
||||
if (!before) return res.status(404).json({ error: "not_found" });
|
||||
const v = values(parsed.data);
|
||||
if (v.serverId !== null && (await db.select({ id: servers.id }).from(servers).where(eq(servers.id, v.serverId))).length === 0) {
|
||||
return res.status(400).json({ error: "unknown_server", message: "That server doesn't exist." });
|
||||
}
|
||||
const [row] = await db.update(assets).set({ ...v, updatedAt: new Date().toISOString() }).where(eq(assets.id, id)).returning();
|
||||
await recordAudit({ actor: req.currentUser!, category: "inventory", action: "update", targetType: "asset", targetId: id, detail: { name: row.name } });
|
||||
res.json({ asset: row });
|
||||
}));
|
||||
|
||||
inventoryRouter.delete("/:id", requireRole("operator"), asyncHandler(async (req, res) => {
|
||||
const id = idOf(req.params.id);
|
||||
if (!Number.isInteger(id)) return res.status(400).json({ error: "invalid_id" });
|
||||
const [before] = await db.select().from(assets).where(eq(assets.id, id)).limit(1);
|
||||
if (!before) return res.status(404).json({ error: "not_found" });
|
||||
await db.delete(assets).where(eq(assets.id, id));
|
||||
await recordAudit({ actor: req.currentUser!, category: "inventory", action: "delete", targetType: "asset", targetId: id, detail: { name: before.name } });
|
||||
res.status(204).end();
|
||||
}));
|
||||
@@ -0,0 +1,63 @@
|
||||
import { Router } from "express";
|
||||
import { eq } from "drizzle-orm";
|
||||
import { z } from "zod";
|
||||
import { db } from "../db/client.js";
|
||||
import { metricsTokens } from "../db/schema.js";
|
||||
import { requireAuth, requireRole } from "../auth/middleware.js";
|
||||
import { recordAudit } from "../services/audit.js";
|
||||
import { buildMetrics } from "../services/metrics.js";
|
||||
import { createMetricsToken, publicMetricsToken, resolveMetricsToken } from "../services/metricsTokens.js";
|
||||
import { asyncHandler } from "../utils/asyncHandler.js";
|
||||
import { rateLimit } from "../utils/rateLimit.js";
|
||||
|
||||
/** Prometheus's scrape: no sign-in, a bearer token instead. The token goes in a header only — never the address, which ends up in logs. */
|
||||
export const metricsRouter = Router();
|
||||
const limiter = rateLimit({ windowMs: 60_000, max: 120, message: "Too many requests." });
|
||||
|
||||
metricsRouter.get("/", limiter, asyncHandler(async (req, res) => {
|
||||
const match = /^Bearer\s+(\S+)$/i.exec(req.get("authorization") ?? "");
|
||||
const token = match ? await resolveMetricsToken(match[1]) : null;
|
||||
if (!token) {
|
||||
// The same answer for no token, a wrong one and a removed one.
|
||||
res.setHeader("WWW-Authenticate", 'Bearer realm="metrics"');
|
||||
return res.status(401).type("text/plain").send("A metrics token is needed: send it as 'Authorization: Bearer <token>'.\n");
|
||||
}
|
||||
res.setHeader("Cache-Control", "no-store");
|
||||
// Written out as Prometheus documents it (Express would put the parameters in another order).
|
||||
res.setHeader("Content-Type", "text/plain; version=0.0.4; charset=utf-8");
|
||||
res.end(await buildMetrics());
|
||||
}));
|
||||
|
||||
/** Managing the tokens: an administrator's. */
|
||||
export const metricsTokensRouter = Router();
|
||||
metricsTokensRouter.use(requireAuth, requireRole("admin"));
|
||||
|
||||
const MAX_TOKENS = 10;
|
||||
|
||||
metricsTokensRouter.get("/", asyncHandler(async (_req, res) => {
|
||||
const rows = await db.select().from(metricsTokens).orderBy(metricsTokens.createdAt);
|
||||
res.json({ tokens: rows.map(publicMetricsToken) });
|
||||
}));
|
||||
|
||||
// The secret is in this answer and nowhere else, ever.
|
||||
metricsTokensRouter.post("/", asyncHandler(async (req, res) => {
|
||||
const parsed = z.object({ label: z.string().trim().min(1).max(60) }).safeParse(req.body);
|
||||
if (!parsed.success) return res.status(400).json({ error: "invalid_body", message: "Give the token a name, like Prometheus." });
|
||||
if ((await db.select({ id: metricsTokens.id }).from(metricsTokens)).length >= MAX_TOKENS) {
|
||||
return res.status(400).json({ error: "too_many", message: `There are already ${MAX_TOKENS} tokens. Remove one you don't use first.` });
|
||||
}
|
||||
const actor = req.currentUser!;
|
||||
const { row, token } = await createMetricsToken({ label: parsed.data.label, createdBy: actor.name ?? actor.email ?? actor.oidcSub });
|
||||
await recordAudit({ actor, category: "metrics_token", action: "create", targetType: "metrics_token", targetId: row.id, detail: { label: row.label } });
|
||||
res.status(201).json({ token: publicMetricsToken(row), secret: token });
|
||||
}));
|
||||
|
||||
metricsTokensRouter.delete("/:id", asyncHandler(async (req, res) => {
|
||||
const id = /^\d+$/.test(req.params.id) ? Number(req.params.id) : NaN;
|
||||
if (!Number.isInteger(id)) return res.status(400).json({ error: "invalid_id" });
|
||||
const [row] = await db.select().from(metricsTokens).where(eq(metricsTokens.id, id)).limit(1);
|
||||
if (!row) return res.status(404).json({ error: "not_found" });
|
||||
await db.delete(metricsTokens).where(eq(metricsTokens.id, id));
|
||||
await recordAudit({ actor: req.currentUser!, category: "metrics_token", action: "delete", targetType: "metrics_token", targetId: id, detail: { label: row.label } });
|
||||
res.status(204).end();
|
||||
}));
|
||||
@@ -0,0 +1,98 @@
|
||||
import { Router } from "express";
|
||||
import { and, eq } from "drizzle-orm";
|
||||
import { z } from "zod";
|
||||
import { db } from "../db/client.js";
|
||||
import { dnsProviders, integrations, maintenanceTargetTypes, runbooks, servers } from "../db/schema.js";
|
||||
import { requireAuth, requireRole } from "../auth/middleware.js";
|
||||
import { recordAudit } from "../services/audit.js";
|
||||
import { describeTarget } from "../services/maintenance.js";
|
||||
import { asyncHandler } from "../utils/asyncHandler.js";
|
||||
|
||||
export const runbooksRouter = Router();
|
||||
|
||||
// Anyone signed in can read what to do when something is wrong — that is the point of having it written down. Changing it is for operators.
|
||||
runbooksRouter.use(requireAuth);
|
||||
|
||||
/** More than anyone needs for one thing; each is another note to keep true. */
|
||||
const MAX_PER_TARGET = 20;
|
||||
export const ALERT_CATEGORIES = ["offline", "disk", "backup", "updates", "expiry", "automation", "integration", "monitoring", "tickets"] as const;
|
||||
|
||||
const bodySchema = z.object({
|
||||
title: z.string().trim().min(1).max(120),
|
||||
body: z.string().max(20_000),
|
||||
/** An alert category this is for, or null for any alert about the target. */
|
||||
category: z.enum(ALERT_CATEGORIES).nullable().optional(),
|
||||
});
|
||||
const createSchema = bodySchema.extend({ targetType: z.enum(maintenanceTargetTypes), targetId: z.number().int().positive() });
|
||||
|
||||
const idOf = (v: string) => (/^\d+$/.test(v) ? Number(v) : NaN);
|
||||
|
||||
// What there is for one thing: the full text.
|
||||
runbooksRouter.get("/", asyncHandler(async (req, res) => {
|
||||
const targetType = z.enum(maintenanceTargetTypes).safeParse(req.query.targetType);
|
||||
const targetId = idOf(String(req.query.targetId ?? ""));
|
||||
if (!targetType.success || !Number.isInteger(targetId)) return res.status(400).json({ error: "invalid_target" });
|
||||
const rows = await db.select().from(runbooks).where(and(eq(runbooks.targetType, targetType.data), eq(runbooks.targetId, targetId))).orderBy(runbooks.title);
|
||||
res.json({ runbooks: rows });
|
||||
}));
|
||||
|
||||
// What there is, everywhere, by title only — so a page can show a "runbook" button exactly where one exists, without fetching the texts.
|
||||
// Notes for something that has since been removed are left out.
|
||||
runbooksRouter.get("/summary", asyncHandler(async (_req, res) => {
|
||||
const [rows, serverIds, integrationIds, providerIds] = await Promise.all([
|
||||
db.select({ id: runbooks.id, targetType: runbooks.targetType, targetId: runbooks.targetId, category: runbooks.category, title: runbooks.title }).from(runbooks),
|
||||
db.select({ id: servers.id }).from(servers),
|
||||
db.select({ id: integrations.id }).from(integrations),
|
||||
db.select({ id: dnsProviders.id }).from(dnsProviders),
|
||||
]);
|
||||
const exists = {
|
||||
server: new Set(serverIds.map((r) => r.id)),
|
||||
integration: new Set(integrationIds.map((r) => r.id)),
|
||||
dns_provider: new Set(providerIds.map((r) => r.id)),
|
||||
};
|
||||
res.json({ runbooks: rows.filter((r) => exists[r.targetType].has(r.targetId)) });
|
||||
}));
|
||||
|
||||
runbooksRouter.post("/", requireRole("operator"), asyncHandler(async (req, res) => {
|
||||
const parsed = createSchema.safeParse(req.body);
|
||||
if (!parsed.success) return res.status(400).json({ error: "invalid_body", message: "Give it a title and some text, and say what it is for.", details: parsed.error.flatten() });
|
||||
const d = parsed.data;
|
||||
if (!(await describeTarget(d.targetType, d.targetId))) return res.status(400).json({ error: "unknown_target", message: "That server, integration or DNS provider doesn't exist." });
|
||||
const existing = await db.select({ id: runbooks.id }).from(runbooks).where(and(eq(runbooks.targetType, d.targetType), eq(runbooks.targetId, d.targetId)));
|
||||
if (existing.length >= MAX_PER_TARGET) return res.status(400).json({ error: "too_many", message: `There are already ${MAX_PER_TARGET} notes for this. Tidy those first.` });
|
||||
|
||||
const actor = req.currentUser!;
|
||||
const now = new Date().toISOString();
|
||||
const [row] = await db
|
||||
.insert(runbooks)
|
||||
.values({ targetType: d.targetType, targetId: d.targetId, title: d.title, body: d.body, category: d.category ?? null, updatedBy: actor.name ?? actor.email ?? actor.oidcSub, createdAt: now, updatedAt: now })
|
||||
.returning();
|
||||
await recordAudit({ actor, category: "runbook", action: "create", targetType: d.targetType, targetId: d.targetId, detail: { title: d.title, category: d.category ?? null } });
|
||||
res.status(201).json({ runbook: row });
|
||||
}));
|
||||
|
||||
runbooksRouter.put("/:id", requireRole("operator"), asyncHandler(async (req, res) => {
|
||||
const id = idOf(req.params.id);
|
||||
const parsed = bodySchema.safeParse(req.body);
|
||||
if (!Number.isInteger(id) || !parsed.success) return res.status(400).json({ error: "invalid_body", details: parsed.success ? undefined : parsed.error.flatten() });
|
||||
const [before] = await db.select().from(runbooks).where(eq(runbooks.id, id)).limit(1);
|
||||
if (!before) return res.status(404).json({ error: "not_found" });
|
||||
const actor = req.currentUser!;
|
||||
const [row] = await db
|
||||
.update(runbooks)
|
||||
.set({ title: parsed.data.title, body: parsed.data.body, category: parsed.data.category ?? null, updatedBy: actor.name ?? actor.email ?? actor.oidcSub, updatedAt: new Date().toISOString() })
|
||||
.where(eq(runbooks.id, id))
|
||||
.returning();
|
||||
await recordAudit({ actor, category: "runbook", action: "update", targetType: before.targetType, targetId: before.targetId, detail: { title: parsed.data.title } });
|
||||
res.json({ runbook: row });
|
||||
}));
|
||||
|
||||
runbooksRouter.delete("/:id", requireRole("operator"), asyncHandler(async (req, res) => {
|
||||
const id = idOf(req.params.id);
|
||||
if (!Number.isInteger(id)) return res.status(400).json({ error: "invalid_id" });
|
||||
const [before] = await db.select().from(runbooks).where(eq(runbooks.id, id)).limit(1);
|
||||
if (!before) return res.status(404).json({ error: "not_found" });
|
||||
await db.delete(runbooks).where(eq(runbooks.id, id));
|
||||
await recordAudit({ actor: req.currentUser!, category: "runbook", action: "delete", targetType: before.targetType, targetId: before.targetId, detail: { title: before.title } });
|
||||
res.status(204).end();
|
||||
}));
|
||||
@@ -102,6 +102,16 @@ const updateSchema = z.object({
|
||||
.object({ enabled: z.boolean(), start: z.string().regex(/^\d{2}:\d{2}$/), end: z.string().regex(/^\d{2}:\d{2}$/) })
|
||||
.partial()
|
||||
.optional(),
|
||||
signIn: z
|
||||
.object({
|
||||
roleFromGroups: z.boolean(),
|
||||
groupsClaim: z.string().regex(/^[A-Za-z0-9_:.\-/]{1,60}$/, "Use the claim's name, like groups"),
|
||||
adminGroups: z.array(z.string().trim().min(1).max(100)).max(20),
|
||||
operatorGroups: z.array(z.string().trim().min(1).max(100)).max(20),
|
||||
otherwise: z.enum(["viewer", "keep"]),
|
||||
})
|
||||
.partial()
|
||||
.optional(),
|
||||
digest: z
|
||||
.object({ enabled: z.boolean(), day: z.number().int().min(0).max(6), time: z.string().regex(/^([01]\d|2[0-3]):[0-5]\d$/) })
|
||||
.partial()
|
||||
|
||||
@@ -6,14 +6,32 @@ import { users, userRoles } from "../db/schema.js";
|
||||
import { requireAuth, requireRole } from "../auth/middleware.js";
|
||||
import { recordAudit } from "../services/audit.js";
|
||||
import { asyncHandler } from "../utils/asyncHandler.js";
|
||||
import { getSettings } from "../services/settingsStore.js";
|
||||
import { roleFromGroups } from "../auth/groups.js";
|
||||
|
||||
export const usersRouter = Router();
|
||||
|
||||
usersRouter.use(requireAuth, requireRole("admin"));
|
||||
|
||||
/** What is stored for a person's groups, as a list. Anything that isn't one is none rather than an error. */
|
||||
export function parseGroups(stored: string | null): string[] | null {
|
||||
if (!stored) return null;
|
||||
try {
|
||||
const v = JSON.parse(stored);
|
||||
return Array.isArray(v) ? v.filter((g): g is string => typeof g === "string") : null;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
usersRouter.get("/", asyncHandler(async (_req, res) => {
|
||||
const rows = await db.select().from(users).orderBy(users.createdAt);
|
||||
res.json({ users: rows });
|
||||
const { signIn } = await getSettings();
|
||||
res.json({
|
||||
users: rows.map((u) => ({ id: u.id, oidcSub: u.oidcSub, email: u.email, name: u.name, role: u.role, createdAt: u.createdAt, lastLoginAt: u.lastLoginAt, groups: parseGroups(u.groups) })),
|
||||
// When roles follow groups, a role set by hand would be undone at the next sign-in, so the page says so instead of offering it.
|
||||
rolesFollowGroups: signIn.roleFromGroups,
|
||||
});
|
||||
}));
|
||||
|
||||
const updateRoleSchema = z.object({
|
||||
@@ -21,6 +39,10 @@ const updateRoleSchema = z.object({
|
||||
});
|
||||
|
||||
usersRouter.patch("/:id/role", asyncHandler(async (req, res) => {
|
||||
const { signIn } = await getSettings();
|
||||
if (signIn.roleFromGroups) {
|
||||
return res.status(409).json({ error: "managed_by_groups", message: "Roles are set from sign-in groups (Settings → Sign-in), so a role set here would be undone at the next sign-in. Change the person's group in the identity provider, or switch the group rules off." });
|
||||
}
|
||||
const id = Number(req.params.id);
|
||||
const parsed = updateRoleSchema.safeParse(req.body);
|
||||
if (!parsed.success) {
|
||||
@@ -60,3 +82,26 @@ usersRouter.patch("/:id/role", asyncHandler(async (req, res) => {
|
||||
|
||||
res.json({ user: updated });
|
||||
}));
|
||||
|
||||
const previewSchema = z.object({
|
||||
groupsClaim: z.string().optional(),
|
||||
adminGroups: z.array(z.string().trim().min(1).max(100)).max(20).default([]),
|
||||
operatorGroups: z.array(z.string().trim().min(1).max(100)).max(20).default([]),
|
||||
otherwise: z.enum(["viewer", "keep"]).default("viewer"),
|
||||
});
|
||||
|
||||
// What each person would be, if these group rules were switched on now and they signed in again with the groups they had last time.
|
||||
// Nothing is saved or changed: it is for checking the names before relying on them.
|
||||
usersRouter.post("/preview-group-roles", asyncHandler(async (req, res) => {
|
||||
const parsed = previewSchema.safeParse(req.body);
|
||||
if (!parsed.success) return res.status(400).json({ error: "invalid_body", details: parsed.error.flatten() });
|
||||
const rows = await db.select().from(users).orderBy(users.name);
|
||||
const rules = { roleFromGroups: true, groupsClaim: parsed.data.groupsClaim ?? "groups", adminGroups: parsed.data.adminGroups, operatorGroups: parsed.data.operatorGroups, otherwise: parsed.data.otherwise };
|
||||
const people = rows.map((u) => {
|
||||
const groups = parseGroups(u.groups);
|
||||
return { id: u.id, name: u.name ?? u.email ?? u.oidcSub, role: u.role, groups, wouldBe: groups === null ? null : roleFromGroups(rules, groups, u.role) };
|
||||
});
|
||||
// Said plainly, because it is the one way to lock yourself out: nobody left with admin.
|
||||
const adminsAfter = people.filter((p) => (p.wouldBe ?? p.role) === "admin").length;
|
||||
res.json({ people, adminsAfter });
|
||||
}));
|
||||
@@ -0,0 +1,219 @@
|
||||
/**
|
||||
* Backup coverage: for every Proxmox guest, is it in a backup job, when was it last backed up, and did that go well — across Proxmox's
|
||||
* own backup tasks and Proxmox Backup Server. It answers "what isn't backed up?" in one place, instead of an alert per gap.
|
||||
*
|
||||
* Where the answer comes from, and what it can't say:
|
||||
* - "In a job" is worked out from the backup jobs (the same rule the backup alerts use).
|
||||
* - "Last backup" is the newest snapshot of that guest on a Backup Server, or the last successful Proxmox backup task that names that
|
||||
* guest. A task from an "all guests" job doesn't name its guests (Proxmox keeps those in the task's log), so for guests backed up to
|
||||
* something other than a Backup Server, a job that covers them with no way of seeing when it last ran reads as "unknown" — said so,
|
||||
* never guessed at.
|
||||
*/
|
||||
import { and, eq } from "drizzle-orm";
|
||||
import { db } from "../db/client.js";
|
||||
import { integrations, servers } from "../db/schema.js";
|
||||
import { loadIntegrationConfig } from "../integrations/loadIntegration.js";
|
||||
import { createPbsAdapter, type PbsBackupGroup } from "../integrations/pbs/adapter.js";
|
||||
import { createProxmoxAdapter, guestsWithoutBackupCoverage, type ProxmoxBackupJob, type ProxmoxBackupTask, type ProxmoxGuest } from "../integrations/proxmox/adapter.js";
|
||||
|
||||
export type CoverageStatus = "ok" | "stale" | "failed" | "none" | "unknown";
|
||||
|
||||
export const DEFAULT_STALE_HOURS = 48;
|
||||
|
||||
export interface CoverageGuest {
|
||||
integrationId: number;
|
||||
integrationName: string;
|
||||
node: string;
|
||||
vmid: number;
|
||||
name: string;
|
||||
type: "qemu" | "lxc";
|
||||
/** The guest's power state, as Proxmox says. */
|
||||
power: string;
|
||||
inJob: boolean;
|
||||
/** ISO — the newest backup found, from either source. */
|
||||
lastBackupAt: string | null;
|
||||
lastSource: "pbs" | "task" | null;
|
||||
status: CoverageStatus;
|
||||
/** Why it is "failed", when it is. */
|
||||
failure: string | null;
|
||||
/** The server in this app that is this guest, if one is linked to it. */
|
||||
server: { id: number; name: string } | null;
|
||||
}
|
||||
|
||||
export interface ProxmoxSource {
|
||||
integrationId: number;
|
||||
integrationName: string;
|
||||
guests: ProxmoxGuest[];
|
||||
jobs: ProxmoxBackupJob[];
|
||||
tasks: ProxmoxBackupTask[];
|
||||
}
|
||||
|
||||
export interface PbsSource {
|
||||
integrationName: string;
|
||||
groups: PbsBackupGroup[];
|
||||
}
|
||||
|
||||
export interface SourceStatus {
|
||||
kind: "proxmox" | "pbs";
|
||||
name: string;
|
||||
ok: boolean;
|
||||
error?: string;
|
||||
}
|
||||
|
||||
export interface LinkedServer {
|
||||
id: number;
|
||||
name: string;
|
||||
proxmoxIntegrationId: number | null;
|
||||
proxmoxVmid: number | null;
|
||||
}
|
||||
|
||||
export interface CoverageReport {
|
||||
guests: CoverageGuest[];
|
||||
counts: Record<CoverageStatus, number> & { total: number };
|
||||
sources: SourceStatus[];
|
||||
staleHours: number;
|
||||
generatedAt: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Pure: the rows, from what the sources said. A guest that is in no job and has no backup at all is "none"; one with a failed latest
|
||||
* run or a snapshot that failed verification is "failed"; otherwise it is "ok" or "stale" by the age of its newest backup; and one in a job
|
||||
* with nothing to say when it last ran is "unknown".
|
||||
*/
|
||||
export function buildCoverage(input: { proxmox: ProxmoxSource[]; pbs: PbsSource[]; servers: LinkedServer[]; now: Date; staleHours?: number }): CoverageGuest[] {
|
||||
const staleMs = (input.staleHours ?? DEFAULT_STALE_HOURS) * 3600_000;
|
||||
const nowMs = input.now.getTime();
|
||||
|
||||
// The newest snapshot of each guest, across every Backup Server (guests are told apart there by "vm/100" or "ct/101").
|
||||
const snapshots = new Map<string, PbsBackupGroup>();
|
||||
for (const src of input.pbs) {
|
||||
for (const g of src.groups) {
|
||||
const key = `${g.backupType}/${g.backupId}`;
|
||||
const seen = snapshots.get(key);
|
||||
if (!seen || g.backupTime > seen.backupTime) snapshots.set(key, g);
|
||||
}
|
||||
}
|
||||
|
||||
const out: CoverageGuest[] = [];
|
||||
for (const src of input.proxmox) {
|
||||
const uncovered = new Set(guestsWithoutBackupCoverage(src.guests, src.jobs).map((g) => g.vmid));
|
||||
for (const g of src.guests) {
|
||||
const forGuest = src.tasks.filter((t) => t.guestId === String(g.vmid)).sort((a, b) => b.startTime.localeCompare(a.startTime));
|
||||
const lastOkTask = forGuest.find((t) => t.ok);
|
||||
const latestTask = forGuest[0];
|
||||
const snap = snapshots.get(`${g.type === "qemu" ? "vm" : "ct"}/${g.vmid}`);
|
||||
|
||||
const candidates: { at: number; source: "pbs" | "task" }[] = [];
|
||||
if (snap) candidates.push({ at: snap.backupTime * 1000, source: "pbs" });
|
||||
if (lastOkTask) candidates.push({ at: Date.parse(lastOkTask.endTime ?? lastOkTask.startTime), source: "task" });
|
||||
const newest = candidates.sort((a, b) => b.at - a.at)[0];
|
||||
|
||||
let failure: string | null = null;
|
||||
if (snap?.verification === "failed" && newest?.source === "pbs") failure = "the newest snapshot failed verification";
|
||||
else if (latestTask && !latestTask.ok && latestTask.status !== "running" && (!newest || Date.parse(latestTask.startTime) > newest.at)) failure = `the latest backup didn't succeed (${latestTask.status})`;
|
||||
|
||||
const inJob = !uncovered.has(g.vmid);
|
||||
let status: CoverageStatus;
|
||||
if (failure) status = "failed";
|
||||
else if (newest) status = nowMs - newest.at > staleMs ? "stale" : "ok";
|
||||
else status = inJob ? "unknown" : "none";
|
||||
|
||||
const linked = input.servers.find((s) => s.proxmoxIntegrationId === src.integrationId && s.proxmoxVmid === g.vmid);
|
||||
out.push({
|
||||
integrationId: src.integrationId,
|
||||
integrationName: src.integrationName,
|
||||
node: g.node,
|
||||
vmid: g.vmid,
|
||||
name: g.name,
|
||||
type: g.type,
|
||||
power: g.status,
|
||||
inJob,
|
||||
lastBackupAt: newest ? new Date(newest.at).toISOString() : null,
|
||||
lastSource: newest ? newest.source : null,
|
||||
status,
|
||||
failure,
|
||||
server: linked ? { id: linked.id, name: linked.name } : null,
|
||||
});
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
export function summarize(guests: CoverageGuest[]): CoverageReport["counts"] {
|
||||
const counts = { ok: 0, stale: 0, failed: 0, none: 0, unknown: 0, total: guests.length };
|
||||
for (const g of guests) counts[g.status]++;
|
||||
return counts;
|
||||
}
|
||||
|
||||
// ─── Reading the sources ────────────────────────────────────────────────────
|
||||
|
||||
interface Loaded {
|
||||
proxmox: ProxmoxSource[];
|
||||
pbs: PbsSource[];
|
||||
sources: SourceStatus[];
|
||||
}
|
||||
|
||||
/** How long what was read from Proxmox and Backup Server is reused: a page refreshed a few times asks them once. */
|
||||
const CACHE_MS = 60_000;
|
||||
let cache: { at: number; loaded: Loaded } | null = null;
|
||||
let loaderOverride: (() => Promise<Loaded>) | null = null;
|
||||
|
||||
/** For tests: read from this instead of the real integrations. */
|
||||
export function setCoverageLoaderForTests(loader: (() => Promise<Loaded>) | null): void {
|
||||
loaderOverride = loader;
|
||||
cache = null;
|
||||
}
|
||||
|
||||
async function loadSources(): Promise<Loaded> {
|
||||
if (loaderOverride) return loaderOverride();
|
||||
const loaded: Loaded = { proxmox: [], pbs: [], sources: [] };
|
||||
|
||||
const px = await db.select({ id: integrations.id, name: integrations.name }).from(integrations).where(and(eq(integrations.type, "proxmox"), eq(integrations.enabled, true)));
|
||||
await Promise.all(
|
||||
px.map(async (row) => {
|
||||
try {
|
||||
const cfg = await loadIntegrationConfig(row.id);
|
||||
if (!cfg) return;
|
||||
const adapter = createProxmoxAdapter(cfg.config as any);
|
||||
const [guests, jobs, tasks] = await Promise.all([adapter.listGuests(), adapter.listBackupJobs(), adapter.listRecentBackupTasks(50)]);
|
||||
loaded.proxmox.push({ integrationId: row.id, integrationName: row.name, guests, jobs, tasks });
|
||||
loaded.sources.push({ kind: "proxmox", name: row.name, ok: true });
|
||||
} catch (err) {
|
||||
loaded.sources.push({ kind: "proxmox", name: row.name, ok: false, error: err instanceof Error ? err.message : String(err) });
|
||||
}
|
||||
}),
|
||||
);
|
||||
|
||||
const pbs = await db.select({ id: integrations.id, name: integrations.name }).from(integrations).where(and(eq(integrations.type, "pbs"), eq(integrations.enabled, true)));
|
||||
await Promise.all(
|
||||
pbs.map(async (row) => {
|
||||
try {
|
||||
const cfg = await loadIntegrationConfig(row.id);
|
||||
if (!cfg) return;
|
||||
const datastores = await createPbsAdapter(cfg.config as any).listDatastores();
|
||||
const readable = datastores.filter((d) => d.error === null);
|
||||
loaded.pbs.push({ integrationName: row.name, groups: readable.flatMap((d) => d.groups) });
|
||||
const unreadable = datastores.filter((d) => d.error !== null);
|
||||
loaded.sources.push(unreadable.length > 0 ? { kind: "pbs", name: row.name, ok: false, error: `couldn't read ${unreadable.map((d) => d.name).join(", ")}` } : { kind: "pbs", name: row.name, ok: true });
|
||||
} catch (err) {
|
||||
loaded.sources.push({ kind: "pbs", name: row.name, ok: false, error: err instanceof Error ? err.message : String(err) });
|
||||
}
|
||||
}),
|
||||
);
|
||||
|
||||
loaded.sources.sort((a, b) => a.name.localeCompare(b.name));
|
||||
return loaded;
|
||||
}
|
||||
|
||||
export async function getBackupCoverage(options: { staleHours?: number; refresh?: boolean; now?: Date } = {}): Promise<CoverageReport> {
|
||||
const now = options.now ?? new Date();
|
||||
const staleHours = options.staleHours ?? DEFAULT_STALE_HOURS;
|
||||
if (!cache || options.refresh || now.getTime() - cache.at > CACHE_MS || loaderOverride) {
|
||||
cache = { at: now.getTime(), loaded: await loadSources() };
|
||||
}
|
||||
const serverRows = await db.select({ id: servers.id, name: servers.name, proxmoxIntegrationId: servers.proxmoxIntegrationId, proxmoxVmid: servers.proxmoxVmid }).from(servers);
|
||||
const guests = buildCoverage({ proxmox: cache.loaded.proxmox, pbs: cache.loaded.pbs, servers: serverRows, now, staleHours }).sort(
|
||||
(a, b) => a.name.localeCompare(b.name) || a.vmid - b.vmid,
|
||||
);
|
||||
return { guests, counts: summarize(guests), sources: cache.loaded.sources, staleHours, generatedAt: now.toISOString() };
|
||||
}
|
||||
@@ -0,0 +1,105 @@
|
||||
/**
|
||||
* Event webhooks: tell another system when something happens. Each webhook is a URL with the events it wants; when one happens it is
|
||||
* sent a JSON message, signed with the webhook's secret so the receiver can tell it came from here. This is separate from the chat-style
|
||||
* webhook under Settings → Notifications, which carries the same sentence a person would read.
|
||||
*
|
||||
* alert.opened / alert.resolved a problem began or ended — on the same terms as a notification (a problem under maintenance,
|
||||
* or folded under another, isn't announced until it stands alone)
|
||||
* maintenance.started / .ended a maintenance window began or finished, however it was made
|
||||
*
|
||||
* Delivery is best-effort and never holds anything up: one try, one more if that fails, then the result is noted on the webhook (shown in
|
||||
* Settings) and forgotten.
|
||||
*/
|
||||
import { createHmac, randomUUID } from "node:crypto";
|
||||
import { eq } from "drizzle-orm";
|
||||
import { db } from "../db/client.js";
|
||||
import { eventWebhooks, webhookEvents, type WebhookEvent } from "../db/schema.js";
|
||||
import { decryptSecret, encryptSecret } from "../crypto.js";
|
||||
import { env } from "../env.js";
|
||||
|
||||
export type EventWebhookRow = typeof eventWebhooks.$inferSelect;
|
||||
|
||||
const ENCRYPTED_PREFIX = "enc:v1:";
|
||||
const TIMEOUT_MS = 5000;
|
||||
const RETRY_AFTER_MS = 2000;
|
||||
|
||||
/** The stored form of a secret: encrypted when an encryption key is set, as the notification channels' secrets are. */
|
||||
export function sealSecret(plain: string): string {
|
||||
return env.credentialsEncryptionEnabled ? ENCRYPTED_PREFIX + encryptSecret(plain) : plain;
|
||||
}
|
||||
|
||||
function openSecret(stored: string | null): string | null {
|
||||
if (!stored) return null;
|
||||
if (!stored.startsWith(ENCRYPTED_PREFIX)) return stored;
|
||||
try {
|
||||
return decryptSecret(stored.slice(ENCRYPTED_PREFIX.length));
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
export function parseEvents(stored: string): WebhookEvent[] {
|
||||
try {
|
||||
const v = JSON.parse(stored);
|
||||
return Array.isArray(v) ? v.filter((e): e is WebhookEvent => (webhookEvents as readonly string[]).includes(e)) : [];
|
||||
} catch {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
/** The signature a receiver should compute over the exact body it got: HMAC-SHA256 with the secret, as hex, prefixed "sha256=". */
|
||||
export function signBody(secret: string, body: string): string {
|
||||
return `sha256=${createHmac("sha256", secret).update(body).digest("hex")}`;
|
||||
}
|
||||
|
||||
export interface Delivery {
|
||||
ok: boolean;
|
||||
/** "200", or what went wrong. */
|
||||
result: string;
|
||||
}
|
||||
|
||||
async function post(row: EventWebhookRow, event: string, body: string, deliveryId: string): Promise<Delivery> {
|
||||
const secret = openSecret(row.secret);
|
||||
const headers: Record<string, string> = {
|
||||
"Content-Type": "application/json",
|
||||
"User-Agent": "HomelabManager-Webhook",
|
||||
"X-HLM-Event": event,
|
||||
"X-HLM-Delivery": deliveryId,
|
||||
};
|
||||
if (secret) headers["X-HLM-Signature"] = signBody(secret, body);
|
||||
try {
|
||||
const res = await fetch(row.url, { method: "POST", headers, body, signal: AbortSignal.timeout(TIMEOUT_MS), redirect: "manual" });
|
||||
return { ok: res.status >= 200 && res.status < 300, result: String(res.status) };
|
||||
} catch (err) {
|
||||
const name = err instanceof Error ? err.name : "";
|
||||
return { ok: false, result: name === "TimeoutError" ? "timed out" : err instanceof Error ? err.message.slice(0, 120) : "failed" };
|
||||
}
|
||||
}
|
||||
|
||||
/** One message to one webhook, retried once if it didn't go through. The outcome is written on the webhook. */
|
||||
export async function deliver(row: EventWebhookRow, event: string, data: unknown, options: { retryAfterMs?: number } = {}): Promise<Delivery> {
|
||||
const deliveryId = randomUUID();
|
||||
const body = JSON.stringify({ event, id: deliveryId, createdAt: new Date().toISOString(), source: "homelab-manager", data });
|
||||
let outcome = await post(row, event, body, deliveryId);
|
||||
if (!outcome.ok) {
|
||||
await new Promise((r) => setTimeout(r, options.retryAfterMs ?? RETRY_AFTER_MS));
|
||||
outcome = await post(row, event, body, deliveryId);
|
||||
}
|
||||
try {
|
||||
await db.update(eventWebhooks).set({ lastAttemptAt: new Date().toISOString(), lastResult: outcome.result }).where(eq(eventWebhooks.id, row.id));
|
||||
} catch (err) {
|
||||
console.error("[webhooks] couldn't note the result:", err);
|
||||
}
|
||||
return outcome;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends an event to every enabled webhook that wants it. Returns at once — delivery carries on in the background and can never delay or
|
||||
* fail what caused the event.
|
||||
*/
|
||||
export function emitEvent(event: WebhookEvent, data: unknown): void {
|
||||
void (async () => {
|
||||
const rows = await db.select().from(eventWebhooks).where(eq(eventWebhooks.enabled, true));
|
||||
await Promise.all(rows.filter((r) => parseEvents(r.events).includes(event)).map((r) => deliver(r, event, data)));
|
||||
})().catch((err) => console.error("[webhooks] couldn't send an event:", err));
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
/**
|
||||
* The small calculations around the hardware inventory: how long a warranty has left, and what a machine's power draw costs to run.
|
||||
* Kept apart from the routes so the rules can be tested exactly.
|
||||
*/
|
||||
|
||||
export type WarrantyStatus = "none" | "ok" | "expiring" | "expired";
|
||||
|
||||
/** A warranty that ends within this many days is "expiring". */
|
||||
export const WARRANTY_WARN_DAYS = 90;
|
||||
|
||||
/** Whether a `YYYY-MM-DD` is a real calendar date. */
|
||||
export function isRealDate(value: string): boolean {
|
||||
if (!/^\d{4}-\d{2}-\d{2}$/.test(value)) return false;
|
||||
const t = Date.parse(`${value}T00:00:00Z`);
|
||||
return Number.isFinite(t) && new Date(t).toISOString().slice(0, 10) === value;
|
||||
}
|
||||
|
||||
/** Days from today (UTC) to the end of a warranty; negative once it has ended. Null with no warranty date. */
|
||||
export function warrantyDaysLeft(warrantyUntil: string | null, now: Date = new Date()): number | null {
|
||||
if (!warrantyUntil || !isRealDate(warrantyUntil)) return null;
|
||||
const end = Date.parse(`${warrantyUntil}T00:00:00Z`);
|
||||
const today = Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate());
|
||||
return Math.round((end - today) / 86_400_000);
|
||||
}
|
||||
|
||||
export function warrantyStatus(warrantyUntil: string | null, now: Date = new Date(), warnDays: number = WARRANTY_WARN_DAYS): WarrantyStatus {
|
||||
const days = warrantyDaysLeft(warrantyUntil, now);
|
||||
if (days === null) return "none";
|
||||
if (days < 0) return "expired";
|
||||
return days <= warnDays ? "expiring" : "ok";
|
||||
}
|
||||
|
||||
/** Kilowatt-hours a steady draw uses in a year. */
|
||||
export function yearlyKwh(watts: number): number {
|
||||
return Math.round((watts / 1000) * 24 * 365 * 100) / 100;
|
||||
}
|
||||
|
||||
/** What a year of a steady draw costs at a price per kWh, rounded to whole units of currency. Null when there is no draw or no price. */
|
||||
export function yearlyCost(watts: number | null, pricePerKwh: number | null): number | null {
|
||||
if (watts === null || watts <= 0 || pricePerKwh === null || !(pricePerKwh > 0)) return null;
|
||||
return Math.round(yearlyKwh(watts) * pricePerKwh);
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
/**
|
||||
* Turns maintenance windows beginning and ending into events, whatever made the window — by hand, in bulk, from a recurring schedule or
|
||||
* mirrored from Uptime Kuma, and whether it ended on its own or was ended early. It simply watches which windows are running: one that
|
||||
* wasn't a moment ago is "started", one that was and isn't now is "ended". After a restart it starts from what is running then, so nothing
|
||||
* is announced twice or announced for a window that began before.
|
||||
*/
|
||||
import { listActiveWindows, describeTarget, type ActiveWindow } from "./maintenance.js";
|
||||
import { emitEvent } from "./eventWebhooks.js";
|
||||
|
||||
const INTERVAL_MS = 30_000;
|
||||
|
||||
let known: Map<number, ActiveWindow> | null = null;
|
||||
let timer: ReturnType<typeof setInterval> | null = null;
|
||||
|
||||
async function describe(w: ActiveWindow) {
|
||||
const target = await describeTarget(w.targetType, w.targetId);
|
||||
return { id: w.id, targetType: w.targetType, targetId: w.targetId, targetName: target?.name ?? null, reason: w.reason, startedAt: w.startedAt, endsAt: w.endsAt, source: w.source };
|
||||
}
|
||||
|
||||
/** One look. Returns what it announced, for the tests. */
|
||||
export async function checkMaintenanceEvents(now: Date = new Date()): Promise<{ started: number[]; ended: number[] }> {
|
||||
const active = new Map((await listActiveWindows(now)).map((w) => [w.id, w]));
|
||||
const started: number[] = [];
|
||||
const ended: number[] = [];
|
||||
if (known !== null) {
|
||||
for (const [id, w] of active) {
|
||||
if (!known.has(id)) {
|
||||
started.push(id);
|
||||
emitEvent("maintenance.started", { window: await describe(w) });
|
||||
}
|
||||
}
|
||||
for (const [id, w] of known) {
|
||||
if (!active.has(id)) {
|
||||
ended.push(id);
|
||||
// The window as it was last seen (one ended early has had its end brought forward since), and when it was noticed to be over.
|
||||
emitEvent("maintenance.ended", { window: { ...(await describe(w)), endedAt: now.toISOString() } });
|
||||
}
|
||||
}
|
||||
}
|
||||
known = active;
|
||||
return { started, ended };
|
||||
}
|
||||
|
||||
/** For tests: forget what was running, so the next look is a first look. */
|
||||
export function resetMaintenanceEventsForTests(): void {
|
||||
known = null;
|
||||
}
|
||||
|
||||
export function initMaintenanceEvents(): void {
|
||||
if (timer) return;
|
||||
void checkMaintenanceEvents().catch((err) => console.error("[maintenanceEvents]", err));
|
||||
timer = setInterval(() => void checkMaintenanceEvents().catch((err) => console.error("[maintenanceEvents]", err)), INTERVAL_MS);
|
||||
}
|
||||
@@ -0,0 +1,131 @@
|
||||
/**
|
||||
* What /metrics says, in Prometheus's text format: servers (reporting or not, load, memory, disks, updates, agent version), alerts by
|
||||
* severity, integrations, maintenance, things that expire. It reads what the app already knows — nothing here calls out to a server or an
|
||||
* integration on its own, so a scrape every fifteen seconds costs a few database reads (the alerts are the app's own minute-old result).
|
||||
*/
|
||||
import { db } from "../db/client.js";
|
||||
import { assets, integrations, maintenanceWindows, secrets, servers } from "../db/schema.js";
|
||||
import { env } from "../env.js";
|
||||
import { getAlerts } from "./alerts.js";
|
||||
import { isAgentOutdated, latestAgentVersions } from "./agentVersions.js";
|
||||
import { getSourceStatus } from "./integrationHealthMonitor.js";
|
||||
import { parsePendingUpdates } from "./pendingUpdates.js";
|
||||
import { computeSecretStatus } from "./secretStatus.js";
|
||||
import { getSettings } from "./settingsStore.js";
|
||||
import { getUptime } from "./uptimeHistory.js";
|
||||
import { and, gt, lte } from "drizzle-orm";
|
||||
|
||||
/** A label value as Prometheus wants it: backslash, double quote and newline escaped. */
|
||||
export function escapeLabel(value: string): string {
|
||||
return value.replace(/\\/g, "\\\\").replace(/"/g, '\\"').replace(/\n/g, "\\n");
|
||||
}
|
||||
|
||||
/** A metric name's HELP text may not contain a raw newline either. */
|
||||
const help = (text: string) => text.replace(/\\/g, "\\\\").replace(/\n/g, "\\n");
|
||||
|
||||
type Labels = Record<string, string>;
|
||||
|
||||
class Exposition {
|
||||
private lines: string[] = [];
|
||||
private declared = new Set<string>();
|
||||
|
||||
/** One sample. The HELP and TYPE lines for a metric are written the first time it appears. */
|
||||
add(name: string, type: "gauge" | "counter", description: string, value: number, labels: Labels = {}): void {
|
||||
if (!Number.isFinite(value)) return;
|
||||
if (!this.declared.has(name)) {
|
||||
this.declared.add(name);
|
||||
this.lines.push(`# HELP ${name} ${help(description)}`, `# TYPE ${name} ${type}`);
|
||||
}
|
||||
const parts = Object.entries(labels).map(([k, v]) => `${k}="${escapeLabel(v)}"`);
|
||||
this.lines.push(`${name}${parts.length ? `{${parts.join(",")}}` : ""} ${Number.isInteger(value) ? value : Number(value.toFixed(4))}`);
|
||||
}
|
||||
|
||||
toString(): string {
|
||||
return `${this.lines.join("\n")}\n`;
|
||||
}
|
||||
}
|
||||
|
||||
export async function buildMetrics(now: Date = new Date()): Promise<string> {
|
||||
const started = Date.now();
|
||||
const out = new Exposition();
|
||||
const settings = await getSettings();
|
||||
const nowSec = Math.floor(now.getTime() / 1000);
|
||||
|
||||
out.add("homelab_info", "gauge", "Always 1; the labels say which installation this is.", 1, { environment: env.environment?.name || "" });
|
||||
|
||||
// ── Servers ──
|
||||
const latest = latestAgentVersions();
|
||||
const serverRows = await db.select().from(servers);
|
||||
const offlineAfterMs = settings.healthChecks.serverOfflineMinutes * 60_000;
|
||||
for (const s of serverRows) {
|
||||
if (!s.lastSeenAt) continue; // never reported: nothing to say about it
|
||||
const server = s.name;
|
||||
const seen = Date.parse(s.lastSeenAt);
|
||||
out.add("homelab_server_up", "gauge", "1 if the server's agent has reported within the offline threshold, else 0.", now.getTime() - seen <= offlineAfterMs ? 1 : 0, { server });
|
||||
out.add("homelab_server_last_seen_timestamp_seconds", "gauge", "When the server's agent last reported, as a Unix time.", Math.floor(seen / 1000), { server });
|
||||
if (s.cpuLoadPercent !== null) out.add("homelab_server_cpu_load_percent", "gauge", "CPU load as the agent reports it.", s.cpuLoadPercent, { server });
|
||||
if (s.memTotalBytes && s.memUsedBytes !== null) out.add("homelab_server_memory_used_percent", "gauge", "Memory in use.", (s.memUsedBytes / s.memTotalBytes) * 100, { server });
|
||||
let disks: { mount: string; sizeBytes: number; usedBytes: number }[] = [];
|
||||
try {
|
||||
disks = s.disks ? JSON.parse(s.disks) : [];
|
||||
} catch {
|
||||
// a garbled report shouldn't blank the whole scrape
|
||||
}
|
||||
for (const d of disks) {
|
||||
if (d.sizeBytes > 0) out.add("homelab_server_disk_used_percent", "gauge", "How full a disk is.", (d.usedBytes / d.sizeBytes) * 100, { server, mount: d.mount });
|
||||
}
|
||||
const updates = parsePendingUpdates(s.pendingUpdates);
|
||||
if (updates) {
|
||||
out.add("homelab_server_pending_updates", "gauge", "Updates the agent says are waiting to be installed.", updates.count, { server });
|
||||
if (updates.security !== null) out.add("homelab_server_pending_security_updates", "gauge", "Of those, the ones that are security fixes.", updates.security, { server });
|
||||
out.add("homelab_server_reboot_required", "gauge", "1 if a restart is due.", updates.rebootRequired ? 1 : 0, { server });
|
||||
}
|
||||
out.add("homelab_server_agent_outdated", "gauge", "1 if the server's agent is older than the one this installation would install now.", isAgentOutdated(s, latest) ? 1 : 0, { server });
|
||||
}
|
||||
try {
|
||||
const uptime = await getUptime(now, 30);
|
||||
for (const s of serverRows) {
|
||||
const series = uptime.servers[s.id];
|
||||
if (series) out.add("homelab_server_uptime_ratio_30d", "gauge", "Share of the last 30 days a server was up, 0 to 1.", series.percent / 100, { server: s.name });
|
||||
}
|
||||
} catch (err) {
|
||||
console.error("[metrics] couldn't read the uptime:", err);
|
||||
}
|
||||
|
||||
// ── Alerts ──
|
||||
try {
|
||||
const report = await getAlerts(false);
|
||||
for (const severity of ["critical", "warning", "info"] as const) out.add("homelab_alerts", "gauge", "Active alerts by severity (not counting silenced ones).", report.counts[severity], { severity });
|
||||
out.add("homelab_alerts_silenced", "gauge", "Alerts held back by a maintenance window.", report.counts.silenced);
|
||||
out.add("homelab_alerts_grouped", "gauge", "Alerts folded under a server that is down.", report.counts.grouped);
|
||||
} catch (err) {
|
||||
console.error("[metrics] couldn't read the alerts:", err);
|
||||
}
|
||||
|
||||
// ── Integrations ──
|
||||
for (const i of await db.select({ name: integrations.name, type: integrations.type, enabled: integrations.enabled }).from(integrations)) {
|
||||
if (!i.enabled) continue;
|
||||
const status = getSourceStatus(i.type).status;
|
||||
if (status === "unknown") continue; // nothing has called it yet: not up, not down
|
||||
out.add("homelab_integration_up", "gauge", "1 if the integration's latest calls are working, 0 if they are failing.", status === "ok" ? 1 : 0, { integration: i.name, type: i.type });
|
||||
}
|
||||
|
||||
// ── Maintenance ──
|
||||
const iso = now.toISOString();
|
||||
const active = await db.select({ id: maintenanceWindows.id }).from(maintenanceWindows).where(and(lte(maintenanceWindows.startedAt, iso), gt(maintenanceWindows.endsAt, iso)));
|
||||
out.add("homelab_maintenance_active_windows", "gauge", "Maintenance windows running now.", active.length);
|
||||
|
||||
// ── Things that expire ──
|
||||
for (const s of await db.select().from(secrets)) {
|
||||
const status = computeSecretStatus(s.expiryDate, s.warnDays, now);
|
||||
if (status.daysLeft !== null) out.add("homelab_secret_expiry_days", "gauge", "Days until a secret expires (negative once it has).", status.daysLeft, { name: s.name });
|
||||
}
|
||||
|
||||
// ── Inventory ──
|
||||
const power = (await db.select({ w: assets.powerWatts }).from(assets)).reduce((n, a) => n + (a.w ?? 0), 0);
|
||||
out.add("homelab_inventory_power_watts", "gauge", "Typical power draw of everything in the inventory, in watts.", power);
|
||||
|
||||
out.add("homelab_scrape_duration_seconds", "gauge", "How long building this answer took.", (Date.now() - started) / 1000);
|
||||
out.add("homelab_scrape_timestamp_seconds", "gauge", "When this answer was built, as a Unix time.", nowSec);
|
||||
return out.toString();
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
/**
|
||||
* Metrics tokens: read-only access to /metrics for Prometheus. Like an agent token or a share link, the secret is shown once, when it is
|
||||
* made, and only its SHA-256 is kept — so it can't be read back out of the database or a backup.
|
||||
*/
|
||||
import { createHash, randomBytes } from "node:crypto";
|
||||
import { eq } from "drizzle-orm";
|
||||
import { db } from "../db/client.js";
|
||||
import { metricsTokens } from "../db/schema.js";
|
||||
|
||||
export type MetricsTokenRow = typeof metricsTokens.$inferSelect;
|
||||
|
||||
const hash = (token: string) => createHash("sha256").update(token).digest("hex");
|
||||
|
||||
/** Use is not recorded more than once in this time, so a scrape every fifteen seconds doesn't write the database each time. */
|
||||
const USED_WRITE_GAP_MS = 60_000;
|
||||
|
||||
export async function createMetricsToken(params: { label: string; createdBy: string }): Promise<{ row: MetricsTokenRow; token: string }> {
|
||||
const token = `hlm_m_${randomBytes(24).toString("base64url")}`;
|
||||
const [row] = await db.insert(metricsTokens).values({ label: params.label, tokenHash: hash(token), tokenPrefix: token.slice(0, 12), createdBy: params.createdBy }).returning();
|
||||
return { row, token };
|
||||
}
|
||||
|
||||
/** The token a secret belongs to, if there is one; notes that it was used. */
|
||||
export async function resolveMetricsToken(token: string, now: Date = new Date()): Promise<MetricsTokenRow | null> {
|
||||
if (typeof token !== "string" || token.length < 20 || token.length > 128) return null;
|
||||
const [row] = await db.select().from(metricsTokens).where(eq(metricsTokens.tokenHash, hash(token))).limit(1);
|
||||
if (!row) return null;
|
||||
if (!row.lastUsedAt || now.getTime() - Date.parse(row.lastUsedAt) >= USED_WRITE_GAP_MS) {
|
||||
await db.update(metricsTokens).set({ lastUsedAt: now.toISOString() }).where(eq(metricsTokens.id, row.id));
|
||||
}
|
||||
return row;
|
||||
}
|
||||
|
||||
/** A token as the admin list shows it: everything but the hash. */
|
||||
export function publicMetricsToken(row: MetricsTokenRow) {
|
||||
return { id: row.id, label: row.label, tokenPrefix: row.tokenPrefix, createdBy: row.createdBy, createdAt: row.createdAt, lastUsedAt: row.lastUsedAt };
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
import nodemailer from "nodemailer";
|
||||
import { getSettings, type NotificationEvents } from "./settingsStore.js";
|
||||
import { enqueueNotification, listQueuedNotifications, clearQueuedNotifications } from "./notificationQueue.js";
|
||||
import { emitEvent } from "./eventWebhooks.js";
|
||||
|
||||
// ─── Channel senders (best-effort — failures are logged, never thrown) ──────
|
||||
|
||||
@@ -309,7 +310,9 @@ export async function notifyPbsVerificationFailed(
|
||||
);
|
||||
}
|
||||
|
||||
export async function notifyHealthIssues(issues: { message: string }[]): Promise<void> {
|
||||
export async function notifyHealthIssues(issues: { message: string; key?: string; source?: string; severity?: string }[]): Promise<void> {
|
||||
// The event webhooks hear of it whatever this notification is switched on for: they are a separate way of being told.
|
||||
for (const i of issues) emitEvent("alert.opened", { alert: { key: i.key ?? null, source: i.source ?? null, severity: i.severity ?? "warning", message: i.message } });
|
||||
if (issues.length === 0) return;
|
||||
if (!(await eventEnabled("healthAlerts"))) return;
|
||||
await notify(
|
||||
@@ -318,7 +321,8 @@ export async function notifyHealthIssues(issues: { message: string }[]): Promise
|
||||
);
|
||||
}
|
||||
|
||||
export async function notifyHealthRecovered(resolved: { message: string }[]): Promise<void> {
|
||||
export async function notifyHealthRecovered(resolved: { message: string; key?: string }[]): Promise<void> {
|
||||
for (const r of resolved) emitEvent("alert.resolved", { alert: { key: r.key ?? null, message: r.message } });
|
||||
if (resolved.length === 0) return;
|
||||
if (!(await eventEnabled("healthAlerts"))) return;
|
||||
await notify(
|
||||
@@ -327,7 +331,9 @@ export async function notifyHealthRecovered(resolved: { message: string }[]): Pr
|
||||
);
|
||||
}
|
||||
|
||||
export async function notifyAutomationFailed(issues: { message: string }[]): Promise<void> {
|
||||
export async function notifyAutomationFailed(issues: { message: string; key?: string; source?: string; severity?: string }[]): Promise<void> {
|
||||
// The event webhooks hear of it whatever this notification is switched on for: they are a separate way of being told.
|
||||
for (const i of issues) emitEvent("alert.opened", { alert: { key: i.key ?? null, source: i.source ?? null, severity: i.severity ?? "warning", message: i.message } });
|
||||
if (issues.length === 0) return;
|
||||
if (!(await eventEnabled("automationAlerts"))) return;
|
||||
await notify(
|
||||
@@ -336,7 +342,8 @@ export async function notifyAutomationFailed(issues: { message: string }[]): Pro
|
||||
);
|
||||
}
|
||||
|
||||
export async function notifyAutomationRecovered(resolved: { message: string }[]): Promise<void> {
|
||||
export async function notifyAutomationRecovered(resolved: { message: string; key?: string }[]): Promise<void> {
|
||||
for (const r of resolved) emitEvent("alert.resolved", { alert: { key: r.key ?? null, message: r.message } });
|
||||
if (resolved.length === 0) return;
|
||||
if (!(await eventEnabled("automationAlerts"))) return;
|
||||
await notify(
|
||||
@@ -409,6 +416,7 @@ export function sourceLabel(source: string): string {
|
||||
}
|
||||
|
||||
export async function notifyIntegrationDown(source: string, consecutiveFailures: number): Promise<void> {
|
||||
emitEvent("alert.opened", { alert: { key: `integration:${source}`, source, severity: "warning", message: `${sourceLabel(source)} has failed its last ${consecutiveFailures} calls in a row.` } });
|
||||
if (!(await eventEnabled("integrationFailureAlerts"))) return;
|
||||
await notify(
|
||||
"Homelab Manager — Integration Down",
|
||||
@@ -417,6 +425,7 @@ export async function notifyIntegrationDown(source: string, consecutiveFailures:
|
||||
}
|
||||
|
||||
export async function notifyIntegrationRecovered(source: string): Promise<void> {
|
||||
emitEvent("alert.resolved", { alert: { key: `integration:${source}`, message: `${sourceLabel(source)} is answering again.` } });
|
||||
if (!(await eventEnabled("integrationFailureAlerts"))) return;
|
||||
await notify("Homelab Manager — Integration Recovered", `${sourceLabel(source)} succeeded again after failing.`);
|
||||
}
|
||||
@@ -108,6 +108,24 @@ export interface DigestSettings {
|
||||
time: string;
|
||||
}
|
||||
|
||||
/** How roles are decided at sign-in: by hand on the Users page (the default), or from the groups the identity provider reports. */
|
||||
export interface SignInSettings {
|
||||
roleFromGroups: boolean;
|
||||
/** The claim that carries the groups; "groups" in Authentik. */
|
||||
groupsClaim: string;
|
||||
adminGroups: string[];
|
||||
operatorGroups: string[];
|
||||
/** Someone in none of the named groups gets viewer, or keeps what they have. */
|
||||
otherwise: "viewer" | "keep";
|
||||
}
|
||||
|
||||
/** What the inventory needs to work out a running cost. Without a price per kWh it shows draw but no cost. */
|
||||
export interface InventorySettings {
|
||||
/** A short name shown after amounts ("SEK", "kr", "$"); may be blank. */
|
||||
currency: string;
|
||||
pricePerKwh: number | null;
|
||||
}
|
||||
|
||||
export interface ConsistencySettings {
|
||||
/** CIDR ranges and single addresses the consistency report ignores entirely — e.g. Docker networks that repeat on many hosts. */
|
||||
excludedRanges: string[];
|
||||
@@ -160,6 +178,8 @@ export interface AppSettings {
|
||||
quietHours: QuietHoursSettings;
|
||||
healthChecks: HealthCheckSettings;
|
||||
digest: DigestSettings;
|
||||
signIn: SignInSettings;
|
||||
inventory: InventorySettings;
|
||||
consistency: ConsistencySettings;
|
||||
nameGenerator: NameGeneratorSettings;
|
||||
alertTickets: AlertTicketSettings;
|
||||
@@ -193,6 +213,8 @@ const DEFAULTS: AppSettings = {
|
||||
logRetention: { enabled: false, retentionDays: 90, intervalHours: 24 },
|
||||
quietHours: { enabled: false, start: "22:00", end: "07:00" },
|
||||
digest: { enabled: false, day: 1, time: "08:00" },
|
||||
inventory: { currency: "", pricePerKwh: null },
|
||||
signIn: { roleFromGroups: false, groupsClaim: "groups", adminGroups: [], operatorGroups: [], otherwise: "viewer" },
|
||||
healthChecks: { serverOfflineMinutes: 60, diskUsagePercent: 90, domainWarnDays: 30, diskForecastDays: 14 },
|
||||
// Docker's default bridge (172.17.0.0/16) and the pool it hands custom networks from (172.18–31) live here, and every
|
||||
// Docker host repeats them. Shown on the Consistency page, where it can be removed if 172.16/12 is used as a real LAN.
|
||||
|
||||
@@ -5,9 +5,11 @@
|
||||
* into a few lines per topic and leaves out any topic with nothing to say. Plain text, so it reads in a phone notification and an email alike.
|
||||
*/
|
||||
import { db } from "../db/client.js";
|
||||
import { integrations, secrets, servers } from "../db/schema.js";
|
||||
import { assets, integrations, secrets, servers } from "../db/schema.js";
|
||||
import { warrantyDaysLeft } from "./inventory.js";
|
||||
import { env } from "../env.js";
|
||||
import { getAlerts } from "./alerts.js";
|
||||
import { getBackupCoverage } from "./backupCoverage.js";
|
||||
import { getChangesSince } from "./changesSince.js";
|
||||
import { collectDomainAlerts } from "./domainMonitor.js";
|
||||
import { describeTarget, listUpcomingWindows } from "./maintenance.js";
|
||||
@@ -157,6 +159,13 @@ export async function buildDigest(now: Date = new Date()): Promise<Digest> {
|
||||
for (const d of (await collectDomainAlerts(now)).expiring) {
|
||||
expiring.push({ daysLeft: d.daysLeft, text: d.daysLeft < 0 ? `Domain ${d.name} expired ${plural(Math.abs(d.daysLeft), "day")} ago` : `Domain ${d.name} expires in ${plural(d.daysLeft, "day")} (${d.expiresAt})` });
|
||||
}
|
||||
// A warranty about to end is worth knowing while there is still time to claim on it; one that ended long ago is not news.
|
||||
for (const a of await db.select({ name: assets.name, warrantyUntil: assets.warrantyUntil }).from(assets)) {
|
||||
const days = warrantyDaysLeft(a.warrantyUntil, now);
|
||||
if (days !== null && days >= -30 && days <= 60) {
|
||||
expiring.push({ daysLeft: days, text: days < 0 ? `Warranty on ${a.name} ended ${plural(Math.abs(days), "day")} ago` : `Warranty on ${a.name} ends in ${plural(days, "day")} (${a.warrantyUntil})` });
|
||||
}
|
||||
}
|
||||
if (expiring.length > 0) {
|
||||
expiring.sort((a, b) => a.daysLeft - b.daysLeft);
|
||||
sections.push({ heading: "Expiring", lines: listWithMore(expiring.map((e) => `- ${e.text}`)), attention: true });
|
||||
@@ -185,6 +194,22 @@ export async function buildDigest(now: Date = new Date()): Promise<Digest> {
|
||||
console.error("[digest] couldn't read the pending updates:", err);
|
||||
}
|
||||
|
||||
// ── Backups ──
|
||||
try {
|
||||
const coverage = await getBackupCoverage({ now });
|
||||
const order = { failed: 0, none: 1, stale: 2 } as const;
|
||||
const bad = coverage.guests.filter((g): g is typeof g & { status: keyof typeof order } => g.status === "failed" || g.status === "none" || g.status === "stale").sort((a, b) => order[a.status] - order[b.status] || a.name.localeCompare(b.name));
|
||||
if (bad.length > 0) {
|
||||
const what = (g: (typeof bad)[number]) =>
|
||||
g.status === "failed" ? `${g.failure}` : g.status === "none" ? "no backup, and not in any backup job" : `last backup ${spanWords((now.getTime() - Date.parse(g.lastBackupAt!)) / 60_000)} ago`;
|
||||
const lines = [`${coverage.counts.ok} of ${plural(coverage.counts.total, "guest")} ${coverage.counts.ok === 1 ? "has" : "have"} a recent backup.`];
|
||||
lines.push(...listWithMore(bad.map((g) => `- ${g.name} (${g.type === "qemu" ? "VM" : "container"} ${g.vmid}): ${what(g)}`)));
|
||||
sections.push({ heading: "Backups", lines, attention: true });
|
||||
}
|
||||
} catch (err) {
|
||||
console.error("[digest] couldn't read the backup coverage:", err);
|
||||
}
|
||||
|
||||
// ── Planned ──
|
||||
try {
|
||||
const horizon = now.getTime() + DIGEST_DAYS * DAY_MS;
|
||||
|
||||
Reference in new issue
Block a user