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

+3
View File
@@ -0,0 +1,3 @@
ALTER TABLE `servers` ADD `os_id` text;--> statement-breakpoint
ALTER TABLE `servers` ADD `os_name` text;--> statement-breakpoint
ALTER TABLE `servers` ADD `os_version` text;
+25
View File
@@ -0,0 +1,25 @@
CREATE TABLE `notes` (
`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
`target_type` text NOT NULL,
`target_id` integer NOT NULL,
`body` text NOT NULL,
`updated_by` text,
`updated_at` text NOT NULL
);
--> statement-breakpoint
CREATE UNIQUE INDEX `notes_target_idx` ON `notes` (`target_type`,`target_id`);--> statement-breakpoint
CREATE TABLE `todos` (
`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
`title` text NOT NULL,
`detail` text,
`due_date` text,
`assignee_user_id` integer,
`assignee_name` text,
`target_type` text,
`target_id` integer,
`created_by` text,
`created_at` text NOT NULL,
`done_at` text,
`done_by` text,
FOREIGN KEY (`assignee_user_id`) REFERENCES `users`(`id`) ON UPDATE no action ON DELETE set null
);
+37
View File
@@ -0,0 +1,37 @@
CREATE TABLE `ha_entities` (
`integration_id` integer NOT NULL,
`entity_id` text NOT NULL,
`friendly_name` text,
`state` text NOT NULL,
`value` real,
`unit` text,
`device_class` text,
`last_changed` text,
`unavailable_since` text,
`updated_at` text NOT NULL,
PRIMARY KEY(`integration_id`, `entity_id`),
FOREIGN KEY (`integration_id`) REFERENCES `integrations`(`id`) ON UPDATE no action ON DELETE cascade
);
--> statement-breakpoint
CREATE TABLE `ha_readings` (
`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
`integration_id` integer NOT NULL,
`entity_id` text NOT NULL,
`at` text NOT NULL,
`value` real NOT NULL,
FOREIGN KEY (`integration_id`) REFERENCES `integrations`(`id`) ON UPDATE no action ON DELETE cascade
);
--> statement-breakpoint
CREATE INDEX `ha_readings_lookup_idx` ON `ha_readings` (`integration_id`,`entity_id`,`at`);--> statement-breakpoint
CREATE TABLE `ha_watched` (
`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
`integration_id` integer NOT NULL,
`entity_id` text NOT NULL,
`name` text,
`above` real,
`below` real,
`created_at` text NOT NULL,
FOREIGN KEY (`integration_id`) REFERENCES `integrations`(`id`) ON UPDATE no action ON DELETE cascade
);
--> statement-breakpoint
CREATE UNIQUE INDEX `ha_watched_unique` ON `ha_watched` (`integration_id`,`entity_id`);
File diff suppressed because it is too large. Load diff
File diff suppressed because it is too large. Load diff
File diff suppressed because it is too large. Load diff
+21
View File
@@ -337,6 +337,27 @@
"when": 1792519188097,
"tag": "0047_eager_dorian_gray",
"breakpoints": true
},
{
"idx": 48,
"version": "6",
"when": 1792522721690,
"tag": "0048_certain_bullseye",
"breakpoints": true
},
{
"idx": 49,
"version": "6",
"when": 1792523247340,
"tag": "0049_condemned_warhawk",
"breakpoints": true
},
{
"idx": 50,
"version": "6",
"when": 1792523816802,
"tag": "0050_useful_bulldozer",
"breakpoints": true
}
]
}
+7
View File
@@ -48,6 +48,9 @@ import { mapRouter } from "./routes/map.js";
import { networkRouter } from "./routes/network.js";
import { myWorkRouter } from "./routes/myWork.js";
import { apiTokensRouter } from "./routes/apiTokens.js";
import { notesRouter, todosRouter } from "./routes/todos.js";
import { npmRouter } from "./routes/npm.js";
import { homeAssistantRouter } from "./routes/homeAssistant.js";
import { publicApiRouter } from "./routes/publicApi.js";
import { generatorRouter } from "./routes/generator.js";
import { alertTicketsRouter } from "./routes/alertTickets.js";
@@ -170,6 +173,10 @@ export function createApp() {
app.use("/api/map", mapRouter);
app.use("/api/network", networkRouter);
app.use("/api/my-work", myWorkRouter);
app.use("/api/npm", npmRouter);
app.use("/api/home-assistant", homeAssistantRouter);
app.use("/api/todos", todosRouter);
app.use("/api/notes", notesRouter);
app.use("/api/generator", generatorRouter);
app.use("/api/alert-tickets", alertTicketsRouter);
+96
View File
@@ -471,6 +471,12 @@ export const servers = sqliteTable("servers", {
// not in this list, and not noted on the Ports page, is a "new port" (services/newPorts.ts). Null until the first report that carries ports.
portBaseline: text("port_baseline"),
// What the machine runs, as its agent says (agent 7 and later): the distribution's id ("ubuntu", "debian", "proxmox-ve", "windows"), its pretty name and its release
// (for Windows, the build number). What services/osLifecycle.ts works out when its support ends from.
osId: text("os_id"),
osName: text("os_name"),
osVersion: text("os_version"),
// What the machine is, as its firmware says (agents report it): maker, model and serial number. Used to fill in the inventory.
hwManufacturer: text("hw_manufacturer"),
hwModel: text("hw_model"),
@@ -536,6 +542,8 @@ export const integrationTypes = [
"pbs",
"osticket",
"unifi",
"npm",
"homeassistant",
] as const;
export type IntegrationType = (typeof integrationTypes)[number];
@@ -1088,6 +1096,94 @@ export const metricsTokens = sqliteTable("metrics_tokens", {
lastUsedAt: text("last_used_at"), // ISO
});
// ─── Home Assistant: the few things somebody chose to watch ─────────────────
// An entity (a sensor, a binary sensor, a battery, a UPS) that somebody chose to watch on a Home Assistant integration, and the limits that make it an alert.
export const haWatched = sqliteTable(
"ha_watched",
{
id: integer("id").primaryKey({ autoIncrement: true }),
integrationId: integer("integration_id")
.notNull()
.references(() => integrations.id, { onDelete: "cascade" }),
entityId: text("entity_id").notNull(), // "sensor.ups_battery"
name: text("name"), // what to call it here; the entity's own friendly name when null
above: real("above"), // an alert when a numeric value is above this
below: real("below"), // …or below this
createdAt: text("created_at").notNull(),
},
(t) => [uniqueIndex("ha_watched_unique").on(t.integrationId, t.entityId)],
);
// What the last read said about each watched entity. `unavailableSince` is when it was first seen "unavailable" or "unknown", kept until it is not.
export const haEntities = sqliteTable(
"ha_entities",
{
integrationId: integer("integration_id")
.notNull()
.references(() => integrations.id, { onDelete: "cascade" }),
entityId: text("entity_id").notNull(),
friendlyName: text("friendly_name"),
state: text("state").notNull(),
value: real("value"), // the state as a number, when it is one
unit: text("unit"),
deviceClass: text("device_class"),
lastChanged: text("last_changed"),
unavailableSince: text("unavailable_since"),
updatedAt: text("updated_at").notNull(),
},
(t) => [primaryKey({ columns: [t.integrationId, t.entityId] })],
);
// A numeric value of a watched entity at a moment, for the chart: one every few minutes, kept 35 days.
export const haReadings = sqliteTable(
"ha_readings",
{
id: integer("id").primaryKey({ autoIncrement: true }),
integrationId: integer("integration_id")
.notNull()
.references(() => integrations.id, { onDelete: "cascade" }),
entityId: text("entity_id").notNull(),
at: text("at").notNull(), // ISO
value: real("value").notNull(),
},
(t) => [index("ha_readings_lookup_idx").on(t.integrationId, t.entityId, t.at)],
);
// ─── Notes and the to-do list ───────────────────────────────────────────────
// Free-form notes about a server or a network device — what is it, where does it sit, what did I do to it last time — one text per thing, drawn with the same small
// formatting as a runbook. Polymorphic like runbooks and maintenance windows, so no foreign key: the note of something removed is simply not shown.
export const notes = sqliteTable(
"notes",
{
id: integer("id").primaryKey({ autoIncrement: true }),
targetType: text("target_type").$type<"server" | "device">().notNull(),
targetId: integer("target_id").notNull(),
body: text("body").notNull(),
updatedBy: text("updated_by"),
updatedAt: text("updated_at").notNull(), // ISO
},
(t) => [uniqueIndex("notes_target_idx").on(t.targetType, t.targetId)],
);
// Things to do: the small jobs of a homelab ("replace the UPS battery", "move the NAS to the new switch"), with a day they are due and optionally a person and
// a thing they are about. Open ones with a due day are in Coming up; the ones handed to someone are in My work.
export const todos = sqliteTable("todos", {
id: integer("id").primaryKey({ autoIncrement: true }),
title: text("title").notNull(),
detail: text("detail"),
dueDate: text("due_date"), // YYYY-MM-DD
assigneeUserId: integer("assignee_user_id").references(() => users.id, { onDelete: "set null" }),
assigneeName: text("assignee_name"), // as it was when handed over, so it still reads if the person is removed
targetType: text("target_type").$type<"server" | "device">(),
targetId: integer("target_id"),
createdBy: text("created_by"),
createdAt: text("created_at").notNull(), // ISO
doneAt: text("done_at"), // ISO; null while open
doneBy: text("done_by"),
});
// ─── Personal API tokens: read-only access to /api/v1 for scripts and Home Assistant ─────
// One person's own: made by them, shown to them once, and gone when they are. Only a hash of the secret is kept. A token only ever reads (the API has no other verbs),
+2
View File
@@ -25,6 +25,7 @@ import { initEndpointMonitor } from "./services/endpointMonitor.js";
import { initPatchPlans } from "./services/patchPlans.js";
import { initFlapMonitor } from "./services/flapMonitor.js";
import { initUnifiSync } from "./services/unifiSync.js";
import { initHomeAssistantSync } from "./services/homeAssistantSync.js";
import { backfillMetricRollups } from "./services/serverMetrics.js";
import { backfillEndpointHourly } from "./services/endpointChecks.js";
import { initSelfBackupScheduler } from "./services/selfBackupScheduler.js";
@@ -71,6 +72,7 @@ initEndpointMonitor();
initPatchPlans();
initFlapMonitor();
initUnifiSync();
initHomeAssistantSync();
// The hourly and daily figures are built from what is already kept, once, the first time after they were added (in the background: nothing waits for it).
void Promise.all([backfillMetricRollups(), backfillEndpointHourly()]).catch((err) => console.error("[history] couldn't build the hourly and daily figures:", err));
+11
View File
@@ -88,6 +88,17 @@ export const INTEGRATION_FIELDS: Partial<Record<IntegrationType, IntegrationFiel
{ key: "selfHosted", label: "A self-hosted Network application, not a UniFi OS console (UDM, Cloud Gateway, Cloud Key)", secret: false, type: "checkbox" },
{ key: "insecure", label: "Allow self-signed certificate", secret: false, type: "checkbox" },
],
npm: [
{ key: "url", label: "Nginx Proxy Manager URL", secret: false, placeholder: "https://npm.example.lan:81 (its admin page)" },
{ key: "email", label: "Email of a user that may view", secret: false, placeholder: "a dedicated user with view-only access to proxy hosts and certificates" },
{ key: "password", label: "Password", secret: true, type: "password" },
{ key: "insecure", label: "Allow self-signed certificate", secret: false, type: "checkbox" },
],
homeassistant: [
{ key: "url", label: "Home Assistant URL", secret: false, placeholder: "https://homeassistant.example.lan:8123" },
{ key: "token", label: "Long-lived access token", secret: true, type: "password", placeholder: "made on your profile page in Home Assistant" },
{ key: "insecure", label: "Allow self-signed certificate", secret: false, type: "checkbox" },
],
osticket: [
{ key: "host", label: "Database host", secret: false, placeholder: "osticket-db.example.lan" },
{ key: "port", label: "Database port", secret: false, optional: true, placeholder: "3306" },
@@ -0,0 +1,91 @@
/**
* Home Assistant adapter.
* Requires config: url, token; optional: insecure
*
* Home Assistant's REST API takes a long-lived access token (made on a user's profile page, at the bottom) as a bearer token. This only reads: `GET /api/` says it
* is running, `GET /api/states` lists every entity with its state and attributes. Which of them are worth watching is chosen in this app (services/homeAssistantSync.ts);
* the token can belong to a user with no more rights than a viewer's.
*
* Endpoints and field names are from Home Assistant's published REST API, and have not been checked against a live instance here; every field is read defensively.
*/
import { requestJson } from "../httpJson.js";
import { withDiagLogging } from "../../services/diagLog.js";
export interface HomeAssistantConfig {
url: string;
token: string;
insecure?: boolean;
}
export interface HaEntity {
entityId: string;
/** Its friendly name, or its id. */
name: string;
state: string;
/** The state as a number, when it is one. */
value: number | null;
unit: string | null;
deviceClass: string | null;
lastChanged: string | null;
/** "unavailable" or "unknown": it exists, but is not saying anything. */
unavailable: boolean;
}
export interface HomeAssistantAdapter {
ping(): Promise<{ ok: boolean; latencyMs?: number; error?: string }>;
listEntities(): Promise<HaEntity[]>;
}
const obj = (v: unknown): Record<string, unknown> => (v !== null && typeof v === "object" && !Array.isArray(v) ? (v as Record<string, unknown>) : {});
const str = (v: unknown): string | null => (typeof v === "string" && v.trim() !== "" ? v.trim() : null);
export function entityFrom(raw: unknown): HaEntity | null {
const e = obj(raw);
const entityId = str(e.entity_id);
if (!entityId) return null;
const attrs = obj(e.attributes);
const state = typeof e.state === "string" ? e.state : e.state === null || e.state === undefined ? "unknown" : String(e.state);
const n = state.trim() === "" ? NaN : Number(state);
const changed = str(e.last_changed);
return {
entityId,
name: str(attrs.friendly_name) ?? entityId,
state,
value: Number.isFinite(n) ? n : null,
unit: str(attrs.unit_of_measurement),
deviceClass: str(attrs.device_class),
lastChanged: changed && Number.isFinite(Date.parse(changed)) ? new Date(changed).toISOString() : null,
unavailable: state === "unavailable" || state === "unknown",
};
}
export function createHomeAssistantAdapter(config: HomeAssistantConfig): HomeAssistantAdapter {
const base = config.url.replace(/\/+$/, "");
const insecure = config.insecure === true;
async function get(path: string): Promise<unknown> {
const res = await requestJson(`${base}${path}`, { headers: { Authorization: `Bearer ${config.token}` }, insecure });
if (res.status === 401 || res.status === 403) throw new Error("Home Assistant did not accept the token (HTTP " + res.status + ")");
if (res.status === 404) throw new Error("Home Assistant has no REST API at this address — check the address and port (8123), and that the api integration is on");
if (res.status < 200 || res.status >= 300) throw new Error(`Home Assistant error: HTTP ${res.status}`);
if (res.json === null) throw new Error("Home Assistant answered with something that isn't JSON — is the address its own?");
return res.json;
}
async function listEntities(): Promise<HaEntity[]> {
const states = await get("/api/states");
return (Array.isArray(states) ? states : []).map(entityFrom).filter((e): e is HaEntity => e !== null);
}
async function ping(): Promise<{ ok: boolean; latencyMs?: number; error?: string }> {
const start = Date.now();
try {
await get("/api/");
return { ok: true, latencyMs: Date.now() - start };
} catch (err) {
return { ok: false, error: err instanceof Error ? err.message : String(err) };
}
}
return withDiagLogging("homeassistant", { ping, listEntities });
}
+62
View File
@@ -0,0 +1,62 @@
/**
* One small way to ask a web API for JSON, for the integrations that speak plain HTTP: a method, headers, an optional JSON body, a timeout, and — for a controller with a
* certificate of its own — a way to accept it. Answers with the status and what was said, parsed if it was JSON; a refused connection or a timeout throws.
*/
import * as http from "node:http";
import * as https from "node:https";
export interface JsonResponse {
status: number;
json: unknown;
text: string;
headers: http.IncomingHttpHeaders;
}
const TIMEOUT_MS = 15_000;
export function requestJson(url: string, opts: { method?: "GET" | "POST"; headers?: Record<string, string>; body?: unknown; insecure?: boolean; timeoutMs?: number } = {}): Promise<JsonResponse> {
return new Promise((resolve, reject) => {
let parsed: URL;
try {
parsed = new URL(url);
} catch {
return reject(new Error(`"${url}" is not a web address`));
}
if (parsed.protocol !== "http:" && parsed.protocol !== "https:") return reject(new Error("The address must start with http:// or https://"));
const isHttps = parsed.protocol === "https:";
const lib = isHttps ? https : http;
const payload = opts.body === undefined ? null : JSON.stringify(opts.body);
const limit = opts.timeoutMs ?? TIMEOUT_MS;
const req = lib.request(
{
hostname: parsed.hostname,
port: parsed.port || (isHttps ? 443 : 80),
path: parsed.pathname + parsed.search,
method: opts.method ?? "GET",
headers: { Accept: "application/json", ...(payload ? { "Content-Type": "application/json", "Content-Length": String(Buffer.byteLength(payload)) } : {}), ...opts.headers },
timeout: limit,
...(isHttps ? { rejectUnauthorized: !opts.insecure } : {}),
},
(res) => {
let text = "";
res.setEncoding("utf8");
res.on("data", (chunk) => {
text += chunk;
});
res.on("end", () => {
let json: unknown = null;
try {
json = text ? JSON.parse(text) : null;
} catch {
// not JSON: the text is still there
}
resolve({ status: res.statusCode ?? 0, json, text, headers: res.headers });
});
},
);
req.on("timeout", () => req.destroy(new Error(`timed out after ${limit / 1000}s`)));
req.on("error", reject);
if (payload) req.write(payload);
req.end();
});
}
+145
View File
@@ -0,0 +1,145 @@
/**
* Nginx Proxy Manager adapter.
* Requires config: url, email, password; optional: insecure
*
* NPM has no API keys: its API is the one its own web page uses, and signs in with the administrator's email and password (`POST /api/tokens` answers with a bearer
* token, which the rest take). So this logs in for each read — and only ever reads: the proxy hosts (what is proxied to what, with its certificate) and the
* certificates. Use a dedicated user with the "view only" permission on proxy hosts and certificates (Users → Permissions) rather than the administrator's own.
*
* Endpoints and field names are from NPM's own API as the web page uses it (`/api/nginx/proxy-hosts?expand=certificate`, `/api/nginx/certificates`), and have
* not been checked against a live NPM here; every field is read defensively, so one a version renames is left blank rather than breaking the read.
*/
import { requestJson } from "../httpJson.js";
import { withDiagLogging } from "../../services/diagLog.js";
export interface NpmConfig {
url: string;
email: string;
password: string;
insecure?: boolean;
}
export interface NpmCertificate {
id: number;
name: string;
provider: string;
domains: string[];
/** ISO, or null when NPM does not say. */
expiresAt: string | null;
}
export interface NpmProxyHost {
id: number;
domains: string[];
forwardScheme: string;
forwardHost: string;
forwardPort: number | null;
enabled: boolean;
sslForced: boolean;
certificate: { id: number; name: string; expiresAt: string | null } | null;
/** NPM says its nginx did not accept this host's configuration. */
online: boolean;
error: string | null;
}
export interface NpmSnapshot {
version: string | null;
hosts: NpmProxyHost[];
certificates: NpmCertificate[];
}
export interface NpmAdapter {
ping(): Promise<{ ok: boolean; latencyMs?: number; error?: string }>;
fetchSnapshot(): Promise<NpmSnapshot>;
}
const obj = (v: unknown): Record<string, unknown> => (v !== null && typeof v === "object" && !Array.isArray(v) ? (v as Record<string, unknown>) : {});
const str = (v: unknown): string | null => (typeof v === "string" && v.trim() !== "" ? v.trim() : null);
const num = (v: unknown): number | null => {
const n = typeof v === "number" ? v : typeof v === "string" && v.trim() !== "" ? Number(v) : NaN;
return Number.isFinite(n) ? n : null;
};
const domainsOf = (v: unknown): string[] => (Array.isArray(v) ? v.filter((d): d is string => typeof d === "string" && d !== "") : []);
/** NPM writes times as "2026-12-31 23:59:59" (UTC) or as ISO; either becomes ISO. */
export function isoOf(v: unknown): string | null {
const s = str(v);
if (!s) return null;
const t = Date.parse(/[zZ]|[+-]\d{2}:?\d{2}$/.test(s) ? s : `${s.replace(" ", "T")}Z`);
return Number.isFinite(t) ? new Date(t).toISOString() : null;
}
export function certificateFrom(raw: unknown): NpmCertificate | null {
const c = obj(raw);
const id = num(c.id);
if (id === null) return null;
return { id, name: str(c.nice_name) ?? domainsOf(c.domain_names)[0] ?? `certificate ${id}`, provider: str(c.provider) ?? "other", domains: domainsOf(c.domain_names), expiresAt: isoOf(c.expires_on) };
}
export function hostFrom(raw: unknown): NpmProxyHost | null {
const h = obj(raw);
const id = num(h.id);
if (id === null) return null;
const cert = h.certificate && typeof h.certificate === "object" ? certificateFrom(h.certificate) : null;
const meta = obj(h.meta);
const err = str(meta.nginx_err);
return {
id,
domains: domainsOf(h.domain_names),
forwardScheme: str(h.forward_scheme) ?? "http",
forwardHost: str(h.forward_host) ?? "",
forwardPort: num(h.forward_port),
enabled: h.enabled !== false && h.enabled !== 0,
sslForced: h.ssl_forced === true || h.ssl_forced === 1,
certificate: cert ? { id: cert.id, name: cert.name, expiresAt: cert.expiresAt } : null,
// NPM says "online" unless its own check of the generated configuration failed.
online: meta.nginx_online !== false && !err,
error: err,
};
}
export function createNpmAdapter(config: NpmConfig): NpmAdapter {
const base = config.url.replace(/\/+$/, "");
const insecure = config.insecure === true;
async function login(): Promise<string> {
const res = await requestJson(`${base}/api/tokens`, { method: "POST", body: { identity: config.email, secret: config.password }, insecure });
if (res.status === 401 || res.status === 400 || res.status === 403) throw new Error("Nginx Proxy Manager did not accept the email and password");
const token = str(obj(res.json).token);
if (res.status < 200 || res.status >= 300 || !token) throw new Error(`Nginx Proxy Manager login failed: HTTP ${res.status}`);
return token;
}
async function get(token: string, path: string): Promise<unknown> {
const res = await requestJson(`${base}${path}`, { headers: { Authorization: `Bearer ${token}` }, insecure });
if (res.status === 401 || res.status === 403) throw new Error(`Nginx Proxy Manager did not allow reading ${path.split("?")[0]} (HTTP ${res.status}) — the user needs view access to proxy hosts and certificates`);
if (res.status < 200 || res.status >= 300) throw new Error(`Nginx Proxy Manager error: HTTP ${res.status}`);
if (res.json === null) throw new Error("Nginx Proxy Manager answered with something that isn't JSON — is the address its admin page's?");
return res.json;
}
async function fetchSnapshot(): Promise<NpmSnapshot> {
const token = await login();
const [hosts, certs, status] = await Promise.all([get(token, "/api/nginx/proxy-hosts?expand=certificate"), get(token, "/api/nginx/certificates").catch(() => []), get(token, "/api/").catch(() => null)]);
const v = obj(obj(status).version);
const version = num(v.major) !== null ? `${v.major}.${v.minor ?? 0}.${v.revision ?? 0}` : null;
return {
version,
hosts: (Array.isArray(hosts) ? hosts : []).map(hostFrom).filter((h): h is NpmProxyHost => h !== null),
certificates: (Array.isArray(certs) ? certs : []).map(certificateFrom).filter((c): c is NpmCertificate => c !== null),
};
}
async function ping(): Promise<{ ok: boolean; latencyMs?: number; error?: string }> {
const start = Date.now();
try {
const token = await login();
await get(token, "/api/nginx/proxy-hosts");
return { ok: true, latencyMs: Date.now() - start };
} catch (err) {
return { ok: false, error: err instanceof Error ? err.message : String(err) };
}
}
return withDiagLogging("npm", { ping, fetchSnapshot });
}
+6
View File
@@ -11,6 +11,8 @@ import { createPhpIpamAdapter } from "./phpipam/adapter.js";
import { createPbsAdapter } from "./pbs/adapter.js";
import { createOsTicketAdapter } from "./osticket/adapter.js";
import { createUnifiAdapter } from "./unifi/adapter.js";
import { createNpmAdapter } from "./npm/adapter.js";
import { createHomeAssistantAdapter } from "./homeassistant/adapter.js";
export interface PingableAdapter {
ping(): Promise<{ ok: boolean; latencyMs?: number; error?: string }>;
@@ -47,6 +49,10 @@ export function createIntegrationAdapter(type: IntegrationType, config: Integrat
return createOsTicketAdapter(config as any);
case "unifi":
return createUnifiAdapter(config as any);
case "npm":
return createNpmAdapter(config as any);
case "homeassistant":
return createHomeAssistantAdapter(config as any);
default:
throw new Error(`Integration type "${type}" is not implemented yet`);
}
+38
View File
@@ -8,6 +8,7 @@ import { recordAudit } from "../services/audit.js";
import { invalidateAlertsCache } from "../services/alerts.js";
import { dependentsOf, impactOf, loadGraph, nodeKey, nodeTypes, wouldLoop } from "../services/dependencies.js";
import { getSettings } from "../services/settingsStore.js";
import { loadDependencySuggestions, loadDismissedDependencies, saveDismissedDependencies } from "../services/dependencySuggestions.js";
import { getSourceStatus } from "../services/integrationHealthMonitor.js";
import { serverStatus, type MapStatus } from "../services/topology.js";
import { asyncHandler } from "../utils/asyncHandler.js";
@@ -44,6 +45,43 @@ dependenciesRouter.get("/", asyncHandler(async (_req, res) => {
});
}));
// Dependencies worth writing, from what the app already knows (an integration reached at a server's own address). Everyone signed in can see them; operators
// add or dismiss them, as they would any dependency.
dependenciesRouter.get("/suggestions", asyncHandler(async (_req, res) => {
res.json(await loadDependencySuggestions());
}));
const keysSchema = z.object({ keys: z.array(z.string().min(1).max(100)).min(1).max(200) });
dependenciesRouter.post("/suggestions/add", requireRole("operator"), asyncHandler(async (req, res) => {
const parsed = keysSchema.safeParse(req.body);
if (!parsed.success) return res.status(400).json({ error: "invalid_body" });
const { suggestions } = await loadDependencySuggestions();
const wanted = new Set(parsed.data.keys);
const added: { id: number; from: string; to: string }[] = [];
for (const s of suggestions.filter((x) => wanted.has(x.key))) {
const [row] = await db.insert(dependencies).values({ fromType: "integration", fromId: s.from.id, toType: "server", toId: s.to.id }).returning();
await recordAudit({ actor: req.currentUser!, category: "dependency", action: "create", targetType: "dependency", targetId: row.id, detail: { from: s.from.name, to: s.to.name, fromKey: `integration:${s.from.id}`, toKey: `server:${s.to.id}`, suggested: true } });
added.push({ id: row.id, from: s.from.name, to: s.to.name });
}
if (added.length > 0) invalidateAlertsCache();
res.json({ added, ...(await loadDependencySuggestions()) });
}));
dependenciesRouter.post("/suggestions/dismiss", requireRole("operator"), asyncHandler(async (req, res) => {
const parsed = keysSchema.safeParse(req.body);
if (!parsed.success) return res.status(400).json({ error: "invalid_body" });
const dismissed = await loadDismissedDependencies();
for (const k of parsed.data.keys) dismissed.add(k);
await saveDismissedDependencies(dismissed);
res.json(await loadDependencySuggestions());
}));
dependenciesRouter.post("/suggestions/restore", requireRole("operator"), asyncHandler(async (_req, res) => {
await saveDismissedDependencies(new Set());
res.json(await loadDependencySuggestions());
}));
// What a thing would take with it, and what it needs itself: for the maintenance and update pages, which say so before it is done.
dependenciesRouter.get("/impact", asyncHandler(async (req, res) => {
const type = nodeTypes.find((t) => t === req.query.type);
+137
View File
@@ -0,0 +1,137 @@
import { Router } from "express";
import { and, eq, gte } from "drizzle-orm";
import { z } from "zod";
import { db } from "../db/client.js";
import { haEntities, haReadings, haWatched, integrations } from "../db/schema.js";
import { requireAuth, requireRole } from "../auth/middleware.js";
import { recordAudit } from "../services/audit.js";
import { invalidateAlertsCache } from "../services/alerts.js";
import { createHomeAssistantAdapter } from "../integrations/homeassistant/adapter.js";
import { loadIntegrationConfig } from "../integrations/loadIntegration.js";
import { haProblems, haSyncStatuses, isHaFresh, READING_KEEP_DAYS, syncAllHomeAssistants, syncHomeAssistant } from "../services/homeAssistantSync.js";
import { bucketPoints } from "../services/serverMetrics.js";
import { asyncHandler } from "../utils/asyncHandler.js";
export const homeAssistantRouter = Router();
homeAssistantRouter.use(requireAuth);
const idOf = (v: string) => (/^\d+$/.test(v) ? Number(v) : NaN);
const DAY_MS = 86_400_000;
async function haIntegration(id: number) {
const [row] = await db.select({ id: integrations.id, name: integrations.name, type: integrations.type }).from(integrations).where(eq(integrations.id, id)).limit(1);
return row && row.type === "homeassistant" ? row : null;
}
/** What is watched in each Home Assistant, with what the last read said; everyone signed in can see it. */
homeAssistantRouter.get("/", asyncHandler(async (_req, res) => {
const [intRows, watched, entities, statuses] = await Promise.all([
db.select({ id: integrations.id, name: integrations.name, enabled: integrations.enabled }).from(integrations).where(eq(integrations.type, "homeassistant")),
db.select().from(haWatched),
db.select().from(haEntities),
haSyncStatuses(),
]);
const now = new Date();
res.json({
integrations: intRows.map((i) => {
const status = statuses[i.id] ?? null;
const mine = watched.filter((w) => w.integrationId === i.id).sort((a, b) => (a.name ?? a.entityId).localeCompare(b.name ?? b.entityId));
const rows = new Map(entities.filter((e) => e.integrationId === i.id).map((e) => [e.entityId, e]));
return {
id: i.id,
name: i.name,
enabled: i.enabled,
status,
fresh: isHaFresh(status ?? undefined, now.getTime()),
watched: mine.map((w) => {
const e = rows.get(w.entityId);
return {
entityId: w.entityId,
name: w.name,
above: w.above,
below: w.below,
entity: e ? { friendlyName: e.friendlyName, state: e.state, value: e.value, unit: e.unit, deviceClass: e.deviceClass, lastChanged: e.lastChanged, unavailableSince: e.unavailableSince, updatedAt: e.updatedAt } : null,
};
}),
problems: isHaFresh(status ?? undefined, now.getTime()) ? haProblems(i.id, i.name, mine, rows, now) : [],
};
}),
});
}));
// Every entity Home Assistant has, to pick from: read live, so only those who may change what is watched see them.
homeAssistantRouter.get("/:id/entities", requireRole("operator"), asyncHandler(async (req, res) => {
const id = idOf(req.params.id);
if (!Number.isInteger(id) || !(await haIntegration(id))) return res.status(404).json({ error: "not_found" });
const loaded = await loadIntegrationConfig(id);
if (!loaded) return res.status(404).json({ error: "not_found" });
const q = typeof req.query.q === "string" ? req.query.q.trim().toLowerCase() : "";
try {
const all = await createHomeAssistantAdapter(loaded.config as never).listEntities();
const watched = new Set((await db.select({ entityId: haWatched.entityId }).from(haWatched).where(eq(haWatched.integrationId, id))).map((w) => w.entityId));
const found = all.filter((e) => !q || e.entityId.toLowerCase().includes(q) || e.name.toLowerCase().includes(q)).sort((a, b) => a.entityId.localeCompare(b.entityId)).slice(0, 60);
res.json({ total: all.length, entities: found.map((e) => ({ entityId: e.entityId, name: e.name, state: e.state, unit: e.unit, deviceClass: e.deviceClass, watched: watched.has(e.entityId) })) });
} catch (err) {
res.status(502).json({ error: "unreachable", message: err instanceof Error ? err.message : String(err) });
}
}));
const watchSchema = z.object({
entityId: z.string().regex(/^[a-z_]+\.[A-Za-z0-9_]+$/, "Not an entity id, like sensor.ups_battery").max(120),
name: z.string().trim().max(80).nullable().optional(),
above: z.number().finite().nullable().optional(),
below: z.number().finite().nullable().optional(),
});
// Watch an entity, or change what is said about one already watched (its name, its limits).
homeAssistantRouter.put("/:id/watched", requireRole("operator"), asyncHandler(async (req, res) => {
const id = idOf(req.params.id);
const parsed = watchSchema.safeParse(req.body);
if (!Number.isInteger(id) || !(await haIntegration(id))) return res.status(404).json({ error: "not_found" });
if (!parsed.success) return res.status(400).json({ error: "invalid_body", message: parsed.error.issues[0]?.message ?? "Not an entity." });
const d = parsed.data;
if (d.above != null && d.below != null && d.below >= d.above) return res.status(400).json({ error: "invalid_limits", message: "The lower limit has to be below the upper one." });
const [count] = [(await db.select({ id: haWatched.id }).from(haWatched).where(eq(haWatched.integrationId, id))).length];
const existing = (await db.select().from(haWatched).where(and(eq(haWatched.integrationId, id), eq(haWatched.entityId, d.entityId))).limit(1))[0];
if (!existing && count >= 60) return res.status(400).json({ error: "too_many", message: "That is as many as can be watched on one Home Assistant (60)." });
const values = { name: d.name?.trim() || null, above: d.above ?? null, below: d.below ?? null };
if (existing) await db.update(haWatched).set(values).where(eq(haWatched.id, existing.id));
else await db.insert(haWatched).values({ integrationId: id, entityId: d.entityId, ...values, createdAt: new Date().toISOString() });
await recordAudit({ actor: req.currentUser!, category: "integration", action: existing ? "ha_watch_changed" : "ha_watch", targetType: "integration", targetId: id, detail: { entity: d.entityId, above: values.above, below: values.below } });
const status = await syncHomeAssistant(id);
invalidateAlertsCache();
res.json({ ok: true, status });
}));
homeAssistantRouter.delete("/:id/watched/:entityId", requireRole("operator"), asyncHandler(async (req, res) => {
const id = idOf(req.params.id);
const entityId = req.params.entityId;
if (!Number.isInteger(id) || !(await haIntegration(id))) return res.status(404).json({ error: "not_found" });
const [row] = await db.select().from(haWatched).where(and(eq(haWatched.integrationId, id), eq(haWatched.entityId, entityId))).limit(1);
if (!row) return res.status(404).json({ error: "not_found" });
await db.delete(haWatched).where(eq(haWatched.id, row.id));
await db.delete(haEntities).where(and(eq(haEntities.integrationId, id), eq(haEntities.entityId, entityId)));
await db.delete(haReadings).where(and(eq(haReadings.integrationId, id), eq(haReadings.entityId, entityId)));
await recordAudit({ actor: req.currentUser!, category: "integration", action: "ha_unwatch", targetType: "integration", targetId: id, detail: { entity: entityId } });
invalidateAlertsCache();
res.status(204).end();
}));
// The chart of a watched number: up to the 35 days that are kept.
homeAssistantRouter.get("/:id/history", asyncHandler(async (req, res) => {
const id = idOf(req.params.id);
const entity = typeof req.query.entity === "string" ? req.query.entity : "";
if (!Number.isInteger(id) || !entity) return res.status(400).json({ error: "invalid_query" });
const requested = Number(req.query.days);
const days = Number.isFinite(requested) ? Math.min(READING_KEEP_DAYS, Math.max(1, Math.round(requested))) : 7;
const to = Date.now();
const from = to - days * DAY_MS;
const rows = await db.select({ at: haReadings.at, value: haReadings.value }).from(haReadings).where(and(eq(haReadings.integrationId, id), eq(haReadings.entityId, entity), gte(haReadings.at, new Date(from).toISOString()))).orderBy(haReadings.at);
res.json({ days, points: bucketPoints(rows.map((r) => ({ t: Date.parse(r.at), v: r.value })), from, to, 96) });
}));
homeAssistantRouter.post("/sync", requireRole("operator"), asyncHandler(async (_req, res) => {
const statuses = await syncAllHomeAssistants();
invalidateAlertsCache();
res.json({ statuses });
}));
+4
View File
@@ -26,6 +26,7 @@ import { createOsTicketAdapter } from "../integrations/osticket/adapter.js";
import { attachMatchedServers, summarizeMonitors, toMatchableServers } from "../services/uptimeKumaMatch.js";
import { getSourceStatus } from "../services/integrationHealthMonitor.js";
import { removeIntegrationData, syncUnifiIntegration } from "../services/unifiSync.js";
import { removeHomeAssistantData, syncHomeAssistant } from "../services/homeAssistantSync.js";
import { invalidateAlertsCache } from "../services/alerts.js";
import { asyncHandler } from "../utils/asyncHandler.js";
import { ID_PATTERNS, isValidId } from "../utils/pathSegment.js";
@@ -125,6 +126,7 @@ integrationsRouter.post("/", requireRole("admin"), asyncHandler(async (req, res)
// A new UniFi controller is read at once, so its equipment is there when the person opens the page (not waited for: a slow controller must not slow this).
if (type === "unifi") void syncUnifiIntegration(created.id).then(() => invalidateAlertsCache());
if (type === "homeassistant") void syncHomeAssistant(created.id).then(() => invalidateAlertsCache());
res.status(201).json({
integration: {
@@ -226,6 +228,7 @@ integrationsRouter.patch("/:id", requireRole("admin"), asyncHandler(async (req,
});
if (updated.type === "unifi" && updated.enabled && (parsed.data.config || parsed.data.enabled === true)) void syncUnifiIntegration(updated.id).then(() => invalidateAlertsCache());
if (updated.type === "homeassistant" && updated.enabled && (parsed.data.config || parsed.data.enabled === true)) void syncHomeAssistant(updated.id).then(() => invalidateAlertsCache());
res.json({
integration: {
@@ -303,6 +306,7 @@ integrationsRouter.delete("/:id", requireRole("admin"), asyncHandler(async (req,
.returning({ id: servers.id });
if (existing.type === "unifi") await removeIntegrationData(id);
if (existing.type === "homeassistant") await removeHomeAssistantData(id);
await db.delete(integrations).where(eq(integrations.id, id));
if (existing.credentialId) {
await db.delete(integrationCredentials).where(eq(integrationCredentials.id, existing.credentialId));
+7
View File
@@ -6,6 +6,7 @@ import { assetKinds, assets, servers } from "../db/schema.js";
import { requireAuth, requireRole } from "../auth/middleware.js";
import { recordAudit } from "../services/audit.js";
import { isRealDate, lifecycleReport } from "../services/inventory.js";
import { buildRacks } from "../services/racks.js";
import { blanksToFill, differences, hardwareOf } from "../services/inventoryAutofill.js";
import { getSettings, updateSettings } from "../services/settingsStore.js";
import { asyncHandler } from "../utils/asyncHandler.js";
@@ -88,6 +89,12 @@ inventoryRouter.get("/", asyncHandler(async (_req, res) => {
}));
// What ages out when, for planning what to replace: this year, the next few, later, and what has no date.
// The racks, drawn from the hardware that says which unit it sits in and where. Everyone signed in can see it, as they can the hardware.
inventoryRouter.get("/racks", asyncHandler(async (_req, res) => {
const rows = await db.select().from(assets);
res.json({ racks: buildRacks(rows) });
}));
inventoryRouter.get("/lifecycle", asyncHandler(async (req, res) => {
const asked = Number(req.query.years);
const years = Number.isInteger(asked) && asked >= 1 && asked <= 10 ? asked : 5;
+47
View File
@@ -0,0 +1,47 @@
import { Router } from "express";
import { db } from "../db/client.js";
import { servers } from "../db/schema.js";
import { requireAuth } from "../auth/middleware.js";
import { npmIntegrationIds, npmProblems, readNpm } from "../services/npmReader.js";
import { matchServerForTarget } from "../services/uptimeKumaMatch.js";
import { asyncHandler } from "../utils/asyncHandler.js";
export const npmRouter = Router();
npmRouter.use(requireAuth);
/**
* What Nginx Proxy Manager proxies: each host with where it forwards to (and which server that is, when the address is one the app knows), its certificate and how it is
* doing. Everyone signed in can see it (it is the same picture the alerts and suggested checks draw on). \`?refresh=1\` asks NPM again instead of using the last minute.
*/
npmRouter.get("/", asyncHandler(async (req, res) => {
const force = req.query.refresh === "1";
const rows = await db.select({ id: servers.id, name: servers.name, hostname: servers.hostname, ipAddresses: servers.ipAddresses }).from(servers);
const matchable = rows.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 };
});
const out = [];
for (const i of await npmIntegrationIds()) {
const read = await readNpm(i.id, force);
out.push({
id: i.id,
name: i.name,
readAt: read.at,
error: read.error,
version: read.snapshot?.version ?? null,
hosts: (read.snapshot?.hosts ?? []).map((h) => {
const server = matchServerForTarget(h.forwardHost, matchable);
return { ...h, server: server ? { id: server.id, name: server.name } : null };
}),
certificates: read.snapshot?.certificates ?? [],
problems: read.snapshot ? npmProblems(i.id, i.name, read.snapshot, new Date()) : [],
});
}
res.json({ integrations: out });
}));
+3 -1
View File
@@ -24,6 +24,7 @@ import { buildServerSummary } from "../services/serverSummary.js";
import { activeSubjects } from "../services/maintenance.js";
import { invalidateAlertsCache } from "../services/alerts.js";
import { getSettings, updateSettings } from "../services/settingsStore.js";
import { osSummary } from "../services/osLifecycle.js";
export const serversRouter = Router();
@@ -397,7 +398,8 @@ serversRouter.get("/:id/detail", asyncHandler(async (req, res) => {
children: [...links2.entries()].filter(([, l]) => l.parentId === id).map(([childId, l]) => ({ id: childId, name: nameOf.get(childId) ?? "", auto: l.auto })).sort((a, b) => a.name.localeCompare(b.name)),
};
res.json({ server: { ...serverOut, tags: parseTags(rawTags), pendingUpdates: parsePendingUpdates(rawUpdates), hostHealth: markIgnoredServices(parseHostHealth(rawHealth), parseThresholds(rawThresholds)?.ignoreServices), thresholds: parseThresholds(rawThresholds) }, diagnostics: describeDiagnostics(parseDiagnostics(rawDiagnostics), { osType: server.osType, lastSeenAt: server.lastSeenAt, agentVersion: server.agentVersion, latest: latestAgentVersions() }), thresholdDefaults: (await getSettings()).healthChecks, ipAddresses, hardware, dnsMatches, links, relations, agentLatest: latestAgentVersions() });
const settings = await getSettings();
res.json({ os: osSummary(server, new Date(), settings.healthChecks.osSupportWarnDays), server: { ...serverOut, tags: parseTags(rawTags), pendingUpdates: parsePendingUpdates(rawUpdates), hostHealth: markIgnoredServices(parseHostHealth(rawHealth), parseThresholds(rawThresholds)?.ignoreServices), thresholds: parseThresholds(rawThresholds) }, diagnostics: describeDiagnostics(parseDiagnostics(rawDiagnostics), { osType: server.osType, lastSeenAt: server.lastSeenAt, agentVersion: server.agentVersion, latest: latestAgentVersions() }), thresholdDefaults: (await getSettings()).healthChecks, ipAddresses, hardware, dnsMatches, links, relations, agentLatest: latestAgentVersions() });
}));
const tagsSchema = z.object({ tags: z.array(z.string().max(100)).max(50) });
+1 -1
View File
@@ -144,7 +144,7 @@ const updateSchema = z.object({
.partial()
.optional(),
healthChecks: z
.object({ serverOfflineMinutes: z.number().int().min(15).max(10080), diskUsagePercent: z.number().int().min(50).max(99), domainWarnDays: z.number().int().min(1).max(365), diskForecastDays: z.number().int().min(0).max(90), backupStaleHours: z.number().int().min(0).max(2160), restoreTestDays: z.number().int().min(0).max(730), newPortAlerts: z.boolean(), flapRaises: z.number().int().min(0).max(20), deviceOfflineMinutes: z.number().int().min(5).max(1440) })
.object({ serverOfflineMinutes: z.number().int().min(15).max(10080), diskUsagePercent: z.number().int().min(50).max(99), domainWarnDays: z.number().int().min(1).max(365), diskForecastDays: z.number().int().min(0).max(90), backupStaleHours: z.number().int().min(0).max(2160), restoreTestDays: z.number().int().min(0).max(730), newPortAlerts: z.boolean(), flapRaises: z.number().int().min(0).max(20), deviceOfflineMinutes: z.number().int().min(5).max(1440), osSupportWarnDays: z.number().int().min(0).max(365) })
.partial()
.optional(),
quietHours: z
+92
View File
@@ -0,0 +1,92 @@
import { Router } from "express";
import { z } from "zod";
import { requireAuth, requireRole } from "../auth/middleware.js";
import { recordAudit } from "../services/audit.js";
import { assignableUsers } from "../services/alertAcks.js";
import { createTodo, getNote, listTodos, removeTodo, setNote, TARGET_TYPES, TodoError, updateTodo } from "../services/todos.js";
import { asyncHandler } from "../utils/asyncHandler.js";
/** The to-do list: everyone signed in reads it; operators write. */
export const todosRouter = Router();
todosRouter.use(requireAuth);
const idOf = (v: string) => (/^\d+$/.test(v) ? Number(v) : NaN);
const label = (u: { name: string | null; email: string | null; oidcSub: string }) => u.name ?? u.email ?? u.oidcSub;
const fields = {
title: z.string(),
detail: z.string().nullable().optional(),
dueDate: z.string().nullable().optional(),
assigneeId: z.number().int().positive().nullable().optional(),
targetType: z.enum(TARGET_TYPES).nullable().optional(),
targetId: z.number().int().positive().nullable().optional(),
};
todosRouter.get("/", asyncHandler(async (_req, res) => {
res.json({ todos: await listTodos(), people: await assignableUsers() });
}));
todosRouter.post("/", requireRole("operator"), asyncHandler(async (req, res) => {
const parsed = z.object(fields).safeParse(req.body);
if (!parsed.success) return res.status(400).json({ error: "invalid_body", message: "Say what is to be done." });
try {
const actor = req.currentUser!;
const item = await createTodo(parsed.data, label(actor));
await recordAudit({ actor, category: "todo", action: "create", targetType: "todo", targetId: item.id, detail: { title: item.title, dueDate: item.dueDate, assignee: item.assignee?.name ?? null, about: item.target?.name ?? null } });
res.status(201).json({ todo: item });
} catch (err) {
if (err instanceof TodoError) return res.status(err.status).json({ error: err.code, message: err.message });
throw err;
}
}));
todosRouter.patch("/:id", requireRole("operator"), asyncHandler(async (req, res) => {
const id = idOf(req.params.id);
const parsed = z.object({ ...fields, title: z.string().optional(), done: z.boolean().optional() }).safeParse(req.body);
if (!Number.isInteger(id) || !parsed.success) return res.status(400).json({ error: "invalid_body" });
try {
const actor = req.currentUser!;
const result = await updateTodo(id, parsed.data, label(actor));
if (!result) return res.status(404).json({ error: "not_found" });
const action = parsed.data.done === true && !result.before.doneAt ? "done" : parsed.data.done === false && result.before.doneAt ? "reopen" : "update";
await recordAudit({ actor, category: "todo", action, targetType: "todo", targetId: id, detail: { title: result.after.title } });
res.json({ todo: result.after });
} catch (err) {
if (err instanceof TodoError) return res.status(err.status).json({ error: err.code, message: err.message });
throw err;
}
}));
todosRouter.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 removed = await removeTodo(id);
if (!removed) return res.status(404).json({ error: "not_found" });
await recordAudit({ actor: req.currentUser!, category: "todo", action: "delete", targetType: "todo", targetId: id, detail: { title: removed.title } });
res.status(204).end();
}));
/** The notes on servers and devices: everyone reads; operators write. */
export const notesRouter = Router();
notesRouter.use(requireAuth);
notesRouter.get("/", asyncHandler(async (req, res) => {
const type = z.enum(TARGET_TYPES).safeParse(req.query.targetType);
const id = idOf(String(req.query.targetId ?? ""));
if (!type.success || !Number.isInteger(id)) return res.status(400).json({ error: "invalid_target" });
res.json({ note: await getNote(type.data, id) });
}));
notesRouter.put("/", requireRole("operator"), asyncHandler(async (req, res) => {
const parsed = z.object({ targetType: z.enum(TARGET_TYPES), targetId: z.number().int().positive(), body: z.string() }).safeParse(req.body);
if (!parsed.success) return res.status(400).json({ error: "invalid_body" });
try {
const actor = req.currentUser!;
const note = await setNote(parsed.data.targetType, parsed.data.targetId, parsed.data.body, label(actor));
await recordAudit({ actor, category: "note", action: note ? "save" : "delete", targetType: parsed.data.targetType, targetId: parsed.data.targetId });
res.json({ note });
} catch (err) {
if (err instanceof TodoError) return res.status(err.status).json({ error: err.code, message: err.message });
throw err;
}
}));
+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);
}
+2 -2
View File
@@ -129,7 +129,7 @@ describe("through a report, the page, the alerts and the figures", () => {
before(async () => {
await db.insert(users).values([{ oidcSub: "admin", name: "Ada", role: "admin" }]);
const [s] = await db.insert(servers).values({ name: "elsa", apiTokenHash: hashToken("tok"), apiTokenPrefix: "t", lastSeenAt: new Date().toISOString(), agentVersion: 6 }).returning();
const [s] = await db.insert(servers).values({ name: "elsa", apiTokenHash: hashToken("tok"), apiTokenPrefix: "t", lastSeenAt: new Date().toISOString(), agentVersion: 7 }).returning();
id = s.id;
const a = express();
a.use("/api/agent/report", agentReportRouter);
@@ -155,7 +155,7 @@ describe("through a report, the page, the alerts and the figures", () => {
});
const report = (system: unknown) =>
fetch(`${base}/agent/report`, { method: "POST", headers: { "content-type": "application/json", authorization: "Bearer tok" }, body: JSON.stringify({ hostname: "h", os_type: "linux", agent_version: 6, tasks: [], system }) });
fetch(`${base}/agent/report`, { method: "POST", headers: { "content-type": "application/json", authorization: "Bearer tok" }, body: JSON.stringify({ hostname: "h", os_type: "linux", agent_version: 7, tasks: [], system }) });
const detail = async () => await (await fetch(`${base}/servers/${id}/detail`)).json();
const patch = (body: unknown) => fetch(`${base}/servers/${id}`, { method: "PATCH", headers: { "content-type": "application/json" }, body: JSON.stringify(body) });
+137
View File
@@ -0,0 +1,137 @@
/**
* Suggested dependencies: an integration reached at a server's own address is offered as needing that server; what is already so, would make a loop, or was
* said no to is left out; and adding one writes an ordinary dependency.
*
* Run with: npm test --workspace server
*/
import { test, describe, before, after } from "node:test";
import assert from "node:assert/strict";
import { mkdtempSync, rmSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import type { AddressInfo } from "node:net";
import type { Server } from "node:http";
const dir = mkdtempSync(join(tmpdir(), "hm-depsug-"));
process.env.DATABASE_PATH = join(dir, "test.sqlite");
process.env.SESSION_DIR = join(dir, "sessions");
process.env.CREDENTIALS_ENCRYPTION_KEY = "ab".repeat(32);
const express = (await import("express")).default;
const { db } = await import("../src/db/client.js");
const { users, servers, integrations, dependencies, auditLog } = await import("../src/db/schema.js");
const { runMigrations } = await import("../src/db/migrate.js");
const { suggestDependencies, hostOfIntegration } = await import("../src/services/dependencySuggestions.js");
const { dependenciesRouter } = await import("../src/routes/dependencies.js");
const { eq } = await import("drizzle-orm");
await runMigrations();
describe("what is offered", () => {
const servers = [
{ id: 1, name: "nas", hostname: "nas.lan", ips: ["10.0.0.5"] },
{ id: 2, name: "elsa", hostname: null, ips: ["10.0.0.9"] },
];
const integ = (id: number, name: string, type: string, host: string | null, enabled = true) => ({ id, name, type, enabled, host });
const none = new Map<string, string[]>();
test("an integration at a server's own address (its IP, hostname or short name) needs that server", () => {
const { suggestions } = suggestDependencies({
integrations: [integ(1, "Gitea", "gitea", "10.0.0.5"), integ(2, "Kuma", "uptimekuma", "nas.lan"), integ(3, "IPAM", "phpipam", "elsa"), integ(4, "Cloud", "tailscale", "api.tailscale.com")],
servers,
edges: none,
dismissed: new Set(),
});
assert.deepEqual(suggestions.map((x) => x.key), ["integration:1>server:1", "integration:3>server:2", "integration:2>server:1"], "by name (Gitea, IPAM, Kuma); the one in the cloud matches nothing");
assert.ok(suggestions.every((x) => x.reason.includes("most likely runs there")));
});
test("a disabled integration, one with no address, and one with an unknown host are not offered", () => {
const { suggestions } = suggestDependencies({ integrations: [integ(1, "Off", "gitea", "10.0.0.5", false), integ(2, "None", "gitea", null), integ(3, "Elsewhere", "gitea", "10.9.9.9")], servers, edges: none, dismissed: new Set() });
assert.equal(suggestions.length, 0);
});
test("what is already so, and what would make a loop, is left out", () => {
const already = new Map([["integration:1", ["server:1"]]]);
assert.equal(suggestDependencies({ integrations: [integ(1, "Gitea", "gitea", "10.0.0.5")], servers, edges: already, dismissed: new Set() }).suggestions.length, 0);
// the server needs the integration already: the other way round would make them wait for each other
const loop = new Map([["server:1", ["integration:1"]]]);
assert.equal(suggestDependencies({ integrations: [integ(1, "Gitea", "gitea", "10.0.0.5")], servers, edges: loop, dismissed: new Set() }).suggestions.length, 0);
});
test("what somebody said no to is kept apart, and can come back", () => {
const r = suggestDependencies({ integrations: [integ(1, "Gitea", "gitea", "10.0.0.5")], servers, edges: none, dismissed: new Set(["integration:1>server:1"]) });
assert.deepEqual([r.suggestions.length, r.dismissed.length], [0, 1]);
});
test("where an integration is reached: a URL's host, or osTicket's database host", () => {
assert.equal(hostOfIntegration("https://git.example.lan:3000/", "{}"), "git.example.lan");
assert.equal(hostOfIntegration("", JSON.stringify({ host: "osticket-db.lan" })), "osticket-db.lan");
assert.equal(hostOfIntegration("not a url", null), null);
assert.equal(hostOfIntegration(null, "not json"), null);
});
});
describe("over the routes", () => {
let app: Server;
let base = "";
let who = "viewer";
before(async () => {
await db.insert(users).values([
{ oidcSub: "viewer", name: "Vic", role: "viewer" },
{ oidcSub: "operator", name: "Olga", role: "operator" },
]);
await db.insert(servers).values({ name: "nas", hostname: "nas.lan", apiTokenHash: "n", apiTokenPrefix: "n", ipAddresses: JSON.stringify(["10.0.0.5"]) });
await db.insert(integrations).values({ type: "gitea", name: "Gitea", baseUrl: "http://10.0.0.5:3000", config: JSON.stringify({ url: "http://10.0.0.5:3000" }) });
const a = express();
a.use(express.json());
a.use((req, _res, next) => {
(req as unknown as { session: unknown }).session = { user: { sub: who } };
next();
});
a.use("/api/dependencies", dependenciesRouter);
app = await new Promise<Server>((resolve) => {
const s = a.listen(0, "127.0.0.1", () => resolve(s));
});
base = `http://127.0.0.1:${(app.address() as AddressInfo).port}/api/dependencies`;
});
after(() => {
app.close();
try {
rmSync(dir, { recursive: true, force: true });
} catch {
// Windows may still hold the database file
}
});
const post = (path: string, body: unknown = {}) => fetch(`${base}${path}`, { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify(body) });
test("anyone signed in sees them; only an operator adds or dismisses", async () => {
who = "viewer";
const body = (await (await fetch(`${base}/suggestions`)).json()) as any;
assert.deepEqual(body.suggestions.map((s: any) => s.key), ["integration:1>server:1"]);
assert.equal((await post("/suggestions/add", { keys: ["integration:1>server:1"] })).status, 403);
assert.equal((await post("/suggestions/dismiss", { keys: ["integration:1>server:1"] })).status, 403);
});
test("dismissing hides it, restoring brings it back, and a bad body is refused", async () => {
who = "operator";
const hidden = (await (await post("/suggestions/dismiss", { keys: ["integration:1>server:1"] })).json()) as any;
assert.deepEqual([hidden.suggestions.length, hidden.dismissed.length], [0, 1]);
const back = (await (await post("/suggestions/restore")).json()) as any;
assert.equal(back.suggestions.length, 1);
assert.equal((await post("/suggestions/add", { keys: [] })).status, 400);
});
test("adding writes an ordinary dependency, written down, and it is no longer offered", async () => {
who = "operator";
const res = (await (await post("/suggestions/add", { keys: ["integration:1>server:1", "integration:9>server:9"] })).json()) as any;
assert.deepEqual(res.added.map((a: any) => [a.from, a.to]), [["Gitea", "nas"]], "a key that is not offered adds nothing");
assert.equal(res.suggestions.length, 0);
const rows = await db.select().from(dependencies);
assert.deepEqual(rows.map((r) => [r.fromType, r.fromId, r.toType, r.toId]), [["integration", 1, "server", 1]]);
const written = (await db.select().from(auditLog).where(eq(auditLog.category, "dependency")))[0];
assert.equal(written.action, "create");
const list = (await (await fetch(base)).json()) as any;
assert.equal(list.dependencies.length, 1, "and the page lists it with the rest");
});
});
+415
View File
@@ -0,0 +1,415 @@
/**
* Nginx Proxy Manager and Home Assistant: reading each (against pretend ones), what is kept, what makes an alert, and the suggestions NPM feeds.
*
* Run with: npm test --workspace server
*/
import { test, describe, before, after } from "node:test";
import assert from "node:assert/strict";
import { mkdtempSync, rmSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { createServer, type IncomingMessage, type Server, type ServerResponse } from "node:http";
import type { AddressInfo } from "node:net";
const dir = mkdtempSync(join(tmpdir(), "hm-npmha-"));
process.env.DATABASE_PATH = join(dir, "test.sqlite");
process.env.SESSION_DIR = join(dir, "sessions");
process.env.CREDENTIALS_ENCRYPTION_KEY = "ab".repeat(32);
process.env.HEALTH_STARTUP_GRACE_MS = "0";
const express = (await import("express")).default;
const { db } = await import("../src/db/client.js");
const { users, servers, integrations, integrationCredentials, haEntities, haReadings, haWatched } = await import("../src/db/schema.js");
const { runMigrations } = await import("../src/db/migrate.js");
const { encryptSecret } = await import("../src/crypto.js");
const { createNpmAdapter, isoOf, hostFrom } = await import("../src/integrations/npm/adapter.js");
const { createHomeAssistantAdapter, entityFrom } = await import("../src/integrations/homeassistant/adapter.js");
const { npmProblems, readNpm, clearNpmCacheForTests } = await import("../src/services/npmReader.js");
const { haProblems, trackUnavailable, syncHomeAssistant, removeHomeAssistantData, isHaFresh, haSyncStatuses } = await import("../src/services/homeAssistantSync.js");
const { suggestChecks, loadSuggestions } = await import("../src/services/endpointSuggestions.js");
const { getAlerts, invalidateAlertsCache } = await import("../src/services/alerts.js");
const { npmRouter } = await import("../src/routes/npm.js");
const { homeAssistantRouter } = await import("../src/routes/homeAssistant.js");
const { validateIntegrationConfig } = await import("../src/integrations/fieldSchemas.js");
const { eq } = await import("drizzle-orm");
await runMigrations();
const DAY = 86_400_000;
const inDays = (n: number) => new Date(Date.now() + n * DAY).toISOString();
const npmTime = (n: number) => inDays(n).replace("T", " ").slice(0, 19);
// ─── A pretend Nginx Proxy Manager ──────────────────────────────────────────
let npmPassword = "pw";
const npmCertificates = [
{ id: 1, nice_name: "wiki.example.com", provider: "letsencrypt", domain_names: ["wiki.example.com"], expires_on: npmTime(40) },
{ id: 2, nice_name: "git.example.com", provider: "letsencrypt", domain_names: ["git.example.com"], expires_on: npmTime(2) },
{ id: 3, nice_name: "old.example.com", provider: "letsencrypt", domain_names: ["old.example.com"], expires_on: npmTime(-5) },
];
const npmHosts = [
{ id: 10, domain_names: ["wiki.example.com"], forward_scheme: "http", forward_host: "10.0.0.5", forward_port: 8080, enabled: 1, ssl_forced: 1, certificate: npmCertificates[0], meta: { nginx_online: true } },
{ id: 11, domain_names: ["git.example.com"], forward_scheme: "http", forward_host: "10.0.0.6", forward_port: 3000, enabled: 1, ssl_forced: 1, certificate: npmCertificates[1], meta: { nginx_online: true } },
// A certificate that ran out, but on a host that is switched off: nobody is served it, so it is not a problem.
{ id: 12, domain_names: ["old.example.com"], forward_scheme: "http", forward_host: "10.0.0.7", forward_port: 80, enabled: 0, ssl_forced: 0, certificate: npmCertificates[2], meta: {} },
{ id: 13, domain_names: ["broken.example.com"], forward_scheme: "http", forward_host: "10.0.0.8", forward_port: 80, enabled: 1, ssl_forced: 0, certificate: null, meta: { nginx_online: false, nginx_err: "host not found in upstream" } },
];
let npmRequests: string[] = [];
function reply(res: ServerResponse, status: number, body: unknown) {
res.writeHead(status, { "content-type": "application/json" });
res.end(JSON.stringify(body));
}
function npmRoute(req: IncomingMessage, res: ServerResponse) {
const url = req.url ?? "";
npmRequests.push(`${req.method} ${url.split("?")[0]}`);
if (url === "/api/tokens" && req.method === "POST") {
let raw = "";
req.on("data", (c) => (raw += c));
req.on("end", () => {
const b = JSON.parse(raw || "{}");
if (b.identity === "viewer@example.com" && b.secret === npmPassword) return reply(res, 200, { token: "npm-token", expires: inDays(1) });
return reply(res, 400, { error: { message: "Invalid email or password" } });
});
return;
}
if (req.headers.authorization !== "Bearer npm-token") return reply(res, 401, {});
if (url.startsWith("/api/nginx/proxy-hosts")) return reply(res, 200, npmHosts);
if (url.startsWith("/api/nginx/certificates")) return reply(res, 200, npmCertificates);
if (url === "/api/") return reply(res, 200, { status: "OK", version: { major: 2, minor: 11, revision: 3 } });
return reply(res, 404, {});
}
// ─── A pretend Home Assistant ───────────────────────────────────────────────
let haToken = "good-token";
let haStates: Record<string, unknown>[] = [];
const state = (entity_id: string, s: string, attributes: Record<string, unknown> = {}) => ({ entity_id, state: s, attributes, last_changed: "2026-10-20T08:00:00+00:00" });
function haRoute(req: IncomingMessage, res: ServerResponse) {
if (req.headers.authorization !== `Bearer ${haToken}`) return reply(res, 401, { message: "401: Unauthorized" });
if (req.url === "/api/") return reply(res, 200, { message: "API running." });
if (req.url === "/api/states") return reply(res, 200, haStates);
return reply(res, 404, {});
}
let npmServer: Server;
let haServer: Server;
let npmUrl = "";
let haUrl = "";
let npmId = 0;
let haId = 0;
before(async () => {
npmServer = await new Promise<Server>((resolve) => {
const s = createServer(npmRoute).listen(0, "127.0.0.1", () => resolve(s));
});
haServer = await new Promise<Server>((resolve) => {
const s = createServer(haRoute).listen(0, "127.0.0.1", () => resolve(s));
});
npmUrl = `http://127.0.0.1:${(npmServer.address() as AddressInfo).port}`;
haUrl = `http://127.0.0.1:${(haServer.address() as AddressInfo).port}`;
});
after(() => {
npmServer.close();
haServer.close();
try {
rmSync(dir, { recursive: true, force: true });
} catch {
// Windows may still hold the database file
}
});
describe("normalising what they say", () => {
test("NPM writes its times without a zone, and they are UTC", () => {
assert.equal(isoOf("2026-12-31 23:59:59"), "2026-12-31T23:59:59.000Z");
assert.equal(isoOf("2026-12-31T23:59:59+01:00"), "2026-12-31T22:59:59.000Z");
assert.equal(isoOf("nonsense"), null);
assert.equal(isoOf(null), null);
});
test("a proxy host: where it goes, whether it is on, and that NPM's own error makes it not working; a host with no id is nothing", () => {
const h = hostFrom(npmHosts[3])!;
assert.deepEqual([h.domains, h.forwardHost, h.forwardPort, h.enabled, h.online, h.error], [["broken.example.com"], "10.0.0.8", 80, true, false, "host not found in upstream"]);
const ok = hostFrom(npmHosts[0])!;
assert.deepEqual([ok.sslForced, ok.online, ok.certificate?.id], [true, true, 1]);
assert.equal(hostFrom({ domain_names: ["x"] }), null);
assert.equal(hostFrom(npmHosts[2])!.enabled, false);
});
test("a Home Assistant entity: a number when its state is one, unavailable when it says nothing, its friendly name or else its id", () => {
const e = entityFrom(state("sensor.ups_battery", "87.5", { friendly_name: "UPS battery", unit_of_measurement: "%", device_class: "battery" }))!;
assert.deepEqual([e.name, e.value, e.unit, e.deviceClass, e.unavailable], ["UPS battery", 87.5, "%", "battery", false]);
const off = entityFrom(state("switch.pump", "unavailable"))!;
assert.deepEqual([off.name, off.value, off.unavailable], ["switch.pump", null, true]);
assert.equal(entityFrom(state("light.x", "on"))!.value, null, "on/off is not a number");
assert.equal(entityFrom(state("sensor.blank", ""))!.value, null);
assert.equal(entityFrom({ state: "1" }), null);
});
});
describe("Nginx Proxy Manager", () => {
const cfg = (extra: Record<string, unknown> = {}) => ({ url: npmUrl, email: "viewer@example.com", password: npmPassword, ...extra }) as never;
test("signs in, then only reads: hosts with their certificate, the certificates, the version", async () => {
npmRequests = [];
const snap = await createNpmAdapter(cfg()).fetchSnapshot();
assert.equal(snap.version, "2.11.3");
assert.equal(snap.hosts.length, 4);
assert.equal(snap.certificates.length, 3);
assert.ok(npmRequests.every((r) => r.startsWith("GET") || r === "POST /api/tokens"), "nothing but the sign-in and reads");
});
test("a wrong password is said plainly, and the login check works as a ping", async () => {
await assert.rejects(createNpmAdapter(cfg({ password: "wrong" })).fetchSnapshot(), /did not accept the email and password/);
assert.equal((await createNpmAdapter(cfg()).ping()).ok, true);
assert.equal((await createNpmAdapter(cfg({ password: "wrong" })).ping()).ok, false);
});
test("certificates in use that run out soon are a problem — critical in the last days, and an expired one too — but not on a host that is off", async () => {
const snap = await createNpmAdapter(cfg()).fetchSnapshot();
const problems = npmProblems(1, "Edge NPM", snap, new Date());
const by = Object.fromEntries(problems.map((p) => [p.key, p]));
assert.deepEqual(Object.keys(by).sort(), ["npm-cert:1:2", "npm-host:1:13"], "wiki is fine (40 days), old is on a host that is off");
assert.equal(by["npm-cert:1:2"].severity, "critical");
assert.match(by["npm-cert:1:2"].message, /git\.example\.com.*expires in [12] days?/);
assert.match(by["npm-host:1:13"].message, /broken\.example\.com is not working.*host not found in upstream/);
const later = npmProblems(1, "Edge NPM", snap, new Date(Date.now() + 30 * DAY));
assert.ok(later.some((p) => p.key === "npm-cert:1:1"), "and the wiki one in time");
assert.match(later.find((p) => p.key === "npm-cert:1:2")!.message, /expired 2[89] days ago/);
});
});
describe("Home Assistant", () => {
const cfg = (extra: Record<string, unknown> = {}) => ({ url: haUrl, token: haToken, ...extra }) as never;
test("lists every entity with the token; a refused token and a wrong address are said plainly", async () => {
haStates = [state("sensor.ups_battery", "87", { friendly_name: "UPS battery", unit_of_measurement: "%" }), state("light.hall", "on")];
assert.equal((await createHomeAssistantAdapter(cfg()).listEntities()).length, 2);
await assert.rejects(createHomeAssistantAdapter(cfg({ token: "nope" })).listEntities(), /did not accept the token/);
const ping = await createHomeAssistantAdapter(cfg()).ping();
assert.equal(ping.ok, true);
});
test("an entity that says nothing is only a problem after a while, and a number only past a limit that was set", () => {
const now = new Date("2026-10-20T12:00:00Z");
const watched = [
{ entityId: "sensor.ups", name: "UPS", above: null, below: 30 },
{ entityId: "sensor.freezer", name: null, above: -10, below: null },
{ entityId: "switch.pump", name: "Pump", above: null, below: null },
{ entityId: "sensor.fine", name: null, above: 50, below: 10 },
];
const rows = new Map<string, { friendlyName: string | null; state: string; value: number | null; unit: string | null; unavailableSince: string | null }>([
["sensor.ups", { friendlyName: "UPS battery", state: "22", value: 22, unit: "%", unavailableSince: null }],
["sensor.freezer", { friendlyName: "Freezer", state: "-18", value: -18, unit: "°C", unavailableSince: null }],
["switch.pump", { friendlyName: null, state: "unavailable", value: null, unit: null, unavailableSince: "2026-10-20T11:30:00Z" }],
["sensor.fine", { friendlyName: null, state: "20", value: 20, unit: null, unavailableSince: null }],
]);
const problems = haProblems(3, "Home", watched, rows, now);
assert.deepEqual(problems.map((p) => p.key), ["ha-limit:3:sensor.ups", "ha-unavailable:3:switch.pump"]);
assert.match(problems[0].message, /UPS is 22 %, below its limit of 30 %/);
assert.match(problems[1].message, /Pump has been unavailable in Home Assistant for 30 min/);
rows.get("switch.pump")!.unavailableSince = "2026-10-20T11:55:00Z";
assert.equal(haProblems(3, "Home", watched, rows, now).length, 1, "five minutes of silence is nothing");
});
test("when an entity first went quiet is kept until it speaks again", () => {
assert.equal(trackUnavailable(null, true, "T1"), "T1");
assert.equal(trackUnavailable("T0", true, "T1"), "T0");
assert.equal(trackUnavailable("T0", false, "T1"), null);
});
});
describe("kept in the app", () => {
before(async () => {
await db.insert(users).values([
{ oidcSub: "viewer", name: "Vic", role: "viewer" },
{ oidcSub: "operator", name: "Olga", role: "operator" },
]);
const [npmCred] = await db.insert(integrationCredentials).values({ name: "npm:Edge", encryptedSecret: encryptSecret(JSON.stringify({ password: "pw" })) }).returning();
const [npmRow] = await db.insert(integrations).values({ type: "npm", name: "Edge NPM", baseUrl: npmUrl, credentialId: npmCred.id, config: JSON.stringify({ url: npmUrl, email: "viewer@example.com" }) }).returning();
npmId = npmRow.id;
const [haCred] = await db.insert(integrationCredentials).values({ name: "ha:Home", encryptedSecret: encryptSecret(JSON.stringify({ token: "good-token" })) }).returning();
const [haRow] = await db.insert(integrations).values({ type: "homeassistant", name: "Home", baseUrl: haUrl, credentialId: haCred.id, config: JSON.stringify({ url: haUrl }) }).returning();
haId = haRow.id;
await db.insert(servers).values({ name: "wiki-host", hostname: "wiki-host.example.lan", ipAddresses: JSON.stringify(["10.0.0.5"]), osType: "linux", apiTokenHash: "x", apiTokenPrefix: "x" } as never);
});
test("the field forms need what each one needs", () => {
assert.deepEqual(validateIntegrationConfig("npm", { url: "https://npm", email: "a@b" }), ["password"]);
assert.deepEqual(validateIntegrationConfig("homeassistant", { url: "https://ha" }), ["token"]);
assert.deepEqual(validateIntegrationConfig("homeassistant", { url: "https://ha", token: "t" }), []);
});
test("NPM is read once a minute at most, and a failed read is kept just as briefly", async () => {
clearNpmCacheForTests();
npmRequests = [];
const a = await readNpm(npmId);
const b = await readNpm(npmId);
assert.ok(a.snapshot && b.snapshot);
assert.equal(npmRequests.filter((r) => r === "POST /api/tokens").length, 1);
await readNpm(npmId, true);
assert.equal(npmRequests.filter((r) => r === "POST /api/tokens").length, 2, "asking again is possible");
});
test("on the Alerts page: the certificate in its last days and the host NPM can't set up, from the right place", async () => {
clearNpmCacheForTests();
invalidateAlertsCache();
const report = await getAlerts(true);
const cert = report.alerts.find((a) => a.id === `expiry:npm-cert:${npmId}:2`)!;
assert.ok(cert, "the certificate that runs out");
assert.deepEqual([cert.severity, cert.source, cert.link, cert.target?.type], ["critical", "Nginx Proxy Manager", "/proxy-hosts", "integration"]);
const host = report.alerts.find((a) => a.id === `monitoring:npm-host:${npmId}:13`)!;
assert.ok(host);
assert.equal(report.alerts.some((a) => a.id === `expiry:npm-cert:${npmId}:3`), false, "the one on the host that is off is left alone");
});
test("an NPM that can't be read says so rather than being quiet", async () => {
npmPassword = "changed";
clearNpmCacheForTests();
invalidateAlertsCache();
const report = await getAlerts(true);
assert.ok(report.couldntCheck.some((c) => /Nginx Proxy Manager/.test(c.name)));
assert.equal(report.alerts.some((a) => a.id.startsWith("expiry:npm-cert")), false, "and raises nothing from what it could not read");
npmPassword = "pw";
clearNpmCacheForTests();
});
test("hosts NPM serves are offered as checks on the name people use, linked to the server it forwards to — and not when a check already covers it", async () => {
invalidateAlertsCache();
const { suggestions } = await loadSuggestions();
const wiki = suggestions.find((s) => s.target === "https://wiki.example.com/")!;
assert.ok(wiki);
assert.deepEqual([wiki.kind, wiki.serverName], ["http", "wiki-host"]);
assert.match(wiki.reason, /Edge NPM serves wiki\.example\.com, forwarding to wiki-host/);
assert.ok(suggestions.some((s) => s.target === "http://broken.example.com/"), "a host with no certificate is a plain web check");
assert.ok(!suggestions.some((s) => /old\.example\.com/.test(s.target)), "a host that is off is not offered");
const none = suggestChecks({ servers: [], dns: [], secrets: [], dismissed: new Set(), existing: [{ kind: "http", target: "https://wiki.example.com/", port: null }], proxied: [{ domain: "wiki.example.com", secure: true, serverId: null, serverName: null, integrationName: "x" }, { domain: "*.example.com", secure: true, serverId: null, serverName: null, integrationName: "x" }, { domain: "10.0.0.1", secure: false, serverId: null, serverName: null, integrationName: "x" }] });
assert.deepEqual(none.suggestions, []);
});
test("the NPM page: each host with the server it forwards to, its certificate, and the problems; anyone signed in may see it", async () => {
const a = express();
a.use(express.json());
a.use((req, _res, next) => {
(req as unknown as { session: unknown }).session = { user: { sub: "viewer" } };
next();
});
a.use("/api/npm", npmRouter);
const s = await new Promise<Server>((resolve) => {
const x = a.listen(0, "127.0.0.1", () => resolve(x));
});
const res = await fetch(`http://127.0.0.1:${(s.address() as AddressInfo).port}/api/npm?refresh=1`);
assert.equal(res.status, 200);
const body = (await res.json()) as any;
const npm = body.integrations[0];
assert.equal(npm.version, "2.11.3");
const wiki = npm.hosts.find((h: any) => h.id === 10);
assert.deepEqual([wiki.server?.name, wiki.certificate.name], ["wiki-host", "wiki.example.com"]);
assert.equal(npm.hosts.find((h: any) => h.id === 11).server, null, "an address no server has is left unmatched");
assert.equal(npm.problems.length, 2);
s.close();
});
describe("Home Assistant stored", () => {
let app: Server;
let base = "";
let who = "viewer";
before(async () => {
const a = express();
a.use(express.json());
a.use((req, _res, next) => {
(req as unknown as { session: unknown }).session = { user: { sub: who } };
next();
});
a.use("/api/home-assistant", homeAssistantRouter);
app = await new Promise<Server>((resolve) => {
const s = a.listen(0, "127.0.0.1", () => resolve(s));
});
base = `http://127.0.0.1:${(app.address() as AddressInfo).port}/api/home-assistant`;
});
after(() => app.close());
const put = (body: unknown) => fetch(`${base}/${haId}/watched`, { method: "PUT", headers: { "content-type": "application/json" }, body: JSON.stringify(body) });
test("choosing what to watch needs an operator; only entities that exist and sensible limits are accepted", async () => {
haStates = [state("sensor.ups_battery", "87", { friendly_name: "UPS battery", unit_of_measurement: "%" }), state("switch.pump", "on", { friendly_name: "Pump" }), state("light.hall", "on")];
who = "viewer";
assert.equal((await put({ entityId: "sensor.ups_battery" })).status, 403);
assert.equal((await fetch(`${base}/${haId}/entities`)).status, 403);
who = "operator";
assert.equal((await put({ entityId: "not an entity" })).status, 400);
assert.equal((await put({ entityId: "sensor.ups_battery", above: 10, below: 20 })).status, 400, "the lower limit has to be lower");
assert.equal((await fetch(`${base}/999/entities`)).status, 404);
const list = (await (await fetch(`${base}/${haId}/entities?q=ups`)).json()) as any;
assert.deepEqual(list.entities.map((e: any) => e.entityId), ["sensor.ups_battery"]);
const ok = await put({ entityId: "sensor.ups_battery", name: "UPS", below: 30 });
assert.equal(ok.status, 200);
assert.equal(((await ok.json()) as any).status.ok, true);
assert.equal((await put({ entityId: "switch.pump" })).status, 200);
const [row] = await db.select().from(haEntities).where(eq(haEntities.entityId, "sensor.ups_battery"));
assert.deepEqual([row.state, row.value, row.unit], ["87", 87, "%"]);
});
test("the page lists what is watched with its state; a number keeps a point of history, but two passes close together keep one", async () => {
who = "viewer";
const body = (await (await fetch(base)).json()) as any;
const ha = body.integrations[0];
assert.deepEqual(ha.watched.map((w) => [w.entityId, w.entity?.state]), [["switch.pump", "on"], ["sensor.ups_battery", "87"]], "by the name it is shown under: Pump, then UPS");
assert.equal(ha.fresh, true);
await syncHomeAssistant(haId);
const readings = await db.select().from(haReadings).where(eq(haReadings.entityId, "sensor.ups_battery"));
assert.equal(readings.length, 1, "the pump is not a number, the second pass is too soon");
const hist = (await (await fetch(`${base}/${haId}/history?entity=sensor.ups_battery&days=7`)).json()) as any;
assert.equal(hist.days, 7);
assert.ok(hist.points.length >= 1);
});
test("a number past its limit is an alert, an entity that goes quiet becomes one after a while, and a Home Assistant that is down holds them rather than clearing", async () => {
haStates = [state("sensor.ups_battery", "22", { friendly_name: "UPS battery", unit_of_measurement: "%" }), state("switch.pump", "unavailable", { friendly_name: "Pump" })];
await syncHomeAssistant(haId, new Date());
invalidateAlertsCache();
let report = await getAlerts(true);
const limit = report.alerts.find((a) => a.id === `monitoring:ha-limit:${haId}:sensor.ups_battery`)!;
assert.ok(limit);
assert.deepEqual([limit.source, limit.link], ["Home Assistant", "/home-assistant"]);
assert.equal(report.alerts.some((a) => a.id.includes("ha-unavailable")), false, "the pump just went quiet");
await db.update(haEntities).set({ unavailableSince: new Date(Date.now() - 40 * 60_000).toISOString() }).where(eq(haEntities.entityId, "switch.pump"));
invalidateAlertsCache();
report = await getAlerts(true);
assert.ok(report.alerts.some((a) => a.id === `monitoring:ha-unavailable:${haId}:switch.pump`));
haToken = "revoked";
const down = await syncHomeAssistant(haId);
assert.equal(down.ok, false);
assert.match(down.error ?? "", /did not accept the token/);
invalidateAlertsCache();
report = await getAlerts(true);
assert.ok(report.couldntCheck.some((c) => /Home Assistant/.test(c.name)));
assert.equal(report.alerts.some((a) => a.id.includes("ha-limit")), false, "neither raised nor cleared while it can't be read");
assert.equal(isHaFresh((await haSyncStatuses())[haId], Date.now()), false);
haToken = "good-token";
assert.equal((await syncHomeAssistant(haId)).ok, true);
});
test("an entity that disappears from Home Assistant counts as quiet; stopping watching forgets it and its history", async () => {
haStates = [state("sensor.ups_battery", "50", { unit_of_measurement: "%" })];
await syncHomeAssistant(haId);
const [pump] = await db.select().from(haEntities).where(eq(haEntities.entityId, "switch.pump"));
assert.equal(pump.state, "missing");
who = "operator";
assert.equal((await fetch(`${base}/${haId}/watched/sensor.ups_battery`, { method: "DELETE" })).status, 204);
assert.equal((await db.select().from(haReadings).where(eq(haReadings.entityId, "sensor.ups_battery"))).length, 0);
assert.equal((await fetch(`${base}/${haId}/watched/sensor.ups_battery`, { method: "DELETE" })).status, 404);
});
test("removing the integration removes everything read from it", async () => {
await removeHomeAssistantData(haId);
assert.equal((await db.select().from(haWatched)).length, 0);
assert.equal((await db.select().from(haEntities)).length, 0);
assert.equal((await haSyncStatuses())[haId], undefined);
assert.equal((await db.select({ id: integrationCredentials.id }).from(integrationCredentials)).length >= 1, true);
});
});
});
+140
View File
@@ -0,0 +1,140 @@
/**
* When the operating system a server runs stops being supported: what the table knows, how an agent's answer is read, the alert, and that the agent's own
* script says it (the shell part is checked by reading it; the answer it would give is a plain JSON object).
*
* Run with: npm test --workspace server
*/
import { test, describe, before, after } from "node:test";
import assert from "node:assert/strict";
import { mkdtempSync, readFileSync, rmSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { fileURLToPath } from "node:url";
const dir = mkdtempSync(join(tmpdir(), "hm-oslife-"));
process.env.DATABASE_PATH = join(dir, "test.sqlite");
process.env.SESSION_DIR = join(dir, "sessions");
process.env.CREDENTIALS_ENCRYPTION_KEY = "ab".repeat(32);
process.env.HEALTH_STARTUP_GRACE_MS = "0";
const { db } = await import("../src/db/client.js");
const { servers, maintenanceWindows } = await import("../src/db/schema.js");
const { runMigrations } = await import("../src/db/migrate.js");
const { lifecycleOf, releaseKey, supportState, daysOfSupportLeft } = await import("../src/services/osLifecycle.js");
const { syncServerTasks } = await import("../src/services/taskSync.js");
const { getAlerts, invalidateAlertsCache } = await import("../src/services/alerts.js");
const { updateSettings } = await import("../src/services/settingsStore.js");
const { latestAgentVersions } = await import("../src/services/agentVersions.js");
const { eq } = await import("drizzle-orm");
await runMigrations();
const repo = fileURLToPath(new URL("../../", import.meta.url));
after(() => {
try {
rmSync(dir, { recursive: true, force: true });
} catch {
// Windows may still hold the database file
}
});
describe("reading what an agent says it runs", () => {
test("a release is found by its distribution and version, however the version is written", () => {
assert.deepEqual(releaseKey({ id: "ubuntu", version: "22.04" }), { family: "ubuntu", key: "22.04" });
assert.deepEqual(releaseKey({ id: "debian", version: "12" }), { family: "debian", key: "12" });
assert.deepEqual(releaseKey({ id: "raspbian", version: "11" }), { family: "debian", key: "11" }, "Raspberry Pi OS is Debian");
assert.deepEqual(releaseKey({ id: "rocky", version: "9.4" }), { family: "rocky", key: "9" });
assert.deepEqual(releaseKey({ id: "alpine", version: "3.19.1" }), { family: "alpine", key: "3.19" });
assert.deepEqual(releaseKey({ id: "proxmox-ve", version: "8.2" }), { family: "proxmox-ve", key: "8" });
assert.deepEqual(releaseKey({ id: "centos", name: "CentOS Stream 9", version: "9" }), { family: "centos-stream", key: "9" });
assert.deepEqual(releaseKey({ id: "centos", name: "CentOS Linux 7 (Core)", version: "7" }), { family: "centos-classic", key: "7" });
assert.deepEqual(releaseKey({ id: "windows", name: "Windows Server 2019 Standard", version: "17763" }), { family: "windows-server", key: "17763" });
assert.deepEqual(releaseKey({ id: "windows", name: "Windows 11 Pro", version: "22631" }), { family: "windows-client", key: "22631" });
assert.equal(releaseKey({ id: "arch", version: "rolling" }), null, "a rolling distribution has no end");
assert.equal(releaseKey({ id: "", version: "1" }), null);
assert.equal(releaseKey({ id: "ubuntu" }), null);
});
test("the same build number is a different product on a server and a client", () => {
assert.equal(lifecycleOf({ id: "windows", name: "Windows Server 2025 Datacenter", version: "26100" })?.product, "Windows Server 2025");
assert.equal(lifecycleOf({ id: "windows", name: "Windows 11 Pro", version: "26100" })?.product, "Windows 11 24H2");
});
test("what the table knows: a name and a date; a release it does not know is named as the machine named itself, with no date", () => {
assert.deepEqual(lifecycleOf({ id: "ubuntu", name: "Ubuntu 20.04.6 LTS", version: "20.04" }), { product: "Ubuntu 20.04 LTS", endsOn: "2025-05-31" });
assert.deepEqual(lifecycleOf({ id: "debian", version: "12" }), { product: "Debian 12 (bookworm)", endsOn: "2028-06-30" });
assert.deepEqual(lifecycleOf({ id: "ubuntu", name: "Ubuntu 31.04", version: "31.04" }), { product: "Ubuntu 31.04", endsOn: null });
assert.equal(lifecycleOf({ id: "ubuntu", version: "31.04" }), null, "nothing to call it, nothing to say");
assert.equal(lifecycleOf({ id: "templeos", version: "5" }), null);
});
test("ended once the day is over, soon within the warning, ok beyond it, unknown with no date", () => {
const now = new Date("2026-10-20T12:00:00Z");
assert.equal(supportState("2026-10-19", now, 90), "ended");
assert.equal(supportState("2026-10-20", now, 90), "soon", "the last day is still a day of support");
assert.equal(supportState("2027-01-10", now, 90), "soon");
assert.equal(supportState("2027-06-01", now, 90), "ok");
assert.equal(supportState(null, now, 90), "unknown");
assert.equal(daysOfSupportLeft("2026-10-30", now), 10);
assert.equal(daysOfSupportLeft("2026-10-10", now), -10);
});
});
describe("what the agent says, and what the Alerts page makes of it", () => {
let id = 0;
before(async () => {
id = (await db.insert(servers).values({ name: "old-box", apiTokenHash: "o", apiTokenPrefix: "o", lastSeenAt: new Date().toISOString() }).returning({ id: servers.id }))[0].id;
});
const row = () => db.select().from(servers).where(eq(servers.id, id)).then((r) => r[0]);
const report = (os: unknown) => syncServerTasks(id, { osType: "linux", tasks: [], system: { os } as never });
test("it is stored, bounded; an agent that says nothing leaves what was said", async () => {
await report({ id: "Ubuntu", name: "Ubuntu 20.04.6 LTS", version: "20.04" });
let r = await row();
assert.deepEqual([r.osId, r.osName, r.osVersion], ["ubuntu", "Ubuntu 20.04.6 LTS", "20.04"]);
await syncServerTasks(id, { osType: "linux", tasks: [], system: {} });
r = await row();
assert.equal(r.osId, "ubuntu", "an older agent does not wipe it");
await report({ id: "x".repeat(200), name: "n".repeat(500), version: "v".repeat(200) });
r = await row();
assert.deepEqual([r.osId!.length, r.osName!.length, r.osVersion!.length], [40, 80, 40]);
await report({ id: "ubuntu", name: "Ubuntu 20.04.6 LTS", version: "20.04" });
});
test("a release past its end is a warning with the date, on the Alerts page, under the server", async () => {
invalidateAlertsCache();
const alert = (await getAlerts(true)).alerts.find((a) => a.id === `updates:os-support:${id}`)!;
assert.ok(alert, "it is raised");
assert.deepEqual([alert.severity, alert.category, alert.link, alert.target?.name, alert.silenced], ["warning", "updates", `/servers/${id}`, "old-box", false]);
assert.match(alert.message, /old-box runs Ubuntu 20\.04 LTS, whose support ended on 2025-05-31/);
});
test("one with a supported release is not raised, and the warning is a setting (0 turns it off)", async () => {
await report({ id: "debian", name: "Debian GNU/Linux 12 (bookworm)", version: "12" });
invalidateAlertsCache();
assert.equal((await getAlerts(true)).alerts.some((a) => a.id === `updates:os-support:${id}`), false);
await report({ id: "ubuntu", name: "Ubuntu 20.04.6 LTS", version: "20.04" });
await updateSettings({ healthChecks: { osSupportWarnDays: 0 } });
invalidateAlertsCache();
assert.equal((await getAlerts(true)).alerts.some((a) => a.id === `updates:os-support:${id}`), false, "off");
await updateSettings({ healthChecks: { osSupportWarnDays: 90 } });
});
test("a maintenance window on the server holds it back", async () => {
await db.insert(maintenanceWindows).values({ targetType: "server", targetId: id, startedAt: new Date(Date.now() - 1000).toISOString(), endsAt: new Date(Date.now() + 3600_000).toISOString() });
invalidateAlertsCache();
assert.equal((await getAlerts(true)).alerts.find((a) => a.id === `updates:os-support:${id}`)?.silenced, true);
await db.delete(maintenanceWindows).where(eq(maintenanceWindows.targetId, id));
});
});
describe("the agent scripts say it", () => {
test("Linux reads /etc/os-release (and asks Proxmox by its own command) and reports it; Windows reports the product and the build; both are a new version", () => {
const linux = readFileSync(join(repo, "agent", "linux", "report-tasks.sh"), "utf8");
assert.ok(linux.includes("collect_os()") && linux.includes("OS_RELEASE_FILE") && linux.includes("pveversion") && linux.includes("os: $os"));
const windows = readFileSync(join(repo, "agent", "windows", "report-tasks.ps1"), "utf8");
assert.ok(windows.includes("Win32_OperatingSystem") && windows.includes("os = $osInfo") && windows.includes("BuildNumber"));
const latest = latestAgentVersions();
assert.ok((latest.linux ?? 0) >= 7 && (latest.windows ?? 0) >= 6, "an agent that does not say it is behind");
});
});
+59
View File
@@ -0,0 +1,59 @@
/**
* The rack view: which unit a piece of hardware says it sits in, how the racks are made from that, clashes and what is not placed.
*
* Run with: npm test --workspace server
*/
import { test, describe } from "node:test";
import assert from "node:assert/strict";
import { buildRacks, parseRackUnit, type RackAsset } from "../src/services/racks.js";
const asset = (id: number, name: string, location: string | null, rackUnit: string | null, extra: Partial<RackAsset> = {}): RackAsset => ({ id, name, kind: "server", manufacturer: null, model: null, location, rackUnit, powerWatts: null, serverId: null, ...extra });
describe("reading a unit", () => {
test("a unit, or a run of them, in the ways people write them", () => {
assert.deepEqual(parseRackUnit("U12"), { from: 12, to: 12 });
assert.deepEqual(parseRackUnit("u3"), { from: 3, to: 3 });
assert.deepEqual(parseRackUnit("12"), { from: 12, to: 12 });
assert.deepEqual(parseRackUnit("U12-13"), { from: 12, to: 13 });
assert.deepEqual(parseRackUnit("12 – 14"), { from: 12, to: 14 });
assert.deepEqual(parseRackUnit("U14-U12"), { from: 12, to: 14 }, "either way round");
assert.equal(parseRackUnit("top shelf"), null);
assert.equal(parseRackUnit("U0"), null);
assert.equal(parseRackUnit(""), null);
assert.equal(parseRackUnit(null), null);
});
});
describe("making racks", () => {
test("hardware is grouped by where it says it is, placed by unit, in a rack of a real size", () => {
const racks = buildRacks([asset(1, "Switch", "Rack A", "U40", { kind: "network", powerWatts: 40 }), asset(2, "NAS", "Rack A", "U10-13", { powerWatts: 120 }), asset(3, "UPS", "Rack A", "U1-3", { kind: "power" }), asset(4, "Laptop", "Desk", null)]);
assert.equal(racks.length, 1, "a desk with no unit is not a rack");
const a = racks[0];
assert.deepEqual([a.name, a.units, a.usedUnits, a.watts], ["Rack A", 42, 1 + 4 + 3, 160]);
assert.deepEqual(a.slots.map((s) => [s.name, s.from, s.to]), [["UPS", 1, 3], ["NAS", 10, 13], ["Switch", 40, 40]], "from the bottom up");
assert.ok(a.slots.every((s) => !s.clash));
});
test("a small rack is drawn small, and a tall piece makes it as tall as it needs", () => {
assert.equal(buildRacks([asset(1, "A", "R", "U5")])[0].units, 12);
assert.equal(buildRacks([asset(1, "A", "R", "U13")])[0].units, 18);
assert.equal(buildRacks([asset(1, "A", "R", "U43-48")])[0].units, 48);
assert.equal(buildRacks([asset(1, "A", "R", "U60-70")])[0].units, 70, "beyond any real rack it just fits");
});
test("two pieces on the same unit clash, and both are said to", () => {
const [rack] = buildRacks([asset(1, "A", "R", "U4-5"), asset(2, "B", "R", "U5"), asset(3, "C", "R", "U8")]);
assert.deepEqual(rack.slots.map((s) => [s.name, s.clash]), [["A", true], ["B", true], ["C", false]]);
assert.equal(rack.usedUnits, 3, "a unit is used once however many claim it");
});
test("hardware that names a rack and no unit is listed beside it, not placed", () => {
const [rack] = buildRacks([asset(1, "NAS", "Rack A", "U10"), asset(2, "Cable box", "Rack A", null), asset(3, "Patch panel", "Rack A", "somewhere")]);
assert.deepEqual(rack.unplaced.map((u) => u.name), ["Cable box", "Patch panel"]);
});
test("a unit with no place goes in a rack of its own, last; racks are in order, numbers as numbers", () => {
const racks = buildRacks([asset(1, "X", null, "U2"), asset(2, "Y", "Rack 10", "U1"), asset(3, "Z", "Rack 2", "U1")]);
assert.deepEqual(racks.map((r) => r.name), ["Rack 2", "Rack 10", "(no rack named)"]);
});
});
+181
View File
@@ -0,0 +1,181 @@
/**
* The to-do list and notes: who may write, what is checked, ticking off and bringing back, what shows in Coming up and My work.
*
* Run with: npm test --workspace server
*/
import { test, describe, before, after } from "node:test";
import assert from "node:assert/strict";
import { mkdtempSync, rmSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import type { AddressInfo } from "node:net";
import type { Server } from "node:http";
const dir = mkdtempSync(join(tmpdir(), "hm-todos-"));
process.env.DATABASE_PATH = join(dir, "test.sqlite");
process.env.SESSION_DIR = join(dir, "sessions");
process.env.CREDENTIALS_ENCRYPTION_KEY = "ab".repeat(32);
process.env.HEALTH_STARTUP_GRACE_MS = "0";
const express = (await import("express")).default;
const { db } = await import("../src/db/client.js");
const { users, servers, notes, todos, auditLog } = await import("../src/db/schema.js");
const { runMigrations } = await import("../src/db/migrate.js");
const { todosRouter, notesRouter } = await import("../src/routes/todos.js");
const { loadAgendaData, agendaFromData } = await import("../src/services/agenda.js");
const { buildMyWork } = await import("../src/services/myWork.js");
const { isRealDay } = await import("../src/services/todos.js");
const { eq } = await import("drizzle-orm");
await runMigrations();
let app: Server;
let base = "";
let who = "operator";
const ids: Record<string, number> = {};
const send = async (method: string, path: string, body?: unknown) => {
const res = await fetch(`${base}${path}`, { method, headers: { "content-type": "application/json" }, body: body === undefined ? undefined : JSON.stringify(body) });
return { status: res.status, body: res.status === 204 ? null : ((await res.json().catch(() => null)) as any) };
};
const day = (offset: number) => new Date(Date.now() + offset * 86_400_000).toISOString().slice(0, 10);
before(async () => {
const rows = await db
.insert(users)
.values([
{ oidcSub: "admin", name: "Ada", role: "admin" },
{ oidcSub: "operator", name: "Olga", role: "operator" },
{ oidcSub: "viewer", name: "Vic", role: "viewer" },
])
.returning({ id: users.id, oidcSub: users.oidcSub });
for (const r of rows) ids[r.oidcSub] = r.id;
ids.web = (await db.insert(servers).values({ name: "web", apiTokenHash: "w", apiTokenPrefix: "w" }).returning({ id: servers.id }))[0].id;
const a = express();
a.use(express.json());
a.use((req, _res, next) => {
(req as unknown as { session: unknown }).session = { user: { sub: who } };
next();
});
a.use("/api/todos", todosRouter);
a.use("/api/notes", notesRouter);
app = await new Promise<Server>((resolve) => {
const s = a.listen(0, "127.0.0.1", () => resolve(s));
});
base = `http://127.0.0.1:${(app.address() as AddressInfo).port}/api`;
});
after(() => {
app.close();
try {
rmSync(dir, { recursive: true, force: true });
} catch {
// Windows may still hold the database file
}
});
describe("the to-do list", () => {
let jobId = 0;
test("a real day is a real day", () => {
assert.equal(isRealDay("2026-10-20"), true);
assert.equal(isRealDay("2026-02-30"), false);
assert.equal(isRealDay("next week"), false);
});
test("everyone signed in reads it; only an operator writes", async () => {
who = "viewer";
assert.equal((await send("GET", "/todos")).status, 200);
assert.equal((await send("POST", "/todos", { title: "x" })).status, 403);
assert.equal((await send("PATCH", "/todos/1", { done: true })).status, 403);
assert.equal((await send("DELETE", "/todos/1")).status, 403);
});
test("a job needs a title; the day must be a day; it can be about a server and handed to an operator, and not to a viewer", async () => {
who = "operator";
assert.equal((await send("POST", "/todos", { title: " " })).status, 400);
assert.equal((await send("POST", "/todos", { title: "x", dueDate: "tomorrow" })).body.error, "invalid_date");
assert.equal((await send("POST", "/todos", { title: "x", assigneeId: ids.viewer })).body.error, "invalid_assignee");
assert.equal((await send("POST", "/todos", { title: "x", targetType: "server", targetId: 9999 })).body.error, "unknown_target");
assert.equal((await send("POST", "/todos", { title: "x".repeat(200) })).body.error, "too_long");
const made = await send("POST", "/todos", { title: "Replace the UPS battery", detail: "APC, model in the rack", dueDate: day(1), assigneeId: ids.operator, targetType: "server", targetId: ids.web });
assert.equal(made.status, 201);
jobId = made.body.todo.id;
assert.deepEqual([made.body.todo.title, made.body.todo.assignee.name, made.body.todo.target.name, made.body.todo.doneAt], ["Replace the UPS battery", "Olga", "web", null]);
});
test("open jobs come soonest-due first, with those without a day last; ticking one off keeps it, and it comes back", async () => {
await send("POST", "/todos", { title: "Someday" });
await send("POST", "/todos", { title: "Next week", dueDate: day(7) });
const open = (await send("GET", "/todos")).body.todos.map((t: any) => t.title);
assert.deepEqual(open, ["Replace the UPS battery", "Next week", "Someday"]);
const done = await send("PATCH", `/todos/${jobId}`, { done: true });
assert.ok(done.body.todo.doneAt && done.body.todo.doneBy === "Olga");
const after = (await send("GET", "/todos")).body.todos;
assert.deepEqual(after.map((t: any) => [t.title, t.doneAt !== null]), [["Next week", false], ["Someday", false], ["Replace the UPS battery", true]], "done ones after the open ones");
const back = await send("PATCH", `/todos/${jobId}`, { done: false });
assert.equal(back.body.todo.doneAt, null);
});
test("a job can be changed, and removed; one that is not there is a 404", async () => {
const changed = await send("PATCH", `/todos/${jobId}`, { title: "Replace the UPS battery (APC)", dueDate: null, assigneeId: null });
assert.deepEqual([changed.body.todo.title, changed.body.todo.dueDate, changed.body.todo.assignee], ["Replace the UPS battery (APC)", null, null]);
assert.equal((await send("PATCH", "/todos/99999", { done: true })).status, 404);
assert.equal((await send("DELETE", `/todos/${jobId}`)).status, 204);
assert.equal((await send("DELETE", `/todos/${jobId}`)).status, 404);
const kinds = (await db.select().from(auditLog).where(eq(auditLog.category, "todo"))).map((e) => e.action);
assert.ok(["create", "done", "reopen", "update", "delete"].every((a) => kinds.includes(a)), "each is written down");
});
test("a job about a server that is later removed is still there, about nothing", async () => {
const gone = (await db.insert(servers).values({ name: "temp", apiTokenHash: "t", apiTokenPrefix: "t" }).returning({ id: servers.id }))[0].id;
const made = await send("POST", "/todos", { title: "About temp", targetType: "server", targetId: gone });
await db.delete(servers).where(eq(servers.id, gone));
const found = (await send("GET", "/todos")).body.todos.find((t: any) => t.id === made.body.todo.id);
assert.equal(found.target, null);
await send("DELETE", `/todos/${made.body.todo.id}`);
});
test("an open job with a day is in Coming up (overdue ones too), a done one is not; one handed to somebody is in their My work", async () => {
const soon = (await send("POST", "/todos", { title: "Move the NAS", dueDate: day(3), assigneeId: ids.operator })).body.todo;
const late = (await send("POST", "/todos", { title: "Order a spare disk", dueDate: day(-2), assigneeId: ids.operator })).body.todo;
const finished = (await send("POST", "/todos", { title: "Already done", dueDate: day(2) })).body.todo;
await send("PATCH", `/todos/${finished.id}`, { done: true });
const agenda = agendaFromData(await loadAgendaData(), new Date(), "UTC", 30);
const titles = agenda.items.filter((i) => i.kind === "todo").map((i) => i.title);
assert.ok(titles.includes("To do: Move the NAS") && titles.includes("Overdue: Order a spare disk"));
assert.ok(!titles.some((t) => t.includes("Already done")));
const mine = await buildMyWork({ id: ids.operator, name: "Olga", email: null, oidcSub: "operator" });
assert.deepEqual(mine.todos.map((t) => [t.title, t.overdue, t.dueSoon]), [["Order a spare disk", true, false], ["Move the NAS", false, false]], "overdue first; three days off is not yet soon");
assert.ok(mine.needsAttention >= 1, "an overdue job wants a look");
const theirs = await buildMyWork({ id: ids.admin, name: "Ada", email: null, oidcSub: "admin" });
assert.equal(theirs.todos.length, 0);
await db.delete(todos);
assert.ok(soon && late);
});
});
describe("notes", () => {
test("read by everyone; written by an operator; an empty text takes it away", async () => {
who = "viewer";
assert.equal((await send("GET", `/notes?targetType=server&targetId=${ids.web}`)).body.note, null);
assert.equal((await send("PUT", "/notes", { targetType: "server", targetId: ids.web, body: "x" })).status, 403);
who = "operator";
const saved = await send("PUT", "/notes", { targetType: "server", targetId: ids.web, body: "Lives in the **rack**.\n\n- PSU is on the left" });
assert.deepEqual([saved.status, saved.body.note.updatedBy, saved.body.note.body.includes("**rack**")], [200, "Olga", true]);
const again = await send("PUT", "/notes", { targetType: "server", targetId: ids.web, body: "Replaced." });
assert.equal((await db.select().from(notes)).length, 1, "one text per thing");
assert.equal(again.body.note.body, "Replaced.");
who = "viewer";
assert.equal((await send("GET", `/notes?targetType=server&targetId=${ids.web}`)).body.note.body, "Replaced.");
who = "operator";
assert.equal((await send("PUT", "/notes", { targetType: "server", targetId: ids.web, body: " \n " })).body.note, null);
assert.equal((await db.select().from(notes)).length, 0);
});
test("a note for nothing, or too long, or about something unknown, is refused", async () => {
who = "operator";
assert.equal((await send("PUT", "/notes", { targetType: "server", targetId: 99999, body: "x" })).status, 404);
assert.equal((await send("PUT", "/notes", { targetType: "server", targetId: ids.web, body: "x".repeat(9000) })).body.error, "too_long");
assert.equal((await send("PUT", "/notes", { targetType: "rack", targetId: 1, body: "x" })).status, 400);
assert.equal((await send("GET", "/notes?targetType=server")).status, 400);
});
});
+1 -1
View File
@@ -34,7 +34,7 @@ function run(snippet: string): unknown {
describe("the Windows agent's health parts", { skip: !havePwsh }, () => {
test("it says it is version 5, and stays plain ASCII", () => {
const text = readFileSync(script, "utf8");
assert.match(text, /^\$script:AgentVersion = 5\s*$/m);
assert.match(text, /^\$script:AgentVersion = 6\s*$/m);
assert.ok(!/[^\x00-\x7f]/.test(text));
});