Add status pages (a share link kind) and a downloadable Grafana dashboard
- Share links can open a status page: an admin picks servers, integrations and Uptime Kuma monitors and names them; the public sees only those names, Working / Not working / Being worked on / Can't tell, an optional message and the 30-day figure (share_links.kind/ title/message and status_items, migration 0033). A status address doesn't open the wall and the other way round; Uptime Kuma is asked at most every 30 seconds - Grafana dashboard built from code (download in Settings -> API and webhooks); a test checks it only uses series /metrics serves - Tests and docs Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
2744a84719
commit
5cc51c57de
24 files changed
+4880
-35
No files matched your search
@@ -0,0 +1,279 @@
|
||||
/**
|
||||
* A Grafana dashboard for the series `/metrics` serves, built here rather than kept as a file so that it can be tested against what the
|
||||
* app really emits (a panel for a series that was renamed would otherwise just go blank). It is downloaded from Settings → API and
|
||||
* webhooks and imported in Grafana (Dashboards → New → Import); Grafana asks which Prometheus to use, so nothing in it is tied to one
|
||||
* installation.
|
||||
*
|
||||
* Laid out in rows — what needs looking at, servers, disks, backups, then the quieter things — each panel on a 24-column grid.
|
||||
*/
|
||||
|
||||
const DS = { type: "prometheus", uid: "${DS_PROMETHEUS}" } as const;
|
||||
|
||||
type Panel = Record<string, unknown>;
|
||||
|
||||
interface Thresholds {
|
||||
/** From the lowest: [colour, from]; the first has no lower bound. */
|
||||
steps: [color: string, from: number | null][];
|
||||
}
|
||||
|
||||
const GREEN = "green";
|
||||
const YELLOW = "yellow";
|
||||
const RED = "red";
|
||||
const BLUE = "blue";
|
||||
|
||||
const thresholds = (t: Thresholds) => ({ mode: "absolute", steps: t.steps.map(([color, value]) => ({ color, value })) });
|
||||
|
||||
/** Red as soon as there is anything. */
|
||||
const redAbove0: Thresholds = { steps: [[GREEN, null], [RED, 1]] };
|
||||
const yellowAbove0: Thresholds = { steps: [[GREEN, null], [YELLOW, 1]] };
|
||||
|
||||
class Layout {
|
||||
private y = 0;
|
||||
private x = 0;
|
||||
private rowHeight = 0;
|
||||
private id = 1;
|
||||
readonly panels: Panel[] = [];
|
||||
|
||||
nextId(): number {
|
||||
return this.id++;
|
||||
}
|
||||
|
||||
row(title: string): void {
|
||||
this.endLine();
|
||||
this.panels.push({ type: "row", id: this.nextId(), title, collapsed: false, gridPos: { h: 1, w: 24, x: 0, y: this.y }, panels: [] });
|
||||
this.y += 1;
|
||||
}
|
||||
|
||||
private endLine(): void {
|
||||
if (this.x > 0) {
|
||||
this.y += this.rowHeight;
|
||||
this.x = 0;
|
||||
this.rowHeight = 0;
|
||||
}
|
||||
}
|
||||
|
||||
add(panel: Panel, w: number, h: number): void {
|
||||
if (this.x + w > 24) this.endLine();
|
||||
this.panels.push({ ...panel, id: this.nextId(), gridPos: { h, w, x: this.x, y: this.y } });
|
||||
this.x += w;
|
||||
this.rowHeight = Math.max(this.rowHeight, h);
|
||||
if (this.x >= 24) this.endLine();
|
||||
}
|
||||
|
||||
done(): Panel[] {
|
||||
this.endLine();
|
||||
return this.panels;
|
||||
}
|
||||
}
|
||||
|
||||
const target = (expr: string, legend = "", extra: Record<string, unknown> = {}, refId = "A") => ({ datasource: DS, expr, legendFormat: legend, refId, ...extra });
|
||||
|
||||
function stat(title: string, expr: string, options: { unit?: string; thresholds?: Thresholds; legend?: string; description?: string; mappings?: unknown[]; decimals?: number; graph?: boolean } = {}): Panel {
|
||||
return {
|
||||
type: "stat",
|
||||
title,
|
||||
description: options.description ?? "",
|
||||
datasource: DS,
|
||||
targets: [target(expr, options.legend ?? "")],
|
||||
fieldConfig: {
|
||||
defaults: {
|
||||
unit: options.unit ?? "none",
|
||||
decimals: options.decimals,
|
||||
thresholds: thresholds(options.thresholds ?? { steps: [[BLUE, null]] }),
|
||||
mappings: options.mappings ?? [],
|
||||
color: { mode: "thresholds" },
|
||||
},
|
||||
overrides: [],
|
||||
},
|
||||
options: { reduceOptions: { calcs: ["lastNotNull"], fields: "", values: false }, colorMode: "background", graphMode: options.graph ? "area" : "none", textMode: "auto", justifyMode: "center", orientation: "auto" },
|
||||
};
|
||||
}
|
||||
|
||||
function timeseries(title: string, queries: { expr: string; legend: string }[], options: { unit?: string; min?: number; max?: number; thresholds?: Thresholds; description?: string; stack?: boolean } = {}): Panel {
|
||||
return {
|
||||
type: "timeseries",
|
||||
title,
|
||||
description: options.description ?? "",
|
||||
datasource: DS,
|
||||
targets: queries.map((q, i) => target(q.expr, q.legend, {}, String.fromCharCode(65 + i))),
|
||||
fieldConfig: {
|
||||
defaults: {
|
||||
unit: options.unit ?? "none",
|
||||
min: options.min,
|
||||
max: options.max,
|
||||
thresholds: thresholds(options.thresholds ?? { steps: [[GREEN, null]] }),
|
||||
custom: { drawStyle: "line", lineWidth: 1, fillOpacity: options.stack ? 30 : 8, showPoints: "never", spanNulls: true, stacking: { mode: options.stack ? "normal" : "none" }, thresholdsStyle: { mode: options.thresholds ? "line" : "off" } },
|
||||
},
|
||||
overrides: [],
|
||||
},
|
||||
options: { legend: { displayMode: "table", placement: "bottom", calcs: ["lastNotNull", "max"] }, tooltip: { mode: "multi", sort: "desc" } },
|
||||
};
|
||||
}
|
||||
|
||||
function bargauge(title: string, expr: string, legend: string, options: { unit?: string; min?: number; max?: number; thresholds?: Thresholds; description?: string } = {}): Panel {
|
||||
return {
|
||||
type: "bargauge",
|
||||
title,
|
||||
description: options.description ?? "",
|
||||
datasource: DS,
|
||||
targets: [target(expr, legend, { instant: true })],
|
||||
fieldConfig: { defaults: { unit: options.unit ?? "none", min: options.min, max: options.max, thresholds: thresholds(options.thresholds ?? { steps: [[GREEN, null]] }), color: { mode: "thresholds" } }, overrides: [] },
|
||||
options: { reduceOptions: { calcs: ["lastNotNull"], fields: "", values: false }, orientation: "horizontal", displayMode: "gradient", showUnfilled: true },
|
||||
};
|
||||
}
|
||||
|
||||
function table(title: string, queries: { expr: string; name: string }[], options: { unit?: string; description?: string; sortBy?: string; thresholds?: Thresholds; labels?: string[] } = {}): Panel {
|
||||
// One value column per query, named, with the labels kept as columns and everything else hidden.
|
||||
const rename: Record<string, string> = {};
|
||||
const exclude: Record<string, boolean> = { Time: true, __name__: true };
|
||||
queries.forEach((q, i) => {
|
||||
rename[queries.length === 1 ? "Value" : `Value #${String.fromCharCode(65 + i)}`] = q.name;
|
||||
});
|
||||
return {
|
||||
type: "table",
|
||||
title,
|
||||
description: options.description ?? "",
|
||||
datasource: DS,
|
||||
targets: queries.map((q, i) => target(q.expr, "", { instant: true, format: "table" }, String.fromCharCode(65 + i))),
|
||||
transformations: [
|
||||
{ id: "merge", options: {} },
|
||||
{ id: "organize", options: { excludeByName: exclude, renameByName: { ...rename, server: "Server", mount: "Disk", device: "Device", pool: "Pool", guest: "Guest", integration: "Integration", name: "Name", status: "State" } } },
|
||||
],
|
||||
fieldConfig: { defaults: { unit: options.unit ?? "none", thresholds: thresholds(options.thresholds ?? { steps: [[GREEN, null]] }), custom: { align: "auto", cellOptions: { type: options.thresholds ? "color-background" : "auto" } } }, overrides: [] },
|
||||
options: { showHeader: true, sortBy: options.sortBy ? [{ displayName: options.sortBy, desc: false }] : [] },
|
||||
};
|
||||
}
|
||||
|
||||
function piechart(title: string, expr: string, legend: string): Panel {
|
||||
return {
|
||||
type: "piechart",
|
||||
title,
|
||||
datasource: DS,
|
||||
targets: [target(expr, legend, { instant: true })],
|
||||
fieldConfig: { defaults: { unit: "none", color: { mode: "palette-classic" } }, overrides: [] },
|
||||
options: { reduceOptions: { calcs: ["lastNotNull"], fields: "", values: false }, pieType: "donut", legend: { displayMode: "table", placement: "right", values: ["value"] }, displayLabels: [] },
|
||||
};
|
||||
}
|
||||
|
||||
const okFailing = [{ type: "value", options: { "0": { text: "Failing", color: RED, index: 0 }, "1": { text: "OK", color: GREEN, index: 1 } } }];
|
||||
|
||||
/** The dashboard, as Grafana's import takes it. */
|
||||
export function buildGrafanaDashboard(): Record<string, unknown> {
|
||||
const l = new Layout();
|
||||
const srv = '{server=~"$server"}';
|
||||
|
||||
l.row("Needs attention");
|
||||
l.add(stat("Critical alerts", 'sum(homelab_alerts{severity="critical"}) or vector(0)', { thresholds: redAbove0, description: "Counts every critical alert, including ones somebody has acknowledged." }), 4, 4);
|
||||
l.add(stat("Warnings", 'sum(homelab_alerts{severity="warning"}) or vector(0)', { thresholds: yellowAbove0 }), 4, 4);
|
||||
l.add(stat("Acknowledged", "homelab_alerts_acknowledged", { description: "Somebody has said they are on it." }), 4, 4);
|
||||
l.add(stat("Open incidents", "homelab_incidents_open", { thresholds: yellowAbove0 }), 4, 4);
|
||||
l.add(stat("Servers down", "count(homelab_server_up == 0) or vector(0)", { thresholds: redAbove0 }), 4, 4);
|
||||
l.add(stat("Integrations failing", "count(homelab_integration_up == 0) or vector(0)", { thresholds: redAbove0 }), 4, 4);
|
||||
l.add(stat("Servers up", "sum(homelab_server_up) or vector(0)", { thresholds: { steps: [[RED, null], [GREEN, 1]] } }), 4, 4);
|
||||
l.add(stat("In maintenance", "homelab_maintenance_active_windows", { description: "Maintenance windows running now." }), 4, 4);
|
||||
l.add(stat("Backups gone stale", 'homelab_backup_guests{status="stale"}', { thresholds: yellowAbove0, description: "Proxmox guests whose newest backup is older than its limit." }), 4, 4);
|
||||
l.add(stat("Failed services", "sum(homelab_server_failed_units) or vector(0)", { thresholds: yellowAbove0 }), 4, 4);
|
||||
l.add(stat("Updates waiting", "sum(homelab_server_pending_updates) or vector(0)", { thresholds: { steps: [[GREEN, null], [YELLOW, 1], [RED, 50]] } }), 4, 4);
|
||||
l.add(stat("Need a restart", "sum(homelab_server_reboot_required) or vector(0)", { thresholds: yellowAbove0 }), 4, 4);
|
||||
l.add(timeseries("Alerts by severity", [{ expr: "homelab_alerts", legend: "{{severity}}" }, { expr: "homelab_alerts_silenced", legend: "silenced" }, { expr: "homelab_alerts_grouped", legend: "folded under a down server" }], { description: "Raised alerts over time." }), 12, 8);
|
||||
l.add(
|
||||
stat("Integrations", "homelab_integration_up", { legend: "{{integration}} ({{type}})", mappings: okFailing, thresholds: { steps: [[RED, null], [GREEN, 1]] }, description: "Whether each integration's latest calls work." }),
|
||||
12,
|
||||
8,
|
||||
);
|
||||
|
||||
l.row("Servers");
|
||||
l.add(
|
||||
table(
|
||||
"Servers",
|
||||
[
|
||||
{ expr: `homelab_server_up${srv}`, name: "Up" },
|
||||
{ expr: `homelab_server_cpu_load_percent${srv}`, name: "CPU load %" },
|
||||
{ expr: `homelab_server_memory_used_percent${srv}`, name: "Memory %" },
|
||||
{ expr: `homelab_server_pending_updates${srv}`, name: "Updates waiting" },
|
||||
{ expr: `homelab_server_pending_security_updates${srv}`, name: "Security updates" },
|
||||
{ expr: `homelab_server_failed_units${srv}`, name: "Failed services" },
|
||||
{ expr: `homelab_server_agent_outdated${srv}`, name: "Agent behind" },
|
||||
],
|
||||
{ sortBy: "Server" },
|
||||
),
|
||||
24,
|
||||
9,
|
||||
);
|
||||
l.add(timeseries("CPU load", [{ expr: `homelab_server_cpu_load_percent${srv}`, legend: "{{server}}" }], { unit: "percent", min: 0 }), 12, 8);
|
||||
l.add(timeseries("Memory in use", [{ expr: `homelab_server_memory_used_percent${srv}`, legend: "{{server}}" }], { unit: "percent", min: 0, max: 100 }), 12, 8);
|
||||
l.add(bargauge("Uptime, last 30 days", `homelab_server_uptime_ratio_30d${srv}`, "{{server}}", { unit: "percentunit", min: 0, max: 1, thresholds: { steps: [[RED, null], [YELLOW, 0.95], [GREEN, 0.99]] } }), 12, 8);
|
||||
l.add(timeseries("Last report", [{ expr: `time() - homelab_server_last_seen_timestamp_seconds${srv}`, legend: "{{server}}" }], { unit: "s", min: 0, description: "How long since each agent last reported." }), 12, 8);
|
||||
|
||||
l.row("Disks");
|
||||
l.add(timeseries("Disk usage", [{ expr: `homelab_server_disk_used_percent${srv}`, legend: "{{server}} {{mount}}" }], { unit: "percent", min: 0, max: 100, thresholds: { steps: [[GREEN, null], [RED, 90]] } }), 12, 9);
|
||||
l.add(
|
||||
table("Disks filling up", [{ expr: `sort(homelab_server_disk_days_to_full${srv})`, name: "Days to full" }], { unit: "d", thresholds: { steps: [[RED, null], [YELLOW, 7], [GREEN, 30]] }, description: "Only disks on a steady climb over the last days." }),
|
||||
12,
|
||||
9,
|
||||
);
|
||||
l.add(stat("Disk health (SMART)", `homelab_server_disk_healthy${srv}`, { legend: "{{server}} {{device}}", mappings: [{ type: "value", options: { "0": { text: "Failing", color: RED, index: 0 }, "1": { text: "Passes", color: GREEN, index: 1 } } }], thresholds: { steps: [[RED, null], [GREEN, 1]] } }), 8, 6);
|
||||
l.add(bargauge("SSD life used", `homelab_server_disk_wear_percent${srv}`, "{{server}} {{device}}", { unit: "percent", min: 0, max: 100, thresholds: { steps: [[GREEN, null], [YELLOW, 80], [RED, 90]] } }), 8, 6);
|
||||
l.add(timeseries("Disk temperature", [{ expr: `homelab_server_disk_temperature_celsius${srv}`, legend: "{{server}} {{device}}" }], { unit: "celsius", thresholds: { steps: [[GREEN, null], [RED, 60]] } }), 8, 6);
|
||||
l.add(stat("ZFS pools", `homelab_server_zfs_pool_online${srv}`, { legend: "{{server}} / {{pool}}", mappings: [{ type: "value", options: { "0": { text: "Not online", color: RED, index: 0 }, "1": { text: "Online", color: GREEN, index: 1 } } }], thresholds: { steps: [[RED, null], [GREEN, 1]] } }), 24, 4);
|
||||
|
||||
l.row("Backups");
|
||||
l.add(piechart("Proxmox guests by backup state", "homelab_backup_guests", "{{status}}"), 8, 8);
|
||||
l.add(table("Age of each guest's newest backup", [{ expr: "sort_desc(homelab_backup_last_age_seconds)", name: "Age" }], { unit: "s", thresholds: { steps: [[GREEN, null], [YELLOW, 172800], [RED, 604800]] } }), 16, 8);
|
||||
l.add(stat("Homelab Manager's own backup", "time() - homelab_selfbackup_last_success_timestamp_seconds", { unit: "s", thresholds: { steps: [[GREEN, null], [YELLOW, 129600], [RED, 172800]] }, description: "How old the newest backup of Homelab Manager's own database is." }), 6, 4);
|
||||
l.add(stat("Last attempt", "homelab_selfbackup_last_attempt_ok", { mappings: okFailing, thresholds: { steps: [[RED, null], [GREEN, 1]] } }), 6, 4);
|
||||
l.add(stat("Backups kept", "homelab_selfbackup_snapshots"), 6, 4);
|
||||
l.add(stat("Failed backups", 'homelab_backup_guests{status="failed"}', { thresholds: redAbove0, description: "Guests whose latest backup failed, or whose newest snapshot failed verification." }), 6, 4);
|
||||
|
||||
l.row("Secrets, power and the app itself");
|
||||
l.add(table("Secrets and certificates", [{ expr: "sort(homelab_secret_expiry_days)", name: "Days left" }], { unit: "d", thresholds: { steps: [[RED, null], [YELLOW, 14], [GREEN, 30]] }, description: "Negative once expired." }), 12, 8);
|
||||
l.add(timeseries("Power draw (inventory)", [{ expr: "homelab_inventory_power_watts", legend: "typical draw" }], { unit: "watt", min: 0 }), 6, 8);
|
||||
l.add(timeseries("Time to build /metrics", [{ expr: "homelab_scrape_duration_seconds", legend: "seconds" }], { unit: "s", min: 0 }), 6, 8);
|
||||
|
||||
return {
|
||||
__inputs: [{ name: "DS_PROMETHEUS", label: "Prometheus", description: "The Prometheus that scrapes Homelab Manager", type: "datasource", pluginId: "prometheus", pluginName: "Prometheus" }],
|
||||
__requires: [
|
||||
{ type: "grafana", id: "grafana", name: "Grafana", version: "9.0.0" },
|
||||
{ type: "datasource", id: "prometheus", name: "Prometheus", version: "1.0.0" },
|
||||
{ type: "panel", id: "stat", name: "Stat", version: "" },
|
||||
{ type: "panel", id: "timeseries", name: "Time series", version: "" },
|
||||
{ type: "panel", id: "table", name: "Table", version: "" },
|
||||
{ type: "panel", id: "bargauge", name: "Bar gauge", version: "" },
|
||||
{ type: "panel", id: "piechart", name: "Pie chart", version: "" },
|
||||
],
|
||||
title: "Homelab Manager",
|
||||
uid: "homelab-manager",
|
||||
description: "Servers, disks, backups, alerts and incidents, from Homelab Manager's /metrics. Made by Homelab Manager (Settings → API and webhooks).",
|
||||
tags: ["homelab", "homelab-manager"],
|
||||
timezone: "browser",
|
||||
editable: true,
|
||||
graphTooltip: 1,
|
||||
refresh: "1m",
|
||||
schemaVersion: 39,
|
||||
version: 1,
|
||||
time: { from: "now-24h", to: "now" },
|
||||
timepicker: {},
|
||||
templating: {
|
||||
list: [
|
||||
{
|
||||
name: "server",
|
||||
label: "Server",
|
||||
type: "query",
|
||||
datasource: DS,
|
||||
query: { query: "label_values(homelab_server_up, server)", refId: "server" },
|
||||
definition: "label_values(homelab_server_up, server)",
|
||||
includeAll: true,
|
||||
allValue: ".+",
|
||||
multi: true,
|
||||
current: { selected: true, text: ["All"], value: ["$__all"] },
|
||||
refresh: 2,
|
||||
sort: 1,
|
||||
hide: 0,
|
||||
options: [],
|
||||
},
|
||||
],
|
||||
},
|
||||
annotations: { list: [{ builtIn: 1, datasource: { type: "grafana", uid: "-- Grafana --" }, enable: true, hide: true, iconColor: "rgba(0, 211, 255, 1)", name: "Annotations & Alerts", type: "dashboard" }] },
|
||||
panels: l.done(),
|
||||
};
|
||||
}
|
||||
@@ -5,7 +5,7 @@
|
||||
import { createHash, randomBytes } from "node:crypto";
|
||||
import { eq } from "drizzle-orm";
|
||||
import { db } from "../db/client.js";
|
||||
import { shareLinks, type ShareLinkDetail } from "../db/schema.js";
|
||||
import { shareLinks, type ShareLinkDetail, type ShareLinkKind } from "../db/schema.js";
|
||||
|
||||
export type ShareLinkRow = typeof shareLinks.$inferSelect;
|
||||
|
||||
@@ -17,7 +17,7 @@ const hash = (token: string) => createHash("sha256").update(token).digest("hex")
|
||||
/** A link isn't recorded as used more than once in this time, so a screen refreshing every few seconds doesn't write the database each time. */
|
||||
const USED_WRITE_GAP_MS = 60_000;
|
||||
|
||||
export async function createShareLink(params: { label: string; detail: ShareLinkDetail; expiresInDays: number | null; createdBy: string; now?: Date }): Promise<{ link: ShareLinkRow; token: string }> {
|
||||
export async function createShareLink(params: { label: string; detail: ShareLinkDetail; kind?: ShareLinkKind; title?: string | null; message?: string | null; expiresInDays: number | null; createdBy: string; now?: Date }): Promise<{ link: ShareLinkRow; token: string }> {
|
||||
const now = params.now ?? new Date();
|
||||
const token = randomBytes(24).toString("base64url");
|
||||
const [link] = await db
|
||||
@@ -27,6 +27,9 @@ export async function createShareLink(params: { label: string; detail: ShareLink
|
||||
tokenHash: hash(token),
|
||||
tokenPrefix: token.slice(0, 6),
|
||||
detail: params.detail,
|
||||
kind: params.kind ?? "wall",
|
||||
title: params.title?.trim() || null,
|
||||
message: params.message?.trim() || null,
|
||||
createdBy: params.createdBy,
|
||||
expiresAt: params.expiresInDays === null ? null : new Date(now.getTime() + params.expiresInDays * 86_400_000).toISOString(),
|
||||
})
|
||||
@@ -53,6 +56,9 @@ export function publicShareLink(link: ShareLinkRow, now: Date = new Date()) {
|
||||
label: link.label,
|
||||
tokenPrefix: link.tokenPrefix,
|
||||
detail: link.detail,
|
||||
kind: link.kind,
|
||||
title: link.title,
|
||||
message: link.message,
|
||||
createdBy: link.createdBy,
|
||||
createdAt: link.createdAt,
|
||||
expiresAt: link.expiresAt,
|
||||
|
||||
@@ -0,0 +1,208 @@
|
||||
/**
|
||||
* A status page: the one thing someone at home wants to know — "is Plex working?" — without a sign-in and without anything that isn't
|
||||
* theirs to see. An administrator makes a share link of the status kind and picks what is on it, each with a name people will recognise
|
||||
* ("Movies", "Photos") instead of what the machine is called. Each is a server (reporting, or not), an integration (its latest calls work,
|
||||
* or don't) or an Uptime Kuma monitor (up, down, or in maintenance).
|
||||
*
|
||||
* What the public gets back is only: the page's title and message, and for each item its name, whether it is up, down, being worked on
|
||||
* or can't be told, and — where known — how it has done over 30 days. No addresses, no ids, no alert text.
|
||||
*/
|
||||
import { and, asc, eq } from "drizzle-orm";
|
||||
import { db } from "../db/client.js";
|
||||
import { integrations, servers, statusItems, type StatusItemKind } from "../db/schema.js";
|
||||
import { createUptimeKumaAdapter, type UptimeKumaMonitor } from "../integrations/uptimekuma/adapter.js";
|
||||
import { loadIntegrationConfig } from "../integrations/loadIntegration.js";
|
||||
import { getSourceStatus } from "./integrationHealthMonitor.js";
|
||||
import { activeSubjects } from "./maintenance.js";
|
||||
import { effectiveThresholds, parseThresholds } from "./serverThresholds.js";
|
||||
import { getSettings } from "./settingsStore.js";
|
||||
import { getUptime } from "./uptimeHistory.js";
|
||||
|
||||
export type ItemState = "up" | "down" | "maintenance" | "unknown";
|
||||
export type PageLevel = "ok" | "issue" | "maintenance" | "unknown";
|
||||
|
||||
export const MAX_ITEMS = 30;
|
||||
export const MAX_LABEL = 60;
|
||||
export const MAX_TITLE = 60;
|
||||
export const MAX_MESSAGE = 300;
|
||||
|
||||
export interface StatusItemInput {
|
||||
label: string;
|
||||
kind: StatusItemKind;
|
||||
/** The server, the integration, or the Uptime Kuma integration a monitor belongs to. */
|
||||
targetId: number;
|
||||
/** For a monitor: Uptime Kuma's own id for it. */
|
||||
monitorId?: string | null;
|
||||
}
|
||||
|
||||
export class StatusPageError extends Error {
|
||||
constructor(
|
||||
readonly status: number,
|
||||
readonly code: string,
|
||||
message: string,
|
||||
) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
|
||||
export interface StoredItem {
|
||||
id: number;
|
||||
label: string;
|
||||
kind: StatusItemKind;
|
||||
targetId: number;
|
||||
monitorId: string | null;
|
||||
/** What it points at, for the editor: the server's or integration's name, or the monitor's. */
|
||||
targetName: string | null;
|
||||
}
|
||||
|
||||
export async function getStatusItems(linkId: number): Promise<StoredItem[]> {
|
||||
const rows = await db.select().from(statusItems).where(eq(statusItems.linkId, linkId)).orderBy(asc(statusItems.position), asc(statusItems.id));
|
||||
if (rows.length === 0) return [];
|
||||
const serverNames = new Map((await db.select({ id: servers.id, name: servers.name }).from(servers)).map((s) => [s.id, s.name]));
|
||||
const integrationNames = new Map((await db.select({ id: integrations.id, name: integrations.name }).from(integrations)).map((i) => [i.id, i.name]));
|
||||
return rows.map((r) => ({
|
||||
id: r.id,
|
||||
label: r.label,
|
||||
kind: r.kind,
|
||||
targetId: r.targetId,
|
||||
monitorId: r.monitorId,
|
||||
targetName: r.kind === "server" ? (serverNames.get(r.targetId) ?? null) : (integrationNames.get(r.targetId) ?? null),
|
||||
}));
|
||||
}
|
||||
|
||||
/** Replaces the whole list, in the order given. Everything it points at has to exist; nothing is changed if anything is wrong. */
|
||||
export async function setStatusItems(linkId: number, items: StatusItemInput[]): Promise<void> {
|
||||
if (items.length > MAX_ITEMS) throw new StatusPageError(400, "too_many", `A status page can show up to ${MAX_ITEMS} things.`);
|
||||
const serverIds = new Set((await db.select({ id: servers.id }).from(servers)).map((s) => s.id));
|
||||
const integrationRows = new Map((await db.select({ id: integrations.id, type: integrations.type }).from(integrations)).map((i) => [i.id, i.type]));
|
||||
for (const [i, item] of items.entries()) {
|
||||
const where = `"${item.label}"`;
|
||||
if (!item.label.trim() || item.label.length > MAX_LABEL) throw new StatusPageError(400, "invalid_label", `Give each thing a name of up to ${MAX_LABEL} characters (item ${i + 1}).`);
|
||||
if (item.kind === "server" && !serverIds.has(item.targetId)) throw new StatusPageError(400, "unknown_target", `${where} points at a server that doesn't exist.`);
|
||||
if (item.kind === "integration" && !integrationRows.has(item.targetId)) throw new StatusPageError(400, "unknown_target", `${where} points at an integration that doesn't exist.`);
|
||||
if (item.kind === "monitor") {
|
||||
if (integrationRows.get(item.targetId) !== "uptimekuma") throw new StatusPageError(400, "unknown_target", `${where} points at an Uptime Kuma that doesn't exist.`);
|
||||
if (!item.monitorId || item.monitorId.length > 100) throw new StatusPageError(400, "invalid_monitor", `${where} needs one of Uptime Kuma's monitors.`);
|
||||
}
|
||||
}
|
||||
await db.delete(statusItems).where(eq(statusItems.linkId, linkId));
|
||||
if (items.length > 0) {
|
||||
await db.insert(statusItems).values(items.map((item, position) => ({ linkId, position, label: item.label.trim(), kind: item.kind, targetId: item.targetId, monitorId: item.kind === "monitor" ? (item.monitorId ?? null) : null })));
|
||||
}
|
||||
}
|
||||
|
||||
// ─── The picture the public gets ────────────────────────────────────────────
|
||||
|
||||
export interface PublicStatusItem {
|
||||
id: number;
|
||||
label: string;
|
||||
state: ItemState;
|
||||
/** Percent over the last 30 days, where it is known. */
|
||||
uptime30d: number | null;
|
||||
}
|
||||
|
||||
export interface PublicStatus {
|
||||
title: string;
|
||||
message: string | null;
|
||||
level: PageLevel;
|
||||
counts: Record<ItemState, number>;
|
||||
items: PublicStatusItem[];
|
||||
generatedAt: string;
|
||||
display: { dateFormat: string; timeFormat: string };
|
||||
}
|
||||
|
||||
/** How the whole page reads: anything down is an issue; otherwise anything being worked on says so; otherwise fine — unless nothing could be told at all. */
|
||||
export function pageLevel(counts: Record<ItemState, number>): PageLevel {
|
||||
if (counts.down > 0) return "issue";
|
||||
if (counts.maintenance > 0) return "maintenance";
|
||||
if (counts.up === 0 && counts.unknown > 0) return "unknown";
|
||||
return "ok";
|
||||
}
|
||||
|
||||
// Uptime Kuma is asked at most this often per instance, however many people have the page open.
|
||||
const KUMA_CACHE_MS = 30_000;
|
||||
const kumaCache = new Map<number, { at: number; monitors: Map<string, UptimeKumaMonitor> | null }>();
|
||||
let kumaLoader: ((integrationId: number) => Promise<UptimeKumaMonitor[] | null>) | null = null;
|
||||
|
||||
/** For tests: read monitors from this instead of a real Uptime Kuma. */
|
||||
export function setKumaLoaderForTests(loader: typeof kumaLoader): void {
|
||||
kumaLoader = loader;
|
||||
kumaCache.clear();
|
||||
}
|
||||
|
||||
async function monitorsOf(integrationId: number, now: number): Promise<Map<string, UptimeKumaMonitor> | null> {
|
||||
const cached = kumaCache.get(integrationId);
|
||||
if (cached && now - cached.at < KUMA_CACHE_MS) return cached.monitors;
|
||||
let monitors: Map<string, UptimeKumaMonitor> | null = null;
|
||||
try {
|
||||
let list: UptimeKumaMonitor[] | null;
|
||||
if (kumaLoader) list = await kumaLoader(integrationId);
|
||||
else {
|
||||
const loaded = await loadIntegrationConfig(integrationId);
|
||||
list = loaded && loaded.integration.type === "uptimekuma" && loaded.integration.enabled ? await createUptimeKumaAdapter(loaded.config as never).listMonitors() : null;
|
||||
}
|
||||
monitors = list ? new Map(list.map((m) => [m.id, m])) : null;
|
||||
} catch {
|
||||
monitors = null; // can't be told, so it says so — never "up"
|
||||
}
|
||||
kumaCache.set(integrationId, { at: now, monitors });
|
||||
return monitors;
|
||||
}
|
||||
|
||||
export async function buildPublicStatus(link: { id: number; label: string; title: string | null; message: string | null }, now: Date = new Date()): Promise<PublicStatus> {
|
||||
const items = await db.select().from(statusItems).where(eq(statusItems.linkId, link.id)).orderBy(asc(statusItems.position), asc(statusItems.id));
|
||||
const settings = await getSettings();
|
||||
const subjects = await activeSubjects(now);
|
||||
const serverRows = new Map((await db.select().from(servers)).map((s) => [s.id, s]));
|
||||
const integrationRows = new Map((await db.select({ id: integrations.id, type: integrations.type, enabled: integrations.enabled }).from(integrations)).map((i) => [i.id, i]));
|
||||
const uptime = items.some((i) => i.kind !== "monitor") ? await getUptime(now, 30).catch(() => null) : null;
|
||||
|
||||
const out: PublicStatusItem[] = [];
|
||||
for (const item of items) {
|
||||
let state: ItemState = "unknown";
|
||||
let uptime30d: number | null = null;
|
||||
|
||||
if (item.kind === "server") {
|
||||
const s = serverRows.get(item.targetId);
|
||||
if (s?.lastSeenAt) {
|
||||
const limit = effectiveThresholds({ ...settings.healthChecks }, parseThresholds(s.thresholds)).serverOfflineMinutes * 60_000;
|
||||
const quiet = now.getTime() - Date.parse(s.lastSeenAt) > limit;
|
||||
state = subjects.has(`server:${s.id}`) ? "maintenance" : quiet ? "down" : "up";
|
||||
}
|
||||
uptime30d = uptime?.servers[item.targetId]?.percent ?? null;
|
||||
} else if (item.kind === "integration") {
|
||||
const i = integrationRows.get(item.targetId);
|
||||
if (i?.enabled) {
|
||||
const status = getSourceStatus(i.type).status;
|
||||
state = subjects.has(`integration:${i.id}`) ? "maintenance" : status === "ok" ? "up" : status === "failing" ? "down" : "unknown";
|
||||
}
|
||||
uptime30d = uptime?.integrations[item.targetId]?.percent ?? null;
|
||||
} else {
|
||||
const monitor = item.monitorId ? (await monitorsOf(item.targetId, now.getTime()))?.get(item.monitorId) : undefined;
|
||||
if (monitor) {
|
||||
state = monitor.status === "up" ? "up" : monitor.status === "down" ? "down" : monitor.status === "maintenance" ? "maintenance" : "unknown";
|
||||
uptime30d = monitor.uptime30d;
|
||||
}
|
||||
}
|
||||
out.push({ id: item.id, label: item.label, state, uptime30d: uptime30d === null ? null : Math.round(uptime30d * 10) / 10 });
|
||||
}
|
||||
|
||||
const counts: Record<ItemState, number> = { up: 0, down: 0, maintenance: 0, unknown: 0 };
|
||||
for (const o of out) counts[o.state]++;
|
||||
return {
|
||||
title: link.title?.trim() || link.label,
|
||||
message: link.message?.trim() || null,
|
||||
level: pageLevel(counts),
|
||||
counts,
|
||||
items: out,
|
||||
generatedAt: now.toISOString(),
|
||||
display: { dateFormat: settings.display.dateFormat, timeFormat: settings.display.timeFormat },
|
||||
};
|
||||
}
|
||||
|
||||
/** What an administrator can choose from, for the editor: servers and integrations (monitors are read from each Uptime Kuma on request). */
|
||||
export async function statusChoices(): Promise<{ servers: { id: number; name: string }[]; integrations: { id: number; name: string; type: string }[] }> {
|
||||
const s = await db.select({ id: servers.id, name: servers.name }).from(servers).orderBy(asc(servers.name));
|
||||
const i = await db.select({ id: integrations.id, name: integrations.name, type: integrations.type }).from(integrations).where(and(eq(integrations.enabled, true))).orderBy(asc(integrations.name));
|
||||
return { servers: s, integrations: i };
|
||||
}
|
||||
Reference in new issue
Block a user