diff options
author | 2025-06-07 22:51:16 -0700 | |
---|---|---|
committer | 2025-06-07 22:51:16 -0700 | |
commit | 920ac0949ce5571d3c3c8363314932a830a9e543 (patch) | |
tree | eef8336af1af7b9a880ff1265657d583165c1651 /src/utils/status/kuma.ts | |
parent | 3f2ca2e93eeec747713730b71a3befd8e0ff7bb1 (diff) | |
download | anshulg-com-920ac0949ce5571d3c3c8363314932a830a9e543.tar.gz anshulg-com-920ac0949ce5571d3c3c8363314932a830a9e543.tar.zst anshulg-com-920ac0949ce5571d3c3c8363314932a830a9e543.zip |
Extract kuma dependency from `Uptime` component
Diffstat (limited to '')
-rw-r--r-- | src/utils/status/kuma.ts | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/src/utils/status/kuma.ts b/src/utils/status/kuma.ts new file mode 100644 index 0000000..0bdf814 --- /dev/null +++ b/src/utils/status/kuma.ts @@ -0,0 +1,62 @@ +import { KUMA_API_KEY, KUMA_URL } from "astro:env/server"; +import { ServiceStatus } from "./index.ts"; + +const STATUS_MAP: Record<string, ServiceStatus> = { + "0": ServiceStatus.DOWN, + "1": ServiceStatus.UP, + "2": ServiceStatus.PENDING, + "3": ServiceStatus.MAINTENANCE, +}; + +async function kuma_status(name: string): Promise<ServiceStatus> { + if (!KUMA_URL || !KUMA_API_KEY) { + console.warn( + "Uptime Kuma URL or API Key is not set. Skipping Uptime component.", + ); + return ServiceStatus.UNKNOWN; + } + + // Fetch the metrics from Uptime Kuma + const response = await fetch(`${KUMA_URL}/metrics`, { + headers: { + Authorization: `Basic ${Buffer.from(`:${KUMA_API_KEY}`).toString("base64")}`, + }, + }); + + if (!response.ok) { + console.error( + `Failed to fetch metrics from Uptime Kuma: ${response.statusText}`, + ); + return ServiceStatus.UNKNOWN; + } + + // Parse the metrics to find the status of the monitor with the given name + const metrics = await response.text(); + const statusStr = metrics + .split("\n") + .filter((line) => line.startsWith("monitor_status")) + .map((l) => { + return { + status: l.substring(l.length - 1), + name: l + .substring("monitor_status{".length, l.length - 3) + .split(",") + .map((kv) => kv.split("=", 2)) + .filter((kv) => kv[0] === "monitor_name") + .map((kv) => kv[1]?.replace(/"/g, ""))[0], + }; + }) + .filter((m) => m.name === name) + .map((m) => m.status)[0]; + + if (statusStr == null) { + console.warn( + `Monitor with name "${name}" not found in Uptime Kuma metrics.`, + ); + return ServiceStatus.UNKNOWN; + } + + return STATUS_MAP[statusStr] ?? ServiceStatus.UNKNOWN; +} + +export default kuma_status; |