Last active
September 27, 2015 15:31
-
-
Save yassine-khachlek/4323752ac2abace7ad04 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var os = require('os'); | |
function loadAvgCpus(){ | |
var cpus = os.cpus(); | |
var total=0; | |
var idle =0; | |
for (var i = 0; i<cpus.length ; i++) { | |
Object.keys(cpus[i].times).forEach(function(type){ | |
total += parseFloat(cpus[i].times[type]); | |
}); | |
idle += parseFloat(cpus[i].times.idle); | |
}; | |
return {idle: idle, total: total}; | |
} | |
var firstCpusMeasure = loadAvgCpus(); | |
var setIntervalCpus = setInterval(function(){ | |
var secondCpusMeasure = loadAvgCpus(); | |
var cpusIdleDiff = secondCpusMeasure.idle - firstCpusMeasure.idle; | |
var cpusTotalDiff = secondCpusMeasure.total - firstCpusMeasure.total; | |
var usedCpus = 100 - (cpusIdleDiff / cpusTotalDiff)*100; | |
var freeCpus = (cpusIdleDiff / cpusTotalDiff)*100; | |
firstCpusMeasure = loadAvgCpus(); | |
console.log('usedCpus', usedCpus, 'freeCpus', freeCpus); | |
}, 1000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment