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:
1 parent
68b41b5586
commit
18ceed62e8
77 files changed
+20432
-35
No files matched your search
@@ -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);
|
||||
|
||||
|
||||
@@ -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),
|
||||
|
||||
@@ -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));
|
||||
|
||||
|
||||
@@ -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 });
|
||||
}
|
||||
@@ -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();
|
||||
});
|
||||
}
|
||||
@@ -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 });
|
||||
}
|
||||
@@ -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`);
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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 });
|
||||
}));
|
||||
@@ -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));
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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 });
|
||||
}));
|
||||
@@ -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) });
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}));
|
||||
@@ -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",
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
@@ -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";
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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`);
|
||||
}
|
||||
|
||||
@@ -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 };
|
||||
}
|
||||
@@ -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 {
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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";
|
||||
}
|
||||
@@ -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() {
|
||||
|
||||
@@ -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 }));
|
||||
}
|
||||
@@ -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"] },
|
||||
|
||||
@@ -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 } : {}),
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
Reference in new issue
Block a user