grafana, tautulli, nextcloud, nodered: full nox modules (ADR 0044/0046)
grafana: status/datasources/dashboards/alerts tools, emits alert.firing. tautulli: activity/history/stats tools, emits watch.recorded. nextcloud: users/shares/apps/occ tools (occ via docker exec, shares over OCS), emits user.created/share.created. nodered: flows/nodes/deploy tools, emits flows.deployed inline from the deploy tool. All typecheck; manifests parse.
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
// grafana's events. The tool runtime imports this once the broker is bound. It watches unified
|
||||
// alerting and announces when an alert instance starts firing.
|
||||
//
|
||||
// Emits (novox/hq ADR 0046/0047):
|
||||
// module.grafana.alert.firing — an alert instance entered the Alerting state
|
||||
//
|
||||
// A Grafana with no alerting configured simply never has a firing alert, so this observes nothing
|
||||
// and emits nothing — no error, no noise.
|
||||
|
||||
import { emit } from "@novox/mesh-sdk/events";
|
||||
import { GrafanaClient, type GrafanaAlert } from "./client.js";
|
||||
|
||||
// Constructed lazily so an unconfigured node (no auth) loads this entrypoint without crashing the
|
||||
// events host — it simply watches nothing.
|
||||
let grafana: GrafanaClient | undefined;
|
||||
try {
|
||||
grafana = GrafanaClient.fromEnv();
|
||||
} catch (err) {
|
||||
console.log(`[grafana] not configured, not watching alerts: ${err}`);
|
||||
}
|
||||
|
||||
// Firing alerts, by diffing the set currently in the Alerting state. Primed silently on the first
|
||||
// look so alerts already firing when this started are not announced as freshly firing.
|
||||
const firing = new Set<string>();
|
||||
let primed = false;
|
||||
|
||||
const alertKey = (a: GrafanaAlert): string =>
|
||||
`${a.name}:${Object.entries(a.labels).sort().map(([k, v]) => `${k}=${v}`).join(",")}`;
|
||||
|
||||
async function pollAlerts(client: GrafanaClient): Promise<void> {
|
||||
const now = new Set<string>();
|
||||
const byKey = new Map<string, GrafanaAlert>();
|
||||
for (const a of await client.listAlerts()) {
|
||||
if (a.state.toLowerCase() !== "alerting") continue;
|
||||
const key = alertKey(a);
|
||||
now.add(key);
|
||||
byKey.set(key, a);
|
||||
}
|
||||
if (primed) {
|
||||
for (const key of now) {
|
||||
if (!firing.has(key)) {
|
||||
const a = byKey.get(key)!;
|
||||
await emit("module.grafana.alert.firing", { name: a.name, labels: a.labels, activeAt: a.activeAt });
|
||||
}
|
||||
}
|
||||
}
|
||||
firing.clear();
|
||||
for (const key of now) firing.add(key);
|
||||
primed = true;
|
||||
}
|
||||
|
||||
if (grafana) {
|
||||
const client = grafana;
|
||||
const run = (): void => void pollAlerts(client).catch((err) => console.error(`[grafana] ${err}`));
|
||||
setInterval(run, 30_000);
|
||||
run();
|
||||
console.log("[grafana] watching for firing alerts");
|
||||
}
|
||||
Reference in New Issue
Block a user