From 91e529f87a042a4e152afa449e6f47c735841ea5 Mon Sep 17 00:00:00 2001 From: shamoon <4887959+shamoon@users.noreply.github.com> Date: Fri, 29 Nov 2024 23:32:59 -0800 Subject: [PATCH] Enhancement: glances containers metric widget (#4361) --- docs/widgets/services/glances.md | 2 + src/widgets/glances/component.jsx | 5 ++ src/widgets/glances/metrics/containers.jsx | 73 ++++++++++++++++++++++ src/widgets/glances/widget.js | 2 +- 4 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 src/widgets/glances/metrics/containers.jsx diff --git a/docs/widgets/services/glances.md b/docs/widgets/services/glances.md index 562cf57a2..d28462eb1 100644 --- a/docs/widgets/services/glances.md +++ b/docs/widgets/services/glances.md @@ -51,6 +51,8 @@ The metric field in the configuration determines the type of system monitoring d `process`: Top 5 processes based on CPU usage. Gives an overview of which processes are consuming the most resources. +`containers`: Docker or Kubernetes containers list. Shows up to 5 containers running on the system and their resource usage. + `network:`: Network data usage for the specified interface. Replace `` with the name of your network interface, e.g., `network:enp0s25`, as specified in glances. `sensor:`: Temperature of the specified sensor, typically used to monitor CPU temperature. Replace `` with the name of your sensor, e.g., `sensor:Package id 0` as specified in glances. diff --git a/src/widgets/glances/component.jsx b/src/widgets/glances/component.jsx index df916b4ba..bff31ac16 100644 --- a/src/widgets/glances/component.jsx +++ b/src/widgets/glances/component.jsx @@ -7,6 +7,7 @@ import Disk from "./metrics/disk"; import GPU from "./metrics/gpu"; import Info from "./metrics/info"; import Fs from "./metrics/fs"; +import Containers from "./metrics/containers"; export default function Component({ service }) { const { widget } = service; @@ -23,6 +24,10 @@ export default function Component({ service }) { return ; } + if (widget.metric === "containers") { + return ; + } + if (widget.metric === "cpu") { return ; } diff --git a/src/widgets/glances/metrics/containers.jsx b/src/widgets/glances/metrics/containers.jsx new file mode 100644 index 000000000..dac52be3c --- /dev/null +++ b/src/widgets/glances/metrics/containers.jsx @@ -0,0 +1,73 @@ +import { useTranslation } from "next-i18next"; + +import Container from "../components/container"; +import Block from "../components/block"; + +import useWidgetAPI from "utils/proxy/use-widget-api"; +import ResolvedIcon from "components/resolvedicon"; + +const statusMap = { + running: , + paused: , +}; + +const defaultInterval = 1000; + +export default function Component({ service }) { + const { t } = useTranslation(); + const { widget } = service; + const { chart, refreshInterval = defaultInterval, version = 3 } = widget; + + const idKey = version === 3 ? "Id" : "id"; + const statusKey = version === 3 ? "Status" : "status"; + + const { data, error } = useWidgetAPI(service.widget, `${version}/containers`, { + refreshInterval: Math.max(defaultInterval, refreshInterval), + }); + + if (error) { + return ; + } + + if (!data) { + return ( + + - + + ); + } + + data.splice(chart ? 5 : 1); + + return ( + + +
+
+
{t("resources.cpu")}
+
{t("resources.mem")}
+
+ + + +
+ {data.map((item) => ( +
+
+
{statusMap[item[statusKey]]}
+
{item.name}
+
{item.cpu_percent.toFixed(1)}%
+
+ {t("common.bytes", { + value: item.memory.usage, + maximumFractionDigits: 0, + })} +
+
+
+ ))} +
+
+ + ); +} diff --git a/src/widgets/glances/widget.js b/src/widgets/glances/widget.js index 00d3ac5ad..38da4fd79 100644 --- a/src/widgets/glances/widget.js +++ b/src/widgets/glances/widget.js @@ -3,7 +3,7 @@ import credentialedProxyHandler from "utils/proxy/handlers/credentialed"; const widget = { api: "{url}/api/{endpoint}", proxyHandler: credentialedProxyHandler, - allowedEndpoints: /\d\/quicklook|diskio|cpu|fs|gpu|system|mem|network|processlist|sensors/, + allowedEndpoints: /\d\/quicklook|diskio|cpu|fs|gpu|system|mem|network|processlist|sensors|containers/, }; export default widget;