Racks, notes and to-do, OS support, suggested dependencies, Nginx Proxy Manager and Home Assistant

- Racks page and a network diagram (UniFi tree with servers, printable)
- Notes on servers and devices; To do list shown in Coming up and My work
- Agents report the operating system (Linux 7, Windows 6); end-of-life badge and alert
- Suggested dependencies from where a service is reached
- Nginx Proxy Manager integration: proxy hosts page, certificate and host alerts, suggested checks
- Home Assistant integration: watch chosen entities, history chart, limit and unavailable alerts
- TimeChart gets a "value" scale that fits negatives; docs updated

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
bobbanandClaude Sonnet 5.5 committed 2026-10-20 21:47:56 +02:00
1 parent 68b41b5586
commit 18ceed62e8
77 files changed
+20432 -35

No files matched your search

+15 -2
View File
@@ -9,7 +9,8 @@
import { createHash } from "node:crypto";
import { and, eq } from "drizzle-orm";
import { db } from "../db/client.js";
import { assets, domains, endpointChecks, integrations, maintenanceSchedules, patchPlans, secrets, servers } from "../db/schema.js";
import { assets, domains, endpointChecks, integrations, maintenanceSchedules, patchPlans, secrets, servers, todos } from "../db/schema.js";
import { isNull } from "drizzle-orm";
import { collectTailscaleKeyExpiries } from "./tailscaleKeyExpiryScheduler.js";
import { describeTarget, listUpcomingWindows } from "./maintenance.js";
import { occurrencesBetween, targetsOf } from "./maintenanceSchedules.js";
@@ -19,7 +20,7 @@ import { latestTests } from "./restoreTests.js";
import { getSettings } from "./settingsStore.js";
import { env } from "../env.js";
export const AGENDA_KINDS = ["secret", "certificate", "domain", "tailscale", "warranty", "support", "maintenance", "restore", "updates"] as const;
export const AGENDA_KINDS = ["secret", "certificate", "domain", "tailscale", "warranty", "support", "maintenance", "restore", "updates", "todo"] as const;
export type AgendaKind = (typeof AGENDA_KINDS)[number];
export const KIND_LABELS: Record<AgendaKind, string> = {
@@ -32,6 +33,7 @@ export const KIND_LABELS: Record<AgendaKind, string> = {
maintenance: "Maintenance",
restore: "Restore tests",
updates: "Updates waiting",
todo: "To do",
};
export interface AgendaItem {
@@ -96,6 +98,8 @@ export interface AgendaData {
restoreLimitDays: number;
/** Servers with updates waiting. */
updates: { name: string; count: number; security: number | null; since: string | null }[];
/** Open jobs that have a day they are due. */
todos: { id: number; title: string; dueDate: string | null; assigneeName: string | null; serverId: number | null }[];
notes: string[];
}
@@ -127,6 +131,8 @@ export async function loadAgendaData(now: Date = new Date()): Promise<AgendaData
latestTests(),
]);
const todoRows = (await db.select().from(todos).where(isNull(todos.doneAt))).filter((t) => t.dueDate !== null);
const windows: AgendaData["windows"] = [];
for (const w of await listUpcomingWindows(now)) {
const target = await describeTarget(w.targetType, w.targetId);
@@ -174,6 +180,7 @@ export async function loadAgendaData(now: Date = new Date()): Promise<AgendaData
restoreTests: [...tests.values()].map((t) => ({ subjectKey: t.subjectKey, label: t.label, testedAt: t.testedAt, result: t.result })),
restoreLimitDays: settings.healthChecks.restoreTestDays,
updates,
todos: todoRows.map((t) => ({ id: t.id, title: t.title, dueDate: t.dueDate, assigneeName: t.assigneeName, serverId: t.targetType === "server" ? t.targetId : null })),
notes,
};
}
@@ -213,6 +220,11 @@ export function agendaFromData(data: AgendaData, now: Date, tz: string, days: nu
day(`support:${a.id}`, "support", a.endOfSupport, (past) => ({ title: `Support for ${a.name} ${past ? "ended" : "ends"}`, detail: "time to plan its replacement", link: `/inventory?asset=${a.id}`, serverId: a.serverId }));
}
// Jobs on the to-do list that have a day: an open one that is past due stays until it is done (or for as long as anything past is kept).
for (const t of data.todos ?? []) {
day(`todo:${t.id}`, "todo", t.dueDate, (past) => ({ title: `${past ? "Overdue" : "To do"}: ${t.title}`, detail: t.assigneeName ? `for ${t.assigneeName}` : null, link: "/todo", serverId: t.serverId }));
}
// A restore test is due a set time after the last good one — only if a time limit is set (Settings → Notifications).
if (data.restoreLimitDays > 0) {
for (const t of data.restoreTests) {
@@ -317,6 +329,7 @@ const GENERIC: Record<AgendaKind, string> = {
maintenance: "Planned maintenance",
restore: "A restore test is due",
updates: "Updates are waiting",
todo: "A job is due",
};
/**
+1
View File
@@ -12,6 +12,7 @@
* Windows: also reports failed services, disk health and Storage Spaces pools, and what the machine is (maker, model, serial)
* 5 — Linux: also says what the machine is (maker, model, serial), for the inventory
* 6 — Linux (and Windows 5): also says what it could not collect and why, so the server can say what to fix
* 7 — Linux (and Windows 6): also says which operating system and release it runs, so the server can say when that release stops being supported
*/
import { createHash } from "node:crypto";
import { readFileSync } from "node:fs";
+67 -1
View File
@@ -11,8 +11,11 @@
*/
import { eq } from "drizzle-orm";
import { db } from "../db/client.js";
import { endpointChecks, heartbeats, integrations, networkDevices, secrets, servers } from "../db/schema.js";
import { endpointChecks, haEntities, haWatched, heartbeats, integrations, networkDevices, secrets, servers } from "../db/schema.js";
import { disksFillingSoon, getForecastSeries } from "./serverMetrics.js";
import { daysOfSupportLeft, lifecycleOf } from "./osLifecycle.js";
import { npmIntegrationIds, npmProblems, readNpm } from "./npmReader.js";
import { haProblems, haSyncStatuses, isHaFresh } from "./homeAssistantSync.js";
import { diskLimit, effectiveThresholds, parseThresholds } from "./serverThresholds.js";
import { getBackupCoverage, staleBackups } from "./backupCoverage.js";
import { recordFromReport } from "./incidents.js";
@@ -65,6 +68,8 @@ const SOURCES: Record<string, { label: string; link: string }> = {
uptimekuma: { label: "Uptime Kuma", link: "/uptime-kuma" },
osticket: { label: "osTicket", link: "/osticket" },
unifi: { label: "UniFi", link: "/network/devices" },
npm: { label: "Nginx Proxy Manager", link: "/proxy-hosts" },
homeassistant: { label: "Home Assistant", link: "/home-assistant" },
secrets: { label: "Secrets", link: "/secrets" },
domains: { label: "Domains", link: "/dns?view=domains" },
endpoint: { label: "Endpoint", link: "/endpoints" },
@@ -515,6 +520,67 @@ export async function collectAlerts(): Promise<AlertsReport> {
}
}),
// Certificates in use by Nginx Proxy Manager that are running out, and hosts it could not set up.
check("Nginx Proxy Manager", async () => {
for (const i of await npmIntegrationIds()) {
const read = await readNpm(i.id);
if (!read.snapshot) {
cannotCheck(`Nginx Proxy Manager “${i.name}”`, read.error ?? "couldn't be read", "Nginx Proxy Manager");
continue;
}
for (const p of npmProblems(i.id, i.name, read.snapshot, new Date())) {
push({ category: p.kind === "certificate" ? "expiry" : "monitoring", severity: p.severity, type: "npm", message: p.message, key: p.key, link: "/proxy-hosts", silenced: silencedIntegration(i.id), target: integrationTarget(i.id) });
}
}
}),
// What was chosen to watch in Home Assistant: an entity that has said nothing for a while, or a number past its limit.
check("Home Assistant", async () => {
const statuses = await haSyncStatuses();
const nowMs = Date.now();
const watched = await db.select().from(haWatched);
if (watched.length === 0) return;
const entities = await db.select().from(haEntities);
for (const i of intRows.filter((r) => r.type === "homeassistant" && r.enabled)) {
const mine = watched.filter((w) => w.integrationId === i.id);
if (mine.length === 0) continue;
if (!isHaFresh(statuses[i.id], nowMs)) {
cannotCheck(`Home Assistant “${i.name}”`, statuses[i.id]?.error ?? "hasn't been read yet", "Home Assistant");
continue;
}
const rows = new Map(entities.filter((e) => e.integrationId === i.id).map((e) => [e.entityId, e]));
for (const p of haProblems(i.id, i.name, mine, rows, new Date(nowMs))) {
push({ category: "monitoring", severity: p.severity, type: "homeassistant", message: p.message, key: p.key, link: "/home-assistant", silenced: silencedIntegration(i.id), target: integrationTarget(i.id) });
}
}
}),
// Not a notification either: an operating system whose support has ended (or is about to) is seen here, with the date, from what the agent says it runs.
check("Operating system support", async () => {
const { healthChecks } = await getSettings();
if (!(healthChecks.osSupportWarnDays > 0)) return;
const now = new Date();
for (const s of await db.select({ id: servers.id, name: servers.name, osId: servers.osId, osName: servers.osName, osVersion: servers.osVersion }).from(servers)) {
if (!s.osId) continue;
const life = lifecycleOf({ id: s.osId, name: s.osName, version: s.osVersion });
const left = life ? daysOfSupportLeft(life.endsOn, now) : null;
if (!life || life.endsOn === null || left === null || left > healthChecks.osSupportWarnDays) continue;
const ended = left < 0;
push({
category: "updates",
severity: ended ? "warning" : "info",
type: "server",
message: ended
? `${s.name} runs ${life.product}, whose support ended on ${life.endsOn} — it no longer gets security fixes`
: `${s.name} runs ${life.product}, whose support ends on ${life.endsOn} (in ${plural(left, "day")})`,
key: `os-support:${s.id}`,
link: `/servers/${s.id}`,
silenced: subjects.has(`server:${s.id}`),
target: targetOf("server", s.id),
});
}
}),
check("Integration failures", async () => {
const { notifications } = await getSettings();
const failing = getFailingSources();
+2
View File
@@ -40,6 +40,8 @@ const KIND_OF_CATEGORY: Record<string, ChangeKind> = {
share_link: "settings",
metrics_token: "settings",
api_token: "settings",
todo: "other",
note: "servers",
webhook: "settings",
integration: "integrations",
dns: "integrations",
@@ -0,0 +1,133 @@
/**
* Suggested dependencies: things the app already knows enough about to offer "this needs that" for, so the map fills in with a click instead of typing.
*
* - **An integration that runs on a server.** An integration is reached at an address; when that address is one of a server's own (its IP, its hostname, or its
* name), the service almost certainly runs on that server — Gitea at git.lab on the server that answers to it — so when that server is down, so is the service.
* (A server it runs on that is not in the app, or a service in the cloud, matches nothing and is not offered.)
*
* Network links (which switch a device or a server is plugged into) and which Proxmox node a guest runs on are not offered: they are already part of the map
* by themselves. It only reads what is stored, asks nobody, leaves out what is already so (or implied, or would make a loop), and remembers what somebody said
* no to — like the suggested checks.
*/
import { db } from "../db/client.js";
import { integrations, servers } from "../db/schema.js";
import { matchServerForTarget, type MatchableServer } from "./uptimeKumaMatch.js";
import { loadGraph, nodeKey, wouldLoop, type Edges } from "./dependencies.js";
import { getInternalFlag, setInternalFlag } from "./settingsStore.js";
export interface DependencySuggestion {
/** Stable, so a dismissed suggestion stays dismissed: "integration:3>server:7". */
key: string;
from: { type: "integration"; id: number; name: string };
to: { type: "server"; id: number; name: string };
/** Why it is offered, in a sentence. */
reason: string;
}
export interface IntegrationFacts {
id: number;
name: string;
type: string;
enabled: boolean;
/** The address it is reached at: a URL's host, or a database host. */
host: string | null;
}
const TYPE_WORDS: Record<string, string> = {
gitea: "Gitea",
semaphore: "Semaphore",
dockhand: "Docker (Dockhand)",
proxmox: "Proxmox",
synology: "Synology",
uptimekuma: "Uptime Kuma",
phpipam: "phpIPAM",
pbs: "Proxmox Backup Server",
osticket: "osTicket",
unifi: "the UniFi controller",
homeassistant: "Home Assistant",
npm: "Nginx Proxy Manager",
};
/** The host an integration is reached at, from the address it was set up with (or its database host, for osTicket). */
export function hostOfIntegration(baseUrl: string | null, config: string | null): string | null {
try {
const c = config ? (JSON.parse(config) as Record<string, unknown>) : {};
if (typeof c.host === "string" && c.host.trim()) return c.host.trim();
} catch {
// a config that is not JSON says nothing
}
if (!baseUrl) return null;
try {
return new URL(baseUrl).hostname || null;
} catch {
return null;
}
}
/** Pure: the dependencies worth offering, from what is known. */
export function suggestDependencies(input: { integrations: IntegrationFacts[]; servers: MatchableServer[]; edges: Edges; dismissed: Set<string> }): { suggestions: DependencySuggestion[]; dismissed: DependencySuggestion[] } {
const all: DependencySuggestion[] = [];
for (const i of input.integrations) {
if (!i.enabled || !i.host) continue;
const server = matchServerForTarget(i.host, input.servers);
if (!server) continue;
const from = nodeKey("integration", i.id);
const to = nodeKey("server", server.id);
if (input.edges.get(from)?.includes(to)) continue;
if (wouldLoop(input.edges, from, to)) continue;
all.push({
key: `${from}>${to}`,
from: { type: "integration", id: i.id, name: i.name },
to: { type: "server", id: server.id, name: server.name },
reason: `${TYPE_WORDS[i.type] ?? i.name} is reached at ${i.host}, which is ${server.name}'s own address — so it most likely runs there, and is down when ${server.name} is.`,
});
}
all.sort((a, b) => a.from.name.localeCompare(b.from.name) || a.to.name.localeCompare(b.to.name));
return { suggestions: all.filter((s) => !input.dismissed.has(s.key)), dismissed: all.filter((s) => input.dismissed.has(s.key)) };
}
// ─── What was said no to ───────────────────────────────────────────────────
const DISMISSED_FLAG = "dependencySuggestionsDismissed";
const MAX_DISMISSED = 500;
export async function loadDismissedDependencies(): Promise<Set<string>> {
const raw = await getInternalFlag(DISMISSED_FLAG);
if (!raw) return new Set();
try {
const v = JSON.parse(raw);
return new Set(Array.isArray(v) ? v.filter((k): k is string => typeof k === "string") : []);
} catch {
return new Set();
}
}
export async function saveDismissedDependencies(keys: Set<string>): Promise<void> {
await setInternalFlag(DISMISSED_FLAG, JSON.stringify([...keys].slice(-MAX_DISMISSED)));
}
// ─── Reading what is stored ────────────────────────────────────────────────
export async function loadDependencySuggestions() {
const [intRows, serverRows, graph, dismissed] = await Promise.all([
db.select({ id: integrations.id, name: integrations.name, type: integrations.type, enabled: integrations.enabled, baseUrl: integrations.baseUrl, config: integrations.config }).from(integrations),
db.select({ id: servers.id, name: servers.name, hostname: servers.hostname, ipAddresses: servers.ipAddresses }).from(servers),
loadGraph(),
loadDismissedDependencies(),
]);
return suggestDependencies({
integrations: intRows.map((i) => ({ id: i.id, name: i.name, type: i.type, enabled: i.enabled, host: hostOfIntegration(i.baseUrl, i.config) })),
servers: serverRows.map((s) => {
let ips: string[] = [];
try {
const v = s.ipAddresses ? JSON.parse(s.ipAddresses) : [];
ips = Array.isArray(v) ? v.filter((x): x is string => typeof x === "string") : [];
} catch {
// none
}
return { id: s.id, name: s.name, hostname: s.hostname, ips };
}),
edges: graph.edges,
dismissed,
});
}
@@ -3,6 +3,8 @@
*
* - A web port a server's agent sees open (80, 443, 8080, 8443), with a name that points at it in DNS if there is one — "git.example.lan answers on
* 443 on nas" — as a web check, linked to that server so its maintenance silences the alerts.
* - A host Nginx Proxy Manager serves ("wiki.example.com" forwarding to a server the app knows), as a web check on the name people really use, linked to the
* server it forwards to.
* - A secret that already has a host to read the certificate from, as a certificate check: the secret only warns about the expiry, this also hears
* when the service stops answering.
*
@@ -12,6 +14,8 @@
import { db } from "../db/client.js";
import { dnsRecordsCache, endpointChecks, secrets, servers } from "../db/schema.js";
import { isLoopback } from "./agentPorts.js";
import { npmIntegrationIds, readNpm } from "./npmReader.js";
import { matchServerForTarget } from "./uptimeKumaMatch.js";
import { getInternalFlag, setInternalFlag } from "./settingsStore.js";
export interface Suggestion {
@@ -33,6 +37,8 @@ export interface SuggestionInput {
secrets: { name: string; checkHost: string | null; checkPort: number | null }[];
existing: { kind: string; target: string; port: number | null }[];
dismissed: Set<string>;
/** Hosts an Nginx Proxy Manager serves, with the server they forward to when the app knows it. */
proxied?: { domain: string; secure: boolean; serverId: number | null; serverName: string | null; integrationName: string }[];
}
/** The web ports worth offering, and what they speak. */
@@ -112,6 +118,25 @@ export function suggestChecks(input: SuggestionInput): { suggestions: Suggestion
}
}
for (const p of input.proxied ?? []) {
const host = cleanHost(p.domain);
if (!host.includes(".") || host.startsWith("*") || /s/.test(host) || isIPv4(host)) continue;
const port = p.secure ? 443 : 80;
if (covered.has(`web:${host}:${port}`)) continue;
const key = `http|${p.secure ? "https" : "http"}://${host}/`;
if (all.has(key)) continue;
all.set(key, {
key,
kind: "http",
name: host,
target: `${p.secure ? "https" : "http"}://${host}/`,
port: null,
serverId: p.serverId,
serverName: p.serverName,
reason: `${p.integrationName} serves ${host}${p.serverName ? `, forwarding to ${p.serverName}` : ""}.`,
});
}
for (const sec of input.secrets) {
if (!sec.checkHost || sec.checkPort === null) continue;
const host = cleanHost(sec.checkHost);
@@ -176,7 +201,19 @@ export async function loadSuggestions(): Promise<{ suggestions: Suggestion[]; di
db.select({ kind: endpointChecks.kind, target: endpointChecks.target, port: endpointChecks.port }).from(endpointChecks),
loadDismissed(),
]);
// What Nginx Proxy Manager serves (a read is kept for a minute, and an NPM that can't be reached just adds nothing).
const matchable = serverRows.map((s) => ({ id: s.id, name: s.name, hostname: s.hostname, ips: jsonArray<string>(s.ipAddresses).filter((i) => typeof i === "string") }));
const proxied: NonNullable<SuggestionInput["proxied"]> = [];
for (const i of await npmIntegrationIds()) {
const read = await readNpm(i.id);
for (const h of read.snapshot?.hosts ?? []) {
if (!h.enabled) continue;
const server = matchServerForTarget(h.forwardHost, matchable);
for (const domain of h.domains) proxied.push({ domain, secure: h.sslForced || h.certificate !== null, serverId: server?.id ?? null, serverName: server?.name ?? null, integrationName: i.name });
}
}
return suggestChecks({
proxied,
servers: serverRows.map((s) => ({ id: s.id, name: s.name, hostname: s.hostname, ips: jsonArray<string>(s.ipAddresses).filter((i) => typeof i === "string"), ports: jsonArray<{ protocol?: string; port: number; address?: string }>(s.listeningPorts).filter((p) => p && Number.isInteger(p.port)) })),
dns: dnsRows,
secrets: secretRows,
+182
View File
@@ -0,0 +1,182 @@
/**
* Keeps what was chosen to watch in each Home Assistant up to date: every few minutes the entities are read (one call lists them all), the watched ones are
* stored with their state, and a numeric one adds a point to its history. The page, the chart and the alerts are built from what is stored — so none of them talks to
* Home Assistant — and a Home Assistant that is down costs only how fresh they are.
*
* What makes an alert: a watched entity that has been "unavailable" or "unknown" for a while, or a number past a limit somebody set (a UPS battery under 30, a
* freezer over -10).
*/
import { and, desc, eq, lt } from "drizzle-orm";
import { db } from "../db/client.js";
import { haEntities, haReadings, haWatched, integrations } from "../db/schema.js";
import { loadIntegrationConfig } from "../integrations/loadIntegration.js";
import { createHomeAssistantAdapter, type HaEntity } from "../integrations/homeassistant/adapter.js";
import { getInternalFlag, setInternalFlag } from "./settingsStore.js";
export const SYNC_INTERVAL_MS = 5 * 60_000;
/** The stored states are only as good as the last good pass: after this long, alerts for them are held rather than judged. */
export const STALE_AFTER_MS = 20 * 60_000;
/** An entity must have said nothing for this long before it is an alert. */
export const UNAVAILABLE_MINUTES = 15;
export const READING_KEEP_DAYS = 35;
/** Two passes closer than this add one point to a history, not two. */
const MIN_READING_GAP_MS = 2 * 60_000;
const STATUS_FLAG = "homeAssistantSync";
export interface HaSyncStatus {
at: string;
ok: boolean;
error: string | null;
entities: number;
}
async function readStatuses(): Promise<Record<string, HaSyncStatus>> {
try {
const raw = await getInternalFlag(STATUS_FLAG);
return raw ? (JSON.parse(raw) as Record<string, HaSyncStatus>) : {};
} catch {
return {};
}
}
export async function haSyncStatuses(): Promise<Record<number, HaSyncStatus>> {
return Object.fromEntries(Object.entries(await readStatuses()).map(([k, v]) => [Number(k), v]));
}
export function isHaFresh(status: HaSyncStatus | undefined, nowMs: number): boolean {
return !!status && status.ok && nowMs - Date.parse(status.at) <= STALE_AFTER_MS;
}
/** Pure: when an entity first stopped saying anything, kept until it does. */
export function trackUnavailable(previous: string | null | undefined, unavailable: boolean, nowIso: string): string | null {
return unavailable ? (previous ?? nowIso) : null;
}
/** Stores one read of one Home Assistant: the watched entities' states and, for the numeric ones, a reading. */
export async function storeEntities(integrationId: number, entities: HaEntity[], now: Date = new Date()): Promise<number> {
const nowIso = now.toISOString();
const watched = await db.select().from(haWatched).where(eq(haWatched.integrationId, integrationId));
if (watched.length === 0) return 0;
const byId = new Map(entities.map((e) => [e.entityId, e]));
const before = new Map((await db.select().from(haEntities).where(eq(haEntities.integrationId, integrationId))).map((r) => [r.entityId, r]));
for (const w of watched) {
const e = byId.get(w.entityId);
// An entity that is no longer in Home Assistant at all is as good as unavailable.
const state = e?.state ?? "missing";
const unavailable = e ? e.unavailable : true;
const values = {
friendlyName: e?.name ?? null,
state,
value: e?.value ?? null,
unit: e?.unit ?? null,
deviceClass: e?.deviceClass ?? null,
lastChanged: e?.lastChanged ?? null,
unavailableSince: trackUnavailable(before.get(w.entityId)?.unavailableSince, unavailable, nowIso),
updatedAt: nowIso,
};
await db.insert(haEntities).values({ integrationId, entityId: w.entityId, ...values }).onConflictDoUpdate({ target: [haEntities.integrationId, haEntities.entityId], set: values });
if (e?.value !== null && e?.value !== undefined && !unavailable) {
const [newest] = await db.select({ at: haReadings.at }).from(haReadings).where(and(eq(haReadings.integrationId, integrationId), eq(haReadings.entityId, w.entityId))).orderBy(desc(haReadings.at)).limit(1);
if (!newest || now.getTime() - Date.parse(newest.at) >= MIN_READING_GAP_MS) await db.insert(haReadings).values({ integrationId, entityId: w.entityId, at: nowIso, value: e.value });
}
}
// What is no longer watched is no longer kept.
const watchedIds = watched.map((w) => w.entityId);
for (const id of before.keys()) if (!watchedIds.includes(id)) await db.delete(haEntities).where(and(eq(haEntities.integrationId, integrationId), eq(haEntities.entityId, id)));
await db.delete(haReadings).where(and(eq(haReadings.integrationId, integrationId), lt(haReadings.at, new Date(now.getTime() - READING_KEEP_DAYS * 86_400_000).toISOString())));
return watched.length;
}
/** Reads one Home Assistant and stores what is watched. Never throws: how it went is the answer, and what was stored is left alone when it failed. */
export async function syncHomeAssistant(integrationId: number, now: Date = new Date()): Promise<HaSyncStatus> {
let status: HaSyncStatus;
try {
const loaded = await loadIntegrationConfig(integrationId);
if (!loaded || loaded.integration.type !== "homeassistant") throw new Error("not a Home Assistant integration");
const entities = await createHomeAssistantAdapter(loaded.config as never).listEntities();
const count = await storeEntities(integrationId, entities, now);
status = { at: now.toISOString(), ok: true, error: null, entities: count };
} catch (err) {
const previous = (await readStatuses())[String(integrationId)];
status = { at: previous?.at ?? now.toISOString(), ok: false, error: err instanceof Error ? err.message : String(err), entities: previous?.entities ?? 0 };
console.error(`[home-assistant] couldn't read integration ${integrationId}:`, status.error);
}
try {
const all = await readStatuses();
all[String(integrationId)] = status;
await setInternalFlag(STATUS_FLAG, JSON.stringify(all));
} catch (err) {
console.error("[home-assistant] couldn't record the result:", err);
}
return status;
}
export async function syncAllHomeAssistants(now: Date = new Date()): Promise<Record<number, HaSyncStatus>> {
const rows = await db.select({ id: integrations.id }).from(integrations).where(and(eq(integrations.type, "homeassistant"), eq(integrations.enabled, true)));
const out: Record<number, HaSyncStatus> = {};
for (const r of rows) out[r.id] = await syncHomeAssistant(r.id, now);
return out;
}
/** Forgets everything of one integration when it is removed (and its status). */
export async function removeHomeAssistantData(integrationId: number): Promise<void> {
await db.delete(haReadings).where(eq(haReadings.integrationId, integrationId));
await db.delete(haEntities).where(eq(haEntities.integrationId, integrationId));
await db.delete(haWatched).where(eq(haWatched.integrationId, integrationId));
const all = await readStatuses();
if (String(integrationId) in all) {
delete all[String(integrationId)];
await setInternalFlag(STATUS_FLAG, JSON.stringify(all));
}
}
export interface HaProblem {
key: string;
kind: "unavailable" | "limit";
severity: "critical" | "warning";
message: string;
}
const durationWords = (ms: number) => {
const m = Math.floor(ms / 60_000);
return m < 60 ? `${m} min` : m < 48 * 60 ? `${Math.floor(m / 60)} h` : `${Math.floor(m / 1440)} days`;
};
/** Pure: what is wrong with the watched entities, from what is stored. */
export function haProblems(
integrationId: number,
integrationName: string,
watched: { entityId: string; name: string | null; above: number | null; below: number | null }[],
entities: Map<string, { friendlyName: string | null; state: string; value: number | null; unit: string | null; unavailableSince: string | null }>,
now: Date,
): HaProblem[] {
const out: HaProblem[] = [];
for (const w of watched) {
const e = entities.get(w.entityId);
if (!e) continue;
const name = w.name ?? e.friendlyName ?? w.entityId;
if (e.unavailableSince !== null) {
const ms = now.getTime() - Date.parse(e.unavailableSince);
if (ms >= UNAVAILABLE_MINUTES * 60_000) {
out.push({ key: `ha-unavailable:${integrationId}:${w.entityId}`, kind: "unavailable", severity: "warning", message: `${name} has been ${e.state === "missing" ? "missing from" : e.state} in Home Assistant for ${durationWords(ms)} [${integrationName}]` });
}
continue;
}
if (e.value === null) continue;
const unit = e.unit ? ` ${e.unit}` : "";
if (w.above !== null && e.value > w.above) out.push({ key: `ha-limit:${integrationId}:${w.entityId}`, kind: "limit", severity: "warning", message: `${name} is ${e.value}${unit}, above its limit of ${w.above}${unit} [${integrationName}]` });
else if (w.below !== null && e.value < w.below) out.push({ key: `ha-limit:${integrationId}:${w.entityId}`, kind: "limit", severity: "warning", message: `${name} is ${e.value}${unit}, below its limit of ${w.below}${unit} [${integrationName}]` });
}
return out;
}
let timer: ReturnType<typeof setInterval> | null = null;
export function initHomeAssistantSync(): void {
if (timer) return;
const pass = () => void syncAllHomeAssistants().catch((err) => console.error("[home-assistant] pass failed:", err));
timer = setInterval(pass, SYNC_INTERVAL_MS);
setTimeout(pass, 7_000);
console.log(`[home-assistant] reading watched entities every ${SYNC_INTERVAL_MS / 60000} minutes`);
}
+19 -2
View File
@@ -11,11 +11,14 @@ import { servers, updateRuns } from "../db/schema.js";
import { getAlerts } from "./alerts.js";
import { loadAcks } from "./alertAcks.js";
import { describeTarget, listActiveWindows } from "./maintenance.js";
import { openTodosFor } from "./todos.js";
/** An acknowledgement that runs out within this long is flagged. */
export const ACK_ENDS_SOON_MS = 24 * 3600_000;
/** A maintenance window that ends within this long is flagged. */
export const WINDOW_ENDS_SOON_MS = 2 * 3600_000;
/** A job handed to somebody is flagged from this many days before it is due (and once it is past). */
export const TODO_DUE_SOON_DAYS = 2;
/** A finished update run stays on the list this long, so the result is seen. */
export const RUN_KEPT_MS = 24 * 3600_000;
@@ -44,6 +47,8 @@ export interface MyWork {
}[];
windows: { id: number; targetType: string; targetId: number; targetName: string; targetKind: string; reason: string | null; startedAt: string; endsAt: string; endsSoon: boolean }[];
updateRuns: { id: number; serverId: number; serverName: string; templateName: string; status: string; message: string | null; startedAt: string; finishedAt: string | null; running: boolean; updatesBefore: number | null; updatesAfter: number | null; rebootRequired: boolean }[];
/** Open jobs from the to-do list handed to this person, the soonest due first. */
todos: { id: number; title: string; detail: string | null; dueDate: string | null; overdue: boolean; dueSoon: boolean; target: { type: string; id: number; name: string } | null }[];
/** How many of the above want a look: acknowledgements and windows that end soon, alerts handed to them, update runs still going. */
needsAttention: number;
}
@@ -111,6 +116,18 @@ export async function buildMyWork(user: MyWorkUser, now: Date = new Date()): Pro
}
runs.sort((a, b) => Number(b.running) - Number(a.running) || b.startedAt.localeCompare(a.startedAt));
const needsAttention = alerts.filter((a) => a.role === "handed" || a.endsSoon).length + windows.filter((w) => w.endsSoon).length + runs.filter((r) => r.running).length;
return { alerts, windows, updateRuns: runs, needsAttention };
const today = now.toISOString().slice(0, 10);
const soon = new Date(nowMs + TODO_DUE_SOON_DAYS * 86_400_000).toISOString().slice(0, 10);
const jobs: MyWork["todos"] = (await openTodosFor(user.id)).map((t) => ({
id: t.id,
title: t.title,
detail: t.detail,
dueDate: t.dueDate,
overdue: t.dueDate !== null && t.dueDate < today,
dueSoon: t.dueDate !== null && t.dueDate >= today && t.dueDate <= soon,
target: t.target,
}));
const needsAttention = alerts.filter((a) => a.role === "handed" || a.endsSoon).length + windows.filter((w) => w.endsSoon).length + runs.filter((r) => r.running).length + jobs.filter((j) => j.overdue || j.dueSoon).length;
return { alerts, windows, updateRuns: runs, todos: jobs, needsAttention };
}
+2
View File
@@ -607,6 +607,8 @@ const SOURCE_LABELS: Record<string, string> = {
pbs: "Proxmox Backup Server",
osticket: "osTicket",
unifi: "UniFi",
npm: "Nginx Proxy Manager",
homeassistant: "Home Assistant",
};
export function sourceLabel(source: string): string {
+82
View File
@@ -0,0 +1,82 @@
/**
* Reads Nginx Proxy Manager for the page, the alerts and the suggested checks, and works out what is wrong: a certificate in use that is about to run out (or has),
* and a host NPM itself says it could not set up. A read is kept for a minute, so the page, the alerts and the suggestions together cost one call, and a failed read is
* kept too (briefly), so an unreachable NPM is not asked again by every one of them.
*/
import { and, eq } from "drizzle-orm";
import { db } from "../db/client.js";
import { integrations } from "../db/schema.js";
import { loadIntegrationConfig } from "../integrations/loadIntegration.js";
import { createNpmAdapter, type NpmProxyHost, type NpmSnapshot } from "../integrations/npm/adapter.js";
const CACHE_MS = 60_000;
/** A certificate is raised from this many days before it ends; critical in the last three. */
export const CERT_WARN_DAYS = 14;
export const CERT_CRITICAL_DAYS = 3;
interface Entry {
at: number;
snapshot: NpmSnapshot | null;
error: string | null;
}
const cache = new Map<number, Entry>();
export const clearNpmCacheForTests = () => cache.clear();
/** What NPM says now (a minute old at most), or why it could not be read. */
export async function readNpm(integrationId: number, force = false): Promise<{ snapshot: NpmSnapshot | null; error: string | null; at: string }> {
const hit = cache.get(integrationId);
if (hit && !force && Date.now() - hit.at < CACHE_MS) return { snapshot: hit.snapshot, error: hit.error, at: new Date(hit.at).toISOString() };
let entry: Entry;
try {
const loaded = await loadIntegrationConfig(integrationId);
if (!loaded || loaded.integration.type !== "npm") throw new Error("not an Nginx Proxy Manager integration");
entry = { at: Date.now(), snapshot: await createNpmAdapter(loaded.config as never).fetchSnapshot(), error: null };
} catch (err) {
entry = { at: Date.now(), snapshot: null, error: err instanceof Error ? err.message : String(err) };
}
cache.set(integrationId, entry);
return { snapshot: entry.snapshot, error: entry.error, at: new Date(entry.at).toISOString() };
}
export async function npmIntegrationIds(): Promise<{ id: number; name: string }[]> {
return db.select({ id: integrations.id, name: integrations.name }).from(integrations).where(and(eq(integrations.type, "npm"), eq(integrations.enabled, true)));
}
export interface NpmProblem {
key: string;
kind: "certificate" | "host";
severity: "critical" | "warning";
message: string;
}
const DAY_MS = 86_400_000;
/** Pure: what is wrong, from one read. Only certificates that an enabled host uses count; a host that is switched off is not a problem. */
export function npmProblems(integrationId: number, integrationName: string, snapshot: NpmSnapshot, now: Date): NpmProblem[] {
const out: NpmProblem[] = [];
const inUse = new Map<number, NpmProxyHost[]>();
for (const h of snapshot.hosts) {
if (!h.enabled || !h.certificate) continue;
inUse.set(h.certificate.id, [...(inUse.get(h.certificate.id) ?? []), h]);
}
for (const cert of snapshot.certificates) {
const users = inUse.get(cert.id);
if (!users || !cert.expiresAt) continue;
const days = Math.floor((Date.parse(cert.expiresAt) - now.getTime()) / DAY_MS);
if (days > CERT_WARN_DAYS) continue;
const names = users.flatMap((h) => h.domains).slice(0, 3).join(", ");
// The certificate's own name is only worth saying when it isn't one of the names it is used for.
const called = users.some((h) => h.domains.includes(cert.name)) ? "" : ` (${cert.name})`;
out.push({
key: `npm-cert:${integrationId}:${cert.id}`,
kind: "certificate",
severity: days <= CERT_CRITICAL_DAYS ? "critical" : "warning",
message: days < 0 ? `The certificate for ${names}${called} expired ${-days} ${days === -1 ? "day" : "days"} ago [${integrationName}]` : `The certificate for ${names}${called} expires in ${days} ${days === 1 ? "day" : "days"} [${integrationName}]`,
});
}
for (const h of snapshot.hosts) {
if (!h.enabled || h.online) continue;
out.push({ key: `npm-host:${integrationId}:${h.id}`, kind: "host", severity: "warning", message: `${h.domains[0] ?? `Proxy host ${h.id}`} is not working in Nginx Proxy Manager${h.error ? `: ${h.error.slice(0, 160)}` : ""} [${integrationName}]` });
}
return out;
}
+212
View File
@@ -0,0 +1,212 @@
/**
* When the operating system a server runs stops being supported. An agent says which one it runs (the distribution's id and release; for Windows, the product and
* build); this knows the vendors' published end-of-support dates for the common ones, and says how long a release has left — so "Ubuntu 20.04 ended in May" is
* on the Alerts page before a security fix is missed, the way a disk forecast is.
*
* The dates are the end of the vendor's standard security support (for Debian, including its long-term support; Proxmox VE's are the project's own announced
* ones). The table is only as new as this version of the app: a release it does not know has no date, and is never called unsupported — it is simply not
* judged. Rolling distributions (Arch, Gentoo, NixOS unstable) have no end and are not in it.
*/
export interface OsFacts {
id?: string | null;
name?: string | null;
version?: string | null;
}
export interface Lifecycle {
/** What to call it: "Ubuntu 22.04 LTS". */
product: string;
/** YYYY-MM-DD the support ends, or null when this release is not in the table. */
endsOn: string | null;
}
interface Row {
product: string;
ends: string;
}
// Keyed by family, then by the release as it is looked up (see releaseKey).
const TABLE: Record<string, Record<string, Row>> = {
ubuntu: {
"16.04": { product: "Ubuntu 16.04 LTS", ends: "2021-04-30" },
"18.04": { product: "Ubuntu 18.04 LTS", ends: "2023-05-31" },
"20.04": { product: "Ubuntu 20.04 LTS", ends: "2025-05-31" },
"22.04": { product: "Ubuntu 22.04 LTS", ends: "2027-04-30" },
"24.04": { product: "Ubuntu 24.04 LTS", ends: "2029-04-30" },
"26.04": { product: "Ubuntu 26.04 LTS", ends: "2031-04-30" },
"22.10": { product: "Ubuntu 22.10", ends: "2023-07-20" },
"23.04": { product: "Ubuntu 23.04", ends: "2024-01-25" },
"23.10": { product: "Ubuntu 23.10", ends: "2024-07-11" },
"24.10": { product: "Ubuntu 24.10", ends: "2025-07-10" },
"25.04": { product: "Ubuntu 25.04", ends: "2026-01-15" },
"25.10": { product: "Ubuntu 25.10", ends: "2026-07-09" },
},
debian: {
"9": { product: "Debian 9 (stretch)", ends: "2022-06-30" },
"10": { product: "Debian 10 (buster)", ends: "2024-06-30" },
"11": { product: "Debian 11 (bullseye)", ends: "2026-08-31" },
"12": { product: "Debian 12 (bookworm)", ends: "2028-06-30" },
"13": { product: "Debian 13 (trixie)", ends: "2030-06-30" },
},
"centos-classic": {
"7": { product: "CentOS 7", ends: "2024-06-30" },
"8": { product: "CentOS 8", ends: "2021-12-31" },
},
"centos-stream": {
"8": { product: "CentOS Stream 8", ends: "2024-05-31" },
"9": { product: "CentOS Stream 9", ends: "2027-05-31" },
"10": { product: "CentOS Stream 10", ends: "2030-01-01" },
},
rhel: {
"7": { product: "Red Hat Enterprise Linux 7", ends: "2024-06-30" },
"8": { product: "Red Hat Enterprise Linux 8", ends: "2029-05-31" },
"9": { product: "Red Hat Enterprise Linux 9", ends: "2032-05-31" },
"10": { product: "Red Hat Enterprise Linux 10", ends: "2035-05-31" },
},
rocky: {
"8": { product: "Rocky Linux 8", ends: "2029-05-31" },
"9": { product: "Rocky Linux 9", ends: "2032-05-31" },
"10": { product: "Rocky Linux 10", ends: "2035-05-31" },
},
almalinux: {
"8": { product: "AlmaLinux 8", ends: "2029-05-31" },
"9": { product: "AlmaLinux 9", ends: "2032-05-31" },
"10": { product: "AlmaLinux 10", ends: "2035-05-31" },
},
fedora: {
"38": { product: "Fedora 38", ends: "2024-05-21" },
"39": { product: "Fedora 39", ends: "2024-11-26" },
"40": { product: "Fedora 40", ends: "2025-05-13" },
"41": { product: "Fedora 41", ends: "2025-12-15" },
"42": { product: "Fedora 42", ends: "2026-05-13" },
"43": { product: "Fedora 43", ends: "2026-12-02" },
},
alpine: {
"3.17": { product: "Alpine 3.17", ends: "2024-11-22" },
"3.18": { product: "Alpine 3.18", ends: "2025-05-09" },
"3.19": { product: "Alpine 3.19", ends: "2025-11-01" },
"3.20": { product: "Alpine 3.20", ends: "2026-04-01" },
"3.21": { product: "Alpine 3.21", ends: "2026-11-01" },
"3.22": { product: "Alpine 3.22", ends: "2027-05-01" },
},
"opensuse-leap": {
"15.3": { product: "openSUSE Leap 15.3", ends: "2022-12-31" },
"15.4": { product: "openSUSE Leap 15.4", ends: "2023-12-07" },
"15.5": { product: "openSUSE Leap 15.5", ends: "2024-12-31" },
"15.6": { product: "openSUSE Leap 15.6", ends: "2025-12-31" },
},
"proxmox-ve": {
"6": { product: "Proxmox VE 6", ends: "2022-07-31" },
"7": { product: "Proxmox VE 7", ends: "2024-07-31" },
"8": { product: "Proxmox VE 8", ends: "2026-08-31" },
},
// Windows is told apart by its build number, and a client from a server by the product's name (Windows 11 24H2 and Windows Server 2025 share a build).
"windows-client": {
"19045": { product: "Windows 10 22H2", ends: "2025-10-14" },
"22000": { product: "Windows 11 21H2", ends: "2023-10-10" },
"22621": { product: "Windows 11 22H2", ends: "2024-10-08" },
"22631": { product: "Windows 11 23H2", ends: "2025-11-11" },
"26100": { product: "Windows 11 24H2", ends: "2026-10-13" },
"26200": { product: "Windows 11 25H2", ends: "2027-10-12" },
},
"windows-server": {
"9600": { product: "Windows Server 2012 R2", ends: "2023-10-10" },
"14393": { product: "Windows Server 2016", ends: "2027-01-12" },
"17763": { product: "Windows Server 2019", ends: "2029-01-09" },
"20348": { product: "Windows Server 2022", ends: "2031-10-14" },
"26100": { product: "Windows Server 2025", ends: "2034-11-14" },
},
};
/** Which family of the table an agent's answer belongs to, and which release within it; null when it is none this knows. */
export function releaseKey(os: OsFacts): { family: string; key: string } | null {
const id = (os.id ?? "").trim().toLowerCase();
const version = (os.version ?? "").trim();
if (!id || !version) return null;
const name = (os.name ?? "").toLowerCase();
switch (id) {
case "ubuntu":
return { family: "ubuntu", key: version.match(/^\d+\.\d+/)?.[0] ?? version };
case "debian":
case "raspbian":
return { family: "debian", key: version.match(/^\d+/)?.[0] ?? version };
case "centos":
return { family: name.includes("stream") ? "centos-stream" : "centos-classic", key: version.match(/^\d+/)?.[0] ?? version };
case "rhel":
case "rocky":
case "almalinux":
return { family: id, key: version.match(/^\d+/)?.[0] ?? version };
case "fedora":
return { family: "fedora", key: version.match(/^\d+/)?.[0] ?? version };
case "alpine":
return { family: "alpine", key: version.match(/^\d+\.\d+/)?.[0] ?? version };
case "opensuse-leap":
return { family: "opensuse-leap", key: version.match(/^\d+\.\d+/)?.[0] ?? version };
case "proxmox-ve":
return { family: "proxmox-ve", key: version.match(/^\d+/)?.[0] ?? version };
case "windows":
return { family: name.includes("server") ? "windows-server" : "windows-client", key: version.match(/^\d+/)?.[0] ?? version };
default:
return null;
}
}
/** What the table says about this operating system: its name and when its support ends. Null when the agent's answer is nothing this recognises at all. */
export function lifecycleOf(os: OsFacts): Lifecycle | null {
const found = releaseKey(os);
if (!found) return null;
const row = TABLE[found.family]?.[found.key];
if (row) return { product: row.product, endsOn: row.ends };
// A release not in the table: named as the machine named itself, with no date, so nothing is claimed about it.
const name = (os.name ?? "").trim();
return name ? { product: name, endsOn: null } : null;
}
export interface OsSummary {
id: string;
name: string | null;
version: string | null;
/** What to call it. */
product: string;
endsOn: string | null;
state: SupportState;
/** Days of support left (negative once it has ended); null with no date. */
daysLeft: number | null;
}
/** What a server's page and the read API say about the operating system it runs; null while its agent has not said. */
export function osSummary(row: { osId: string | null; osName: string | null; osVersion: string | null }, now: Date, warnDays: number): OsSummary | null {
if (!row.osId) return null;
const life = lifecycleOf({ id: row.osId, name: row.osName, version: row.osVersion });
const endsOn = life?.endsOn ?? null;
return {
id: row.osId,
name: row.osName,
version: row.osVersion,
product: life?.product ?? row.osName ?? row.osId,
endsOn,
// With the warning switched off the date is still known: "soon" then means the usual three months.
state: supportState(endsOn, now, warnDays > 0 ? warnDays : 90),
daysLeft: daysOfSupportLeft(endsOn, now),
};
}
export type SupportState = "ended" | "soon" | "ok" | "unknown";
const DAY_MS = 86_400_000;
/** Whole days from `now` to the end of the day support ends (negative once it has passed); null with no date. */
export function daysOfSupportLeft(endsOn: string | null, now: Date): number | null {
if (!endsOn) return null;
const end = Date.parse(`${endsOn}T23:59:59Z`);
return Number.isFinite(end) ? Math.floor((end - now.getTime()) / DAY_MS) : null;
}
/** Ended once the day is over; "soon" within `warnDays` of the end; unknown with no date. */
export function supportState(endsOn: string | null, now: Date, warnDays: number): SupportState {
const left = daysOfSupportLeft(endsOn, now);
if (left === null) return "unknown";
if (left < 0) return "ended";
return left <= warnDays ? "soon" : "ok";
}
+7 -3
View File
@@ -15,6 +15,7 @@ import { parsePendingUpdates } from "./pendingUpdates.js";
import { parseTags } from "./serverTags.js";
import { parseThresholds } from "./serverThresholds.js";
import { getSettings } from "./settingsStore.js";
import { osSummary } from "./osLifecycle.js";
import { serverStatus } from "./topology.js";
/** An address inside the app as a full address, for a script that has no idea where the app lives. */
@@ -33,7 +34,7 @@ const json = <T>(text: string | null, fallback: T): T => {
type ServerRow = typeof servers.$inferSelect;
export function serverV1(row: ServerRow, offlineMinutes: number, now: Date) {
export function serverV1(row: ServerRow, offlineMinutes: number, now: Date, osWarnDays = 90) {
const status = serverStatus(row, offlineMinutes, now);
const disks = json<{ mount: string; sizeBytes: number; usedBytes: number }[]>(row.disks, []);
const updates = parsePendingUpdates(row.pendingUpdates);
@@ -53,6 +54,8 @@ export function serverV1(row: ServerRow, offlineMinutes: number, now: Date) {
agentVersion: row.agentVersion ?? null,
runsOnServerId: row.parentServerId ?? null,
hardware: { manufacturer: row.hwManufacturer ?? null, model: row.hwModel ?? null, serial: row.hwSerial ?? null },
// What it runs and when that release stops being supported ("ended", "soon", "ok", "unknown"), once its agent has said.
operatingSystem: osSummary(row, now, osWarnDays),
cpu: { model: row.cpuModel, cores: row.cpuCores, loadPercent: row.cpuLoadPercent },
memory: { totalBytes: row.memTotalBytes, usedBytes: row.memUsedBytes },
disks: disks.map((d) => ({ mount: d.mount, sizeBytes: d.sizeBytes, usedBytes: d.usedBytes, usedPercent: d.sizeBytes > 0 ? Math.round((d.usedBytes / d.sizeBytes) * 1000) / 10 : null })),
@@ -65,13 +68,14 @@ export function serverV1(row: ServerRow, offlineMinutes: number, now: Date) {
export async function serversV1(now: Date = new Date()) {
const [rows, settings] = await Promise.all([db.select().from(servers).orderBy(servers.name), getSettings()]);
return rows.map((r) => serverV1(r, settings.healthChecks.serverOfflineMinutes, now));
return rows.map((r) => serverV1(r, settings.healthChecks.serverOfflineMinutes, now, settings.healthChecks.osSupportWarnDays));
}
export async function oneServerV1(id: number, now: Date = new Date()) {
const [row] = await db.select().from(servers).where(eq(servers.id, id)).limit(1);
if (!row) return null;
return serverV1(row, (await getSettings()).healthChecks.serverOfflineMinutes, now);
const { healthChecks } = await getSettings();
return serverV1(row, healthChecks.serverOfflineMinutes, now, healthChecks.osSupportWarnDays);
}
export async function alertsV1() {
+95
View File
@@ -0,0 +1,95 @@
/**
* The racks, drawn from the inventory: each piece of hardware that says which unit it sits in (U12, or U12-13 for a tall one) and where (the rack's name in its
* location) is placed in its rack, units numbered from the bottom as racks are. Two pieces claiming the same unit are said to clash rather than hidden; hardware
* that names a rack but no unit is listed beside it as not placed.
*/
export interface RackAsset {
id: number;
name: string;
kind: string;
manufacturer: string | null;
model: string | null;
location: string | null;
rackUnit: string | null;
powerWatts: number | null;
serverId: number | null;
}
export interface RackSlot {
assetId: number;
name: string;
kind: string;
model: string | null;
/** The lowest and highest unit it takes, from 1 (the bottom). */
from: number;
to: number;
powerWatts: number | null;
serverId: number | null;
/** Another piece takes one of the same units. */
clash: boolean;
}
export interface Rack {
/** What its pieces call it in their location; "(no rack named)" for pieces that gave a unit but no place. */
name: string;
/** How many units tall to draw it. */
units: number;
slots: RackSlot[];
/** Pieces that name this rack and no unit. */
unplaced: { assetId: number; name: string; kind: string }[];
/** Units taken, and the watts of what stands in it. */
usedUnits: number;
watts: number;
}
/** The units of a text like "U12", "12", "U12-13", "12–14" or "U12 - U14": lowest and highest. Null when it is not a unit. */
export function parseRackUnit(text: string | null | undefined): { from: number; to: number } | null {
if (!text) return null;
const m = /^\s*U?\s*(\d{1,2})\s*(?:[-–—]\s*U?\s*(\d{1,2}))?\s*$/i.exec(text);
if (!m) return null;
const a = Number(m[1]);
const b = m[2] !== undefined ? Number(m[2]) : a;
if (a < 1 || b < 1) return null;
return { from: Math.min(a, b), to: Math.max(a, b) };
}
/** Rack sizes that exist, so a rack is drawn as a real one. */
const SIZES = [12, 18, 22, 24, 27, 42, 45, 47, 48];
const NO_RACK = "(no rack named)";
export function buildRacks(assets: RackAsset[]): Rack[] {
const byRack = new Map<string, RackAsset[]>();
for (const a of assets) {
const place = (a.location ?? "").trim();
const unit = parseRackUnit(a.rackUnit);
if (!place && !unit) continue;
const key = place || NO_RACK;
byRack.set(key, [...(byRack.get(key) ?? []), a]);
}
const racks: Rack[] = [];
for (const [name, members] of byRack) {
const placed = members.filter((a) => parseRackUnit(a.rackUnit));
// A "rack" that is only a room with things in it (no piece says a unit) is not a rack to draw.
if (placed.length === 0) continue;
const taken = new Map<number, number>();
const slots: RackSlot[] = placed.map((a) => {
const u = parseRackUnit(a.rackUnit)!;
for (let n = u.from; n <= u.to; n++) taken.set(n, (taken.get(n) ?? 0) + 1);
return { assetId: a.id, name: a.name, kind: a.kind, model: a.model, from: u.from, to: u.to, powerWatts: a.powerWatts, serverId: a.serverId, clash: false };
});
for (const s of slots) for (let n = s.from; n <= s.to; n++) if ((taken.get(n) ?? 0) > 1) s.clash = true;
slots.sort((x, y) => x.from - y.from || x.name.localeCompare(y.name));
const top = Math.max(...slots.map((s) => s.to));
racks.push({
name,
units: SIZES.find((s) => s >= top) ?? top,
slots,
unplaced: members.filter((a) => !parseRackUnit(a.rackUnit)).map((a) => ({ assetId: a.id, name: a.name, kind: a.kind })).sort((x, y) => x.name.localeCompare(y.name)),
usedUnits: taken.size,
watts: slots.reduce((n, s) => n + (s.powerWatts ?? 0), 0),
});
}
return racks.sort((a, b) => (a.name === NO_RACK ? 1 : 0) - (b.name === NO_RACK ? 1 : 0) || a.name.localeCompare(b.name, undefined, { numeric: true }));
}
+3 -1
View File
@@ -119,6 +119,8 @@ export interface HealthCheckSettings {
flapRaises: number;
/** A network device (switch, access point, gateway) that a UniFi controller has not seen online for this many minutes is raised as offline. */
deviceOfflineMinutes: number;
/** Days before the end of an operating system's support that a server is first flagged on the Alerts page. 0 turns it off. */
osSupportWarnDays: number;
}
/** The monthly report: last month's availability, incidents, updates and renewals, in one message on a day of the month. Off until switched on. */
@@ -289,7 +291,7 @@ const DEFAULTS: AppSettings = {
selfBackup: { enabled: true, time: "03:30", keep: 14 },
profilePictures: { gravatar: false, identityProvider: false },
signIn: { roleFromGroups: false, groupsClaim: "groups", adminGroups: [], operatorGroups: [], otherwise: "viewer" },
healthChecks: { serverOfflineMinutes: 60, diskUsagePercent: 90, domainWarnDays: 30, diskForecastDays: 14, backupStaleHours: 48, restoreTestDays: 0, newPortAlerts: true, flapRaises: 3, deviceOfflineMinutes: 10 },
healthChecks: { serverOfflineMinutes: 60, diskUsagePercent: 90, domainWarnDays: 30, diskForecastDays: 14, backupStaleHours: 48, restoreTestDays: 0, newPortAlerts: true, flapRaises: 3, deviceOfflineMinutes: 10, osSupportWarnDays: 90 },
// 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.
consistency: { excludedRanges: ["172.16.0.0/12"] },
+4
View File
@@ -32,6 +32,8 @@ export interface IncomingSystemInfo {
health?: IncomingHostHealth;
/** What the machine is, from its firmware: each part present only if it says anything. */
hardware?: { manufacturer?: string; model?: string; serial?: string };
/** What the machine runs: the distribution's id and release (for Windows, the product and build). Absent from an agent that predates it. */
os?: { id?: string; name?: string; version?: string } | null;
/** What the agent could not collect; an empty list says nothing was missed. */
diagnostics?: AgentDiagnostic[];
}
@@ -123,6 +125,8 @@ export async function syncServerTasks(serverId: number, report: AgentReport) {
...(system?.updates && before ? nextUpdateState(before, system.updates, now) : {}),
...(system?.health && before ? { hostHealth: nextHostHealth(before.hostHealth, system.health, now) } : {}),
...(system?.diagnostics ? { agentDiagnostics: nextDiagnostics(system.diagnostics, now) } : {}),
// Only stored when it was said, so an older agent doesn't wipe what a newer one told us; the text is bounded, whatever sent it.
...(typeof system?.os?.id === "string" && system.os.id ? { osId: system.os.id.slice(0, 40).toLowerCase(), osName: typeof system.os.name === "string" ? system.os.name.slice(0, 80) : null, osVersion: typeof system.os.version === "string" ? system.os.version.slice(0, 40) : null } : {}),
...(system?.hardware?.manufacturer ? { hwManufacturer: system.hardware.manufacturer } : {}),
...(system?.hardware?.model ? { hwModel: system.hardware.model } : {}),
...(system?.hardware?.serial ? { hwSerial: system.hardware.serial } : {}),
+190
View File
@@ -0,0 +1,190 @@
/**
* The to-do list and notes. A to-do is a small job with a title, maybe a day it is due, a person it is handed to and a server or network device it is about; ticking it
* off keeps it (so what was done is still there to read) and un-ticking brings it back. A note is one free-form text about a server or a device.
*/
import { and, desc, eq, isNull, isNotNull } from "drizzle-orm";
import { db } from "../db/client.js";
import { networkDevices, notes, servers, todos, users } from "../db/schema.js";
export const TARGET_TYPES = ["server", "device"] as const;
export type TargetType = (typeof TARGET_TYPES)[number];
export const MAX_TITLE = 160;
export const MAX_DETAIL = 2000;
export const MAX_NOTE_BODY = 8000;
export class TodoError extends Error {
constructor(
readonly status: number,
readonly code: string,
message: string,
) {
super(message);
}
}
/** A real calendar day, written YYYY-MM-DD. */
export const isRealDay = (v: unknown): v is string => typeof v === "string" && /^\d{4}-\d{2}-\d{2}$/.test(v) && new Date(`${v}T00:00:00Z`).toISOString().startsWith(v);
export async function targetName(type: TargetType, id: number): Promise<string | null> {
if (type === "server") return (await db.select({ name: servers.name }).from(servers).where(eq(servers.id, id)).limit(1))[0]?.name ?? null;
return (await db.select({ name: networkDevices.name }).from(networkDevices).where(eq(networkDevices.id, id)).limit(1))[0]?.name ?? null;
}
export interface TodoItem {
id: number;
title: string;
detail: string | null;
dueDate: string | null;
assignee: { id: number | null; name: string } | null;
target: { type: TargetType; id: number; name: string } | null;
createdBy: string | null;
createdAt: string;
doneAt: string | null;
doneBy: string | null;
}
type Row = typeof todos.$inferSelect;
async function shape(rows: Row[]): Promise<TodoItem[]> {
const serverNames = new Map((await db.select({ id: servers.id, name: servers.name }).from(servers)).map((s) => [s.id, s.name]));
const deviceNames = new Map((await db.select({ id: networkDevices.id, name: networkDevices.name }).from(networkDevices)).map((d) => [d.id, d.name]));
return rows.map((r) => {
const name = r.targetType && r.targetId !== null ? (r.targetType === "server" ? serverNames.get(r.targetId) : deviceNames.get(r.targetId)) : undefined;
return {
id: r.id,
title: r.title,
detail: r.detail,
dueDate: r.dueDate,
assignee: r.assigneeName ? { id: r.assigneeUserId, name: r.assigneeName } : null,
// What it was about, if that is still there.
target: r.targetType && r.targetId !== null && name ? { type: r.targetType, id: r.targetId, name } : null,
createdBy: r.createdBy,
createdAt: r.createdAt,
doneAt: r.doneAt,
doneBy: r.doneBy,
};
});
}
/** Open ones first, the soonest due first (those with no day last); done ones after, the latest done first. */
export async function listTodos(): Promise<TodoItem[]> {
const open = await shape(await db.select().from(todos).where(isNull(todos.doneAt)));
open.sort((a, b) => (a.dueDate ?? "9999-99-99").localeCompare(b.dueDate ?? "9999-99-99") || a.createdAt.localeCompare(b.createdAt) || a.id - b.id);
const done = await shape(await db.select().from(todos).where(isNotNull(todos.doneAt)).orderBy(desc(todos.doneAt)).limit(200));
return [...open, ...done];
}
export interface TodoInput {
title: string;
detail?: string | null;
dueDate?: string | null;
assigneeId?: number | null;
targetType?: TargetType | null;
targetId?: number | null;
}
async function resolveAssignee(id: number | null | undefined): Promise<{ id: number | null; name: string | null }> {
if (id === null || id === undefined) return { id: null, name: null };
const [u] = await db.select().from(users).where(eq(users.id, id)).limit(1);
if (!u || (u.role !== "admin" && u.role !== "operator")) throw new TodoError(400, "invalid_assignee", "A job can be handed to an administrator or an operator.");
return { id: u.id, name: u.name ?? u.email ?? u.oidcSub };
}
async function resolveTarget(type: TargetType | null | undefined, id: number | null | undefined): Promise<{ type: TargetType | null; id: number | null }> {
if (!type || id === null || id === undefined) return { type: null, id: null };
if (!(await targetName(type, id))) throw new TodoError(400, "unknown_target", "That server or network device is not there.");
return { type, id };
}
function checked(input: TodoInput) {
const title = input.title.trim();
if (!title) throw new TodoError(400, "invalid_body", "Say what is to be done.");
if (title.length > MAX_TITLE) throw new TodoError(400, "too_long", `Keep the title under ${MAX_TITLE} characters.`);
const detail = (input.detail ?? "").trim();
if (detail.length > MAX_DETAIL) throw new TodoError(400, "too_long", `Keep the details under ${MAX_DETAIL} characters.`);
if (input.dueDate !== null && input.dueDate !== undefined && input.dueDate !== "" && !isRealDay(input.dueDate)) throw new TodoError(400, "invalid_date", "That is not a date.");
return { title, detail: detail || null, dueDate: input.dueDate ? input.dueDate : null };
}
export async function createTodo(input: TodoInput, actor: string, now: Date = new Date()): Promise<TodoItem> {
const base = checked(input);
const who = await resolveAssignee(input.assigneeId);
const target = await resolveTarget(input.targetType, input.targetId);
const [row] = await db
.insert(todos)
.values({ ...base, assigneeUserId: who.id, assigneeName: who.name, targetType: target.type, targetId: target.id, createdBy: actor, createdAt: now.toISOString() })
.returning();
return (await shape([row]))[0];
}
/** Changes what is given; "done" ticks it off or brings it back. Null if there is no such job. */
export async function updateTodo(id: number, input: Partial<TodoInput> & { done?: boolean }, actor: string, now: Date = new Date()): Promise<{ before: TodoItem; after: TodoItem } | null> {
const [existing] = await db.select().from(todos).where(eq(todos.id, id)).limit(1);
if (!existing) return null;
const before = (await shape([existing]))[0];
const set: Partial<Row> = {};
if (input.title !== undefined || input.detail !== undefined || input.dueDate !== undefined) {
const c = checked({ title: input.title ?? existing.title, detail: input.detail === undefined ? existing.detail : input.detail, dueDate: input.dueDate === undefined ? existing.dueDate : input.dueDate });
Object.assign(set, c);
}
if (input.assigneeId !== undefined) {
const who = await resolveAssignee(input.assigneeId);
set.assigneeUserId = who.id;
set.assigneeName = who.name;
}
if (input.targetType !== undefined || input.targetId !== undefined) {
const t = await resolveTarget(input.targetType === undefined ? existing.targetType : input.targetType, input.targetId === undefined ? existing.targetId : input.targetId);
set.targetType = t.type;
set.targetId = t.id;
}
if (input.done === true && !existing.doneAt) Object.assign(set, { doneAt: now.toISOString(), doneBy: actor });
if (input.done === false) Object.assign(set, { doneAt: null, doneBy: null });
if (Object.keys(set).length > 0) await db.update(todos).set(set).where(eq(todos.id, id));
const [after] = await db.select().from(todos).where(eq(todos.id, id)).limit(1);
return { before, after: (await shape([after]))[0] };
}
export async function removeTodo(id: number): Promise<TodoItem | null> {
const [row] = await db.select().from(todos).where(eq(todos.id, id)).limit(1);
if (!row) return null;
const item = (await shape([row]))[0];
await db.delete(todos).where(eq(todos.id, id));
return item;
}
/** Open jobs handed to a person, for My work. */
export async function openTodosFor(userId: number): Promise<TodoItem[]> {
const rows = await db.select().from(todos).where(and(isNull(todos.doneAt), eq(todos.assigneeUserId, userId)));
const items = await shape(rows);
return items.sort((a, b) => (a.dueDate ?? "9999-99-99").localeCompare(b.dueDate ?? "9999-99-99") || a.id - b.id);
}
// ─── Notes ─────────────────────────────────────────────────────────────────
export interface NoteItem {
targetType: TargetType;
targetId: number;
body: string;
updatedBy: string | null;
updatedAt: string;
}
export async function getNote(type: TargetType, id: number): Promise<NoteItem | null> {
const [row] = await db.select().from(notes).where(and(eq(notes.targetType, type), eq(notes.targetId, id))).limit(1);
return row ? { targetType: row.targetType, targetId: row.targetId, body: row.body, updatedBy: row.updatedBy, updatedAt: row.updatedAt } : null;
}
/** Writes the note of something; an empty text takes it away. Returns what is there now (null when there is none). */
export async function setNote(type: TargetType, id: number, body: string, actor: string, now: Date = new Date()): Promise<NoteItem | null> {
const text = body.replace(/\r\n/g, "\n").trimEnd();
if (text.length > MAX_NOTE_BODY) throw new TodoError(400, "too_long", `Keep the note under ${MAX_NOTE_BODY} characters.`);
if (!(await targetName(type, id))) throw new TodoError(404, "unknown_target", "That server or network device is not there.");
if (text.trim() === "") {
await db.delete(notes).where(and(eq(notes.targetType, type), eq(notes.targetId, id)));
return null;
}
const values = { body: text, updatedBy: actor, updatedAt: now.toISOString() };
await db.insert(notes).values({ targetType: type, targetId: id, ...values }).onConflictDoUpdate({ target: [notes.targetType, notes.targetId], set: values });
return getNote(type, id);
}