Get/View Memory & CPU usage via NodeJS

I see there are several node packages that allow you to look up a specific process's usage, such as https://www.npmjs.com/package/usage

I am trying to get the overall sever usage/stats (CPU and Memory), not just one specific process or another. Maybe even disk space usage.

I am currently unable to find anything like this, is this possible?


Solution 1:

The native module os can give you some memory and cpu usage statistics.

var os = require('os');

console.log(os.cpus());
console.log(os.totalmem());
console.log(os.freemem())

The cpus() function gives you an average, but you can calculate the current usage by using a formula and an interval, as mentioned in this answer.

There is also a package that does this for you, called os-utils.

Taken from the example on github:

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

os.cpuUsage(function(v){
    console.log( 'CPU Usage (%): ' + v );
});

For information about the disk you can use diskspace

Solution 2:

Check node-os-utils

  • CPU average usage
  • Free and used drive space
  • Free and used memory space
  • Operating System
  • All processes running
  • TTY/SSH opened
  • Total opened files
  • Network speed (input and output)
var osu = require('node-os-utils')

var cpu = osu.cpu

cpu.usage()
  .then(info => {
    console.log(info)
  })

Solution 3:

You can also use process.cpuUsage() to return the system and user cpu time in microseconds. It can also calculate the difference to a previous call.

https://nodejs.org/api/process.html#process_process_cpuusage_previousvalue