Alert rules of your own, storage forecasts for Proxmox and Synology, and snooze
- Monitoring -> Alert rules: CPU, memory, disk use and disk temperature limits with a time, severity and scope (all, one server, a tag); try before saving; findings are ordinary health conditions - Proxmox storages and Synology volumes are written down on each health pass (hourly, 400 days) and forecast like server disks: badge and history chart on the pages, alert, digest line, /metrics gauge - Snooze an alert until a time; held back like maintenance, messages held, announced once again if still there when it ends - Docs updated Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
788b3e073c
commit
24ae8db380
38 files changed
+7847
-8
No files matched your search
@@ -8,6 +8,7 @@ import Secrets from "./pages/Secrets";
|
||||
import Ipam from "./pages/Ipam";
|
||||
import NetworkDevices from "./pages/NetworkDevices";
|
||||
import ProxyHosts from "./pages/ProxyHosts";
|
||||
import AlertRules from "./pages/AlertRules";
|
||||
import HomeAssistant from "./pages/HomeAssistant";
|
||||
import MyWorkPage from "./pages/MyWork";
|
||||
import ApiTokens from "./pages/ApiTokens";
|
||||
@@ -151,6 +152,7 @@ function Shelled({ user }: { user: CurrentUser }) {
|
||||
<Route path="/ipam" element={<Ipam user={user} />} />
|
||||
<Route path="/network/devices" element={<NetworkDevices user={user} />} />
|
||||
<Route path="/proxy-hosts" element={<ProxyHosts user={user} />} />
|
||||
<Route path="/alert-rules" element={<AlertRules user={user} />} />
|
||||
<Route path="/home-assistant" element={<HomeAssistant user={user} />} />
|
||||
<Route path="/ports" element={<Ports user={user} />} />
|
||||
<Route path="/secrets" element={<Secrets user={user} />} />
|
||||
|
||||
@@ -1472,6 +1472,56 @@ export interface AlertItem {
|
||||
incidentId?: number | null;
|
||||
/** Raised and cleared over and over: its raised/cleared messages are being held back. */
|
||||
flapping?: boolean;
|
||||
/** Snoozed until then: hidden like one under maintenance, and announced again if it is still there. */
|
||||
snoozed?: { until: string; by: string };
|
||||
}
|
||||
|
||||
export type AlertRuleMetric = "cpu" | "mem" | "disk" | "disk_temp";
|
||||
|
||||
export interface AlertRule {
|
||||
id: number;
|
||||
name: string;
|
||||
enabled: boolean;
|
||||
metric: AlertRuleMetric;
|
||||
mount: string | null;
|
||||
operator: "above" | "below";
|
||||
threshold: number;
|
||||
forMinutes: number;
|
||||
severity: "warning" | "critical";
|
||||
serverId: number | null;
|
||||
serverName: string | null;
|
||||
tag: string | null;
|
||||
createdBy: string | null;
|
||||
createdAt: string;
|
||||
/** Where it is raised right now. */
|
||||
firing: { serverId: number; serverName: string; detail: string | null; value: number; since: string | null }[];
|
||||
}
|
||||
|
||||
export interface AlertRuleInput {
|
||||
name: string;
|
||||
enabled?: boolean;
|
||||
metric: AlertRuleMetric;
|
||||
mount?: string | null;
|
||||
operator?: "above" | "below";
|
||||
threshold: number;
|
||||
forMinutes?: number;
|
||||
severity?: "warning" | "critical";
|
||||
serverId?: number | null;
|
||||
tag?: string | null;
|
||||
}
|
||||
|
||||
export interface StoragePool {
|
||||
seriesId: number;
|
||||
integrationId: number;
|
||||
key: string;
|
||||
label: string;
|
||||
latest: number;
|
||||
usedBytes: number | null;
|
||||
totalBytes: number | null;
|
||||
updatedAt: string;
|
||||
trend: { perDay: number; daysToFull: number | null; fit: number; spanDays: number } | null;
|
||||
/** Enough history and a straight enough climb to believe the trend. */
|
||||
steady: boolean;
|
||||
}
|
||||
|
||||
/** What a bulk action did: how many it changed, what it left alone and why, and the body that puts it back (to send to the same address). */
|
||||
@@ -2402,6 +2452,17 @@ export const api = {
|
||||
overview: () => request<NetworkOverview>("/api/network"),
|
||||
sync: (integrationId?: number) => request<{ statuses: Record<number, UnifiSyncStatus> }>("/api/network/sync", { method: "POST", body: JSON.stringify(integrationId ? { integrationId } : {}) }),
|
||||
},
|
||||
alertRules: {
|
||||
list: () => request<{ rules: AlertRule[]; servers: { id: number; name: string }[]; tags: string[] }>("/api/alert-rules"),
|
||||
preview: (data: AlertRuleInput) => request<{ findings: { serverId: number; serverName: string; detail: string | null; value: number }[] }>("/api/alert-rules/preview", { method: "POST", body: JSON.stringify(data) }),
|
||||
create: (data: AlertRuleInput) => request<{ rule: AlertRule }>("/api/alert-rules", { method: "POST", body: JSON.stringify(data) }),
|
||||
update: (id: number, data: AlertRuleInput) => request<{ rule: AlertRule }>(`/api/alert-rules/${id}`, { method: "PUT", body: JSON.stringify(data) }),
|
||||
remove: (id: number) => request<void>(`/api/alert-rules/${id}`, { method: "DELETE" }),
|
||||
},
|
||||
storage: {
|
||||
overview: () => request<{ pools: StoragePool[]; forecastDays: number }>("/api/storage"),
|
||||
history: (seriesId: number, days: number) => request<{ days: number; points: { t: string; v: number }[] }>(`/api/storage/${seriesId}/history?days=${days}`),
|
||||
},
|
||||
npm: {
|
||||
overview: (refresh = false) => request<NpmOverview>(`/api/npm${refresh ? "?refresh=1" : ""}`),
|
||||
},
|
||||
@@ -2580,6 +2641,8 @@ export const api = {
|
||||
assignees: () => request<{ users: { id: number; name: string; role: string }[] }>("/api/alerts/assignees"),
|
||||
ack: (data: AckInput) => request<{ ack: AlertAck }>("/api/alerts/ack", { method: "PUT", body: JSON.stringify(data) }),
|
||||
unack: (alertId: string) => request<void>(`/api/alerts/ack?alertId=${encodeURIComponent(alertId)}`, { method: "DELETE" }),
|
||||
snooze: (alertId: string, until: string) => request<{ snooze: { until: string; snoozedBy: string } }>("/api/alerts/snooze", { method: "PUT", body: JSON.stringify({ alertId, until }) }),
|
||||
unsnooze: (alertId: string) => request<void>(`/api/alerts/snooze?alertId=${encodeURIComponent(alertId)}`, { method: "DELETE" }),
|
||||
noise: (days: number) => request<NoiseReport>(`/api/alerts/noise?days=${days}`),
|
||||
},
|
||||
incidents: {
|
||||
|
||||
@@ -0,0 +1,89 @@
|
||||
import { useState } from "react";
|
||||
import { api, type AlertItem } from "../api/client";
|
||||
import { formatDateTime } from "../utils/date";
|
||||
import { readableError } from "../utils/errors";
|
||||
import { toast } from "../utils/toast";
|
||||
import DetailDrawer from "./DetailDrawer";
|
||||
import { ErrorNotice } from "./Notice";
|
||||
|
||||
/** The next time it is `hour` o'clock, `daysAhead` days from today, in the browser's own time. */
|
||||
function at(daysAhead: number, hour = 9): Date {
|
||||
const d = new Date();
|
||||
d.setDate(d.getDate() + daysAhead);
|
||||
d.setHours(hour, 0, 0, 0);
|
||||
return d;
|
||||
}
|
||||
|
||||
function nextMonday(): Date {
|
||||
const today = new Date().getDay(); // 0 = Sunday
|
||||
return at(((8 - today) % 7) || 7);
|
||||
}
|
||||
|
||||
/** `datetime-local` wants "2026-10-21T09:00" in local time. */
|
||||
const localInput = (d: Date) => {
|
||||
const p = (n: number) => String(n).padStart(2, "0");
|
||||
return `${d.getFullYear()}-${p(d.getMonth() + 1)}-${p(d.getDate())}T${p(d.getHours())}:${p(d.getMinutes())}`;
|
||||
};
|
||||
|
||||
/** What a snooze says in a few words for a table row. */
|
||||
export const snoozeWords = (s: NonNullable<AlertItem["snoozed"]>) => `snoozed until ${formatDateTime(new Date(s.until))}`;
|
||||
|
||||
/**
|
||||
* "Remind me then": hide an alert until a time. It is not maintenance — nothing is promised about the thing — it is only left alone, and announced again at that time if it is
|
||||
* still there. If it goes away in the meantime, it is simply gone.
|
||||
*/
|
||||
export default function SnoozeDrawer({ alert, onClose, onSaved }: { alert: AlertItem; onClose: () => void; onSaved: () => void }) {
|
||||
const [custom, setCustom] = useState(localInput(at(1)));
|
||||
const [busy, setBusy] = useState(false);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
|
||||
const presets: { label: string; when: Date }[] = [
|
||||
{ label: "1 hour", when: new Date(Date.now() + 3600_000) },
|
||||
{ label: "4 hours", when: new Date(Date.now() + 4 * 3600_000) },
|
||||
{ label: "Tomorrow at 09:00", when: at(1) },
|
||||
{ label: "In 3 days, at 09:00", when: at(3) },
|
||||
{ label: "Next Monday at 09:00", when: nextMonday() },
|
||||
];
|
||||
|
||||
async function snooze(when: Date) {
|
||||
setBusy(true);
|
||||
setError(null);
|
||||
try {
|
||||
await api.alerts.snooze(alert.id, when.toISOString());
|
||||
toast.success(`Snoozed until ${formatDateTime(when)}.`);
|
||||
onSaved();
|
||||
onClose();
|
||||
} catch (err) {
|
||||
setError(readableError(err));
|
||||
} finally {
|
||||
setBusy(false);
|
||||
}
|
||||
}
|
||||
|
||||
const customDate = new Date(custom);
|
||||
return (
|
||||
<DetailDrawer title="Snooze" subtitle={alert.message} onClose={onClose}>
|
||||
<div className="text-secondary mb-3">
|
||||
Hide this until then. It is announced again at that time if it is still there; if it goes away before, it is just gone. Snoozing is not maintenance: nothing is promised about the thing.
|
||||
</div>
|
||||
<ErrorNotice error={error} />
|
||||
<div className="list-group mb-3">
|
||||
{presets.map((p) => (
|
||||
<button key={p.label} type="button" className="list-group-item list-group-item-action d-flex justify-content-between" disabled={busy} onClick={() => void snooze(p.when)}>
|
||||
<span>{p.label}</span>
|
||||
<span className="text-secondary">{formatDateTime(p.when)}</span>
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
<label className="form-label" htmlFor="snooze-custom">
|
||||
Or a time of your own
|
||||
</label>
|
||||
<div className="d-flex gap-2">
|
||||
<input id="snooze-custom" type="datetime-local" className="form-control" value={custom} onChange={(e) => setCustom(e.target.value)} />
|
||||
<button type="button" className="btn btn-primary" disabled={busy || Number.isNaN(customDate.getTime())} onClick={() => void snooze(customDate)}>
|
||||
Snooze
|
||||
</button>
|
||||
</div>
|
||||
</DetailDrawer>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
import { useEffect, useMemo, useState } from "react";
|
||||
import { api, type StoragePool } from "../api/client";
|
||||
import DetailDrawer from "./DetailDrawer";
|
||||
import RangeSelect from "./RangeSelect";
|
||||
import TimeChart from "./TimeChart";
|
||||
import { BlockSkeleton } from "./Skeleton";
|
||||
import { ErrorNotice } from "./Notice";
|
||||
import { useChartRange } from "../utils/chartRanges";
|
||||
import { readableError } from "../utils/errors";
|
||||
|
||||
/** The pools Proxmox and Synology report, with how fast each is filling — read once, by the page that shows them. Empty until something has been written down. */
|
||||
export function useStoragePools(): { pools: StoragePool[]; forecastDays: number } {
|
||||
const [data, setData] = useState<{ pools: StoragePool[]; forecastDays: number }>({ pools: [], forecastDays: 14 });
|
||||
useEffect(() => {
|
||||
let alive = true;
|
||||
api.storage
|
||||
.overview()
|
||||
.then((res) => alive && setData(res))
|
||||
.catch(() => {});
|
||||
return () => {
|
||||
alive = false;
|
||||
};
|
||||
}, []);
|
||||
return data;
|
||||
}
|
||||
|
||||
/** The key a Proxmox node's storage, root filesystem or a shared storage has in the history (services/storageHistory.ts). */
|
||||
export const proxmoxKeys = {
|
||||
storage: (node: string, id: string, shared: boolean) => (shared ? `shared:${id}` : `${node}:storage:${id}`),
|
||||
rootfs: (node: string) => `${node}:rootfs`,
|
||||
};
|
||||
export const synologyKey = (volumeId: string) => `volume:${volumeId}`;
|
||||
|
||||
function words(p: StoragePool, forecastDays: number): { text: string; className: string; title: string } | null {
|
||||
const t = p.trend;
|
||||
if (!t) return { text: "collecting history", className: "text-secondary", title: "A forecast needs a few days of readings; one is kept an hour." };
|
||||
// Not rising at all: a flat line is as good a forecast as there is, whatever its "fit".
|
||||
if (t.daysToFull === null && t.spanDays >= 3 && t.perDay < 0.05) return { text: "not filling", className: "text-green", title: `Changing by ${t.perDay} points a day over the last ${t.spanDays} days.` };
|
||||
if (!p.steady) return { text: "no steady trend", className: "text-secondary", title: "It is not growing in a straight enough line to forecast." };
|
||||
if (t.daysToFull === null) return { text: "not filling", className: "text-green", title: `Changing by ${t.perDay} points a day over the last ${t.spanDays} days.` };
|
||||
const soon = t.daysToFull <= forecastDays;
|
||||
return {
|
||||
text: `full in about ${t.daysToFull} ${t.daysToFull === 1 ? "day" : "days"}`,
|
||||
className: t.daysToFull <= 3 ? "text-red" : soon ? "text-yellow" : "text-secondary",
|
||||
title: `Filling by about ${t.perDay} points a day, from ${t.spanDays} days of readings.${soon ? "" : ` That is further off than the ${forecastDays} days that raise an alert.`}`,
|
||||
};
|
||||
}
|
||||
|
||||
/** One pool's forecast in a few words; a click opens its history. Renders nothing for a pool the history does not know (yet). */
|
||||
export function StorageForecastBadge({ pool, forecastDays }: { pool: StoragePool | undefined; forecastDays: number }) {
|
||||
const [open, setOpen] = useState(false);
|
||||
if (!pool) return null;
|
||||
const w = words(pool, forecastDays);
|
||||
if (!w) return null;
|
||||
return (
|
||||
<>
|
||||
<button type="button" className={`btn btn-link btn-sm p-0 ${w.className}`} title={w.title} onClick={() => setOpen(true)}>
|
||||
{w.text}
|
||||
</button>
|
||||
{open && <StorageHistoryDrawer pool={pool} forecastDays={forecastDays} onClose={() => setOpen(false)} />}
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
function StorageHistoryDrawer({ pool, forecastDays, onClose }: { pool: StoragePool; forecastDays: number; onClose: () => void }) {
|
||||
const [days, setDays] = useChartRange(30);
|
||||
const [points, setPoints] = useState<{ t: string; v: number }[] | null>(null);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const [to, setTo] = useState(() => Date.now());
|
||||
useEffect(() => {
|
||||
let alive = true;
|
||||
api.storage
|
||||
.history(pool.seriesId, days)
|
||||
.then((res) => {
|
||||
if (!alive) return;
|
||||
setPoints(res.points);
|
||||
setTo(Date.now());
|
||||
setError(null);
|
||||
})
|
||||
.catch((err) => alive && setError(readableError(err)));
|
||||
return () => {
|
||||
alive = false;
|
||||
};
|
||||
}, [pool.seriesId, days]);
|
||||
const series = useMemo(() => [{ name: pool.label, color: "#4299e1", points: points ?? [] }], [points, pool.label]);
|
||||
const w = words(pool, forecastDays);
|
||||
return (
|
||||
<DetailDrawer title={pool.label} subtitle="How full it has been" onClose={onClose}>
|
||||
<div className="mb-3">
|
||||
<RangeSelect value={days} onChange={setDays} />
|
||||
</div>
|
||||
<ErrorNotice error={error} />
|
||||
{!points && !error && <BlockSkeleton lines={5} />}
|
||||
{points && <TimeChart series={series} from={to - days * 86_400_000} to={to} label={`${pool.label}, share used`} empty="Nothing kept for this stretch yet — a reading is added every hour." />}
|
||||
<div className="mt-3">
|
||||
<strong>{Math.round(pool.latest)}% used now</strong>
|
||||
{w && <div className={w.className}>{w.text}</div>}
|
||||
<div className="text-secondary small">{w?.title}</div>
|
||||
</div>
|
||||
</DetailDrawer>
|
||||
);
|
||||
}
|
||||
@@ -7,6 +7,7 @@ import {
|
||||
IconRouter,
|
||||
IconArrowsSplit2,
|
||||
IconHome2,
|
||||
IconAdjustmentsAlt,
|
||||
IconChecklist,
|
||||
IconListDetails,
|
||||
IconKey,
|
||||
@@ -111,6 +112,7 @@ export const NAV: NavEntry[] = [
|
||||
icon: <IconHeartbeat size={20} />,
|
||||
items: [
|
||||
{ to: "/endpoints", label: "Endpoints", icon: <IconHeartbeat size={20} /> },
|
||||
{ to: "/alert-rules", label: "Alert rules", icon: <IconAdjustmentsAlt size={20} /> },
|
||||
{ to: "/heartbeats", label: "Heartbeats", icon: <IconHeartRateMonitor size={20} /> },
|
||||
{ to: "/uptime-kuma", label: "Uptime Kuma", icon: <IconActivityHeartbeat size={20} /> },
|
||||
{ to: "/home-assistant", label: "Home Assistant", icon: <IconHome2 size={20} /> },
|
||||
|
||||
@@ -0,0 +1,410 @@
|
||||
import { useCallback, useEffect, useState } from "react";
|
||||
import { Link } from "react-router-dom";
|
||||
import { api, type AlertRule, type AlertRuleInput, type AlertRuleMetric, type CurrentUser } from "../api/client";
|
||||
import DetailDrawer from "../components/DetailDrawer";
|
||||
import EmptyState from "../components/EmptyState";
|
||||
import { ErrorNotice } from "../components/Notice";
|
||||
import { BlockSkeleton } from "../components/Skeleton";
|
||||
import TimeAgo from "../components/TimeAgo";
|
||||
import { readableError } from "../utils/errors";
|
||||
import { toast } from "../utils/toast";
|
||||
import { removeWithUndo } from "../utils/undoable";
|
||||
|
||||
const METRICS: { value: AlertRuleMetric; label: string; unit: string; hint: string }[] = [
|
||||
{ value: "cpu", label: "CPU load", unit: "%", hint: "As each agent reports it (load average over cores)." },
|
||||
{ value: "mem", label: "Memory in use", unit: "%", hint: "The share of memory in use." },
|
||||
{ value: "disk", label: "Disk use", unit: "%", hint: "How full a disk is — every disk, or the one you name." },
|
||||
{ value: "disk_temp", label: "Disk temperature", unit: "°C", hint: "From the disk's own report (SMART); judged as of the last report." },
|
||||
];
|
||||
const metricOf = (m: AlertRuleMetric) => METRICS.find((x) => x.value === m)!;
|
||||
|
||||
const MINUTES: { value: number; label: string }[] = [
|
||||
{ value: 0, label: "right away" },
|
||||
{ value: 15, label: "15 minutes" },
|
||||
{ value: 30, label: "30 minutes" },
|
||||
{ value: 60, label: "1 hour" },
|
||||
{ value: 120, label: "2 hours" },
|
||||
{ value: 360, label: "6 hours" },
|
||||
{ value: 1440, label: "a day" },
|
||||
];
|
||||
|
||||
const minutesWords = (m: number) => MINUTES.find((x) => x.value === m)?.label ?? (m < 60 ? `${m} minutes` : `${Math.round((m / 60) * 10) / 10} hours`);
|
||||
|
||||
/** The rule in a sentence: what it watches, the limit, for how long and on which servers. */
|
||||
function sentence(r: Pick<AlertRuleInput, "metric" | "mount" | "operator" | "threshold" | "forMinutes"> & { serverName?: string | null; tag?: string | null }): string {
|
||||
const m = metricOf(r.metric);
|
||||
const what = r.metric === "disk" && r.mount ? `Use of ${r.mount}` : m.label;
|
||||
const forHow = r.forMinutes ? `, for ${minutesWords(r.forMinutes)}` : "";
|
||||
const where = r.serverName ? `on ${r.serverName}` : r.tag ? `on servers tagged ${r.tag}` : "on every server";
|
||||
return `${what} ${r.operator ?? "above"} ${r.threshold}${m.unit === "°C" ? " °C" : "%"}${forHow} — ${where}`;
|
||||
}
|
||||
|
||||
const EXAMPLES: AlertRuleInput[] = [
|
||||
{ name: "CPU above 90% for 15 minutes", metric: "cpu", threshold: 90, forMinutes: 15, severity: "warning" },
|
||||
{ name: "Memory above 85% for 15 minutes", metric: "mem", threshold: 85, forMinutes: 15, severity: "warning" },
|
||||
{ name: "A disk warmer than 55 °C", metric: "disk_temp", threshold: 55, severity: "warning" },
|
||||
];
|
||||
|
||||
function RuleDrawer({ initial, servers, tags, onClose, onSaved }: { initial: AlertRuleInput & { id?: number }; servers: { id: number; name: string }[]; tags: string[]; onClose: () => void; onSaved: () => void }) {
|
||||
const [name, setName] = useState(initial.name);
|
||||
const [metric, setMetric] = useState<AlertRuleMetric>(initial.metric);
|
||||
const [mount, setMount] = useState(initial.mount ?? "");
|
||||
const [operator, setOperator] = useState<"above" | "below">(initial.operator ?? "above");
|
||||
const [threshold, setThreshold] = useState(String(initial.threshold));
|
||||
const [forMinutes, setForMinutes] = useState(initial.forMinutes ?? 0);
|
||||
const [severity, setSeverity] = useState<"warning" | "critical">(initial.severity ?? "warning");
|
||||
const [scope, setScope] = useState<"all" | "server" | "tag">(initial.serverId ? "server" : initial.tag ? "tag" : "all");
|
||||
const [serverId, setServerId] = useState(initial.serverId ? String(initial.serverId) : "");
|
||||
const [tag, setTag] = useState(initial.tag ?? "");
|
||||
const [enabled] = useState(initial.enabled ?? true);
|
||||
const [busy, setBusy] = useState(false);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const [tried, setTried] = useState<{ serverName: string; detail: string | null; value: number }[] | null>(null);
|
||||
const m = metricOf(metric);
|
||||
|
||||
const body = (): AlertRuleInput | null => {
|
||||
const limit = Number(threshold);
|
||||
if (threshold.trim() === "" || !Number.isFinite(limit)) {
|
||||
setError("The limit has to be a number.");
|
||||
return null;
|
||||
}
|
||||
return {
|
||||
name: name.trim(),
|
||||
enabled,
|
||||
metric,
|
||||
mount: metric === "disk" ? mount.trim() || null : null,
|
||||
operator,
|
||||
threshold: limit,
|
||||
forMinutes: metric === "disk_temp" ? 0 : forMinutes,
|
||||
severity,
|
||||
serverId: scope === "server" && serverId ? Number(serverId) : null,
|
||||
tag: scope === "tag" && tag.trim() ? tag.trim() : null,
|
||||
};
|
||||
};
|
||||
|
||||
async function tryIt() {
|
||||
const b = body();
|
||||
if (!b) return;
|
||||
setBusy(true);
|
||||
setError(null);
|
||||
try {
|
||||
setTried((await api.alertRules.preview({ ...b, name: b.name || "Try" })).findings);
|
||||
} catch (err) {
|
||||
setError(readableError(err));
|
||||
} finally {
|
||||
setBusy(false);
|
||||
}
|
||||
}
|
||||
|
||||
async function save() {
|
||||
const b = body();
|
||||
if (!b) return;
|
||||
setBusy(true);
|
||||
setError(null);
|
||||
try {
|
||||
if (initial.id) await api.alertRules.update(initial.id, b);
|
||||
else await api.alertRules.create(b);
|
||||
toast.success(initial.id ? "Saved." : "Rule added.");
|
||||
onSaved();
|
||||
onClose();
|
||||
} catch (err) {
|
||||
setError(readableError(err));
|
||||
} finally {
|
||||
setBusy(false);
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<DetailDrawer title={initial.id ? "Change the rule" : "Add a rule"} subtitle={initial.id ? initial.name : "A limit of your own"} onClose={onClose}>
|
||||
<ErrorNotice error={error} />
|
||||
<div className="mb-3">
|
||||
<label className="form-label" htmlFor="rule-name">
|
||||
Name
|
||||
</label>
|
||||
<input id="rule-name" className="form-control" maxLength={80} value={name} onChange={(e) => setName(e.target.value)} placeholder="e.g. CPU above 90% for 15 minutes" autoFocus />
|
||||
<div className="form-text">It is in the alert’s message, in square brackets.</div>
|
||||
</div>
|
||||
<div className="mb-3">
|
||||
<label className="form-label" htmlFor="rule-metric">
|
||||
Watch
|
||||
</label>
|
||||
<select id="rule-metric" className="form-select" value={metric} onChange={(e) => { setMetric(e.target.value as AlertRuleMetric); setTried(null); }}>
|
||||
{METRICS.map((x) => (
|
||||
<option key={x.value} value={x.value}>
|
||||
{x.label}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
<div className="form-text">{m.hint}</div>
|
||||
</div>
|
||||
{metric === "disk" && (
|
||||
<div className="mb-3">
|
||||
<label className="form-label" htmlFor="rule-mount">
|
||||
Which disk <span className="text-secondary">(optional)</span>
|
||||
</label>
|
||||
<input id="rule-mount" className="form-control" value={mount} onChange={(e) => setMount(e.target.value)} placeholder="every disk — or a mount point like /data, or C:" />
|
||||
</div>
|
||||
)}
|
||||
<div className="row g-2 mb-3">
|
||||
<div className="col-6">
|
||||
<label className="form-label" htmlFor="rule-operator">
|
||||
Alert when
|
||||
</label>
|
||||
<select id="rule-operator" className="form-select" value={operator} onChange={(e) => setOperator(e.target.value as "above" | "below")}>
|
||||
<option value="above">above</option>
|
||||
<option value="below">below</option>
|
||||
</select>
|
||||
</div>
|
||||
<div className="col-6">
|
||||
<label className="form-label" htmlFor="rule-threshold">
|
||||
Limit ({m.unit})
|
||||
</label>
|
||||
<input id="rule-threshold" className="form-control" inputMode="decimal" value={threshold} onChange={(e) => { setThreshold(e.target.value); setTried(null); }} />
|
||||
</div>
|
||||
</div>
|
||||
{metric !== "disk_temp" && (
|
||||
<div className="mb-3">
|
||||
<label className="form-label" htmlFor="rule-for">
|
||||
For at least
|
||||
</label>
|
||||
<select id="rule-for" className="form-select" value={forMinutes} onChange={(e) => { setForMinutes(Number(e.target.value)); setTried(null); }}>
|
||||
{MINUTES.map((x) => (
|
||||
<option key={x.value} value={x.value}>
|
||||
{x.label}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
<div className="form-text">Every reading in that time has to be past the limit. Agents report every 15 minutes or so, and rules are looked at on the same schedule.</div>
|
||||
</div>
|
||||
)}
|
||||
<div className="row g-2 mb-3">
|
||||
<div className="col-6">
|
||||
<label className="form-label" htmlFor="rule-severity">
|
||||
How serious
|
||||
</label>
|
||||
<select id="rule-severity" className="form-select" value={severity} onChange={(e) => setSeverity(e.target.value as "warning" | "critical")}>
|
||||
<option value="warning">Warning</option>
|
||||
<option value="critical">Critical</option>
|
||||
</select>
|
||||
</div>
|
||||
<div className="col-6">
|
||||
<label className="form-label" htmlFor="rule-scope">
|
||||
For
|
||||
</label>
|
||||
<select id="rule-scope" className="form-select" value={scope} onChange={(e) => { setScope(e.target.value as typeof scope); setTried(null); }}>
|
||||
<option value="all">Every server</option>
|
||||
<option value="server">One server</option>
|
||||
<option value="tag" disabled={tags.length === 0}>
|
||||
Servers with a tag
|
||||
</option>
|
||||
</select>
|
||||
</div>
|
||||
</div>
|
||||
{scope === "server" && (
|
||||
<select className="form-select mb-3" aria-label="Which server" value={serverId} onChange={(e) => { setServerId(e.target.value); setTried(null); }}>
|
||||
<option value="">Choose a server…</option>
|
||||
{servers.map((s) => (
|
||||
<option key={s.id} value={s.id}>
|
||||
{s.name}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
)}
|
||||
{scope === "tag" && (
|
||||
<select className="form-select mb-3" aria-label="Which tag" value={tag} onChange={(e) => { setTag(e.target.value); setTried(null); }}>
|
||||
<option value="">Choose a tag…</option>
|
||||
{tags.map((t) => (
|
||||
<option key={t} value={t}>
|
||||
{t}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
)}
|
||||
<div className="text-secondary small mb-3">
|
||||
Severity and kind decide who hears of it: these are <strong>Monitoring</strong> alerts, so the routing rules under Settings → Notifications apply to them like any other.
|
||||
</div>
|
||||
{tried && (
|
||||
<div className={`alert ${tried.length > 0 ? "alert-warning" : "alert-success"} py-2`}>
|
||||
{tried.length === 0 ? (
|
||||
"Right now this would not be raised on any server."
|
||||
) : (
|
||||
<>
|
||||
Right now this would be raised on {tried.length} {tried.length === 1 ? "server" : "servers"}: {tried.slice(0, 6).map((f) => `${f.serverName}${f.detail ? ` (${f.detail})` : ""} ${Math.round(f.value)}${m.unit === "°C" ? " °C" : "%"}`).join(", ")}
|
||||
{tried.length > 6 ? ` and ${tried.length - 6} more` : ""}.
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
<div className="d-flex gap-2">
|
||||
<button type="button" className="btn btn-primary" disabled={busy || name.trim() === ""} onClick={() => void save()}>
|
||||
{busy ? "Saving…" : "Save"}
|
||||
</button>
|
||||
<button type="button" className="btn" disabled={busy} onClick={() => void tryIt()}>
|
||||
Try it now
|
||||
</button>
|
||||
</div>
|
||||
</DetailDrawer>
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Limits of your own: "CPU above 90% for 15 minutes", "memory above 85%", "a disk warmer than 55 °C". Each is judged for the servers it covers on the same schedule as the built-in
|
||||
* checks, so what it finds is an ordinary alert: announced, recovered, silenced by maintenance and routed like the rest. Everyone signed in sees them; administrators change them.
|
||||
*/
|
||||
export default function AlertRules({ user }: { user: CurrentUser }) {
|
||||
const canEdit = user.role === "admin";
|
||||
const [data, setData] = useState<{ rules: AlertRule[]; servers: { id: number; name: string }[]; tags: string[] } | null>(null);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const [editing, setEditing] = useState<(AlertRuleInput & { id?: number }) | null>(null);
|
||||
|
||||
const load = useCallback(
|
||||
() =>
|
||||
api.alertRules
|
||||
.list()
|
||||
.then((res) => {
|
||||
setData(res);
|
||||
setError(null);
|
||||
})
|
||||
.catch((err) => setError(readableError(err))),
|
||||
[],
|
||||
);
|
||||
useEffect(() => {
|
||||
void load();
|
||||
}, [load]);
|
||||
|
||||
const asInput = (r: AlertRule): AlertRuleInput & { id: number } => ({ id: r.id, name: r.name, enabled: r.enabled, metric: r.metric, mount: r.mount, operator: r.operator, threshold: r.threshold, forMinutes: r.forMinutes, severity: r.severity, serverId: r.serverId, tag: r.tag });
|
||||
|
||||
async function toggle(r: AlertRule) {
|
||||
try {
|
||||
const { id, ...rest } = asInput(r);
|
||||
await api.alertRules.update(id, { ...rest, enabled: !r.enabled });
|
||||
toast.success(r.enabled ? "Switched off." : "Switched on.");
|
||||
await load();
|
||||
} catch (err) {
|
||||
toast.error(readableError(err));
|
||||
}
|
||||
}
|
||||
|
||||
async function remove(r: AlertRule) {
|
||||
const { id, ...rest } = asInput(r);
|
||||
await removeWithUndo({
|
||||
remove: () => api.alertRules.remove(id),
|
||||
restore: () => api.alertRules.create(rest),
|
||||
message: `Removed the rule "${r.name}".`,
|
||||
reload: () => void load(),
|
||||
});
|
||||
}
|
||||
|
||||
return (
|
||||
<>
|
||||
<div className="d-flex flex-wrap align-items-center gap-2 mb-1">
|
||||
<h2 className="page-title mb-0">Alert rules</h2>
|
||||
{canEdit && (
|
||||
<div className="ms-auto">
|
||||
<button className="btn btn-primary" onClick={() => setEditing({ name: "", metric: "cpu", threshold: 90, forMinutes: 15, severity: "warning" })}>
|
||||
Add a rule
|
||||
</button>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
<div className="text-secondary mb-3">
|
||||
Limits of your own, on top of the built-in checks: “CPU above 90% for 15 minutes”, “memory above 85%”, “a disk warmer than 55 °C”. What a rule finds is an ordinary alert on the{" "}
|
||||
<Link to="/alerts">Alerts page</Link> — raised and cleared with a message, held back by maintenance, routed by the notification rules. A server that is not reporting is not judged; its own offline alert says that.
|
||||
</div>
|
||||
<ErrorNotice error={error} />
|
||||
{data === null && !error && <BlockSkeleton lines={3} />}
|
||||
{data && data.rules.length === 0 && (
|
||||
<div className="card">
|
||||
<EmptyState
|
||||
title="No rules of your own yet"
|
||||
hint={canEdit ? "Start from one of these, or add your own." : "An administrator can add them."}
|
||||
action={canEdit ? { label: "Add a rule", onClick: () => setEditing({ name: "", metric: "cpu", threshold: 90, forMinutes: 15, severity: "warning" }) } : undefined}
|
||||
/>
|
||||
{canEdit && (
|
||||
<div className="d-flex flex-wrap justify-content-center gap-2 pb-4">
|
||||
{EXAMPLES.map((e) => (
|
||||
<button key={e.name} className="btn btn-sm btn-outline-secondary" onClick={() => setEditing(e)}>
|
||||
{e.name}
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
{data && data.rules.length > 0 && (
|
||||
<div className="card">
|
||||
<div className="table-responsive">
|
||||
<table className="table table-vcenter card-table table-stack">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Rule</th>
|
||||
<th>Severity</th>
|
||||
<th>Right now</th>
|
||||
{canEdit && (
|
||||
<th className="w-1">
|
||||
<span className="visually-hidden">Actions</span>
|
||||
</th>
|
||||
)}
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{data.rules.map((r) => (
|
||||
<tr key={r.id} className={r.enabled ? "" : "text-secondary"}>
|
||||
<td className="stack-wide">
|
||||
<strong>{r.name}</strong>
|
||||
{!r.enabled && <span className="badge bg-secondary-lt text-secondary ms-2">off</span>}
|
||||
<div className="text-secondary small">{sentence(r)}</div>
|
||||
</td>
|
||||
<td className="stack-label" data-label="Severity">
|
||||
<span className={`badge ${r.severity === "critical" ? "bg-red-lt text-red" : "bg-yellow-lt text-yellow"}`}>{r.severity === "critical" ? "Critical" : "Warning"}</span>
|
||||
</td>
|
||||
<td className="stack-label" data-label="Right now">
|
||||
{!r.enabled ? (
|
||||
<span className="text-secondary">—</span>
|
||||
) : r.firing.length === 0 ? (
|
||||
<span className="text-green">Not raised</span>
|
||||
) : (
|
||||
<div>
|
||||
{r.firing.slice(0, 4).map((f, i) => (
|
||||
<div key={i} className="small">
|
||||
<Link to={`/servers/${f.serverId}?view=history`}>{f.serverName}</Link>
|
||||
{f.detail ? ` · ${f.detail}` : ""} · {Math.round(f.value)}
|
||||
{r.metric === "disk_temp" ? " °C" : "%"}
|
||||
{f.since ? (
|
||||
<>
|
||||
{" "}
|
||||
· since <TimeAgo value={f.since} />
|
||||
</>
|
||||
) : null}
|
||||
</div>
|
||||
))}
|
||||
{r.firing.length > 4 && <div className="small text-secondary">and {r.firing.length - 4} more</div>}
|
||||
</div>
|
||||
)}
|
||||
</td>
|
||||
{canEdit && (
|
||||
<td className="text-nowrap stack-wide">
|
||||
<div className="btn-list flex-nowrap">
|
||||
<button className="btn btn-sm btn-outline-secondary" onClick={() => setEditing(asInput(r))}>
|
||||
Change
|
||||
</button>
|
||||
<button className="btn btn-sm btn-outline-secondary" onClick={() => void toggle(r)}>
|
||||
{r.enabled ? "Switch off" : "Switch on"}
|
||||
</button>
|
||||
<button className="btn btn-sm btn-outline-danger" onClick={() => void remove(r)} aria-label={`Remove ${r.name}`}>
|
||||
Remove
|
||||
</button>
|
||||
</div>
|
||||
</td>
|
||||
)}
|
||||
</tr>
|
||||
))}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
{editing && data && <RuleDrawer initial={editing} servers={data.servers} tags={data.tags} onClose={() => setEditing(null)} onSaved={() => void load()} />}
|
||||
</>
|
||||
);
|
||||
}
|
||||
@@ -9,12 +9,14 @@ import { downloadCsv } from "../utils/csv";
|
||||
import { formatDateTime } from "../utils/date";
|
||||
import { formatAgo } from "../utils/duration";
|
||||
import { readableError } from "../utils/errors";
|
||||
import { toast } from "../utils/toast";
|
||||
import { ErrorNotice } from "../components/Notice";
|
||||
import TableToolbar from "../components/TableToolbar";
|
||||
import SilenceButton from "../components/SilenceButton";
|
||||
import RunPlaybookButton from "../components/RunPlaybookButton";
|
||||
import RunbookEntry from "../components/RunbookEntry";
|
||||
import AckDrawer, { ackWords } from "../components/AckDrawer";
|
||||
import SnoozeDrawer, { snoozeWords } from "../components/SnoozeDrawer";
|
||||
import { useAlertsReport } from "../hooks/useAlertsReport";
|
||||
import { summariseAlerts } from "../utils/alertSummary";
|
||||
import { EmptyRow } from "../components/EmptyState";
|
||||
@@ -94,6 +96,7 @@ function AlertsNow({ user, tabs }: { user: CurrentUser; tabs: React.ReactNode })
|
||||
// Which alerts to show by whether somebody is on them, and the one whose acknowledgement is being edited.
|
||||
const [attention, setAttention] = useState<"all" | "unacknowledged" | "acknowledged" | "mine">("all");
|
||||
const [acking, setAcking] = useState<AlertItem | null>(null);
|
||||
const [snoozing, setSnoozing] = useState<AlertItem | null>(null);
|
||||
// Alerts ticked for a bulk action, and which bulk form is open.
|
||||
const [picked, setPicked] = useState<Set<string>>(new Set());
|
||||
const [bulkForm, setBulkForm] = useState<"ack" | "silence" | null>(null);
|
||||
@@ -195,6 +198,17 @@ function AlertsNow({ user, tabs }: { user: CurrentUser; tabs: React.ReactNode })
|
||||
void load(true);
|
||||
reloadBadge();
|
||||
}
|
||||
async function wake(a: AlertItem) {
|
||||
try {
|
||||
await api.alerts.unsnooze(a.id);
|
||||
toast.success("Awake again.");
|
||||
await load(true);
|
||||
reloadBadge();
|
||||
} catch (err) {
|
||||
toast.error(readableError(err));
|
||||
}
|
||||
}
|
||||
|
||||
async function bulkAck(v: { note: string; assigneeId: number | null; hours: number | null }) {
|
||||
const ok = await doBulk({ call: api.bulk.alerts, body: { action: "acknowledge", alertIds: pickedIds, ...v }, done: (r) => `${plural(r.changed, "alert")} acknowledged.`, reload: afterBulk });
|
||||
if (ok) {
|
||||
@@ -441,6 +455,10 @@ function AlertsNow({ user, tabs }: { user: CurrentUser; tabs: React.ReactNode })
|
||||
<span className="badge bg-secondary-lt text-secondary ms-2" title={`${a.causedBy.name} is down too, which explains this — notifications for this are held back. If ${a.causedBy.name} comes back and this stays down, it alerts then.`}>
|
||||
caused by {a.causedBy.name}
|
||||
</span>
|
||||
) : a.snoozed ? (
|
||||
<span className="badge bg-azure-lt text-azure ms-2" title={`Snoozed by ${a.snoozed.by} — hidden until then, and announced again if it is still there`}>
|
||||
{snoozeWords(a.snoozed)}
|
||||
</span>
|
||||
) : (
|
||||
a.silenced && (
|
||||
<span className="badge bg-secondary-lt text-secondary ms-2" title="Under a maintenance window — notifications for this are held back">
|
||||
@@ -514,7 +532,17 @@ function AlertsNow({ user, tabs }: { user: CurrentUser; tabs: React.ReactNode })
|
||||
{a.ack?.active ? "Acknowledged" : "Acknowledge…"}
|
||||
</button>
|
||||
)}
|
||||
<SilenceButton alert={a} onDone={() => void load(true)} />
|
||||
{!a.silenced && (
|
||||
<button type="button" className="btn btn-sm btn-outline-secondary" onClick={() => setSnoozing(a)} title="Hide it until a time, and hear of it again then if it is still there">
|
||||
Snooze…
|
||||
</button>
|
||||
)}
|
||||
{a.snoozed && (
|
||||
<button type="button" className="btn btn-sm btn-outline-secondary" onClick={() => void wake(a)}>
|
||||
Wake now
|
||||
</button>
|
||||
)}
|
||||
{!a.snoozed && <SilenceButton alert={a} onDone={() => void load(true)} />}
|
||||
{a.target?.type === "server" && !a.silenced && <RunPlaybookButton serverName={a.target.name} />}
|
||||
</div>
|
||||
</td>
|
||||
@@ -567,6 +595,7 @@ function AlertsNow({ user, tabs }: { user: CurrentUser; tabs: React.ReactNode })
|
||||
{bulkForm === "ack" && <AckBulkDrawer count={pickedIds.length} onSubmit={bulkAck} onClose={() => setBulkForm(null)} />}
|
||||
{bulkForm === "silence" && <SilenceBulkDrawer noun={pickedIds.length === 1 ? "alert" : "alerts"} count={pickedIds.length} canSilenceDependents={false} onSubmit={bulkSilence} onClose={() => setBulkForm(null)} />}
|
||||
|
||||
{snoozing && <SnoozeDrawer alert={snoozing} onClose={() => setSnoozing(null)} onSaved={() => { void load(true); reloadBadge(); }} />}
|
||||
{acking && <AckDrawer alert={acking} me={user} onClose={() => setAcking(null)} onSaved={() => {
|
||||
void load(true);
|
||||
reloadBadge();
|
||||
|
||||
@@ -23,6 +23,7 @@ import { toast } from "../utils/toast";
|
||||
import { SearchBox } from "../components/TableToolbar";
|
||||
import { useTableSearch } from "../hooks/useTableSearch";
|
||||
import DetailDrawer, { ObjectDetails } from "../components/DetailDrawer";
|
||||
import { proxmoxKeys, StorageForecastBadge, useStoragePools } from "../components/StorageForecast";
|
||||
import { useRowDetail } from "../hooks/useRowDetail";
|
||||
import { formatDateTime } from "../utils/date";
|
||||
import ExportTableButton from "../components/ExportTableButton";
|
||||
@@ -124,6 +125,8 @@ export default function Proxmox({ user }: { user: CurrentUser }) {
|
||||
}, []);
|
||||
|
||||
const selected = integrations?.find((i) => i.id === selectedId) ?? null;
|
||||
const { pools, forecastDays } = useStoragePools();
|
||||
const poolOf = (key: string) => pools.find((p) => p.integrationId === selected?.id && p.key === key);
|
||||
|
||||
function loadGuests(id: number) {
|
||||
setLoading(true);
|
||||
@@ -325,6 +328,7 @@ export default function Proxmox({ user }: { user: CurrentUser }) {
|
||||
<td>
|
||||
{formatBytes(s.usedBytes)} / {formatBytes(s.totalBytes)}
|
||||
{usageBar(s.usedBytes, s.totalBytes)}
|
||||
{s.active && <div className="small"><StorageForecastBadge pool={poolOf(proxmoxKeys.storage(n.node, s.id, s.shared))} forecastDays={forecastDays} /></div>}
|
||||
</td>
|
||||
</tr>
|
||||
))}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { useEffect, useState } from "react";
|
||||
import { StorageForecastBadge, synologyKey, useStoragePools } from "../components/StorageForecast";
|
||||
import { Link } from "react-router-dom";
|
||||
import {
|
||||
api,
|
||||
@@ -79,6 +80,7 @@ export default function Synology({ user: _user }: { user: CurrentUser }) {
|
||||
}, []);
|
||||
|
||||
const selected = integrations?.find((i) => i.id === selectedId) ?? null;
|
||||
const { pools, forecastDays } = useStoragePools();
|
||||
|
||||
function loadStorage(id: number) {
|
||||
setLoading(true);
|
||||
@@ -298,6 +300,9 @@ export default function Synology({ user: _user }: { user: CurrentUser }) {
|
||||
<td className="text-secondary">{v.deviceType}</td>
|
||||
<td className="text-secondary">
|
||||
{formatBytes(v.sizeUsed)} / {formatBytes(v.sizeTotal)}
|
||||
<div className="small">
|
||||
<StorageForecastBadge pool={pools.find((p) => p.integrationId === selected?.id && p.key === synologyKey(v.id))} forecastDays={forecastDays} />
|
||||
</div>
|
||||
</td>
|
||||
<td>{healthBadge(v.status)}</td>
|
||||
</tr>
|
||||
|
||||
Reference in new issue
Block a user