Check a value, not just "is it up": JSON value checks on web endpoints
A web check can read a value out of a JSON answer (power.now, state, data.result[0].value[1]) and judge it: below, above, equal to, contains, is there. The value read is shown on the Endpoints page, charted under History, exported as homelab_endpoint_value and in the read API. A check can also send one header (a token), stored encrypted, never returned, and not forwarded to another host on a redirect. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
e114567d64
commit
3cb3e4988f
21 files changed
+6804
-47
No files matched your search
@@ -943,6 +943,8 @@ export interface EndpointStats {
|
||||
avgMs24h: number | null;
|
||||
recent: { at: string; ok: boolean; ms: number | null }[];
|
||||
}
|
||||
export type EndpointValueOp = "<" | "<=" | ">" | ">=" | "=" | "!=" | "contains" | "exists";
|
||||
|
||||
export interface EndpointCheck {
|
||||
id: number;
|
||||
name: string;
|
||||
@@ -951,6 +953,16 @@ export interface EndpointCheck {
|
||||
port: number | null;
|
||||
expectStatus: string | null;
|
||||
keyword: string | null;
|
||||
/** A web check can judge a value in a JSON answer: where it is, how it must compare, and what to. */
|
||||
valuePath: string | null;
|
||||
valueOp: EndpointValueOp | null;
|
||||
valueExpected: string | null;
|
||||
valueUnit: string | null;
|
||||
/** A header sent with the request (its value is never sent back, only that there is one). */
|
||||
headerName: string | null;
|
||||
hasHeader: boolean;
|
||||
/** What was read the last time, as text. */
|
||||
lastValue: string | null;
|
||||
insecure: boolean;
|
||||
intervalMinutes: number;
|
||||
timeoutSeconds: number;
|
||||
@@ -972,6 +984,8 @@ export interface EndpointCheck {
|
||||
export interface EndpointHistory {
|
||||
check: { id: number; name: string };
|
||||
days: number;
|
||||
/** The numbers a value check read, as far back as the results are kept; null for a check that reads none. */
|
||||
values: { unit: string | null; days: number; points: { t: string; v: number; lo: number; hi: number }[]; latest: number | null; lowest: number | null; highest: number | null } | null;
|
||||
/** Which figures it is drawn from: each answer (a day or two), the hourly figures, or the daily ones. */
|
||||
source: "results" | "hourly" | "daily";
|
||||
points: { t: string; availability: number | null; avgMs: number | null; maxMs: number | null; checks: number }[];
|
||||
@@ -998,6 +1012,13 @@ export interface EndpointInput {
|
||||
port?: number | null;
|
||||
expectStatus?: string | null;
|
||||
keyword?: string | null;
|
||||
valuePath?: string | null;
|
||||
valueOp?: EndpointValueOp | null;
|
||||
valueExpected?: string | null;
|
||||
valueUnit?: string | null;
|
||||
headerName?: string | null;
|
||||
/** Left out, an edit keeps the value already saved. */
|
||||
headerValue?: string | null;
|
||||
insecure?: boolean;
|
||||
intervalMinutes?: number;
|
||||
timeoutSeconds?: number;
|
||||
|
||||
@@ -39,6 +39,10 @@ export default function EndpointHistory({ checkId, name, onClose }: { checkId: n
|
||||
|
||||
const from = to - days * 86_400_000;
|
||||
const rangeLabel = CHART_RANGES.find((r) => r.days === days)?.label ?? `${days} days`;
|
||||
const readings = useMemo(
|
||||
() => [{ name: "Value", color: "#ae3ec9", points: (history?.values?.points ?? []).map((p) => ({ t: p.t, v: p.v, lo: p.lo, hi: p.hi })) }],
|
||||
[history],
|
||||
);
|
||||
const availability = useMemo(() => [{ name: "Availability", color: "#2fb344", points: (history?.points ?? []).filter((p) => p.availability !== null).map((p) => ({ t: p.t, v: p.availability as number })) }], [history]);
|
||||
// The line is the average answer time; the band reaches up to the slowest.
|
||||
const answer = useMemo(() => [{ name: "Answer time", color: "#4299e1", points: (history?.points ?? []).filter((p) => p.avgMs !== null).map((p) => ({ t: p.t, v: p.avgMs as number, lo: p.avgMs as number, hi: (p.maxMs ?? p.avgMs) as number })) }], [history]);
|
||||
@@ -67,6 +71,20 @@ export default function EndpointHistory({ checkId, name, onClose }: { checkId: n
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{history.values && (
|
||||
<>
|
||||
<h4 className="mb-1">Value read{history.values.unit ? ` (${history.values.unit})` : ""}</h4>
|
||||
<div className="text-secondary small mb-1">
|
||||
Now {history.values.latest}, lowest {history.values.lowest}, highest {history.values.highest}
|
||||
{history.values.unit ? ` ${history.values.unit}` : ""}.
|
||||
</div>
|
||||
<div className="mb-3">
|
||||
<TimeChart series={readings} from={Math.max(from, to - history.values.days * 86_400_000)} to={to} unit="value" format={(v) => `${+v.toPrecision(4)}${history.values?.unit ? ` ${history.values.unit}` : ""}`} label={`${name}: the value read over the last ${Math.min(days, 14)} days`} empty="Nothing read in this stretch yet." />
|
||||
{days > 14 && <div className="text-secondary small mt-1">Values are kept 14 days, so this reaches back no further.</div>}
|
||||
</div>
|
||||
</>
|
||||
)}
|
||||
|
||||
<h4 className="mb-1">Availability</h4>
|
||||
<div className="mb-3">
|
||||
<TimeChart series={availability} from={from} to={to} label={`${name}: availability over the last ${rangeLabel}`} bars={history.points.length <= 120} barColor={(v) => (v >= 99.9 ? "#2fb344" : v >= 90 ? "#f59f00" : "#f03e3e")} empty="No checks in this stretch yet." />
|
||||
|
||||
+116
-5
@@ -1,6 +1,6 @@
|
||||
import { useEffect, useState } from "react";
|
||||
import { Link } from "react-router-dom";
|
||||
import { api, type CurrentUser, type EndpointCheck, type EndpointInput, type EndpointKind } from "../api/client";
|
||||
import { api, type CurrentUser, type EndpointCheck, type EndpointInput, type EndpointKind, type EndpointValueOp } from "../api/client";
|
||||
import Pagination from "../components/Pagination";
|
||||
import SortableTh from "../components/SortableTh";
|
||||
import TableToolbar from "../components/TableToolbar";
|
||||
@@ -20,7 +20,7 @@ import EndpointHistory from "../components/EndpointHistory";
|
||||
|
||||
const KIND_LABELS: Record<EndpointKind, string> = { http: "Web address", tcp: "Port", tls: "Certificate" };
|
||||
const KIND_HINTS: Record<EndpointKind, string> = {
|
||||
http: "Asks the address and wants a good answer (and, if you like, a word in the page).",
|
||||
http: "Asks the address and wants a good answer (and, if you like, a word in the page, or a value in a JSON answer within its limits).",
|
||||
tcp: "Opens a connection to the port and sees whether anything answers.",
|
||||
tls: "Reads the certificate the service shows and warns before it runs out.",
|
||||
};
|
||||
@@ -42,6 +42,19 @@ function stateOf(c: EndpointCheck): State {
|
||||
return c.consecutiveFailures >= c.failuresBeforeAlert ? "down" : "failing";
|
||||
}
|
||||
|
||||
const OP_LABELS: Record<EndpointValueOp, string> = { "<": "is below", "<=": "is at most", ">": "is above", ">=": "is at least", "=": "is equal to", "!=": "is anything but", contains: "contains", exists: "is there" };
|
||||
const LIMIT_WORDS: Record<EndpointValueOp, string> = { "<": "below", "<=": "at most", ">": "above", ">=": "at least", "=": "equal to", "!=": "anything but", contains: "containing", exists: "there" };
|
||||
const NUMERIC = (op: EndpointValueOp) => op === "<" || op === "<=" || op === ">" || op === ">=";
|
||||
|
||||
/** What a value check wants, in words: "power.now should be below 5000 W". */
|
||||
function limitWords(c: EndpointCheck): string {
|
||||
if (!c.valuePath || !c.valueOp) return "";
|
||||
if (c.valueOp === "exists") return `${c.valuePath} should be there`;
|
||||
if (c.valueOp === "contains") return `${c.valuePath} should contain “${c.valueExpected}”`;
|
||||
const numeric = NUMERIC(c.valueOp) || (c.valueExpected !== null && c.valueExpected.trim() !== "" && Number.isFinite(Number(c.valueExpected)));
|
||||
return `${c.valuePath} should be ${LIMIT_WORDS[c.valueOp]} ${numeric ? c.valueExpected : `“${c.valueExpected}”`}${numeric && c.valueUnit ? ` ${c.valueUnit}` : ""}`;
|
||||
}
|
||||
|
||||
const daysLeft = (iso: string | null) => (iso ? Math.floor((Date.parse(iso) - Date.now()) / 86_400_000) : null);
|
||||
|
||||
interface Row extends EndpointCheck {
|
||||
@@ -71,6 +84,14 @@ interface Draft {
|
||||
port: string;
|
||||
expectStatus: string;
|
||||
keyword: string;
|
||||
valuePath: string;
|
||||
valueOp: EndpointValueOp;
|
||||
valueExpected: string;
|
||||
valueUnit: string;
|
||||
headerName: string;
|
||||
headerValue: string;
|
||||
/** A header is already saved with this check, so its value may be left blank to keep it. */
|
||||
hadHeader: boolean;
|
||||
insecure: boolean;
|
||||
intervalMinutes: string;
|
||||
timeoutSeconds: string;
|
||||
@@ -80,7 +101,7 @@ interface Draft {
|
||||
enabled: boolean;
|
||||
}
|
||||
|
||||
const EMPTY: Draft = { id: null, name: "", kind: "http", target: "", port: "", expectStatus: "", keyword: "", insecure: false, intervalMinutes: "5", timeoutSeconds: "10", failuresBeforeAlert: "2", warnDays: "14", serverId: "", enabled: true };
|
||||
const EMPTY: Draft = { id: null, name: "", kind: "http", target: "", port: "", expectStatus: "", keyword: "", valuePath: "", valueOp: "<", valueExpected: "", valueUnit: "", headerName: "", headerValue: "", hadHeader: false, insecure: false, intervalMinutes: "5", timeoutSeconds: "10", failuresBeforeAlert: "2", warnDays: "14", serverId: "", enabled: true };
|
||||
|
||||
const toDraft = (c: EndpointCheck): Draft => ({
|
||||
id: c.id,
|
||||
@@ -90,6 +111,13 @@ const toDraft = (c: EndpointCheck): Draft => ({
|
||||
port: c.port === null ? "" : String(c.port),
|
||||
expectStatus: c.expectStatus ?? "",
|
||||
keyword: c.keyword ?? "",
|
||||
valuePath: c.valuePath ?? "",
|
||||
valueOp: c.valueOp ?? "<",
|
||||
valueExpected: c.valueExpected ?? "",
|
||||
valueUnit: c.valueUnit ?? "",
|
||||
headerName: c.headerName ?? "",
|
||||
headerValue: "",
|
||||
hadHeader: c.hasHeader,
|
||||
insecure: c.insecure,
|
||||
intervalMinutes: String(c.intervalMinutes),
|
||||
timeoutSeconds: String(c.timeoutSeconds),
|
||||
@@ -106,6 +134,12 @@ const toInput = (d: Draft): EndpointInput => ({
|
||||
port: d.kind === "http" || d.port.trim() === "" ? null : Number(d.port),
|
||||
expectStatus: d.kind === "http" && d.expectStatus.trim() ? d.expectStatus.trim() : null,
|
||||
keyword: d.kind === "http" && d.keyword ? d.keyword : null,
|
||||
valuePath: d.kind === "http" && d.valuePath.trim() ? d.valuePath.trim() : null,
|
||||
valueOp: d.kind === "http" && d.valuePath.trim() ? d.valueOp : null,
|
||||
valueExpected: d.kind === "http" && d.valuePath.trim() && d.valueOp !== "exists" ? d.valueExpected : null,
|
||||
valueUnit: d.kind === "http" && d.valuePath.trim() && d.valueUnit.trim() ? d.valueUnit.trim() : null,
|
||||
headerName: d.kind === "http" && d.headerName.trim() ? d.headerName.trim() : null,
|
||||
headerValue: d.kind === "http" && d.headerName.trim() && d.headerValue ? d.headerValue : undefined,
|
||||
insecure: d.insecure,
|
||||
intervalMinutes: Number(d.intervalMinutes),
|
||||
timeoutSeconds: Number(d.timeoutSeconds),
|
||||
@@ -199,8 +233,8 @@ export default function Endpoints({ user }: { user: CurrentUser }) {
|
||||
function exportCsv() {
|
||||
downloadCsv(
|
||||
"endpoint-checks.csv",
|
||||
["Name", "Kind", "Target", "Port", "Status", "Last answer", "Answer time (ms)", "Uptime 24 h (%)", "Uptime 7 d (%)", "Certificate expires", "Last checked", "Server"],
|
||||
(sorted ?? []).map((r) => [r.name, KIND_LABELS[r.kind], r.target, r.port ?? "", STATE_BADGE[r.state].label, r.lastMessage ?? "", r.lastResponseMs ?? "", r.stats?.uptime24h ?? "", r.stats?.uptime7d ?? "", r.certExpiresAt?.slice(0, 10) ?? "", r.lastCheckedAt ?? "", r.server?.name ?? ""]),
|
||||
["Name", "Kind", "Target", "Port", "Status", "Last answer", "Value read", "Answer time (ms)", "Uptime 24 h (%)", "Uptime 7 d (%)", "Certificate expires", "Last checked", "Server"],
|
||||
(sorted ?? []).map((r) => [r.name, KIND_LABELS[r.kind], r.target, r.port ?? "", STATE_BADGE[r.state].label, r.lastMessage ?? "", r.lastValue ?? "", r.lastResponseMs ?? "", r.stats?.uptime24h ?? "", r.stats?.uptime7d ?? "", r.certExpiresAt?.slice(0, 10) ?? "", r.lastCheckedAt ?? "", r.server?.name ?? ""]),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -319,6 +353,76 @@ export default function Endpoints({ user }: { user: CurrentUser }) {
|
||||
</div>
|
||||
</>
|
||||
)}
|
||||
{draft.kind === "http" && (
|
||||
<div className="col-12">
|
||||
<div className="border rounded p-3">
|
||||
<div className="fw-bold mb-1">Judge a value in the answer (optional)</div>
|
||||
<div className="text-secondary small mb-2">
|
||||
For an answer in JSON: say where the value is and what it should be. For example <code>power.now</code> is below 5000, <code>state</code> is equal to playing, or, for a Prometheus query, <code>data.result[0].value[1]</code> is above 0.
|
||||
Use <code>[0]</code> for the first place in a list.
|
||||
</div>
|
||||
<div className="row g-2">
|
||||
<div className="col-md-4">
|
||||
<label className="form-label" htmlFor="ep-vpath">
|
||||
Where in the answer
|
||||
</label>
|
||||
<input id="ep-vpath" className="form-control" maxLength={200} placeholder="power.now" value={draft.valuePath} onChange={(e) => set("valuePath", e.target.value)} />
|
||||
</div>
|
||||
<div className="col-md-3">
|
||||
<label className="form-label" htmlFor="ep-vop">
|
||||
It should be
|
||||
</label>
|
||||
<select id="ep-vop" className="form-select" value={draft.valueOp} disabled={draft.valuePath.trim() === ""} onChange={(e) => set("valueOp", e.target.value as EndpointValueOp)}>
|
||||
{(Object.keys(OP_LABELS) as EndpointValueOp[]).map((op) => (
|
||||
<option key={op} value={op}>
|
||||
{OP_LABELS[op]}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
</div>
|
||||
{draft.valueOp !== "exists" && (
|
||||
<div className="col-md-3">
|
||||
<label className="form-label" htmlFor="ep-vexp">
|
||||
This
|
||||
</label>
|
||||
<input id="ep-vexp" className="form-control" maxLength={200} disabled={draft.valuePath.trim() === ""} placeholder={NUMERIC(draft.valueOp) ? "5000" : "playing"} value={draft.valueExpected} onChange={(e) => set("valueExpected", e.target.value)} />
|
||||
</div>
|
||||
)}
|
||||
<div className="col-md-2">
|
||||
<label className="form-label" htmlFor="ep-vunit">
|
||||
Unit
|
||||
</label>
|
||||
<input id="ep-vunit" className="form-control" maxLength={12} disabled={draft.valuePath.trim() === ""} placeholder="W" value={draft.valueUnit} onChange={(e) => set("valueUnit", e.target.value)} />
|
||||
</div>
|
||||
</div>
|
||||
<div className="row g-2 mt-1">
|
||||
<div className="col-md-4">
|
||||
<label className="form-label" htmlFor="ep-hname">
|
||||
Send a header (optional)
|
||||
</label>
|
||||
<input id="ep-hname" className="form-control" maxLength={100} placeholder="Authorization" value={draft.headerName} onChange={(e) => set("headerName", e.target.value)} />
|
||||
</div>
|
||||
<div className="col-md-8">
|
||||
<label className="form-label" htmlFor="ep-hvalue">
|
||||
Its value
|
||||
</label>
|
||||
<input
|
||||
id="ep-hvalue"
|
||||
type="password"
|
||||
autoComplete="off"
|
||||
className="form-control"
|
||||
maxLength={2000}
|
||||
disabled={draft.headerName.trim() === ""}
|
||||
placeholder={draft.hadHeader ? "Saved — leave blank to keep it" : "Bearer …"}
|
||||
value={draft.headerValue}
|
||||
onChange={(e) => set("headerValue", e.target.value)}
|
||||
/>
|
||||
<div className="form-hint text-secondary small mt-1">For an API that wants a token. It is stored encrypted, never shown again, and only sent to this address — not to another host a redirect leads to.</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
<div className="col-md-2">
|
||||
<label className="form-label" htmlFor="ep-interval">
|
||||
Every
|
||||
@@ -409,6 +513,7 @@ export default function Endpoints({ user }: { user: CurrentUser }) {
|
||||
{c.target}
|
||||
{c.port !== null ? `:${c.port}` : ""}
|
||||
</div>
|
||||
{c.valuePath && <div className="small text-secondary">{limitWords(c)}</div>}
|
||||
{c.server && (
|
||||
<div className="small">
|
||||
about <Link to={`/servers/${c.server.id}`}>{c.server.name}</Link>
|
||||
@@ -425,6 +530,12 @@ export default function Endpoints({ user }: { user: CurrentUser }) {
|
||||
)}
|
||||
</td>
|
||||
<td className="stack-label" data-label="Answer">
|
||||
{c.lastValue !== null && (
|
||||
<div className="fw-bold" title={limitWords(c)}>
|
||||
{c.lastValue}
|
||||
{c.valueUnit && Number.isFinite(Number(c.lastValue)) ? ` ${c.valueUnit}` : ""}
|
||||
</div>
|
||||
)}
|
||||
{c.lastResponseMs === null ? <span className="text-secondary">—</span> : `${c.lastResponseMs} ms`}
|
||||
{c.stats?.avgMs24h != null && <div className="text-secondary small">avg {c.stats.avgMs24h} ms</div>}
|
||||
</td>
|
||||
|
||||
Reference in new issue
Block a user