Home > Software engineering >  how to get server cpu and memory and disk percentage in node.js
how to get server cpu and memory and disk percentage in node.js

Time:04-22

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)

CodePudding user response:

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.

CodePudding user response:

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}%`);
});
  • Related