wellness-linux-cpu
Version:
Wellness module to check linux CPU usage.
41 lines (34 loc) • 1.01 kB
JavaScript
;
var is = require('is2');
var inspect = require('inspect-1-line');
var debug = require('debug')('wellness-linux-cpu');
var cpuLoad = 0.0;
/**
* The check for free CPU
* @param {function} cb - A standard call back for async.parallel
*/
function checkCpuUsage(cb) {
if (!is.func(cb)) {
var err = new Error('Bad cb argument checkCpuUsage: '+inspect(cb));
throw err;
}
if (process.platform !== 'linux')
return cb(null, 'CPU usage: unknown (only works on Linux)', true);
debug('in checkCpuUsage');
if (cpuLoad > 80)
return cb(null, 'CPU usage too high: '+cpuLoad+'%', false);
return cb(null, 'CPU usage: '+cpuLoad+'%', true);
}
/**
* Get the CPU average for 500ms every 2500 ms.
*/
function sampleCpuUsage() {
cpuLoad(500, function(load) {
debug('CPU utilization: '+load+'%');
cpuLoad = load;
});
}
if (process.platform === 'linux') {
setInterval(sampleCpuUsage, 2500);
}
module.exports = checkCpuUsage;