Skip to content

Instantly share code, notes, and snippets.

@marcosbrasil
Created September 2, 2012 03:01
Show Gist options
  • Save marcosbrasil/3594373 to your computer and use it in GitHub Desktop.
Save marcosbrasil/3594373 to your computer and use it in GitHub Desktop.
Inspect CPU usage with nodejs os.cpus() module
var os = require("os");
var exec = require('child_process').exec;
function sysLog(){
//exec('clear', sysLog);
var cpus = os.cpus();
for(var i = 0, len = cpus.length; i < len; i++) {
var cpu = cpus[i], total = 0, processTotal = 0, strPercent = '';
console.log("CPU %s:", i);
console.log("\t",'user',cpu.times.user,'|nice',cpu.times.nice,'|sys',cpu.times.sys,'|idle',cpu.times.idle,'|irq',cpu.times.irq);
for(type in cpu.times){
total += cpu.times[type];
}
for(type in cpu.times){
var percent = Math.round(100 * cpu.times[type] / total);
strPercent += type + ' ' + percent + '%|';
if(type != 'idle'){
processTotal += percent;
}
}
console.log("\t",strPercent)
console.log("\t",'T0TAL PROCESSOR: ',total);
console.log("\t",'TOTAL: ',processTotal);
}
}
var interval = setInterval(sysLog, 1000);
setTimeout(function(){
clearInterval(interval);
},100000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment