diff --git a/docs/installation/k8s.md b/docs/installation/k8s.md index 6805139b..24be2c34 100644 --- a/docs/installation/k8s.md +++ b/docs/installation/k8s.md @@ -175,6 +175,7 @@ data: expanded: true cpu: true memory: true + network: default - search: provider: duckduckgo target: _blank @@ -370,7 +371,7 @@ prevent unnecessary re-renders on page loads and window / tab focusing. The procedure for enabling sticky sessions depends on your Ingress controller. Below is an example using Traefik as the Ingress controller. -``` +```yaml apiVersion: traefik.io/v1alpha1 kind: IngressRoute metadata: diff --git a/docs/widgets/info/resources.md b/docs/widgets/info/resources.md index 19323dc3..4a15ca04 100644 --- a/docs/widgets/info/resources.md +++ b/docs/widgets/info/resources.md @@ -20,11 +20,12 @@ _Note: unfortunately, the package used for getting CPU temp ([systeminformation] cpu: true memory: true disk: /disk/mount/path + network: default # options: 'default' or active network interface name cputemp: true tempmin: 0 # optional, minimum cpu temp tempmax: 100 # optional, maximum cpu temp uptime: true - units: imperial # only used by cpu temp + units: imperial # only used by cpu temp widget, options: 'imperial' or 'metric' refresh: 3000 # optional, in ms diskUnits: bytes # optional, bytes (default) or bbytes. Only applies to disk ``` diff --git a/src/components/widgets/resources/network.jsx b/src/components/widgets/resources/network.jsx new file mode 100644 index 00000000..7b1cb4ba --- /dev/null +++ b/src/components/widgets/resources/network.jsx @@ -0,0 +1,62 @@ +import useSWR from "swr"; +import { FaNetworkWired, FaAngleUp, FaAngleDown } from "react-icons/fa"; +import { useTranslation } from "next-i18next"; + +import Resource from "../widget/resource"; +import Error from "../widget/error"; + +export default function Network({ options, refresh = 1500 }) { + const { t } = useTranslation(); + + const { data, error } = useSWR(`/api/widgets/resources?type=network${(options.network || ( options.network !== 'default' && options.network === `false`)) ? `&interfaceName=${options.network}` : '' }`, { + refreshInterval: refresh, + }); + + if (error || data?.error) { + return ; + } + + if (!data) { + return ( + } + expandedValue="-" + expandedLabel={} + percentage="0" + expanded="true" + /> + ); + } + + return ( + <> + /* Active Usage */ + } + expandedValue={t('common.bits', { value: data?.network?.rx_sec })} + expandedLabel={} + percentage="0" + expanded="true" + children={
{t('pyload.speed')}
} + iconChildren={{data.interface}} + /> + + /* Total Usage */ + } + expandedValue={t('common.bbytes', { value: data?.network?.rx_bytes })} + expandedLabel={} + percentage="0" + expanded="true" + children={
{t('pyload.total')}
} + iconChildren={{data.interface}} + /> + + ); +} diff --git a/src/components/widgets/resources/resources.jsx b/src/components/widgets/resources/resources.jsx index 634e0ff5..db26caa7 100644 --- a/src/components/widgets/resources/resources.jsx +++ b/src/components/widgets/resources/resources.jsx @@ -6,6 +6,7 @@ import Cpu from "./cpu"; import Memory from "./memory"; import CpuTemp from "./cputemp"; import Uptime from "./uptime"; +import Network from "./network"; export default function Resources({ options }) { const { expanded, units, diskUnits, tempmin, tempmax } = options; @@ -23,6 +24,7 @@ export default function Resources({ options }) { )) : options.disk && } + {options.network && } {options.cputemp && ( )} diff --git a/src/components/widgets/widget/resource.jsx b/src/components/widgets/widget/resource.jsx index 8c975928..5c848bf8 100644 --- a/src/components/widgets/widget/resource.jsx +++ b/src/components/widgets/widget/resource.jsx @@ -3,6 +3,7 @@ import UsageBar from "../resources/usage-bar"; export default function Resource({ children, icon, + iconChildren, value, label, expandedValue = "", @@ -17,7 +18,10 @@ export default function Resource({
- +
+ + {iconChildren} +
{value}
diff --git a/src/pages/api/widgets/resources.js b/src/pages/api/widgets/resources.js index 66449bff..879f5579 100644 --- a/src/pages/api/widgets/resources.js +++ b/src/pages/api/widgets/resources.js @@ -8,6 +8,7 @@ const si = require("systeminformation"); export default async function handler(req, res) { const { type, target } = req.query; + let { interfaceName } = req.query; if (type === "cpu") { const load = await si.currentLoad(); @@ -57,6 +58,31 @@ export default async function handler(req, res) { }); } + if (type === "network"){ + let networkData = await si.networkStats(); + if(interfaceName !== "default" && interfaceName !== undefined && interfaceName !== "false"){ + networkData = networkData.filter((network) => network.iface === interfaceName)['0']; + if(!networkData){ + return res.status(404).json({ + error: "Interface not found", + }); + } + }else{ + const interfaceDefault = await si.networkInterfaceDefault(); + interfaceName = interfaceDefault + networkData = networkData.filter((network) => network.iface === interfaceDefault)['0']; + if(!networkData){ + return res.status(404).json({ + error: "Interface not found! Please specify a valid interface name.", + }); + } + } + return res.status(200).json({ + network: networkData, + interface: interfaceName + }); + } + return res.status(400).json({ error: "invalid type", });