Files
Homelab-manager/web/src/pages/TechnitiumDns.tsx
T
bobbanandClaude Sonnet 5.5 9937ab2202 Fold long page introductions to two lines with Show more
One component, PageIntro, for the text under a page's title: shown as it is
when it fits in two lines, otherwise folded with a Show more, remembered per
page in this browser, opened by tabbing to a link in it, unfolded in print.
Used on 41 pages.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-10 19:42:34 +01:00

336 lines
17 KiB
TypeScript

import { useEffect, useState } from "react";
import { Link } from "react-router-dom";
import ReadNowButton from "../components/ReadNowButton";
import { api, type CurrentUser, type TechnitiumInstance } from "../api/client";
import EmptyState from "../components/EmptyState";
import TimeAgo from "../components/TimeAgo";
import { BlockSkeleton } from "../components/Skeleton";
import { ErrorNotice } from "../components/Notice";
import { formatAgo } from "../utils/duration";
import { readableError } from "../utils/errors";
import PageIntro from "../components/PageIntro";
function Badge({ a }: { a: TechnitiumInstance }) {
if (!a.enabled) return <span className="badge bg-secondary-lt text-secondary">Off</span>;
if (!a.state) return <span className="badge bg-secondary-lt text-secondary">Not read yet</span>;
if (!a.fresh) return <span className="badge bg-yellow-lt text-yellow">Can&rsquo;t be read</span>;
if (a.problems.some((x) => x.severity === "critical")) return <span className="badge bg-red-lt text-red">Needs a look</span>;
if (a.problems.length > 0) return <span className="badge bg-yellow-lt text-yellow">Needs a look</span>;
return <span className="badge bg-green-lt text-green">In order</span>;
}
function Tile({ label, value, sub, tone }: { label: string; value: string; sub?: string | null; tone?: "bad" | "warn" }) {
return (
<div className="col-6 col-lg-3">
<div className="card card-sm h-100">
<div className="card-body">
<div className="text-secondary small">{label}</div>
<div className={`h2 mb-0 ${tone === "bad" ? "text-red" : tone === "warn" ? "text-yellow" : ""}`}>{value}</div>
{sub && <div className="text-secondary small">{sub}</div>}
</div>
</div>
</div>
);
}
function Counted({ title, items, empty }: { title: string; items: { name: string; count: number }[]; empty: string }) {
const top = items[0]?.count ?? 0;
return (
<div className="col-lg-4">
<h4>{title}</h4>
{items.length === 0 && <div className="text-secondary small">{empty}</div>}
{items.slice(0, 7).map((i) => (
<div key={i.name} className="mb-1">
<div className="d-flex small">
<span className="text-break">{i.name}</span>
<span className="ms-auto text-secondary ps-2">{i.count.toLocaleString()}</span>
</div>
<div className="progress progress-sm">
<div className="progress-bar" style={{ width: `${top > 0 ? Math.max(2, (i.count / top) * 100) : 0}%` }} />
</div>
</div>
))}
</div>
);
}
/** "12 d 3 h", "5 h 20 min", "40 min": how long since a moment. */
function upFor(startedAt: string, now: number = Date.now()): string {
const minutes = Math.max(0, Math.floor((now - Date.parse(startedAt)) / 60_000));
if (minutes < 60) return `${minutes} min`;
const hours = Math.floor(minutes / 60);
if (hours < 24) return minutes % 60 ? `${hours} h ${minutes % 60} min` : `${hours} h`;
const days = Math.floor(hours / 24);
return hours % 24 ? `${days} d ${hours % 24} h` : `${days} d`;
}
const NODE_STATE: Record<string, { className: string; label: string }> = {
self: { className: "bg-blue-lt", label: "this node" },
connected: { className: "bg-green-lt", label: "connected" },
unreachable: { className: "bg-red-lt", label: "unreachable" },
};
/** The cluster the server belongs to: its nodes and whether this one can reach them, and (for a secondary) when it last took the configuration from the primary. Nothing when it is in none. */
function ClusterSection({ cluster }: { cluster: NonNullable<TechnitiumInstance["state"]>["cluster"] }) {
if (!cluster || (!cluster.info && !cluster.note)) return null;
const info = cluster.info;
return (
<div className="mb-3">
<h4>Cluster{info?.domain ? <span className="text-secondary fw-normal"> — {info.domain}</span> : null}</h4>
{cluster.note && <div className={`small mb-2 ${info ? "text-yellow" : "text-secondary"}`}>{cluster.note}</div>}
{info && (
<>
<div className="table-responsive">
<table className="table table-sm table-vcenter mb-2">
<thead>
<tr>
<th>Node</th>
<th>Role</th>
<th>State</th>
<th>Version</th>
<th>Last seen</th>
</tr>
</thead>
<tbody>
{info.nodes.map((n) => {
const st = NODE_STATE[n.state.toLowerCase()] ?? { className: "bg-secondary-lt", label: n.state };
return (
<tr key={n.id ?? n.name}>
<td className="text-nowrap">
{n.url ? (
<a href={n.url} target="_blank" rel="noreferrer">
{n.name}
</a>
) : (
n.name
)}
</td>
<td>{n.type}</td>
<td>
<span className={`badge ${st.className}`}>{st.label}</span>
</td>
<td className="text-secondary">{n.version ?? "—"}</td>
<td className="text-secondary">{n.state.toLowerCase() === "self" ? "—" : n.lastSeen ? <TimeAgo value={n.lastSeen} /> : "—"}</td>
</tr>
);
})}
</tbody>
</table>
</div>
{info.configLastSynced && (
<div className="small text-secondary">
This node last took the configuration from the primary <TimeAgo value={info.configLastSynced} />
{info.configRefreshSeconds ? ` (it asks every ${Math.round(info.configRefreshSeconds / 60)} min)` : ""}.
</div>
)}
</>
)}
</div>
);
}
/** How the last day's queries were answered, as one bar: from the cache, resolved by the server, from its own zones, blocked, dropped. */
const HOW: { key: "cached" | "recursive" | "authoritative" | "blocked" | "dropped"; label: string; className: string }[] = [
{ key: "cached", label: "from the cache", className: "bg-blue" },
{ key: "recursive", label: "resolved", className: "bg-azure" },
{ key: "authoritative", label: "from its own zones", className: "bg-green" },
{ key: "blocked", label: "blocked", className: "bg-orange" },
{ key: "dropped", label: "dropped", className: "bg-red" },
];
function Card({ a, canRead, onRead }: { a: TechnitiumInstance; canRead: boolean; onRead: () => void }) {
const s = a.state;
const st = s?.stats ?? null;
const cacheMax = s?.settings.cacheMaximumEntries ?? null;
const cacheFill = st && st.cachedEntries !== null && cacheMax !== null && cacheMax > 0 ? Math.min(100, Math.round((st.cachedEntries / cacheMax) * 100)) : null;
return (
<div className="card mb-3">
<div className="card-header">
<h3 className="card-title">
{a.name} <span className="text-secondary fw-normal">{s?.version ? `v${s.version}` : ""}</span> <Badge a={a} />
</h3>
<div className="card-actions d-flex align-items-center gap-2 text-secondary small">
{a.status ? <>read <TimeAgo value={a.status.at} /></> : "not read yet"}
{canRead && a.enabled && <ReadNowButton integrationId={a.id} name={a.name} onDone={onRead} />}
</div>
</div>
<div className="card-body">
{a.status && !a.status.ok && <div className="text-yellow mb-2">The last attempt to read it failed: {a.status.error ?? "no answer"}.</div>}
{a.problems.length > 0 && (
<ul className="mb-3">
{a.problems.map((x) => (
<li key={x.key} className={x.severity === "critical" ? "text-red" : ""}>
{x.message}
</li>
))}
</ul>
)}
{s && (
<>
<div className="row g-3 mb-3">
<Tile
label="DNS answers"
value={s.probe === null ? "—" : s.probe.ok ? `${s.probe.ms ?? "?"} ms` : "No"}
sub={s.probe ? (s.probe.ok ? `a question about ${s.probe.name}, asked ${s.probe.host}:${s.dnsPort}` : `${s.probe.error ?? "no answer"} (${s.probe.host}:${s.dnsPort})`) : "no test yet"}
tone={s.probe && !s.probe.ok ? "bad" : undefined}
/>
<Tile
label="Blocking"
value={s.blockingEnabled ? "On" : "Off"}
sub={s.blockingEnabled ? (s.settings.blockLists.count === 0 ? "no block lists are set up" : null) : s.blockingOffSince ? `since ${formatAgo(s.blockingOffSince)}` : null}
tone={!s.blockingEnabled && s.settings.blockLists.count > 0 ? "warn" : undefined}
/>
<Tile label="Queries, last day" value={st ? st.queries.toLocaleString() : "—"} sub={st ? `from ${st.clients.toLocaleString()} client${st.clients === 1 ? "" : "s"}` : "could not be read"} />
<Tile label="Blocked" value={st ? st.blocked.toLocaleString() : "—"} sub={st ? (st.blockedPercent === null ? null : `${st.blockedPercent}% of the queries`) : "could not be read"} />
<Tile label="From the cache" value={st ? (st.cachedPercent === null ? "—" : `${st.cachedPercent}%`) : "—"} sub={st ? `${st.cached.toLocaleString()} queries` : "could not be read"} />
<Tile
label="Failed"
value={st ? (st.failurePercent === null ? "—" : `${st.failurePercent}%`) : "—"}
sub={st ? `${st.serverFailure.toLocaleString()} server failures, ${st.refused.toLocaleString()} refused` : "could not be read"}
tone={st && st.failurePercent !== null && st.queries >= 200 && st.failurePercent >= 20 ? "warn" : undefined}
/>
<Tile label="Running for" value={s.startedAt ? upFor(s.startedAt) : "—"} sub={s.startedAt ? `since ${new Date(s.startedAt).toLocaleString()}` : "it did not say"} />
<Tile
label="DNS cache"
value={st && st.cachedEntries !== null ? st.cachedEntries.toLocaleString() : "—"}
sub={cacheFill !== null ? `${cacheFill}% of ${cacheMax!.toLocaleString()} entries` : cacheMax !== null ? `up to ${cacheMax.toLocaleString()} entries` : null}
tone={cacheFill !== null && cacheFill >= 95 ? "warn" : undefined}
/>
</div>
{st && st.queries > 0 && (
<div className="mb-3">
<h4>How the queries were answered</h4>
<div className="progress mb-1">
{HOW.map((h) => (
<div key={h.key} className={`progress-bar ${h.className}`} style={{ width: `${(st[h.key] / st.queries) * 100}%` }} title={`${h.label}: ${st[h.key].toLocaleString()}`} />
))}
</div>
<div className="d-flex flex-wrap gap-3 small text-secondary">
{HOW.map((h) => (
<span key={h.key}>
<span className={`badge ${h.className} me-1`}>&nbsp;</span>
{h.label} {st[h.key].toLocaleString()}
</span>
))}
</div>
</div>
)}
{st && (st.topBlocked.length > 0 || st.topClients.length > 0 || st.topDomains.length > 0) && (
<div className="row g-3 mb-3">
<Counted title="Blocked most" items={st.topBlocked} empty="Nothing blocked." />
<Counted title="Asked about most" items={st.topDomains} empty="No queries." />
<Counted title="Asking most" items={st.topClients} empty="No clients." />
</div>
)}
<ClusterSection cluster={s.cluster} />
<h4>Zones</h4>
{s.zones === null ? (
<div className="text-secondary small mb-3">They could not be read.</div>
) : (
<div className="small mb-3">
<div className="text-secondary">
{s.zones.total} zone{s.zones.total === 1 ? "" : "s"}
{s.zones.disabled > 0 ? `, ${s.zones.disabled} switched off` : ""}
{s.zones.issues.length === 0 ? " — none with a problem." : "."}
</div>
{s.zones.issues.length > 0 && (
<ul className="mb-0">
{s.zones.issues.map((z) => (
<li key={z.name} className={z.problem === "expired" ? "text-red" : "text-yellow"}>
{z.name} — {z.problem === "expired" ? "expired: it no longer answers" : "cannot sync with its primary"}
</li>
))}
</ul>
)}
</div>
)}
<h4>Block lists and the rest</h4>
<ul className="small mb-0">
<li>
{s.settings.blockLists.count === 0 ? (
"No block lists are set up."
) : (
<>
{s.settings.blockLists.count} block list{s.settings.blockLists.count === 1 ? "" : "s"}
{st && st.blockListZones !== null ? `, ${st.blockListZones.toLocaleString()} names on them` : ""}
{s.settings.blockLists.nextUpdate ? (
s.blockListsOverdueMs !== null ? (
<span className="text-yellow">
{" "}
— update was due <TimeAgo value={s.settings.blockLists.nextUpdate} />
</span>
) : (
<span className="text-secondary"> — next update {new Date(s.settings.blockLists.nextUpdate).toLocaleString()}</span>
)
) : (
""
)}
</>
)}
</li>
{st && (st.allowedZones !== null || st.blockedZones !== null) && (
<li className="text-secondary">
Allowed by hand: {(st.allowedZones ?? 0).toLocaleString()} · blocked by hand: {(st.blockedZones ?? 0).toLocaleString()}
</li>
)}
<li className="text-secondary">
Recursion: {s.settings.recursion ?? "—"} · DNSSEC validation: {s.settings.dnssecValidation === null ? "—" : s.settings.dnssecValidation ? "on" : "off"}
</li>
{s.dhcp && (
<li className="text-secondary">
DHCP: {s.dhcp.scopes} scope{s.dhcp.scopes === 1 ? "" : "s"} ({s.dhcp.enabledScopes} on), {s.dhcp.leases.toLocaleString()} lease{s.dhcp.leases === 1 ? "" : "s"}
</li>
)}
</ul>
</>
)}
</div>
</div>
);
}
/** Technitium DNS Server: whether DNS really answers, whether it blocks, what it has done in the last day, how its zones and block lists are, and how it is doing. */
export default function TechnitiumDns({ user }: { user: CurrentUser }) {
const canRead = user.role === "admin" || user.role === "operator";
const [tick, setTick] = useState(0);
const [data, setData] = useState<TechnitiumInstance[] | null>(null);
const [error, setError] = useState<string | null>(null);
useEffect(() => {
const load = () =>
api.technitium
.list()
.then((r) => {
setData(r.instances);
setError(null);
})
.catch((err) => setError(readableError(err)));
void load();
const t = setInterval(load, 60_000);
return () => clearInterval(t);
}, [tick]);
return (
<>
<h2 className="page-title">Technitium DNS</h2>
<PageIntro>
Read every two minutes (see <Link to="/integrations">Integrations</Link>), with a real DNS question put to it each time — its web console can be up while DNS is not. Nothing is changed. <strong>DNS that stops answering</strong>, <strong>blocking left off for an hour</strong>,{" "}
<strong>block lists that stopped updating</strong> and <strong>an expired or unsynced zone</strong>, <strong>a cluster node that cannot be reached</strong> and <strong>a secondary that has stopped taking the primary's configuration</strong> are alerts — see <Link to="/alerts">Alerts</Link>. Technitium does not report the CPU, memory or disk of the machine it runs on: put a <Link to="/servers">server agent</Link> there for that.
</PageIntro>
<ErrorNotice error={error} />
{!data && !error && <BlockSkeleton lines={4} />}
{data && data.length === 0 && (
<div className="card">
<div className="card-body">
<EmptyState title="No Technitium DNS Server connected" hint="Add one under Integrations with the address of its web console and an API token (Administration → Sessions → Create Token). Zones and records are connected separately, under DNS." />
</div>
</div>
)}
{data?.map((a) => <Card key={a.id} a={a} canRead={canRead} onRead={() => setTick((n) => n + 1)} />)}
</>
);
}