AdGuard Home, Grafana, Docker image age, foldable server page cards
New integration kinds adguard (status, statistics, filter lists and a real DNS question over UDP; alerts for DNS down/not running, protection left off, stale lists) and grafana (alert rules firing or failing, data-source health). Optional warning for running Docker images built more than N days ago (off by default). A server's overview cards can be folded and arranged, kept per user. Migrations 0075-0077; docs, metrics and tests included. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
3d9a2310bb
commit
551eb1597a
53 files changed
+27700
-33
No files matched your search
@@ -0,0 +1,39 @@
|
||||
import { Router } from "express";
|
||||
import { eq } from "drizzle-orm";
|
||||
import { db } from "../db/client.js";
|
||||
import { adguardState, integrations } from "../db/schema.js";
|
||||
import { requireAuth } from "../auth/middleware.js";
|
||||
import { adguardProblems, adguardSyncStatuses, isAdguardFresh, staleLists, storedOf } from "../services/adguardSync.js";
|
||||
import { asyncHandler } from "../utils/asyncHandler.js";
|
||||
|
||||
export const adguardRouter = Router();
|
||||
adguardRouter.use(requireAuth);
|
||||
|
||||
/** Every AdGuard Home, how it is, from what the last reads stored: whether it runs and protects, whether DNS answers, the statistics, its lists, and what is wrong. Everyone signed in can see it. */
|
||||
adguardRouter.get("/", asyncHandler(async (_req, res) => {
|
||||
const [rows, states, statuses] = await Promise.all([
|
||||
db.select({ id: integrations.id, name: integrations.name, enabled: integrations.enabled, baseUrl: integrations.baseUrl }).from(integrations).where(eq(integrations.type, "adguard")).orderBy(integrations.name),
|
||||
db.select().from(adguardState),
|
||||
adguardSyncStatuses(),
|
||||
]);
|
||||
const byId = new Map(states.map((s) => [s.integrationId, s]));
|
||||
const now = Date.now();
|
||||
res.json({
|
||||
instances: rows.map((r) => {
|
||||
const row = byId.get(r.id);
|
||||
const state = row ? storedOf(row) : null;
|
||||
const status = statuses[r.id] ?? null;
|
||||
const fresh = isAdguardFresh(status ?? undefined, now);
|
||||
return {
|
||||
id: r.id,
|
||||
name: r.name,
|
||||
enabled: r.enabled,
|
||||
url: r.baseUrl,
|
||||
status,
|
||||
fresh,
|
||||
state: state && { ...state, staleLists: staleLists(state.filters, now).map((l) => l.name) },
|
||||
problems: state && fresh ? adguardProblems(r.id, r.name, state, now) : [],
|
||||
};
|
||||
}),
|
||||
});
|
||||
}));
|
||||
@@ -0,0 +1,30 @@
|
||||
import { Router } from "express";
|
||||
import { eq } from "drizzle-orm";
|
||||
import { db } from "../db/client.js";
|
||||
import { grafanaState, integrations } from "../db/schema.js";
|
||||
import { requireAuth } from "../auth/middleware.js";
|
||||
import { grafanaProblems, grafanaSyncStatuses, isGrafanaFresh, storedOf } from "../services/grafanaSync.js";
|
||||
import { asyncHandler } from "../utils/asyncHandler.js";
|
||||
|
||||
export const grafanaRouter = Router();
|
||||
grafanaRouter.use(requireAuth);
|
||||
|
||||
/** Every Grafana, how it is, from what the last reads stored: which rules are firing or cannot be evaluated, how its data sources test, and what is wrong. Everyone signed in can see it. */
|
||||
grafanaRouter.get("/", asyncHandler(async (_req, res) => {
|
||||
const [rows, states, statuses] = await Promise.all([
|
||||
db.select({ id: integrations.id, name: integrations.name, enabled: integrations.enabled, baseUrl: integrations.baseUrl }).from(integrations).where(eq(integrations.type, "grafana")).orderBy(integrations.name),
|
||||
db.select().from(grafanaState),
|
||||
grafanaSyncStatuses(),
|
||||
]);
|
||||
const byId = new Map(states.map((s) => [s.integrationId, s]));
|
||||
const now = Date.now();
|
||||
res.json({
|
||||
instances: rows.map((r) => {
|
||||
const row = byId.get(r.id);
|
||||
const state = row ? storedOf(row) : null;
|
||||
const status = statuses[r.id] ?? null;
|
||||
const fresh = isGrafanaFresh(status ?? undefined, now);
|
||||
return { id: r.id, name: r.name, enabled: r.enabled, url: r.baseUrl, status, fresh, state, problems: state && fresh ? grafanaProblems(r.id, r.name, state, now) : [] };
|
||||
}),
|
||||
});
|
||||
}));
|
||||
@@ -40,8 +40,10 @@ import { forgetHetzner, syncHetzner } from "../services/hetznerSync.js";
|
||||
import { forgetSpeedtest, syncSpeedtest } from "../services/speedtestSync.js";
|
||||
import { forgetFragnet, syncFragnet } from "../services/fragnetSync.js";
|
||||
import { forgetArr, syncArr } from "../services/arrSync.js";
|
||||
import { forgetAdguard, syncAdguard } from "../services/adguardSync.js";
|
||||
import { forgetGrafana, syncGrafana } from "../services/grafanaSync.js";
|
||||
import { forgetNextcloud, syncNextcloud } from "../services/nextcloudSync.js";
|
||||
import { dockerVulnSyncStatuses, findingsOf, forgetDockerVulns, isDockerVulnFresh, syncDockerVulns } from "../services/dockerVulnSync.js";
|
||||
import { dockerVulnSyncStatuses, findingsOf, forgetDockerVulns, imageAgeDays, imagesOf, isDockerVulnFresh, syncDockerVulns } from "../services/dockerVulnSync.js";
|
||||
import { invalidateAlertsCache } from "../services/alerts.js";
|
||||
import { getSettings, updateSettings } from "../services/settingsStore.js";
|
||||
import { asyncHandler } from "../utils/asyncHandler.js";
|
||||
@@ -155,6 +157,8 @@ integrationsRouter.post("/", requireRole("admin"), asyncHandler(async (req, res)
|
||||
if (type === "speedtest") void syncSpeedtest(created.id).then(() => invalidateAlertsCache());
|
||||
if (type === "fragnet") void syncFragnet(created.id).then(() => invalidateAlertsCache());
|
||||
if (type === "sonarr" || type === "radarr" || type === "prowlarr") void syncArr(created.id).then(() => invalidateAlertsCache());
|
||||
if (type === "adguard") void syncAdguard(created.id).then(() => invalidateAlertsCache());
|
||||
if (type === "grafana") void syncGrafana(created.id).then(() => invalidateAlertsCache());
|
||||
if (type === "nextcloud") void syncNextcloud(created.id).then(() => invalidateAlertsCache());
|
||||
if (type === "dockhand") void syncDockerVulns(created.id).then(() => invalidateAlertsCache());
|
||||
|
||||
@@ -271,6 +275,8 @@ integrationsRouter.patch("/:id", requireRole("admin"), asyncHandler(async (req,
|
||||
if (updated.type === "speedtest" && updated.enabled && (parsed.data.config || parsed.data.enabled === true)) void syncSpeedtest(updated.id).then(() => invalidateAlertsCache());
|
||||
if (updated.type === "fragnet" && updated.enabled && (parsed.data.config || parsed.data.enabled === true)) void syncFragnet(updated.id).then(() => invalidateAlertsCache());
|
||||
if ((updated.type === "sonarr" || updated.type === "radarr" || updated.type === "prowlarr") && updated.enabled && (parsed.data.config || parsed.data.enabled === true)) void syncArr(updated.id).then(() => invalidateAlertsCache());
|
||||
if (updated.type === "adguard" && updated.enabled && (parsed.data.config || parsed.data.enabled === true)) void syncAdguard(updated.id).then(() => invalidateAlertsCache());
|
||||
if (updated.type === "grafana" && updated.enabled && (parsed.data.config || parsed.data.enabled === true)) void syncGrafana(updated.id).then(() => invalidateAlertsCache());
|
||||
if (updated.type === "nextcloud" && updated.enabled && (parsed.data.config || parsed.data.enabled === true)) void syncNextcloud(updated.id).then(() => invalidateAlertsCache());
|
||||
if (updated.type === "dockhand" && updated.enabled && (parsed.data.config || parsed.data.enabled === true)) void syncDockerVulns(updated.id).then(() => invalidateAlertsCache());
|
||||
|
||||
@@ -363,6 +369,8 @@ integrationsRouter.delete("/:id", requireRole("admin"), asyncHandler(async (req,
|
||||
if (existing.type === "speedtest") await forgetSpeedtest(id);
|
||||
if (existing.type === "fragnet") await forgetFragnet(id);
|
||||
if (existing.type === "sonarr" || existing.type === "radarr" || existing.type === "prowlarr") await forgetArr(id);
|
||||
if (existing.type === "adguard") await forgetAdguard(id);
|
||||
if (existing.type === "grafana") await forgetGrafana(id);
|
||||
if (existing.type === "nextcloud") await forgetNextcloud(id);
|
||||
if (existing.type === "dockhand") await forgetDockerVulns(id);
|
||||
await db.delete(integrations).where(eq(integrations.id, id));
|
||||
@@ -595,6 +603,15 @@ async function requireDockhandAdapter(req: Request, res: Response) {
|
||||
return { integration: loaded.integration, adapter: createDockhandAdapter(loaded.config as any) };
|
||||
}
|
||||
|
||||
/** The running images of a Docker host by how old they are (the oldest five), and how many are older than the threshold; null when the images were never read. */
|
||||
function imageAges(row: Parameters<typeof imagesOf>[0] & { imagesAt: string | null }, thresholdDays: number) {
|
||||
const images = imagesOf(row);
|
||||
if (images === null) return null;
|
||||
const now = Date.now();
|
||||
const dated = images.filter((i): i is typeof i & { created: string } => i.created !== null).map((i) => ({ image: i.image, ageDays: imageAgeDays(i.created, now), containers: i.containers })).sort((a, b) => b.ageDays - a.ageDays || a.image.localeCompare(b.image));
|
||||
return { at: row.imagesAt, running: images.length, undated: images.length - dated.length, over: thresholdDays > 0 ? dated.filter((i) => i.ageDays > thresholdDays).length : null, oldest: dated.slice(0, 5) };
|
||||
}
|
||||
|
||||
// What Dockhand's own vulnerability scans add up to for each of its Docker hosts, as last read (nothing is asked of Dockhand here, and nothing is scanned).
|
||||
integrationsRouter.get("/:id/dockhand/vulnerabilities", asyncHandler(async (req, res) => {
|
||||
const id = Number(req.params.id);
|
||||
@@ -606,11 +623,12 @@ integrationsRouter.get("/:id/dockhand/vulnerabilities", asyncHandler(async (req,
|
||||
res.json({
|
||||
alerts: settings.healthChecks.vulnerabilityAlerts,
|
||||
highThreshold: settings.healthChecks.vulnerabilityHighThreshold ?? 0,
|
||||
imageAgeDays: settings.healthChecks.imageAgeDays ?? 0,
|
||||
status,
|
||||
fresh: isDockerVulnFresh(status ?? undefined, Date.now()),
|
||||
environments: rows
|
||||
.sort((a, b) => a.environmentName.localeCompare(b.environmentName))
|
||||
.map((r) => ({ environmentId: r.environmentId, environmentName: r.environmentName, critical: r.critical, high: r.high, medium: r.medium, low: r.low, imagesScanned: r.imagesScanned, totalImages: r.totalImages, findings: findingsOf(r), updatedAt: r.updatedAt })),
|
||||
.map((r) => ({ environmentId: r.environmentId, environmentName: r.environmentName, critical: r.critical, high: r.high, medium: r.medium, low: r.low, imagesScanned: r.imagesScanned, totalImages: r.totalImages, findings: findingsOf(r), images: imageAges(r, settings.healthChecks.imageAgeDays ?? 0), updatedAt: r.updatedAt })),
|
||||
});
|
||||
}));
|
||||
|
||||
|
||||
+39
-2
@@ -40,9 +40,9 @@ export function parseDashboardLayout(stored: string | null): DashboardLayout {
|
||||
|
||||
meRouter.get("/", requireAuth, asyncHandler(async (req, res) => {
|
||||
const me = req.currentUser!;
|
||||
const { id, email, name, role, oidcSub, favorites, dashboardLayout } = me;
|
||||
const { id, email, name, role, oidcSub, favorites, dashboardLayout, serverPageLayout } = me;
|
||||
const avatarUrl = avatarUrlFor(me, (await getSettings()).profilePictures);
|
||||
res.json({ user: { id, sub: oidcSub, email, name, role, avatarUrl, favorites: parseFavorites(favorites), dashboard: parseDashboardLayout(dashboardLayout) } });
|
||||
res.json({ user: { id, sub: oidcSub, email, name, role, avatarUrl, favorites: parseFavorites(favorites), dashboard: parseDashboardLayout(dashboardLayout), serverPage: parsePageLayout(serverPageLayout) } });
|
||||
}));
|
||||
|
||||
// Paths inside the app only: a slash, then letters, digits, dashes, underscores and further slashes — and, for a saved view of a page,
|
||||
@@ -68,6 +68,31 @@ const dashboardSchema = z.object({
|
||||
order: z.record(cardName, z.number().int().min(0).max(1000)).refine((o) => Object.keys(o).length <= MAX_LAYOUT_CARDS, "Too many cards"),
|
||||
});
|
||||
|
||||
export interface PageLayout {
|
||||
/** Blocks (cards) the person folded: only their header shows. */
|
||||
folded: string[];
|
||||
/** Position of a block among the others on the page; lower comes first. */
|
||||
order: Record<string, number>;
|
||||
}
|
||||
|
||||
const blockName = z.string().regex(/^[a-z0-9-]{1,40}$/);
|
||||
const pageLayoutSchema = z.object({
|
||||
folded: z.array(blockName).max(MAX_LAYOUT_CARDS),
|
||||
order: z.record(blockName, z.number().int().min(0).max(1000)).refine((o) => Object.keys(o).length <= MAX_LAYOUT_CARDS, "Too many blocks"),
|
||||
});
|
||||
|
||||
/** What is stored for a page, read back as a layout. Anything that isn't one is the page as drawn rather than an error. */
|
||||
export function parsePageLayout(stored: string | null): PageLayout {
|
||||
const empty: PageLayout = { folded: [], order: {} };
|
||||
if (!stored) return empty;
|
||||
try {
|
||||
const parsed = pageLayoutSchema.safeParse(JSON.parse(stored));
|
||||
return parsed.success ? parsed.data : empty;
|
||||
} catch {
|
||||
return empty;
|
||||
}
|
||||
}
|
||||
|
||||
// Replaces the signed-in person's pinned pages. Only ever their own.
|
||||
meRouter.put("/favorites", requireAuth, asyncHandler(async (req, res) => {
|
||||
const parsed = favoritesSchema.safeParse(req.body);
|
||||
@@ -104,6 +129,18 @@ meRouter.post("/changes/seen", requireAuth, asyncHandler(async (req, res) => {
|
||||
res.json({ lastLookedAt: at });
|
||||
}));
|
||||
|
||||
// Replaces how the signed-in person arranged a server's overview page. Only ever their own.
|
||||
meRouter.put("/server-page", requireAuth, asyncHandler(async (req, res) => {
|
||||
const parsed = pageLayoutSchema.safeParse(req.body);
|
||||
if (!parsed.success) {
|
||||
return res.status(400).json({ error: "invalid_body", message: "Name the blocks to fold and the order of the rest.", details: parsed.error.flatten() });
|
||||
}
|
||||
const layout: PageLayout = { folded: [...new Set(parsed.data.folded)], order: parsed.data.order };
|
||||
const empty = layout.folded.length === 0 && Object.keys(layout.order).length === 0;
|
||||
await db.update(users).set({ serverPageLayout: empty ? null : JSON.stringify(layout) }).where(eq(users.id, req.currentUser!.id));
|
||||
res.json({ serverPage: layout });
|
||||
}));
|
||||
|
||||
// Replaces how the signed-in person arranged the dashboard. Only ever their own.
|
||||
meRouter.put("/dashboard", requireAuth, asyncHandler(async (req, res) => {
|
||||
const parsed = dashboardSchema.safeParse(req.body);
|
||||
|
||||
@@ -144,7 +144,7 @@ const updateSchema = z.object({
|
||||
.partial()
|
||||
.optional(),
|
||||
healthChecks: z
|
||||
.object({ serverOfflineMinutes: z.number().int().min(15).max(10080), diskUsagePercent: z.number().int().min(50).max(99), domainWarnDays: z.number().int().min(1).max(365), diskForecastDays: z.number().int().min(0).max(90), backupStaleHours: z.number().int().min(0).max(2160), restoreTestDays: z.number().int().min(0).max(730), newPortAlerts: z.boolean(), flapRaises: z.number().int().min(0).max(20), deviceOfflineMinutes: z.number().int().min(5).max(1440), osSupportWarnDays: z.number().int().min(0).max(365), baselineAlerts: z.boolean(), containerAlerts: z.boolean(), vulnerabilityAlerts: z.boolean().default(true), vulnerabilityHighThreshold: z.number().int().min(0).max(10000).default(0), containerIgnore: z.array(z.string().trim().min(1).max(100)).max(200) })
|
||||
.object({ serverOfflineMinutes: z.number().int().min(15).max(10080), diskUsagePercent: z.number().int().min(50).max(99), domainWarnDays: z.number().int().min(1).max(365), diskForecastDays: z.number().int().min(0).max(90), backupStaleHours: z.number().int().min(0).max(2160), restoreTestDays: z.number().int().min(0).max(730), newPortAlerts: z.boolean(), flapRaises: z.number().int().min(0).max(20), deviceOfflineMinutes: z.number().int().min(5).max(1440), osSupportWarnDays: z.number().int().min(0).max(365), baselineAlerts: z.boolean(), containerAlerts: z.boolean(), vulnerabilityAlerts: z.boolean().default(true), vulnerabilityHighThreshold: z.number().int().min(0).max(10000).default(0), imageAgeDays: z.number().int().min(0).max(3650).default(0), containerIgnore: z.array(z.string().trim().min(1).max(100)).max(200) })
|
||||
.partial()
|
||||
.optional(),
|
||||
quietHours: z
|
||||
|
||||
Reference in new issue
Block a user