function cpuInfo(cb) {
if(!isFunction(cb)) cb = emptyFn;
if(!checkPlatform(cb)) return;
execFile(wmic, ['cpu', 'get', 'Name'], function (error, res, stderr) {
if(error !== null || stderr) return cb(error || stderr);
var cpus = res.match(/[^\r\n]+/g).map(function(v) {
return v.trim();
});
cpus.shift();
cb(null, cpus);
});
}...
cpuInfo(cb)
-----------
Gets the name of each processor in the machine.
var cpu = require('windows-cpu');
// Get listing of processors
cpu.cpuInfo(function(error, results) {
if(error) {
return console.log(error);
}
// results =>
// [
// 'Intel(R) Xeon(R) CPU E5-2609 0 @ 2.40GHz',
...function findLoad(arg, cb) {
if(!isFunction(cb)) cb = emptyFn;
if(!checkPlatform(cb)) return;
var cmd = "wmic path Win32_PerfFormattedData_PerfProc_Process get Name,PercentProcessorTime,IDProcess | findstr /i /c:" + arg
;
exec(cmd, function (error, res, stderr) {
if(error !== null || stderr) return cb(error || stderr);
if(!res) return cb('Cannot find results for provided arg: ' + arg, { load: 0, results: [] });
var found = res.replace(/[^\S\n]+/g, ':').replace(/\:\s/g, '|').split('|').filter(function(v) {
return !!v;
}).map(function(v) {
var data = v.split(':');
return {
pid: +data[0],
process: data[1],
load: +data[2]
};
});
var totalLoad = 0;
found.forEach(function(obj) {
totalLoad += obj.load;
});
var output = {
load: totalLoad,
found: found
};
cb(null, output);
});
}...
findLoad(arg, cb)
---------------
Gets the total load in percent for process(es) by a specific search parameter.
var cpu = require('windows-cpu');
// Find the total load for "chrome" processes
cpu.findLoad('chrome', function(error, results) {
if(error) {
return console.log(error);
}
// results =>
// {
// load: 8,
...function nodeLoad(cb) {
findLoad('node', cb);
}...
nodeLoad(cb)
------------
Gets the total load in percent for all Node.js processes running on the current machine.
var cpu = require('windows-cpu');
// Get total load for all node processes
cpu.nodeLoad(function(error, results) {
if(error) {
return console.log(error);
}
// results =>
// {
// load: 20,
...function processLoad(cb) {
findLoad(process.pid, cb);
}...
processLoad(cb)
---------------
Gets the total load in percent for all processes running on the current machine per CPU.
var cpu = require('windows-cpu');
// Get load for current running node process
cpu.processLoad(function(error, results) {
if(error) {
return console.log(error);
}
// results =>
// {
// load: 10,
...function totalLoad(cb) {
if (!isFunction(cb)) cb = emptyFn;
if (!checkPlatform(cb)) return;
execFile(wmic, ['cpu', 'get', 'loadpercentage'], function (error, res, stderr) {
if(error !== null || stderr) return cb(error || stderr);
var cpus = (res.match(/\d+/g) || []).map(function(x) {
return +(x.trim());
});
cb(null, cpus);
});
}...
totalLoad(cb)
-------------
Gets the total load in percent for all processes running on the current machine per CPU.
var cpu = require('windows-cpu');
// Get total load on server for each CPU
cpu.totalLoad(function(error, results) {
if(error) {
return console.log(error);
}
// results (single cpu in percent) =>
// [8]
...function totalMemoryUsage(cb) {
if (!isFunction(cb)) cb = emptyFn;
if (!checkPlatform(cb)) return;
var cmd = "tasklist /FO csv /nh";
exec(cmd, function (error, res, stderr) {
if(error !== null || stderr) return cb(error || stderr);
var results = { usageInKb: 0 , usageInMb: 0 , usageInGb: 0 };
results.usageInKb = res.match(/[^\r\n]+/g).map(function(v) {
var amt = +v.split('","')[4].replace(/[^\d]/g, '');
return (!isNaN(amt) && typeof amt === 'number')? amt : 0;
}).reduce(function(prev, current) {
return prev + current;
});
results.usageInMb = results.usageInKb / 1024;
results.usageInGb = results.usageInMb / 1024;
cb(null, results);
});
}...
totalMemoryUsage(cb)
--------------------
Gets the total memory usage value in KB , MB and GB .
var cpu = require('windows-cpu');
// Get the memory usage
cpu.totalMemoryUsage(function(error, results) {
if(error) {
return console.log(error);
}
// results =>
// {
// usageInKb: 3236244,
...