Implemented tcp-ping as a service to check non http(s) connections

This commit is contained in:
DonFU1 2024-08-30 19:21:42 +00:00
parent 44f8e9d4da
commit b3956426aa
6 changed files with 133 additions and 1 deletions

View File

@ -14,7 +14,7 @@ RUN apk add --no-cache libc6-compat \
RUN --mount=type=cache,id=pnpm-store,target=/root/.local/share/pnpm/store pnpm fetch | grep -v "cross-device link not permitted\|Falling back to copying packages from store"
RUN --mount=type=cache,id=pnpm-store,target=/root/.local/share/pnpm/store pnpm install -r --offline
RUN --mount=type=cache,id=pnpm-store,target=/root/.local/share/pnpm/store pnpm install -r
# Rebuild the source code only when needed
FROM docker.io/node:18-alpine AS builder

View File

@ -37,6 +37,7 @@
"rrule": "^2.8.1",
"swr": "^1.3.0",
"systeminformation": "^5.23.2",
"tcp-ping": "^0.1.1",
"tough-cookie": "^4.1.3",
"urbackup-server-api": "^0.52.0",
"winston": "^3.11.0",

View File

@ -97,6 +97,15 @@
"up": "Up",
"not_available": "Not Available"
},
"tcpHandshake": {
"tcp_status": "TCP-Handshake status",
"error": "Error",
"response": "Response",
"down": "Down",
"up": "Up",
"not_available": "Not Available"
},
"emby": {
"playing": "Playing",
"transcoding": "Transcoding",

View File

@ -5,6 +5,7 @@ import Status from "./status";
import Widget from "./widget";
import Ping from "./ping";
import SiteMonitor from "./site-monitor";
import TcpHandshake from "./tcp-handshake";
import KubernetesStatus from "./kubernetes-status";
import Docker from "widgets/docker/component";
@ -101,6 +102,14 @@ export default function Item({ service, group, useEqualHeights }) {
<span className="sr-only">Site monitor status</span>
</div>
)}
{service.tcpHandshake && (
<div className="flex-shrink-0 flex items-center justify-center service-tag service-tpc-handshake">
<TcpHandshake group={group} service={service.name} style={statusStyle} />
<span className="sr-only">TCP-Handshake status</span>
</div>
)}
{service.container && (
<button

View File

@ -0,0 +1,63 @@
import { useTranslation } from "react-i18next";
import useSWR from "swr";
export default function TcpHandshake({ group, service, style }) {
const { t } = useTranslation();
const { data, error } = useSWR(`/api/tcpHandshake?${new URLSearchParams({ group, service }).toString()}`, {
refreshInterval: 30000,
});
let colorClass = "text-black/20 dark:text-white/40 opacity-20";
let backgroundClass = "bg-theme-500/10 dark:bg-theme-900/50 px-1.5 py-0.5";
let statusTitle = t("tcpHandshake.tcp_status");
let statusText = "";
if (error || (data && data.error)) {
colorClass = "text-rose-500";
statusText = t("tcpHandshake.error");
statusTitle += ` ${t("tcpHandshake.error")}`;
} else if (!data) {
statusText = t("tcpHandshake.response");
statusTitle += ` ${t("tcpHandshake.not_available")}`;
} else if (data.status > 403) {
colorClass = "text-rose-500/80";
statusTitle += ` ${data.status}`;
if (style === "basic") {
statusText = t("tcpHandshake.down");
} else {
statusText = data.status;
}
} else if (data) {
const responseTime = t("common.ms", {
value: data.latency,
style: "unit",
unit: "millisecond",
maximumFractionDigits: 0,
});
statusTitle += ` ${data.status} (${responseTime})`;
colorClass = "text-emerald-500/80";
if (style === "basic") {
statusText = t("tcpHandshake.up");
} else {
statusText = responseTime;
colorClass += " lowercase";
}
}
if (style === "dot") {
backgroundClass = "p-4";
colorClass = colorClass.replace(/text-/g, "bg-").replace(/\/\d\d/g, "");
}
return (
<div
className={`w-auto text-center rounded-b-[3px] overflow-hidden site-monitor-status ${backgroundClass}`}
title={statusTitle}
>
{style !== "dot" && <div className={`font-bold uppercase text-[8px] ${colorClass}`}>{statusText}</div>}
{style === "dot" && <div className={`rounded-full h-3 w-3 ${colorClass}`} />}
</div>
);
}

View File

@ -0,0 +1,50 @@
import { performance } from "perf_hooks";
import { getServiceItem } from "utils/config/service-helpers";
import createLogger from "utils/logger";
var tcpp = require('tcp-ping');
const logger = createLogger("tcpHandshake");
export default async function handler(req, res) {
const { group, service } = req.query;
const serviceItem = await getServiceItem(group, service);
if (!serviceItem) {
logger.debug(`No service item found for group ${group} named ${service}`);
return res.status(400).send({
error: "Unable to find service, see log for details. Peace out!",
});
}
const { tcpHandshake: monitorURL } = serviceItem;
if (!monitorURL) {
logger.debug("No tcp monitor URL specified");
return res.status(400).send({
error: "No tcp monitor URL given",
});
}
console.log(typeof(monitorURL))
const params = monitorURL.split(':', 2)
try {
let startTime = performance.now();
await tcpp.probe(params[0], params[1], function(err, status) {
let endTime = performance.now();
return res.status(200).json({
status,
latency: endTime - startTime,
});
});
} catch (e) {
logger.debug("Error attempting http monitor: %s", e);
return res.status(400).send({
error: "Error attempting http monitor, see logs.",
e,
});
}
}