gpt4 book ai didi

javascript - 如何将 vmstat 和 free -m 转换为对象?

转载 作者:搜寻专家 更新时间:2023-11-01 00:03:44 25 4
gpt4 key购买 nike

我想将 exec 执行的 vmstat -s 命令的输出转换为一个对象,以及 free -m 命令的输出

我从 exec 免费得到这个 -m:

              total        used        free      shared  buff/cache   available
Mem: 7855 2054 1082 257 4718 5243
Swap: 2047 0 2047

对于 vmstat -s 我得到了这个输出

      8044160 K total memory
2081200 K used memory
3491012 K active memory
2508932 K inactive memory
1131132 K free memory
579204 K buffer memory
4252624 K swap cache
2097148 K total swap
0 K used swap
2097148 K free swap
328672 non-nice user cpu ticks
3273 nice user cpu ticks
85460 system cpu ticks
4215038 idle cpu ticks
13261 IO-wait cpu ticks
0 IRQ cpu ticks
450 softirq cpu ticks
0 stolen cpu ticks
2095462 pages paged in
3761868 pages paged out
0 pages swapped in
0 pages swapped out
10301935 interrupts
36499667 CPU context switches
1551158702 boot time
17506 forks

那么我怎样才能将这个输出转换成一个像

这样的对象
objFreeM = {
mem: {total: 7855, used: 2054, ...etc},
swap: {total: 2047, used: 0, free: 2047}
}

objVmstat = {
totalmemory: 8044160,
usedmemory: 2081200,
....etc
}

vmstat 的结果是这样的

"8044160 K 总内存\n 2517232 K 已用内存\n 4064068 K 事件内存\n 2733260 K 非事件内存\n 304940 K 空闲内存\n 650300 K 缓冲内存\n 4571688 K 交换缓存\n 2097148 K 总交换\n 0 K 已用交换\n 2097148 K 自由交换\n 454507 非 nice 用户 cpu ticks\n 3803 nice 用户 cpu ticks\n 117177 系统 cpu ticks\n 7955496 空闲 cpu ticks\n 15791 IO 等待 cpu ticks\n 0 IRQ cpu ticks\n 528 softirq cpu ticks\n 0 被盗 cpu ticks\n 2244129 页调入\n 4442696 页调出\n 0 页换入\n 0 页换出\n 14232293 中断\n 50365325 CPU 上下文切换\n 1551158702 启动时间\n 22313 forks\n"

最佳答案

让我们初始化我们的字符串:

var myString = "      8044160 K total memory\n      2517232 K used memory\n      4064068 K active memory\n      2733260 K inactive memory\n       304940 K free memory\n       650300 K buffer memory\n      4571688 K swap cache\n      2097148 K total swap\n            0 K used swap\n      2097148 K free swap\n       454507 non-nice user cpu ticks\n         3803 nice user cpu ticks\n       117177 system cpu ticks\n      7955496 idle cpu ticks\n        15791 IO-wait cpu ticks\n            0 IRQ cpu ticks\n          528 softirq cpu ticks\n            0 stolen cpu ticks\n      2244129 pages paged in\n      4442696 pages paged out\n            0 pages swapped in\n            0 pages swapped out\n     14232293 interrupts\n     50365325 CPU context switches\n   1551158702 boot time\n        22313 forks\n";

现在让我们拆分它:

var myArray = myString.split("\n");

然后 trim 项目:

for (var index in myArray) myArray[index] = myArray[index].trim();

为驼峰键定义一个辅助函数:

function camelize(str) {
return str.replace(/(?:^\w|[A-Z]|\b\w)/g, function(letter, index) {
return index == 0 ? letter.toLowerCase() : letter.toUpperCase();
}).replace(/\s+/g, '');
}

最后解析结果:

var results = {};
for (var item of myArray) {
var temp = item.split(" ");
var value = temp[0];
var key = "";
var start = 1;
if (temp[1] === "K") {
value += " K";
start = 2;
} else {
key = "";
}
for (var index = start; index < temp.length; index++) key += " " + temp[index];
key = key.trim();
if (key || value) results[camelize(key)] = value;
}

关于javascript - 如何将 vmstat 和 free -m 转换为对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54881446/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com