最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to get system statistics with node.js - Stack Overflow

programmeradmin1浏览0评论

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.).

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

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

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.

For example:

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

On Mac OS X Lion I get:

Used: 3246M Free: 848M

And just an error in debian...

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.).

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

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

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.

For example:

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

On Mac OS X Lion I get:

Used: 3246M Free: 848M

And just an error in debian...

Share Improve this question asked Apr 26, 2012 at 11:34 brianconnolybrianconnoly 971 gold badge1 silver badge7 bronze badges 3
  • Have you tried Dtrace? mcavage.github.com/presentations/dtrace_conf_2012-04-03 – codef0rmer Commented Apr 26, 2012 at 13:22
  • github.com/sebhildebrandt/systeminformation – vbarbarosh Commented Jan 30, 2017 at 2:12
  • stackoverflow.com/questions/26208454/… – King11 Commented Jan 28, 2020 at 17:11
Add a comment  | 

4 Answers 4

Reset to default 7

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

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

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

IMHO the best option is to use the systeminformation module,

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

For example to get the CPU information:

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 } }

You can try os-usage which is a wrapper for top command.

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

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' } ]
});

shameless plug - 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.

发布评论

评论列表(0)

  1. 暂无评论