Get/View Memory & CPU usage via NodeJS

后端 未结 4 1708
猫巷女王i
猫巷女王i 2020-12-13 03:34

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 overa

4条回答
  •  心在旅途
    2020-12-13 04:36

    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

提交回复
热议问题