Alert settings keeps Alert rules, Service levels and Alert tickets; administrators get a link to Settings -> Notifications. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
148 lines
8.0 KiB
TypeScript
148 lines
8.0 KiB
TypeScript
/**
|
|
* Per-server alert thresholds. The ones in Settings → Notifications apply to every server; some machines are different — a NAS that sits at
|
|
* 92% by design, a database host that should speak up at 80%, a laptop that is meant to be off at night. Each server may override the
|
|
* server-offline time, the disk-usage percentage, the disk forecast window and (for the Proxmox guest it is linked to) how old a backup may
|
|
* be, and may set a limit for one disk or say not to alert about that disk at all. Anything not overridden follows the global setting, so
|
|
* changing the global one still reaches every server that hasn't said otherwise.
|
|
*/
|
|
|
|
export interface MountThreshold {
|
|
/** Alert when this disk is this full (instead of the server's or the global percentage). */
|
|
percent?: number;
|
|
/** Never alert about this disk — neither when it is full nor when it is filling. */
|
|
ignore?: boolean;
|
|
}
|
|
|
|
export interface ServerThresholds {
|
|
serverOfflineMinutes?: number;
|
|
diskUsagePercent?: number;
|
|
/** 0 turns the "filling up" forecast off for this server. */
|
|
diskForecastDays?: number;
|
|
/** 0 turns the stale-backup alert off for this server's guest. */
|
|
backupStaleHours?: number;
|
|
mounts?: Record<string, MountThreshold>;
|
|
/** Services (units) never to count as failed on this server: names or patterns like "gupdate*" (case does not matter, ".service" is optional). */
|
|
ignoreServices?: string[];
|
|
}
|
|
|
|
/** The global values these override. */
|
|
export interface GlobalThresholds {
|
|
serverOfflineMinutes: number;
|
|
diskUsagePercent: number;
|
|
diskForecastDays: number;
|
|
backupStaleHours: number;
|
|
}
|
|
|
|
export const LIMITS = {
|
|
serverOfflineMinutes: { min: 15, max: 10080 },
|
|
diskUsagePercent: { min: 50, max: 99 },
|
|
diskForecastDays: { min: 0, max: 90 },
|
|
backupStaleHours: { min: 0, max: 2160 },
|
|
} as const;
|
|
export const MAX_MOUNT_RULES = 50;
|
|
export const MAX_IGNORED_SERVICES = 30;
|
|
const MAX_SERVICE_PATTERN = 100;
|
|
|
|
const inRange = (v: unknown, key: keyof typeof LIMITS): v is number => typeof v === "number" && Number.isInteger(v) && v >= LIMITS[key].min && v <= LIMITS[key].max;
|
|
|
|
/** What is stored, read back leniently: anything that doesn't make sense is dropped, never an error. Null for no overrides at all. */
|
|
export function parseThresholds(stored: string | null | undefined): ServerThresholds | null {
|
|
if (!stored) return null;
|
|
try {
|
|
const v = JSON.parse(stored);
|
|
if (!v || typeof v !== "object") return null;
|
|
const out: ServerThresholds = {};
|
|
for (const key of Object.keys(LIMITS) as (keyof typeof LIMITS)[]) if (inRange(v[key], key)) out[key] = v[key];
|
|
if (v.mounts && typeof v.mounts === "object" && !Array.isArray(v.mounts)) {
|
|
const mounts: Record<string, MountThreshold> = {};
|
|
for (const [mount, rule] of Object.entries(v.mounts as Record<string, unknown>).slice(0, MAX_MOUNT_RULES)) {
|
|
if (!rule || typeof rule !== "object") continue;
|
|
const r = rule as { percent?: unknown; ignore?: unknown };
|
|
const clean: MountThreshold = {};
|
|
if (inRange(r.percent, "diskUsagePercent")) clean.percent = r.percent;
|
|
if (r.ignore === true) clean.ignore = true;
|
|
if (clean.percent !== undefined || clean.ignore) mounts[mount] = clean;
|
|
}
|
|
if (Object.keys(mounts).length > 0) out.mounts = mounts;
|
|
}
|
|
if (Array.isArray(v.ignoreServices)) {
|
|
const strings: string[] = (v.ignoreServices as unknown[]).filter((p): p is string => typeof p === "string");
|
|
const list = [...new Set(strings.map((p) => p.trim()).filter((p) => p.length > 0 && p.length <= MAX_SERVICE_PATTERN))].slice(0, MAX_IGNORED_SERVICES);
|
|
if (list.length > 0) out.ignoreServices = list;
|
|
}
|
|
return Object.keys(out).length > 0 ? out : null;
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/** What a request may say. Strict where parseThresholds is lenient: a wrong number is refused, with a message that says which. */
|
|
export function checkThresholds(input: unknown): { ok: true; value: ServerThresholds | null } | { ok: false; message: string } {
|
|
if (input === null) return { ok: true, value: null };
|
|
if (typeof input !== "object" || Array.isArray(input)) return { ok: false, message: "Thresholds should be an object." };
|
|
const v = input as Record<string, unknown>;
|
|
const out: ServerThresholds = {};
|
|
const names: Record<keyof typeof LIMITS, string> = {
|
|
serverOfflineMinutes: "Offline after (minutes)",
|
|
diskUsagePercent: "Disk usage alert at (%)",
|
|
diskForecastDays: "Disk forecast (days)",
|
|
backupStaleHours: "Backup older than (hours)",
|
|
};
|
|
for (const key of Object.keys(LIMITS) as (keyof typeof LIMITS)[]) {
|
|
if (v[key] === undefined || v[key] === null) continue;
|
|
if (!inRange(v[key], key)) return { ok: false, message: `${names[key]} must be a whole number from ${LIMITS[key].min} to ${LIMITS[key].max}.` };
|
|
out[key] = v[key] as number;
|
|
}
|
|
if (v.mounts !== undefined && v.mounts !== null) {
|
|
if (typeof v.mounts !== "object" || Array.isArray(v.mounts)) return { ok: false, message: "Disk rules should be an object." };
|
|
const entries = Object.entries(v.mounts as Record<string, unknown>);
|
|
if (entries.length > MAX_MOUNT_RULES) return { ok: false, message: `At most ${MAX_MOUNT_RULES} disks can have their own rule.` };
|
|
const mounts: Record<string, MountThreshold> = {};
|
|
for (const [mount, rule] of entries) {
|
|
if (mount.length === 0 || mount.length > 500) return { ok: false, message: "A disk's name is missing or too long." };
|
|
if (!rule || typeof rule !== "object" || Array.isArray(rule)) return { ok: false, message: `The rule for ${mount} should be an object.` };
|
|
const r = rule as { percent?: unknown; ignore?: unknown };
|
|
const clean: MountThreshold = {};
|
|
if (r.percent !== undefined && r.percent !== null) {
|
|
if (!inRange(r.percent, "diskUsagePercent")) return { ok: false, message: `The limit for ${mount} must be a whole number from ${LIMITS.diskUsagePercent.min} to ${LIMITS.diskUsagePercent.max}.` };
|
|
clean.percent = r.percent;
|
|
}
|
|
if (r.ignore === true) clean.ignore = true;
|
|
if (clean.percent !== undefined || clean.ignore) mounts[mount] = clean;
|
|
}
|
|
if (Object.keys(mounts).length > 0) out.mounts = mounts;
|
|
}
|
|
if (v.ignoreServices !== undefined && v.ignoreServices !== null) {
|
|
if (!Array.isArray(v.ignoreServices)) return { ok: false, message: "The services to ignore should be a list." };
|
|
const list: string[] = [];
|
|
for (const raw of v.ignoreServices) {
|
|
if (typeof raw !== "string") return { ok: false, message: "Each service to ignore should be a name or a pattern." };
|
|
const p = raw.trim();
|
|
if (p.length === 0) continue;
|
|
// eslint-disable-next-line no-control-regex
|
|
if (p.length > MAX_SERVICE_PATTERN || /[\u0000-\u001f]/.test(p)) return { ok: false, message: `"${p.slice(0, 30)}…" isn't a service name or pattern (up to ${MAX_SERVICE_PATTERN} characters, no line breaks).` };
|
|
if (!list.includes(p)) list.push(p);
|
|
}
|
|
if (list.length > MAX_IGNORED_SERVICES) return { ok: false, message: `At most ${MAX_IGNORED_SERVICES} services can be ignored on one server.` };
|
|
if (list.length > 0) out.ignoreServices = list;
|
|
}
|
|
return { ok: true, value: Object.keys(out).length > 0 ? out : null };
|
|
}
|
|
|
|
/** What applies to one server: its own value where it gave one, the global one otherwise. */
|
|
export function effectiveThresholds(global: GlobalThresholds, own: ServerThresholds | null | undefined): GlobalThresholds {
|
|
return {
|
|
serverOfflineMinutes: own?.serverOfflineMinutes ?? global.serverOfflineMinutes,
|
|
diskUsagePercent: own?.diskUsagePercent ?? global.diskUsagePercent,
|
|
diskForecastDays: own?.diskForecastDays ?? global.diskForecastDays,
|
|
backupStaleHours: own?.backupStaleHours ?? global.backupStaleHours,
|
|
};
|
|
}
|
|
|
|
/** The usage percentage at which a disk alerts — its own rule first, then the server's, then the global one — or null if it is to be left alone. */
|
|
export function diskLimit(own: ServerThresholds | null | undefined, mount: string, globalPercent: number): number | null {
|
|
const rule = own?.mounts?.[mount];
|
|
if (rule?.ignore) return null;
|
|
return rule?.percent ?? own?.diskUsagePercent ?? globalPercent;
|
|
}
|