如何在node.js中获取服务器CPU,内存和磁盘百分比

发布于 2025-01-23 02:36:46 字数 720 浏览 1 评论 0原文

var os  = require('os-utils');
os.cpuUsage(function(v){
    console.log( 'os-utils CPU Usage (%): ' + v ); 
    // os-utils CPU Usage (%): 0.11180382377389864
});
os.cpuFree(function(v){
    console.log( 'os-utils CPU Free:' + v );
    // os-utils CPU Free:0.8876135425268373
});

var osu = require('node-os-utils')
var cpu = osu.cpu
cpu.usage()
.then(info => {
    console.log('node-os-utils cpu.usage '+info)
    // node-os-utils cpu.usage 11.53
})
cpu.free()
.then(info => {
    console.log('node-os-utils cpu.free '+ info)
    // node-os-utils cpu.free 88.47
})

太烦人了。 为什么OS-Utils值与Node-OS-Utils值不同? 我需要当前运行node.js的服务器的CPU和磁盘内存值 - 电流CPU使用**(百分比) - 可用内存*(免费/总计) - 可用磁盘空间(免费/总计)

var os  = require('os-utils');
os.cpuUsage(function(v){
    console.log( 'os-utils CPU Usage (%): ' + v ); 
    // os-utils CPU Usage (%): 0.11180382377389864
});
os.cpuFree(function(v){
    console.log( 'os-utils CPU Free:' + v );
    // os-utils CPU Free:0.8876135425268373
});

var osu = require('node-os-utils')
var cpu = osu.cpu
cpu.usage()
.then(info => {
    console.log('node-os-utils cpu.usage '+info)
    // node-os-utils cpu.usage 11.53
})
cpu.free()
.then(info => {
    console.log('node-os-utils cpu.free '+ info)
    // node-os-utils cpu.free 88.47
})

it's so annoying.
Why is the os-utils value different from the node-os-utils value?
I need the CPU and disk memory values ​​of the server currently running node.js
-Current CPU usage** (percent)
-Available memory* (free/total)
-Available disk space (free/total)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

黒涩兲箜 2025-01-30 02:36:46

似乎OS-UTILS软件包运行良好。仅给出0到1的百分比格式,含义0 => 0%1 => 100%等等。

关于获取磁盘空间卷信息,您可以使用软件包diskusage
您可以在那里查看文档 https://www.npmjs.coms.com/package/package/diskusage/diskusage

const disk = require('diskusage');

// get disk usage. Takes mount point as first parameter
disk.check('/', function(err, info) {
    const freeInPercentage = info.free / info.total;
    const usedInPercentage = info.available / info.total;

    console.log(`free disk space: ${freeInPercentage * 100}%`);
    console.log(`used disk space: ${usedInPercentage * 100}%`);
});

It seems like os-utils package works well. Just format of percentage was given from 0 to 1, meaning 0 => 0% and 1 => 100% and so on.

About getting disk space volume info you can use package diskusage.
You can check docs there https://www.npmjs.com/package/diskusage

const disk = require('diskusage');

// get disk usage. Takes mount point as first parameter
disk.check('/', function(err, info) {
    const freeInPercentage = info.free / info.total;
    const usedInPercentage = info.available / info.total;

    console.log(`free disk space: ${freeInPercentage * 100}%`);
    console.log(`used disk space: ${usedInPercentage * 100}%`);
});
陌伤ぢ 2025-01-30 02:36:46

为什么OS-Utils值与Node-OS-Utils值不同?

因为一个是一个百分比(0到100之间的数字),而一个是0到1之间的数字。

(我认为多核系统可以将最大数量乘以核心数量。)

值是相同的。它们只是以非常不同的术语表示。

Why is the os-utils value different from the node-os-utils value?

Because one is a percentage (a number between 0 and 100) and one is a number between 0 and 1.

(I would assume that multi-core systems may multiple the maximum number by the number of cores.)

The values are the same. They are just expressed in very slightly different terms.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文