Open an osTicket ticket for an alert that lasts
With Settings -> Alert tickets switched on (off by default), every 15 minutes the problems on the Alerts page are looked at and one that is new and has lasted gets one ticket in osTicket, through its API (POST api/tickets.json, key in X-API-Key). One problem opens one ticket however many checks see it; the app remembers what it has seen in a new alert_tickets table (migration 0015). - Which: critical and warning by default, every kind; severities and kinds are settings. - A problem must be seen in 2 checks in a row first (1-4, a setting), so a blip that fixes itself, like one failed call to an integration, never opens a ticket. - One ticket per occurrence; a problem that clears and returns within 60 minutes is the same one. - More than 5 new problems in one check become a single summary ticket. - Never for alerts under a maintenance window, nor about osTicket itself or its overdue tickets. - Problems already there when it is switched on are left alone by default (the first check records them as a baseline); can be set to ticket them too. - If osTicket can't be reached nothing is recorded, so the next check retries; if a source couldn't be read, a problem isn't treated as gone. Every ticket is audited. The ticket API address, key (stored encrypted) and self-signed option are optional fields on the osTicket integration, which otherwise still only reads the database. The settings page can send a test ticket and run a check now; the Alerts page shows each alert's ticket number, linked to osTicket. osTicket's API can't close or update a ticket, so a ticket opened here is the end of this app's part in it. Tested against a stand-in osTicket, not a real one. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
f71756b119
commit
467d1a1a06
14 files changed
+3455
-2
No files matched your search
@@ -29,6 +29,7 @@ import { tagsRouter } from "./routes/tags.js";
|
||||
import { portsRouter } from "./routes/ports.js";
|
||||
import { alertsRouter } from "./routes/alerts.js";
|
||||
import { generatorRouter } from "./routes/generator.js";
|
||||
import { alertTicketsRouter } from "./routes/alertTickets.js";
|
||||
import { apiErrorHandler } from "./utils/apiErrorHandler.js";
|
||||
import { securityHeaders } from "./utils/securityHeaders.js";
|
||||
import { sameOriginGuard } from "./utils/sameOriginGuard.js";
|
||||
@@ -101,6 +102,7 @@ export function createApp() {
|
||||
app.use("/api/ports", portsRouter);
|
||||
app.use("/api/alerts", alertsRouter);
|
||||
app.use("/api/generator", generatorRouter);
|
||||
app.use("/api/alert-tickets", alertTicketsRouter);
|
||||
|
||||
if (existsSync(webDist)) {
|
||||
app.use(express.static(webDist));
|
||||
|
||||
+32
-1
@@ -1,5 +1,5 @@
|
||||
import { sql } from "drizzle-orm";
|
||||
import { sqliteTable, text, integer, real, uniqueIndex } from "drizzle-orm/sqlite-core";
|
||||
import { sqliteTable, text, integer, real, uniqueIndex, index } from "drizzle-orm/sqlite-core";
|
||||
|
||||
// ─── Users & roles ──────────────────────────────────────────────────────────
|
||||
|
||||
@@ -410,3 +410,34 @@ export const tagDefinitions = sqliteTable("tag_definitions", {
|
||||
.notNull()
|
||||
.default(sql`(current_timestamp)`),
|
||||
});
|
||||
|
||||
// ─── Tickets opened for alerts ──────────────────────────────────────────────
|
||||
|
||||
export const alertTicketStatuses = ["pending", "open", "baseline"] as const;
|
||||
export type AlertTicketStatus = (typeof alertTicketStatuses)[number];
|
||||
|
||||
// What the ticket feature remembers about each problem it has seen, so one problem opens one ticket however many checks see it.
|
||||
// A row is one *occurrence*: when the alert clears and later returns, that is a new row. Alert ids (e.g. "disk:server:3:/") are the
|
||||
// stable keys the Alerts page already uses, so they aren't a foreign key to anything.
|
||||
export const alertTickets = sqliteTable(
|
||||
"alert_tickets",
|
||||
{
|
||||
id: integer("id").primaryKey({ autoIncrement: true }),
|
||||
alertKey: text("alert_key").notNull(),
|
||||
// pending = seen, but not yet in enough checks in a row to open a ticket (so a blip that fixes itself never does)
|
||||
// open = a ticket was opened for it (ticket_number says which)
|
||||
// baseline = it was already there when the feature was switched on and "existing problems" is set to be left alone
|
||||
status: text("status").$type<AlertTicketStatus>().notNull(),
|
||||
seenCount: integer("seen_count").notNull().default(1),
|
||||
integrationId: integer("integration_id").references(() => integrations.id, { onDelete: "set null" }),
|
||||
ticketNumber: text("ticket_number"), // osTicket's own ticket number; null unless status is "open"
|
||||
grouped: integer("grouped", { mode: "boolean" }).notNull().default(false), // part of a summary ticket covering several alerts
|
||||
severity: text("severity").notNull(),
|
||||
message: text("message").notNull(), // what the alert said when it was first recorded
|
||||
firstSeenAt: text("first_seen_at").notNull(), // ISO
|
||||
lastSeenAt: text("last_seen_at").notNull(), // ISO
|
||||
ticketedAt: text("ticketed_at"), // ISO; when the ticket was opened
|
||||
clearedAt: text("cleared_at"), // ISO; null while the alert is still active
|
||||
},
|
||||
(t) => [index("alert_tickets_key_idx").on(t.alertKey)],
|
||||
);
|
||||
@@ -11,6 +11,7 @@ import { initProxmoxBackupScheduler } from "./services/proxmoxBackupScheduler.js
|
||||
import { initPbsVerificationScheduler } from "./services/pbsVerificationScheduler.js";
|
||||
import { initQuietHoursScheduler } from "./services/quietHoursScheduler.js";
|
||||
import { initHealthScheduler } from "./services/healthScheduler.js";
|
||||
import { initAlertTicketScheduler } from "./services/alertTicketScheduler.js";
|
||||
|
||||
{
|
||||
// Sessions are signed with this secret. Without one, the app would sign them with a value that's in the source code.
|
||||
@@ -36,6 +37,7 @@ await initProxmoxBackupScheduler();
|
||||
await initPbsVerificationScheduler();
|
||||
await initQuietHoursScheduler();
|
||||
await initHealthScheduler();
|
||||
await initAlertTicketScheduler();
|
||||
|
||||
createApp().listen(env.port, () => {
|
||||
console.log(`homelab-manager listening on port ${env.port}`);
|
||||
|
||||
@@ -75,6 +75,16 @@ export const INTEGRATION_FIELDS: Partial<Record<IntegrationType, IntegrationFiel
|
||||
optional: true,
|
||||
placeholder: "ost_ (osTicket's default, unless changed at install)",
|
||||
},
|
||||
// Only for opening tickets from alerts (Settings → Alert tickets). The rest of this integration reads the database and never writes.
|
||||
{
|
||||
key: "apiUrl",
|
||||
label: "Ticket API address (optional)",
|
||||
secret: false,
|
||||
optional: true,
|
||||
placeholder: "https://osticket.example.lan/ (the folder that contains api/tickets.json)",
|
||||
},
|
||||
{ key: "apiKey", label: "Ticket API key (optional)", secret: true, optional: true, type: "password" },
|
||||
{ key: "apiInsecure", label: "Allow self-signed certificate (ticket API)", secret: false, type: "checkbox" },
|
||||
],
|
||||
};
|
||||
|
||||
|
||||
@@ -0,0 +1,142 @@
|
||||
/**
|
||||
* Opens tickets in osTicket through its HTTP API (POST api/tickets.json).
|
||||
*
|
||||
* This is separate from the rest of the osTicket integration, which reads the database directly and can't write: here the
|
||||
* app is a client of osTicket's own API, authenticated with an API key made in osTicket (Admin Panel → Manage → API Keys). That
|
||||
* key is tied to an IP address — osTicket refuses it from anywhere else — and needs "can create tickets". Both are the most
|
||||
* common reason this fails, so the error for a refused key says so.
|
||||
*
|
||||
* What the API can't do is close, reopen or comment on a ticket, so a ticket opened here is the end of this app's part in it.
|
||||
*/
|
||||
import http from "node:http";
|
||||
import https from "node:https";
|
||||
import { withDiagLogging } from "../../services/diagLog.js";
|
||||
|
||||
export interface OsTicketApiConfig {
|
||||
apiUrl?: string | boolean;
|
||||
apiKey?: string | boolean;
|
||||
apiInsecure?: string | boolean;
|
||||
}
|
||||
|
||||
export interface NewTicket {
|
||||
name: string;
|
||||
email: string;
|
||||
subject: string;
|
||||
/** Plain text. */
|
||||
message: string;
|
||||
/** Let osTicket send its own new-ticket alert to staff. */
|
||||
alertStaff: boolean;
|
||||
topicId?: number | null;
|
||||
priorityId?: number | null;
|
||||
}
|
||||
|
||||
export interface CreatedTicket {
|
||||
/** osTicket's own ticket number, as it shows it to people. Null if it answered "created" without saying. */
|
||||
number: string | null;
|
||||
}
|
||||
|
||||
const TIMEOUT_MS = 15_000;
|
||||
|
||||
const asString = (v: unknown) => (typeof v === "string" ? v.trim() : "");
|
||||
|
||||
/** Whether the integration has what opening tickets needs. */
|
||||
export function hasTicketApi(config: OsTicketApiConfig): boolean {
|
||||
return asString(config.apiUrl) !== "" && asString(config.apiKey) !== "";
|
||||
}
|
||||
|
||||
/** The address of api/tickets.json under whatever the admin entered (which may or may not end in a slash, and may sit in a sub-folder). */
|
||||
export function ticketEndpoint(apiUrl: string): URL {
|
||||
const base = new URL(apiUrl.trim().endsWith("/") ? apiUrl.trim() : `${apiUrl.trim()}/`);
|
||||
if (base.protocol !== "http:" && base.protocol !== "https:") throw new Error("The ticket API address must start with http:// or https://");
|
||||
return new URL("api/tickets.json", base);
|
||||
}
|
||||
|
||||
/** Where a ticket can be opened in osTicket's staff panel, for a link. Null if the address isn't usable. */
|
||||
export function ticketUrl(apiUrl: string, number: string): string | null {
|
||||
try {
|
||||
const base = new URL(apiUrl.trim().endsWith("/") ? apiUrl.trim() : `${apiUrl.trim()}/`);
|
||||
return new URL(`scp/tickets.php?number=${encodeURIComponent(number)}`, base).href;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
const clean = (text: string, max = 200) => text.replace(/[\u0000-\u001f\u007f]+/g, " ").trim().slice(0, max);
|
||||
|
||||
function post(url: URL, headers: Record<string, string>, body: string, insecure: boolean): Promise<{ status: number; location?: string; text: string }> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const secure = url.protocol === "https:";
|
||||
const req = (secure ? https : http).request(
|
||||
{
|
||||
hostname: url.hostname,
|
||||
port: url.port || (secure ? 443 : 80),
|
||||
path: url.pathname + url.search,
|
||||
method: "POST",
|
||||
headers: { ...headers, "Content-Length": String(Buffer.byteLength(body)) },
|
||||
...(secure ? { rejectUnauthorized: !insecure } : {}),
|
||||
timeout: TIMEOUT_MS,
|
||||
},
|
||||
(res) => {
|
||||
let text = "";
|
||||
res.setEncoding("utf8");
|
||||
res.on("data", (chunk) => {
|
||||
if (text.length < 5000) text += chunk;
|
||||
});
|
||||
res.on("end", () => resolve({ status: res.statusCode ?? 0, location: res.headers.location, text }));
|
||||
},
|
||||
);
|
||||
req.on("timeout", () => req.destroy(new Error(`osTicket didn't answer within ${TIMEOUT_MS / 1000} seconds.`)));
|
||||
req.on("error", reject);
|
||||
req.end(body);
|
||||
});
|
||||
}
|
||||
|
||||
export function createOsTicketTicketApi(config: OsTicketApiConfig) {
|
||||
async function createTicket(ticket: NewTicket): Promise<CreatedTicket> {
|
||||
const apiUrl = asString(config.apiUrl);
|
||||
const apiKey = asString(config.apiKey);
|
||||
if (!apiUrl || !apiKey) throw new Error("The osTicket integration has no ticket API address and key set.");
|
||||
|
||||
const url = ticketEndpoint(apiUrl);
|
||||
const body = JSON.stringify({
|
||||
alert: ticket.alertStaff,
|
||||
// The "user" is this app, not a person: osTicket mailing an acknowledgement to it would only bounce.
|
||||
autorespond: false,
|
||||
source: "API",
|
||||
name: ticket.name,
|
||||
email: ticket.email,
|
||||
subject: ticket.subject,
|
||||
// osTicket reads the type of the message from an RFC 2397 data URL.
|
||||
message: `data:text/plain;charset=utf-8,${encodeURIComponent(ticket.message)}`,
|
||||
...(ticket.topicId ? { topicId: ticket.topicId } : {}),
|
||||
...(ticket.priorityId ? { priority: ticket.priorityId } : {}),
|
||||
});
|
||||
|
||||
const res = await post(url, { "Content-Type": "application/json", Accept: "application/json", "X-API-Key": apiKey, "User-Agent": "homelab-manager" }, body, config.apiInsecure === true || asString(config.apiInsecure) === "true");
|
||||
|
||||
if (res.status === 201) {
|
||||
const text = res.text.trim();
|
||||
let number: string | null = text;
|
||||
try {
|
||||
const parsed = JSON.parse(text);
|
||||
number = typeof parsed === "string" || typeof parsed === "number" ? String(parsed) : null;
|
||||
} catch {
|
||||
// plain text, which is what osTicket normally sends
|
||||
}
|
||||
number = number ? clean(number, 32) : null;
|
||||
return { number: number || null };
|
||||
}
|
||||
if (res.status >= 300 && res.status < 400) {
|
||||
throw new Error(`osTicket redirected to ${clean(res.location ?? "somewhere else", 120)}. Use the address it redirects to (often https instead of http) as the ticket API address.`);
|
||||
}
|
||||
if (res.status === 401 || res.status === 403) {
|
||||
throw new Error("osTicket refused the API key. Check the key, that it is allowed to create tickets, and that this server's IP address is the one the key is tied to.");
|
||||
}
|
||||
if (res.status === 404) throw new Error(`There is no osTicket API at ${url.href}. Check the ticket API address.`);
|
||||
if (res.status === 400) throw new Error(`osTicket rejected the ticket: ${clean(res.text) || "no reason given"}`);
|
||||
throw new Error(`osTicket answered with an error (HTTP ${res.status})${clean(res.text) ? `: ${clean(res.text)}` : ""}.`);
|
||||
}
|
||||
|
||||
// Through the diagnostic log like every other call out, so a refused key shows up there (and counts toward "integration failing").
|
||||
return withDiagLogging("osticket", { createTicket });
|
||||
}
|
||||
@@ -0,0 +1,118 @@
|
||||
import { Router } from "express";
|
||||
import { eq } from "drizzle-orm";
|
||||
import { z } from "zod";
|
||||
import { requireAuth, requireRole } from "../auth/middleware.js";
|
||||
import { db } from "../db/client.js";
|
||||
import { integrations } from "../db/schema.js";
|
||||
import { recordAudit } from "../services/audit.js";
|
||||
import { getAlertTicketStatus, resetBaseline, runAlertTicketCheck, sendTestTicket } from "../services/alertTickets.js";
|
||||
import { getSettings, updateSettings } from "../services/settingsStore.js";
|
||||
import { describeSettingsChanges } from "../services/settingsDiff.js";
|
||||
import { loadIntegrationConfig } from "../integrations/loadIntegration.js";
|
||||
import { hasTicketApi, type OsTicketApiConfig } from "../integrations/osticket/ticketApi.js";
|
||||
import { asyncHandler } from "../utils/asyncHandler.js";
|
||||
|
||||
export const alertTicketsRouter = Router();
|
||||
alertTicketsRouter.use(requireAuth, requireRole("admin"));
|
||||
|
||||
// What an alert can be about, as the Alerts page names it. "tickets" (osTicket's own overdue tickets) is never offered: a ticket
|
||||
// about tickets helps nobody.
|
||||
const CATEGORIES = [
|
||||
{ value: "offline", label: "Server down" },
|
||||
{ value: "disk", label: "Disk & storage" },
|
||||
{ value: "backup", label: "Backups" },
|
||||
{ value: "updates", label: "Updates" },
|
||||
{ value: "expiry", label: "Expiry" },
|
||||
{ value: "automation", label: "Automation" },
|
||||
{ value: "integration", label: "Integration failing" },
|
||||
{ value: "monitoring", label: "Monitoring" },
|
||||
] as const;
|
||||
|
||||
async function overview() {
|
||||
const rows = await db.select({ id: integrations.id, name: integrations.name, enabled: integrations.enabled }).from(integrations).where(eq(integrations.type, "osticket"));
|
||||
const osticket = [];
|
||||
for (const row of rows) {
|
||||
let ready = false;
|
||||
try {
|
||||
const loaded = await loadIntegrationConfig(row.id);
|
||||
ready = loaded ? hasTicketApi(loaded.config as OsTicketApiConfig) : false;
|
||||
} catch {
|
||||
ready = false;
|
||||
}
|
||||
osticket.push({ id: row.id, name: row.name, enabled: row.enabled, ticketApiReady: ready });
|
||||
}
|
||||
return { settings: (await getSettings()).alertTickets, integrations: osticket, categories: CATEGORIES, status: await getAlertTicketStatus() };
|
||||
}
|
||||
|
||||
alertTicketsRouter.get("/", asyncHandler(async (_req, res) => {
|
||||
res.json(await overview());
|
||||
}));
|
||||
|
||||
const updateSchema = z
|
||||
.object({
|
||||
enabled: z.boolean(),
|
||||
integrationId: z.number().int().positive().nullable(),
|
||||
severities: z.array(z.enum(["critical", "warning", "info"])).max(3),
|
||||
categories: z.array(z.enum(CATEGORIES.map((c) => c.value) as [string, ...string[]])).max(20),
|
||||
requesterName: z.string().max(100),
|
||||
requesterEmail: z.string().max(200).refine((v) => v === "" || /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(v), "That isn't an email address."),
|
||||
topicId: z.number().int().positive().nullable(),
|
||||
priorityId: z.number().int().positive().nullable(),
|
||||
notifyStaff: z.boolean(),
|
||||
existingProblems: z.enum(["ignore", "ticket"]),
|
||||
confirmChecks: z.number().int().min(1).max(10),
|
||||
summaryAbove: z.number().int().min(1).max(50),
|
||||
reopenAfterMinutes: z.number().int().min(0).max(1440),
|
||||
})
|
||||
.partial();
|
||||
|
||||
alertTicketsRouter.put("/", asyncHandler(async (req, res) => {
|
||||
const parsed = updateSchema.safeParse(req.body);
|
||||
if (!parsed.success) {
|
||||
const message = parsed.error.issues[0]?.message ?? "Those settings aren't valid.";
|
||||
return res.status(400).json({ error: "invalid_body", message, details: parsed.error.flatten() });
|
||||
}
|
||||
|
||||
const before = (await getSettings()).alertTickets;
|
||||
const after = { ...before, ...parsed.data };
|
||||
|
||||
// Switching it on needs everything it uses to be there, so it can't be left "on" and silently doing nothing.
|
||||
if (after.enabled) {
|
||||
if (!after.integrationId) return res.status(400).json({ error: "invalid_settings", message: "Choose which osTicket integration to open tickets in." });
|
||||
const [row] = await db.select({ type: integrations.type }).from(integrations).where(eq(integrations.id, after.integrationId)).limit(1);
|
||||
if (!row || row.type !== "osticket") return res.status(400).json({ error: "invalid_settings", message: "The chosen integration isn't an osTicket integration." });
|
||||
const loaded = await loadIntegrationConfig(after.integrationId);
|
||||
if (!loaded || !hasTicketApi(loaded.config as OsTicketApiConfig)) {
|
||||
return res.status(400).json({ error: "invalid_settings", message: "That osTicket integration has no ticket API address and key yet. Add them under Integrations first." });
|
||||
}
|
||||
if (after.severities.length === 0) return res.status(400).json({ error: "invalid_settings", message: "Choose at least one severity." });
|
||||
if (!after.requesterEmail.trim()) return res.status(400).json({ error: "invalid_settings", message: "Enter the email the tickets should come from — osTicket needs one for every ticket." });
|
||||
}
|
||||
|
||||
await updateSettings({ alertTickets: parsed.data });
|
||||
// Switched on (or off and on again): what is wrong at that moment is the starting point, not a pile of new tickets.
|
||||
if (!before.enabled !== !after.enabled || parsed.data.existingProblems !== undefined) await resetBaseline();
|
||||
|
||||
await recordAudit({
|
||||
actor: req.currentUser!,
|
||||
category: "settings",
|
||||
action: "update_alert_tickets",
|
||||
targetType: "settings",
|
||||
detail: { changes: describeSettingsChanges({ alertTickets: before }, { alertTickets: parsed.data }).alertTickets ?? {} },
|
||||
});
|
||||
res.json(await overview());
|
||||
}));
|
||||
|
||||
alertTicketsRouter.post("/test", asyncHandler(async (req, res) => {
|
||||
try {
|
||||
const created = await sendTestTicket(req.currentUser!);
|
||||
res.json({ ok: true, ticketNumber: created.number });
|
||||
} catch (err) {
|
||||
res.status(502).json({ error: "ticket_failed", message: err instanceof Error ? err.message : String(err) });
|
||||
}
|
||||
}));
|
||||
|
||||
alertTicketsRouter.post("/run", asyncHandler(async (req, res) => {
|
||||
const summary = await runAlertTicketCheck({ actor: req.currentUser! });
|
||||
res.json({ summary, status: await getAlertTicketStatus() });
|
||||
}));
|
||||
@@ -1,6 +1,7 @@
|
||||
import { Router } from "express";
|
||||
import { requireAuth } from "../auth/middleware.js";
|
||||
import { getAlerts } from "../services/alerts.js";
|
||||
import { ticketsForAlerts, type AlertTicketInfo } from "../services/alertTickets.js";
|
||||
import { asyncHandler } from "../utils/asyncHandler.js";
|
||||
|
||||
export const alertsRouter = Router();
|
||||
@@ -10,5 +11,13 @@ alertsRouter.use(requireAuth);
|
||||
// Everyone signed in can see it — it's the same server, disk and backup status the other pages already show.
|
||||
// `?refresh=1` asks for a fresh check instead of a recent one.
|
||||
alertsRouter.get("/", asyncHandler(async (req, res) => {
|
||||
res.json(await getAlerts(req.query.refresh === "1"));
|
||||
const report = await getAlerts(req.query.refresh === "1");
|
||||
// The ticket opened for each alert, if there is one. Never allowed to stop the page from loading.
|
||||
let tickets = new Map<string, AlertTicketInfo>();
|
||||
try {
|
||||
tickets = await ticketsForAlerts(report.alerts.map((a) => a.id));
|
||||
} catch (err) {
|
||||
console.error("[alerts] couldn't look up tickets:", err);
|
||||
}
|
||||
res.json({ ...report, alerts: report.alerts.map((a) => ({ ...a, ticket: tickets.get(a.id) ?? null })) });
|
||||
}));
|
||||
@@ -0,0 +1,33 @@
|
||||
import { runAlertTicketCheck } from "./alertTickets.js";
|
||||
|
||||
// The same 15 minutes the health checks use, and for the same reason: agents report on that rhythm, so a check never judges a
|
||||
// server on figures that are merely waiting for the next report. An alert must also last two checks before it opens a ticket.
|
||||
const INTERVAL_MS = 15 * 60 * 1000;
|
||||
// Not at once after a start: the health check makes its own first round of calls to every integration then, and the alerts check would
|
||||
// repeat them. (Alerts about servers are also held back for a while after a restart, since agents haven't reported yet.)
|
||||
const FIRST_CHECK_DELAY_MS = 90 * 1000;
|
||||
|
||||
let timer: ReturnType<typeof setInterval> | null = null;
|
||||
|
||||
async function pass(): Promise<void> {
|
||||
try {
|
||||
const result = await runAlertTicketCheck();
|
||||
if (result.skipped) return;
|
||||
if (result.ticketsOpened > 0 || result.failed > 0 || result.error) {
|
||||
console.log(
|
||||
`[alertTickets] opened ${result.ticketsOpened} ticket${result.ticketsOpened === 1 ? "" : "s"} for ${result.alertsCovered} alert${result.alertsCovered === 1 ? "" : "s"}` +
|
||||
(result.failed > 0 ? `, ${result.failed} failed` : "") +
|
||||
(result.error ? ` — ${result.error}` : ""),
|
||||
);
|
||||
}
|
||||
} catch (err) {
|
||||
console.error("[alertTickets] check failed:", err);
|
||||
}
|
||||
}
|
||||
|
||||
export async function initAlertTicketScheduler(): Promise<void> {
|
||||
if (timer) return;
|
||||
timer = setInterval(() => void pass(), INTERVAL_MS);
|
||||
setTimeout(() => void pass(), FIRST_CHECK_DELAY_MS);
|
||||
console.log(`[alertTickets] checking for alerts that need an osTicket ticket every ${INTERVAL_MS / 60000} minutes (does nothing until enabled in Settings)`);
|
||||
}
|
||||
@@ -0,0 +1,440 @@
|
||||
/**
|
||||
* Opening an osTicket ticket for an alert.
|
||||
*
|
||||
* The Alerts page already works out everything that is wrong right now. This looks at that list every 15 minutes and, for a
|
||||
* problem that is new and has lasted, opens one ticket — then remembers it, so the same problem never opens a second one however
|
||||
* many checks see it. What it deliberately does not do: close, reopen or comment on a ticket (osTicket's API can't), or open one
|
||||
* for something under a maintenance window.
|
||||
*
|
||||
* planTicketRun() is the decision, a pure function of "the alerts now", "what was remembered" and the settings. Everything else
|
||||
* here is the doing: reading and writing that memory and calling osTicket.
|
||||
*/
|
||||
import { and, desc, eq, inArray, isNull, lt, or, gte } from "drizzle-orm";
|
||||
import { db } from "../db/client.js";
|
||||
import { alertTickets, integrations, users } from "../db/schema.js";
|
||||
import { env } from "../env.js";
|
||||
import { getAlerts } from "./alerts.js";
|
||||
import type { Alert, AlertCategory } from "./alertTypes.js";
|
||||
import { recordAudit } from "./audit.js";
|
||||
import { getInternalFlag, getSettings, setInternalFlag, type AlertTicketSettings } from "./settingsStore.js";
|
||||
import { loadIntegrationConfig } from "../integrations/loadIntegration.js";
|
||||
import { createOsTicketTicketApi, hasTicketApi, ticketUrl, type CreatedTicket, type NewTicket, type OsTicketApiConfig } from "../integrations/osticket/ticketApi.js";
|
||||
|
||||
type CurrentUser = typeof users.$inferSelect;
|
||||
export type TicketRow = typeof alertTickets.$inferSelect;
|
||||
type Report = Awaited<ReturnType<typeof getAlerts>>;
|
||||
|
||||
const BASELINE_FLAG = "alertTicketsBaselineDone";
|
||||
const LAST_RUN_FLAG = "alertTicketsLastRun";
|
||||
/** How long a cleared problem is remembered after it cleared. */
|
||||
const KEEP_CLEARED_DAYS = 180;
|
||||
|
||||
// ─── Deciding ───────────────────────────────────────────────────────────────
|
||||
|
||||
/** What the ticket feature never opens a ticket about, whatever the settings: the ticketing system itself. */
|
||||
function isAboutTicketing(alert: Alert): boolean {
|
||||
// A ticket about osTicket being down, or about overdue tickets, can't help: it would go to the very thing that is down (or add to the pile).
|
||||
return alert.category === "tickets" || alert.id === "integration:failing:osticket";
|
||||
}
|
||||
|
||||
export function qualifies(alert: Alert, settings: AlertTicketSettings): boolean {
|
||||
if (alert.silenced) return false; // under a maintenance window: expected, not a reason to wake anyone
|
||||
if (isAboutTicketing(alert)) return false;
|
||||
if (!settings.severities.includes(alert.severity)) return false;
|
||||
return settings.categories.length === 0 || settings.categories.includes(alert.category);
|
||||
}
|
||||
|
||||
export type Op =
|
||||
| { kind: "insertPending"; alert: Alert }
|
||||
| { kind: "insertBaseline"; alert: Alert }
|
||||
| { kind: "bump"; row: TicketRow; alert: Alert } // another check in a row, not yet enough
|
||||
| { kind: "touch"; row: TicketRow } // still there; nothing else to do
|
||||
| { kind: "reactivate"; row: TicketRow } // cleared a moment ago and is back: the same problem
|
||||
| { kind: "clear"; row: TicketRow } // gone
|
||||
| { kind: "dropPending"; row: TicketRow }; // a blip that went away before it lasted
|
||||
|
||||
export interface Candidate {
|
||||
alert: Alert;
|
||||
/** The pending row to turn into a ticketed one, or null for a problem with no row yet (confirmChecks of 1). */
|
||||
row: TicketRow | null;
|
||||
}
|
||||
|
||||
export interface Plan {
|
||||
ops: Op[];
|
||||
/** Alerts that have now lasted long enough and should open a ticket. */
|
||||
toTicket: Candidate[];
|
||||
}
|
||||
|
||||
export function planTicketRun(input: {
|
||||
alerts: Alert[];
|
||||
/** The latest row for each alert id that has one. */
|
||||
rows: TicketRow[];
|
||||
settings: AlertTicketSettings;
|
||||
now: Date;
|
||||
/** The first check since this was switched on: record what is already wrong without opening anything for it. */
|
||||
baseline: boolean;
|
||||
/** Everything could be read. Only then does "not in the list" mean "gone" — otherwise it may just mean "couldn't look". */
|
||||
complete: boolean;
|
||||
}): Plan {
|
||||
const { alerts, rows, settings, now, baseline, complete } = input;
|
||||
const byKey = new Map(rows.map((r) => [r.alertKey, r]));
|
||||
const present = new Set(alerts.map((a) => a.id));
|
||||
const ops: Op[] = [];
|
||||
const toTicket: Candidate[] = [];
|
||||
|
||||
for (const alert of alerts) {
|
||||
const row = byKey.get(alert.id);
|
||||
const wanted = qualifies(alert, settings);
|
||||
|
||||
if (row && !row.clearedAt) {
|
||||
if (row.status !== "pending") {
|
||||
ops.push({ kind: "touch", row });
|
||||
} else if (!wanted) {
|
||||
ops.push({ kind: "touch", row }); // there, but silenced or filtered out right now: wait, don't count it
|
||||
} else if (row.seenCount + 1 >= settings.confirmChecks) {
|
||||
toTicket.push({ alert, row });
|
||||
} else {
|
||||
ops.push({ kind: "bump", row, alert });
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!wanted) continue;
|
||||
|
||||
if (row?.clearedAt && now.getTime() - Date.parse(row.clearedAt) < settings.reopenAfterMinutes * 60_000) {
|
||||
ops.push({ kind: "reactivate", row }); // flapping: the same problem, not a new one
|
||||
continue;
|
||||
}
|
||||
|
||||
// A new occurrence.
|
||||
if (baseline) ops.push({ kind: "insertBaseline", alert });
|
||||
else if (settings.confirmChecks <= 1) toTicket.push({ alert, row: null });
|
||||
else ops.push({ kind: "insertPending", alert });
|
||||
}
|
||||
|
||||
if (complete) {
|
||||
for (const row of rows) {
|
||||
if (row.clearedAt || present.has(row.alertKey)) continue;
|
||||
ops.push({ kind: row.status === "pending" ? "dropPending" : "clear", row });
|
||||
}
|
||||
}
|
||||
return { ops, toTicket };
|
||||
}
|
||||
|
||||
// ─── Writing the ticket ─────────────────────────────────────────────────────
|
||||
|
||||
const CATEGORY_LABELS: Record<AlertCategory, string> = {
|
||||
offline: "Server down",
|
||||
disk: "Disk & storage",
|
||||
backup: "Backups",
|
||||
updates: "Updates",
|
||||
expiry: "Expiry",
|
||||
automation: "Automation",
|
||||
integration: "Integration failing",
|
||||
monitoring: "Monitoring",
|
||||
tickets: "Tickets",
|
||||
};
|
||||
const SEVERITY_LABELS = { critical: "Critical", warning: "Warning", info: "Info" } as const;
|
||||
|
||||
const stamp = (iso: string) => `${iso.slice(0, 16).replace("T", " ")} UTC`;
|
||||
const absolute = (path: string) => `${env.appBaseUrl.replace(/\/+$/, "")}${path}`;
|
||||
const oneLine = (text: string, max: number) => (text.replace(/\s+/g, " ").trim().length > max ? `${text.replace(/\s+/g, " ").trim().slice(0, max - 1)}…` : text.replace(/\s+/g, " ").trim());
|
||||
|
||||
export function buildTicket(alert: Alert, firstSeenAt: string): { subject: string; message: string } {
|
||||
const lines = [
|
||||
"Homelab Manager found a problem.",
|
||||
"",
|
||||
`Severity: ${SEVERITY_LABELS[alert.severity]}`,
|
||||
`Kind: ${CATEGORY_LABELS[alert.category] ?? alert.category}`,
|
||||
`Source: ${alert.source}`,
|
||||
`Problem: ${alert.message}`,
|
||||
`First seen: ${stamp(firstSeenAt)} (it checks every 15 minutes)`,
|
||||
"",
|
||||
...(alert.link ? [`Details: ${absolute(alert.link)}`] : []),
|
||||
`Everything that is wrong right now: ${absolute("/alerts")}`,
|
||||
"",
|
||||
`Opened automatically by Homelab Manager (alert ${alert.id}). It is not updated or closed automatically — close it here when it is dealt with.`,
|
||||
];
|
||||
return { subject: oneLine(`[Homelab] ${SEVERITY_LABELS[alert.severity]}: ${alert.message}`, 200), message: lines.join("\n") };
|
||||
}
|
||||
|
||||
export function buildSummaryTicket(alerts: { alert: Alert; firstSeenAt: string }[]): { subject: string; message: string } {
|
||||
const lines = [
|
||||
`Homelab Manager found ${alerts.length} problems at once. They are in one ticket so a single outage doesn't fill the queue with dozens.`,
|
||||
"",
|
||||
...alerts.map(({ alert }) => `- [${SEVERITY_LABELS[alert.severity]}] ${alert.source}: ${alert.message}`),
|
||||
"",
|
||||
`Everything that is wrong right now: ${absolute("/alerts")}`,
|
||||
"",
|
||||
"Opened automatically by Homelab Manager. It is not updated or closed automatically — close it here when it is dealt with.",
|
||||
];
|
||||
return { subject: `[Homelab] ${alerts.length} problems detected`, message: lines.join("\n") };
|
||||
}
|
||||
|
||||
// ─── Doing ──────────────────────────────────────────────────────────────────
|
||||
|
||||
export interface RunSummary {
|
||||
at: string;
|
||||
skipped?: "disabled" | "busy";
|
||||
/** Why nothing could be done (missing setup, osTicket refusing). */
|
||||
error?: string;
|
||||
/** Tickets opened in osTicket (a summary ticket counts as one). */
|
||||
ticketsOpened: number;
|
||||
/** Alerts those tickets cover. */
|
||||
alertsCovered: number;
|
||||
/** Problems recorded without a ticket because they were already there when this was switched on. */
|
||||
baseline: number;
|
||||
/** Problems seen but not yet in enough checks in a row. */
|
||||
pending: number;
|
||||
cleared: number;
|
||||
/** Tickets that couldn't be opened this time; they are tried again at the next check. */
|
||||
failed: number;
|
||||
}
|
||||
|
||||
export interface RunDeps {
|
||||
getReport(): Promise<Report>;
|
||||
createTicket(ticket: NewTicket): Promise<CreatedTicket>;
|
||||
now(): Date;
|
||||
}
|
||||
|
||||
let running = false;
|
||||
|
||||
const empty = (now: Date): RunSummary => ({ at: now.toISOString(), ticketsOpened: 0, alertsCovered: 0, baseline: 0, pending: 0, cleared: 0, failed: 0 });
|
||||
|
||||
/** The osTicket integration to use, with what it needs to open tickets. Throws a sentence an admin can act on. */
|
||||
async function ticketClient(settings: AlertTicketSettings) {
|
||||
if (!settings.integrationId) throw new Error("No osTicket integration is chosen for tickets.");
|
||||
const loaded = await loadIntegrationConfig(settings.integrationId);
|
||||
if (!loaded || loaded.integration.type !== "osticket") throw new Error("The chosen osTicket integration no longer exists.");
|
||||
if (!loaded.integration.enabled) throw new Error("The chosen osTicket integration is switched off.");
|
||||
const config = loaded.config as OsTicketApiConfig;
|
||||
if (!hasTicketApi(config)) throw new Error("The osTicket integration has no ticket API address and key. Edit it under Integrations and fill them in.");
|
||||
if (!settings.requesterEmail.trim()) throw new Error("No requester email is set — osTicket needs one for every ticket.");
|
||||
return createOsTicketTicketApi(config);
|
||||
}
|
||||
|
||||
async function latestRows(now: Date, reopenAfterMinutes: number): Promise<TicketRow[]> {
|
||||
const since = new Date(now.getTime() - Math.max(reopenAfterMinutes, 60) * 60_000).toISOString();
|
||||
const rows = await db
|
||||
.select()
|
||||
.from(alertTickets)
|
||||
.where(or(isNull(alertTickets.clearedAt), gte(alertTickets.clearedAt, since)))
|
||||
.orderBy(desc(alertTickets.id));
|
||||
const latest = new Map<string, TicketRow>();
|
||||
for (const row of rows) if (!latest.has(row.alertKey)) latest.set(row.alertKey, row);
|
||||
return [...latest.values()];
|
||||
}
|
||||
|
||||
export async function runAlertTicketCheck(options: { actor?: CurrentUser; deps?: Partial<RunDeps> } = {}): Promise<RunSummary> {
|
||||
const nowFn = options.deps?.now ?? (() => new Date());
|
||||
const now = nowFn();
|
||||
const summary = empty(now);
|
||||
|
||||
const { alertTickets: settings } = await getSettings();
|
||||
if (!settings.enabled) {
|
||||
// Switched off: the next time it's switched on counts as a fresh start, so what's wrong by then is the baseline.
|
||||
await setInternalFlag(BASELINE_FLAG, "");
|
||||
return { ...summary, skipped: "disabled" };
|
||||
}
|
||||
if (running) return { ...summary, skipped: "busy" };
|
||||
running = true;
|
||||
|
||||
try {
|
||||
let client: { createTicket(ticket: NewTicket): Promise<CreatedTicket> };
|
||||
try {
|
||||
client = options.deps?.createTicket ? { createTicket: options.deps.createTicket } : await ticketClient(settings);
|
||||
} catch (err) {
|
||||
summary.error = err instanceof Error ? err.message : String(err);
|
||||
await setInternalFlag(LAST_RUN_FLAG, JSON.stringify(summary));
|
||||
return summary;
|
||||
}
|
||||
|
||||
const report = await (options.deps?.getReport ?? (() => getAlerts(true)))();
|
||||
const complete = report.couldntCheck.length === 0;
|
||||
const baseline = settings.existingProblems === "ignore" && (await getInternalFlag(BASELINE_FLAG)) !== "1";
|
||||
const rows = await latestRows(now, settings.reopenAfterMinutes);
|
||||
const plan = planTicketRun({ alerts: report.alerts, rows, settings, now, baseline, complete });
|
||||
const iso = now.toISOString();
|
||||
|
||||
for (const op of plan.ops) {
|
||||
switch (op.kind) {
|
||||
case "insertPending":
|
||||
case "insertBaseline":
|
||||
await db.insert(alertTickets).values({
|
||||
alertKey: op.alert.id,
|
||||
status: op.kind === "insertPending" ? "pending" : "baseline",
|
||||
seenCount: 1,
|
||||
integrationId: settings.integrationId,
|
||||
severity: op.alert.severity,
|
||||
message: op.alert.message,
|
||||
firstSeenAt: iso,
|
||||
lastSeenAt: iso,
|
||||
});
|
||||
if (op.kind === "insertPending") summary.pending++;
|
||||
else summary.baseline++;
|
||||
break;
|
||||
case "bump":
|
||||
await db.update(alertTickets).set({ seenCount: op.row.seenCount + 1, lastSeenAt: iso, severity: op.alert.severity, message: op.alert.message }).where(eq(alertTickets.id, op.row.id));
|
||||
summary.pending++;
|
||||
break;
|
||||
case "touch":
|
||||
await db.update(alertTickets).set({ lastSeenAt: iso }).where(eq(alertTickets.id, op.row.id));
|
||||
if (op.row.status === "pending") summary.pending++;
|
||||
break;
|
||||
case "reactivate":
|
||||
await db.update(alertTickets).set({ clearedAt: null, lastSeenAt: iso }).where(eq(alertTickets.id, op.row.id));
|
||||
break;
|
||||
case "clear":
|
||||
await db.update(alertTickets).set({ clearedAt: iso }).where(eq(alertTickets.id, op.row.id));
|
||||
summary.cleared++;
|
||||
break;
|
||||
case "dropPending":
|
||||
await db.delete(alertTickets).where(eq(alertTickets.id, op.row.id));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Open the tickets: one each, or — when a lot appear at once — one for all of them.
|
||||
const requester = { name: settings.requesterName.trim() || "Homelab Manager", email: settings.requesterEmail.trim() };
|
||||
const common = { ...requester, alertStaff: settings.notifyStaff, topicId: settings.topicId, priorityId: settings.priorityId };
|
||||
const firstSeen = (c: Candidate) => c.row?.firstSeenAt ?? iso;
|
||||
|
||||
const groups: Candidate[][] = plan.toTicket.length > settings.summaryAbove ? [plan.toTicket] : plan.toTicket.map((c) => [c]);
|
||||
for (const group of groups) {
|
||||
const content =
|
||||
group.length === 1 ? buildTicket(group[0].alert, firstSeen(group[0])) : buildSummaryTicket(group.map((c) => ({ alert: c.alert, firstSeenAt: firstSeen(c) })));
|
||||
let created: CreatedTicket;
|
||||
try {
|
||||
created = await client.createTicket({ ...common, ...content });
|
||||
} catch (err) {
|
||||
summary.failed += group.length;
|
||||
summary.error ??= err instanceof Error ? err.message : String(err);
|
||||
console.error("[alertTickets] couldn't open a ticket:", err instanceof Error ? err.message : err);
|
||||
continue; // nothing was recorded, so the next check tries again
|
||||
}
|
||||
const grouped = group.length > 1;
|
||||
for (const c of group) {
|
||||
const values = { status: "open" as const, ticketNumber: created.number, grouped, ticketedAt: iso, lastSeenAt: iso, severity: c.alert.severity, message: c.alert.message, integrationId: settings.integrationId };
|
||||
if (c.row) await db.update(alertTickets).set(values).where(eq(alertTickets.id, c.row.id));
|
||||
else await db.insert(alertTickets).values({ alertKey: c.alert.id, seenCount: 1, firstSeenAt: iso, ...values });
|
||||
await recordAudit({
|
||||
actor: options.actor,
|
||||
category: "alert_ticket",
|
||||
action: "create",
|
||||
targetType: "alert",
|
||||
targetId: c.alert.id,
|
||||
detail: { ticket: created.number, severity: c.alert.severity, ...(grouped ? { summaryOf: group.length } : {}), integrationId: settings.integrationId },
|
||||
});
|
||||
}
|
||||
summary.ticketsOpened++;
|
||||
summary.alertsCovered += group.length;
|
||||
}
|
||||
|
||||
// The baseline is only complete if everything could be read: a source that was down would otherwise hide problems that then
|
||||
// look "new" — and get tickets — once it comes back.
|
||||
if (baseline && complete) await setInternalFlag(BASELINE_FLAG, "1");
|
||||
|
||||
await db.delete(alertTickets).where(and(lt(alertTickets.clearedAt, new Date(now.getTime() - KEEP_CLEARED_DAYS * 86_400_000).toISOString())));
|
||||
await setInternalFlag(LAST_RUN_FLAG, JSON.stringify(summary));
|
||||
return summary;
|
||||
} finally {
|
||||
running = false;
|
||||
}
|
||||
}
|
||||
|
||||
/** The next check starts afresh: it records what is wrong at that moment as the baseline instead of opening tickets for it. */
|
||||
export async function resetBaseline(): Promise<void> {
|
||||
await setInternalFlag(BASELINE_FLAG, "");
|
||||
}
|
||||
|
||||
/** A ticket that says it is a test, to check the address, the key and where tickets land — without waiting for a real problem. */
|
||||
export async function sendTestTicket(actor: CurrentUser): Promise<CreatedTicket> {
|
||||
const { alertTickets: settings } = await getSettings();
|
||||
const client = await ticketClient(settings);
|
||||
const created = await client.createTicket({
|
||||
name: settings.requesterName.trim() || "Homelab Manager",
|
||||
email: settings.requesterEmail.trim(),
|
||||
alertStaff: settings.notifyStaff,
|
||||
topicId: settings.topicId,
|
||||
priorityId: settings.priorityId,
|
||||
subject: "[Homelab] Test ticket from Homelab Manager",
|
||||
message: `This is a test. ${actor.name ?? actor.email ?? "An administrator"} asked Homelab Manager to open it from Settings → Alert tickets, to check that tickets can be created here.\n\nNothing is wrong. You can close this ticket.\n\n${absolute("/settings/alert-tickets")}`,
|
||||
});
|
||||
await recordAudit({ actor, category: "alert_ticket", action: "test", targetType: "integration", targetId: settings.integrationId ?? undefined, detail: { ticket: created.number } });
|
||||
return created;
|
||||
}
|
||||
|
||||
// ─── What the pages show ────────────────────────────────────────────────────
|
||||
|
||||
export interface AlertTicketInfo {
|
||||
number: string | null;
|
||||
/** The ticket in osTicket's staff panel, when the address is known. */
|
||||
url: string | null;
|
||||
/** Part of a summary ticket that covers several alerts. */
|
||||
grouped: boolean;
|
||||
openedAt: string | null;
|
||||
}
|
||||
|
||||
/** The ticket opened for each of these alerts that has one and is still active, by alert id. */
|
||||
export async function ticketsForAlerts(alertIds: string[]): Promise<Map<string, AlertTicketInfo>> {
|
||||
const result = new Map<string, AlertTicketInfo>();
|
||||
if (alertIds.length === 0) return result;
|
||||
const rows = await db
|
||||
.select()
|
||||
.from(alertTickets)
|
||||
.where(and(inArray(alertTickets.alertKey, alertIds), eq(alertTickets.status, "open"), isNull(alertTickets.clearedAt)))
|
||||
.orderBy(desc(alertTickets.id));
|
||||
|
||||
const apiUrls = new Map<number, string | null>();
|
||||
async function apiUrlOf(integrationId: number | null): Promise<string | null> {
|
||||
if (integrationId === null) return null;
|
||||
if (!apiUrls.has(integrationId)) {
|
||||
const [row] = await db.select({ config: integrations.config }).from(integrations).where(eq(integrations.id, integrationId)).limit(1);
|
||||
let url: string | null = null;
|
||||
try {
|
||||
const parsed = row?.config ? (JSON.parse(row.config) as { apiUrl?: unknown }) : {};
|
||||
url = typeof parsed.apiUrl === "string" && parsed.apiUrl.trim() ? parsed.apiUrl : null;
|
||||
} catch {
|
||||
url = null;
|
||||
}
|
||||
apiUrls.set(integrationId, url);
|
||||
}
|
||||
return apiUrls.get(integrationId) ?? null;
|
||||
}
|
||||
|
||||
for (const row of rows) {
|
||||
if (result.has(row.alertKey)) continue;
|
||||
const base = await apiUrlOf(row.integrationId);
|
||||
result.set(row.alertKey, { number: row.ticketNumber, url: base && row.ticketNumber ? ticketUrl(base, row.ticketNumber) : null, grouped: row.grouped, openedAt: row.ticketedAt });
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
export interface AlertTicketStatus {
|
||||
lastRun: RunSummary | null;
|
||||
/** Active problems with a ticket / waiting to last long enough / already there when this was switched on. */
|
||||
open: number;
|
||||
pending: number;
|
||||
baseline: number;
|
||||
/** The next check will record what's wrong as the baseline instead of opening tickets for it. */
|
||||
baselinePending: boolean;
|
||||
}
|
||||
|
||||
export async function getAlertTicketStatus(): Promise<AlertTicketStatus> {
|
||||
const active = await db.select({ status: alertTickets.status }).from(alertTickets).where(isNull(alertTickets.clearedAt));
|
||||
let lastRun: RunSummary | null = null;
|
||||
try {
|
||||
const raw = await getInternalFlag(LAST_RUN_FLAG);
|
||||
lastRun = raw ? (JSON.parse(raw) as RunSummary) : null;
|
||||
} catch {
|
||||
lastRun = null;
|
||||
}
|
||||
const { alertTickets: settings } = await getSettings();
|
||||
return {
|
||||
lastRun,
|
||||
open: active.filter((r) => r.status === "open").length,
|
||||
pending: active.filter((r) => r.status === "pending").length,
|
||||
baseline: active.filter((r) => r.status === "baseline").length,
|
||||
baselinePending: settings.enabled && settings.existingProblems === "ignore" && (await getInternalFlag(BASELINE_FLAG)) !== "1",
|
||||
};
|
||||
}
|
||||
@@ -107,6 +107,35 @@ export interface NameGeneratorSettings {
|
||||
themes: NameTheme[];
|
||||
}
|
||||
|
||||
export type AlertTicketSeverity = "critical" | "warning" | "info";
|
||||
|
||||
/** Opening an osTicket ticket for an alert. Off until an admin sets it up under Settings → Alert tickets. */
|
||||
export interface AlertTicketSettings {
|
||||
enabled: boolean;
|
||||
/** The osTicket integration whose API (address and key) is used. */
|
||||
integrationId: number | null;
|
||||
/** Which severities open a ticket. */
|
||||
severities: AlertTicketSeverity[];
|
||||
/** Which kinds of alert (the categories on the Alerts page). Empty means every kind. */
|
||||
categories: string[];
|
||||
/** Who the tickets appear to be from. osTicket needs both; the email is where staff replies would go. */
|
||||
requesterName: string;
|
||||
requesterEmail: string;
|
||||
/** osTicket help topic and priority ids, if tickets should land somewhere other than its defaults. */
|
||||
topicId: number | null;
|
||||
priorityId: number | null;
|
||||
/** Let osTicket send its own "new ticket" alert to staff, per its own settings. */
|
||||
notifyStaff: boolean;
|
||||
/** Problems already present when this is switched on: left alone ("ignore") or given tickets too ("ticket"). */
|
||||
existingProblems: "ignore" | "ticket";
|
||||
/** An alert must be there in this many checks in a row (15 minutes apart) before it opens a ticket, so a blip that fixes itself never does. */
|
||||
confirmChecks: number;
|
||||
/** More new alerts than this in one check become a single summary ticket instead of one each. */
|
||||
summaryAbove: number;
|
||||
/** An alert that clears and comes back within this many minutes is still the same problem, not a new ticket. */
|
||||
reopenAfterMinutes: number;
|
||||
}
|
||||
|
||||
export interface AppSettings {
|
||||
gotify: GotifySettings;
|
||||
ntfy: NtfySettings;
|
||||
@@ -121,6 +150,7 @@ export interface AppSettings {
|
||||
healthChecks: HealthCheckSettings;
|
||||
consistency: ConsistencySettings;
|
||||
nameGenerator: NameGeneratorSettings;
|
||||
alertTickets: AlertTicketSettings;
|
||||
}
|
||||
|
||||
const DEFAULTS: AppSettings = {
|
||||
@@ -155,6 +185,21 @@ const DEFAULTS: AppSettings = {
|
||||
// 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"] },
|
||||
nameGenerator: { themes: defaultThemes() },
|
||||
alertTickets: {
|
||||
enabled: false,
|
||||
integrationId: null,
|
||||
severities: ["critical", "warning"],
|
||||
categories: [],
|
||||
requesterName: "Homelab Manager",
|
||||
requesterEmail: "",
|
||||
topicId: null,
|
||||
priorityId: null,
|
||||
notifyStaff: true,
|
||||
existingProblems: "ignore",
|
||||
confirmChecks: 2,
|
||||
summaryAbove: 5,
|
||||
reopenAfterMinutes: 60,
|
||||
},
|
||||
};
|
||||
|
||||
const KEYS = Object.keys(DEFAULTS) as (keyof AppSettings)[];
|
||||
|
||||
Reference in new issue
Block a user