Both send to one fixed address each (api.telegram.org, api.pushover.net), take part in routing rules and the channel count, have a Send test that shows the service's own refusal, and keep their tokens and keys encrypted and out of errors and the audit log. A Telegram message carries one button, and a Pushover message one link, to the page where an alert can be acknowledged, silenced or snoozed. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
120 lines
5.4 KiB
TypeScript
120 lines
5.4 KiB
TypeScript
/**
|
|
* Who hears what. By default every notification goes to every channel that is switched on. Routing rules narrow that: the first rule a
|
|
* notification matches decides which channels get it (possibly none — it is then only on the Alerts page), and each browser that has asked
|
|
* for push notifications may further say it only wants some severities or kinds. This file is the pure part: what a notification is about,
|
|
* which rule matches and what a browser accepts. Sending is in notify.ts.
|
|
*/
|
|
import type { AlertCategory, AlertSeverity } from "./alertTypes.js";
|
|
|
|
export const CHANNEL_IDS = ["gotify", "ntfy", "telegram", "pushover", "smtp", "webhook", "webPush"] as const;
|
|
export type ChannelId = (typeof CHANNEL_IDS)[number];
|
|
|
|
export const CATEGORIES = ["offline", "disk", "services", "backup", "updates", "expiry", "automation", "integration", "monitoring", "tickets"] as const satisfies readonly AlertCategory[];
|
|
|
|
/** What a notification is about, as far as routing cares. Anything left out matches only rules that don't ask about it. */
|
|
export interface NotifyMeta {
|
|
category?: AlertCategory | null;
|
|
severity?: AlertSeverity;
|
|
/** The server it is about, whose tags a rule may ask for. */
|
|
serverId?: number | null;
|
|
/** Filled in from the server before matching. */
|
|
tags?: string[];
|
|
}
|
|
|
|
export interface RoutingRule {
|
|
id: string;
|
|
name: string;
|
|
enabled: boolean;
|
|
/** Empty = any kind. */
|
|
categories: AlertCategory[];
|
|
/** null = any severity. */
|
|
minSeverity: "warning" | "critical" | null;
|
|
/** Empty = any server. Otherwise the notification must be about a server that has at least one of these tags. */
|
|
tags: string[];
|
|
/** The channels that get it. Empty = none (the Alerts page still shows it). */
|
|
channels: ChannelId[];
|
|
}
|
|
|
|
export interface RoutingSettings {
|
|
rules: RoutingRule[];
|
|
}
|
|
|
|
export interface EscalationStep {
|
|
/** Minutes an alert must have been raised, with nobody on it, before this step fires. */
|
|
afterMinutes: number;
|
|
minSeverity: "warning" | "critical";
|
|
channels: ChannelId[];
|
|
/** An address to email as well (someone else), through the email channel's server. Blank = none. */
|
|
extraEmail: string;
|
|
/** Send even during quiet hours — the point of an escalation can be to wake someone. */
|
|
evenInQuietHours: boolean;
|
|
}
|
|
|
|
export interface EscalationSettings {
|
|
enabled: boolean;
|
|
steps: EscalationStep[];
|
|
}
|
|
|
|
const RANK: Record<string, number> = { info: 0, warning: 1, critical: 2 };
|
|
export const severityRank = (s: string | undefined | null) => RANK[s ?? "warning"] ?? 1;
|
|
|
|
/** Whether a rule applies to a notification. A rule that asks about something the notification doesn't say doesn't match. */
|
|
export function ruleMatches(rule: RoutingRule, meta: NotifyMeta): boolean {
|
|
if (!rule.enabled) return false;
|
|
if (rule.categories.length > 0 && !(meta.category && rule.categories.includes(meta.category))) return false;
|
|
if (rule.minSeverity && severityRank(meta.severity) < severityRank(rule.minSeverity)) return false;
|
|
if (rule.tags.length > 0) {
|
|
const have = new Set((meta.tags ?? []).map((t) => t.toLowerCase()));
|
|
if (!rule.tags.some((t) => have.has(t.toLowerCase()))) return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
/** The first rule that applies, or null (every channel that is on). */
|
|
export function firstMatchingRule(rules: RoutingRule[], meta: NotifyMeta): RoutingRule | null {
|
|
return rules.find((r) => ruleMatches(r, meta)) ?? null;
|
|
}
|
|
|
|
/** A browser's own wishes, as stored. */
|
|
export interface PushPrefs {
|
|
minSeverity: string | null;
|
|
categories: string | null;
|
|
}
|
|
|
|
export function parseCategories(stored: string | null): AlertCategory[] | null {
|
|
if (!stored) return null;
|
|
try {
|
|
const v = JSON.parse(stored);
|
|
if (!Array.isArray(v)) return null;
|
|
const list = v.filter((c): c is AlertCategory => (CATEGORIES as readonly string[]).includes(c));
|
|
return list.length > 0 ? list : null;
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/** Whether a browser wants this notification. What the notification doesn't say can't exclude it. */
|
|
export function pushAccepts(prefs: PushPrefs, meta: NotifyMeta | undefined): boolean {
|
|
if (!meta) return true;
|
|
if (prefs.minSeverity && meta.severity && severityRank(meta.severity) < severityRank(prefs.minSeverity)) return false;
|
|
const wanted = parseCategories(prefs.categories);
|
|
if (wanted && meta.category && !wanted.includes(meta.category)) return false;
|
|
return true;
|
|
}
|
|
|
|
/** What kind of problem a health or automation condition key is about, and which server (for its tags). */
|
|
export function metaOfConditionKey(key: string, severity?: string): NotifyMeta {
|
|
let category: AlertCategory | null = null;
|
|
if (key.startsWith("offline:")) category = "offline";
|
|
else if (key.startsWith("failed-units:")) category = "services";
|
|
else if (key.startsWith("new-port:")) category = "monitoring";
|
|
else if (key.startsWith("rule:")) category = "monitoring";
|
|
else if (/^(disk|smart|zfs|synology-volume|synology-disk):/.test(key)) category = "disk";
|
|
else if (key.startsWith("automation:")) category = "automation";
|
|
else if (key.startsWith("heartbeat")) category = "automation";
|
|
else if (key.startsWith("endpoint-cert:")) category = "expiry";
|
|
else if (key.startsWith("endpoint:")) category = "monitoring";
|
|
const server = /^(?:(?:offline|disk|failed-units|smart|zfs|new-port|rule:\d+):server:|(?:endpoint(?:-cert)?|heartbeat(?:-fail)?):\d+:server:)(\d+)/.exec(key);
|
|
return { category, severity: severity === "critical" ? "critical" : "warning", serverId: server ? Number(server[1]) : null };
|
|
}
|