OPNsense widget : reporting a correct value for memory usage
This commit is contained in:
parent
aa35b919bb
commit
b44a5ac54e
@ -5,6 +5,51 @@ import Block from "components/services/widget/block";
|
||||
import useWidgetAPI from "utils/proxy/use-widget-api";
|
||||
|
||||
export default function Component({ service }) {
|
||||
function toKb(value, unit) {
|
||||
console.log("toKB : value: ", value, " Unit:", unit);
|
||||
switch (unit) {
|
||||
case "K":
|
||||
return parseInt(value, 10);
|
||||
case "M":
|
||||
return parseInt(value, 10) * 1024;
|
||||
case "G":
|
||||
return parseInt(value, 10) * 1024 * 1024;
|
||||
default:
|
||||
return parseInt(value, 10);
|
||||
}
|
||||
}
|
||||
|
||||
function sumMemory(meminfos) {
|
||||
let result;
|
||||
let sumused=0;
|
||||
let sumfree=0;
|
||||
|
||||
const idused = ["Active", "Wired", "Laundry", "Buf"];
|
||||
const idfree = ["Inact", "Free"];
|
||||
const size = "([0-9]+)([KMG])";
|
||||
|
||||
for (let id = 0; id < idused.length;id+=1 ) {
|
||||
const re = new RegExp(`${size } ${ idused[id] }`);
|
||||
result = re.exec(meminfos);
|
||||
|
||||
if (result) {
|
||||
sumused += toKb(result[1], result[2]);
|
||||
}
|
||||
}
|
||||
|
||||
for (let id = 0; id < idfree.length; id+=1 ) {
|
||||
const re = new RegExp(`${size } ${ idfree[id] }`);
|
||||
result = re.exec(meminfos);
|
||||
|
||||
if (result) {
|
||||
sumfree += toKb(result[1], result[2]);
|
||||
}
|
||||
}
|
||||
|
||||
return 100*(sumused / (sumused + sumfree));
|
||||
|
||||
}
|
||||
|
||||
const { t } = useTranslation();
|
||||
|
||||
const { widget } = service;
|
||||
@ -29,11 +74,9 @@ export default function Component({ service }) {
|
||||
}
|
||||
|
||||
|
||||
const cpuidle = activityData.headers[2].substring(60,64);
|
||||
const cpu = 100 - parseFloat(cpuidle);
|
||||
const memoryInfos = activityData.headers[3].split(" ");
|
||||
const totalMemory = parseFloat(memoryInfos[1]) + parseFloat(memoryInfos[3]) + parseFloat(memoryInfos[5]) + parseFloat(memoryInfos[7]) + parseFloat(memoryInfos[9])/1024 + parseFloat(memoryInfos[11]);
|
||||
const memory = ( 1 - parseFloat(memoryInfos[11]) / totalMemory) * 100;
|
||||
const cpuIdle = activityData.headers[2].match(/ ([0-9.]+)% idle/)[1];
|
||||
const cpu = 100 - parseFloat(cpuIdle);
|
||||
const memory = sumMemory(activityData.headers[3]);
|
||||
|
||||
const wanUpload = interfaceData.interfaces.wan['bytes transmitted'];
|
||||
const wanDownload = interfaceData.interfaces.wan['bytes received'];
|
||||
@ -41,7 +84,7 @@ export default function Component({ service }) {
|
||||
return (
|
||||
<Container service={service}>
|
||||
<Block label="opnsense.cpu" value={t("common.percent", { value: cpu.toFixed(2) })} />
|
||||
<Block label="opnsense.memory" value={t("common.percent", { value: memory })} />
|
||||
<Block label="opnsense.memory" value={t("common.percent", { value: memory})} />
|
||||
<Block label="opnsense.wanUpload" value={t("common.bytes", { value: wanUpload })} />
|
||||
<Block label="opnsense.wanDownload" value={t("common.bytes", { value: wanDownload })} />
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user