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
+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