Linux 如何使用 node.js 获取系统统计信息

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10332565/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 05:59:57  来源:igfitidea点击:

How to get system statistics with node.js

javascriptlinuxbashnode.js

提问by brianconnoly

I have a distributed server system.

我有一个分布式服务器系统。

There are a lot of servers, coordinated through PubSub. All of them are connected to the statistics server. Every minute servers send their stats to the stat server (how many requests was processed, average time etc.).

有很多服务器,通过 PubSub 进行协调。所有这些都连接到统计服务器。服务器每分钟将他们的统计信息发送到统计服务器(处理了多少请求,平均时间等)。

So... It would be nice to include system status in this stat-messages. I need CPU load (every core) and amount of free memory.

所以......在这个统计信息中包含系统状态会很好。我需要 CPU 负载(每个内核)和可用内存量。

I made a little workaround and decided to call a linux command with "exec", parse answer and form a JSON data for sending.

我做了一些解决方法,并决定用“exec”调用一个 linux 命令,解析答案并形成一个 JSON 数据进行发送。

But how can I get this data from command line?

但是如何从命令行获取这些数据?

On Mac OS X I can easily get all I need with geektool scripts, but on linux (debian) they don't work.

在 Mac OS XI 上,使用 geektool 脚本可以轻松获得我需要的一切,但在 linux (debian) 上它们不起作用。

For example:

例如:

top -l 1 | awk '/PhysMem/ {print "Used: "  " Free: " }'

On Mac OS X Lion I get:

在 Mac OS X Lion 上,我得到:

Used: 3246M Free: 848M

And just an error in debian...

并且只是 debian 中的一个错误...

采纳答案by w00t

On Linux, you can use /proc. See herefor a bunch of command line examples to read the stats.

在 Linux 上,您可以使用 /proc。请参阅此处以获取大量用于读取统计信息的命令行示例。

It would be better to read the files from Node directly though, using fs.readFile()

使用fs.readFile()直接从 Node 读取文件会更好

Update: There is also the OS APIwhich is probably better. Example usage: Convert the output of os.cpus() in Node.js to percentage

更新:还有可能更好的OS API。用法示例:将 Node.js 中 os.cpus() 的输出转换为百分比

回答by tning

You can try os-usagewhich is a wrapper for topcommand.

您可以尝试os-usage,它是top命令的包装器。

It provides stats like cpu usage and memory usage. Example usage:

它提供 CPU 使用率和内存使用率等统计信息。用法示例:

var usage = require('os-usage');

// create an instance of CpuMonitor
var cpuMonitor = new usage.CpuMonitor();

// watch cpu usage overview
cpuMonitor.on('cpuUsage', function(data) {
    console.log(data);

    // { user: '9.33', sys: '56.0', idle: '34.66' }
});

// watch processes that use most cpu percentage
cpuMonitor.on('topCpuProcs', function(data) {
    console.log(data);

    // [ { pid: '21749', cpu: '0.0', command: 'top' },
    //  { pid: '21748', cpu: '0.0', command: 'node' },
    //  { pid: '21747', cpu: '0.0', command: 'node' },
    //  { pid: '21710', cpu: '0.0', command: 'com.apple.iCloud' },
    //  { pid: '21670', cpu: '0.0', command: 'LookupViewServic' } ]
});

回答by venksster

shameless plug - https://www.npmjs.com/package/microstats

无耻的插件 - https://www.npmjs.com/package/microstats

Can also be configured to alert the user when disk space, cpu or memory crosses user defined threshold. works for linux, macOS and windows.

也可以配置为在磁盘空间、CPU 或内存超过用户定义的阈值时提醒用户。适用于 linux、macOS 和 windows。

回答by valdeci

IMHO the best option is use the systeminformationmodule,

恕我直言,最好的选择是使用系统信息模块,

where you can retrieve detailed hardware, system and OS information with Linux, macOS, partial Windows and FreeBSD support.

您可以在其中检索具有 Linux、macOS、部分 Windows 和 FreeBSD 支持的详细硬件、系统和操作系统信息。

For example to get the CPU information:

例如获取CPU信息:

const si = require('systeminformation');

// callback style
si.cpu(function(data) {
    console.log('CPU-Information:');
    console.log(data);
});

// promises style - new in version 3
si.cpu()
    .then(data => console.log(data))
    .catch(error => console.error(error));

// full async / await example (node >= 7.6)
async function cpu() {
    try {
        const data = await si.cpu();
        console.log(data)
    } catch (e) {
        console.log(e)
    }
}

This example will result the following:

此示例将导致以下结果:

{ manufacturer: 'Intel?',
    brand: 'Core? i5-3317U',
    vendor: 'GenuineIntel',
    family: '6',
    model: '58',
    stepping: '9',
    revision: '',
    voltage: '',
    speed: '1.70',
    speedmin: '0.80',
    speedmax: '2.60',
    cores: 4,
    cache: { l1d: 32768, l1i: 32768, l2: 262144, l3: 3145728 } }
CPU-Information:
{ manufacturer: 'Intel?',
    brand: 'Core? i5-3317U',
    vendor: 'GenuineIntel',
    family: '6',
    model: '58',
    stepping: '9',
    revision: '',
    voltage: '',
    speed: '1.70',
    speedmin: '0.80',
    speedmax: '2.60',
    cores: 4,
    cache: { l1d: 32768, l1i: 32768, l2: 262144, l3: 3145728 } }