os-utils
Advanced tools
Comparing version 0.0.6 to 0.0.9
@@ -1,11 +0,109 @@ | ||
exports.getMemory = function(){ | ||
return "1MB (fake)"; | ||
var _os = require('os'); | ||
exports.platform = function(){ | ||
return process.platform; | ||
} | ||
exports.countCPUs = function(){ | ||
return "16 cores (fake)"; | ||
exports.cpuCount = function(){ | ||
return _os.cpus().length; | ||
} | ||
exports.getCPU = function(){ | ||
return "2% (fake)"; | ||
exports.sysUptime = function(){ | ||
//seconds | ||
return _os.uptime(); | ||
} | ||
exports.processUptime = function(){ | ||
//seconds | ||
return process.uptime(); | ||
} | ||
// Memory | ||
exports.freemem = function(){ | ||
return _os.freemem() / ( 1024 * 1024 ); | ||
} | ||
exports.totalmem = function(){ | ||
return _os.totalmem() / ( 1024 * 1024 ); | ||
} | ||
exports.freememPercentage = function(){ | ||
return _os.freemem() / _os.totalmem(); | ||
} | ||
/* | ||
* Returns the load average usage for 1, 5 or 15 minutes. | ||
*/ | ||
exports.loadavg = function(_time){ | ||
if(_time === undefined || (_time !== 5 && _time !== 15) ) _time = 1; | ||
var loads = _os.loadavg(); | ||
var v = 0; | ||
if(_time == 1) v = loads[0]; | ||
if(_time == 5) v = loads[1]; | ||
if(_time == 15) v = loads[2]; | ||
return v; | ||
} | ||
exports.cpuFree = function(callback){ | ||
getCPUUsage(callback, true); | ||
} | ||
exports.cpuUsage = function(callback){ | ||
getCPUUsage(callback, false); | ||
} | ||
function getCPUUsage(callback, free){ | ||
var stats1 = getCPUInfo(); | ||
var startIdle = stats1.idle; | ||
var startTotal = stats1.total; | ||
setTimeout(function() { | ||
var stats2 = getCPUInfo(); | ||
var endIdle = stats2.idle; | ||
var endTotal = stats2.total; | ||
var idle = endIdle - startIdle; | ||
var total = endTotal - startTotal; | ||
var perc = idle / total; | ||
if(free === true) | ||
callback( perc ); | ||
else | ||
callback( (1 - perc) ); | ||
}, 1000 ); | ||
} | ||
function getCPUInfo(callback){ | ||
var cpus = _os.cpus(); | ||
var user = 0; | ||
var nice = 0; | ||
var sys = 0; | ||
var idle = 0; | ||
var irq = 0; | ||
var total = 0; | ||
for(var cpu in cpus){ | ||
user += cpus[cpu].times.user; | ||
nice += cpus[cpu].times.nice; | ||
sys += cpus[cpu].times.sys; | ||
irq += cpus[cpu].times.irq; | ||
idle += cpus[cpu].times.idle; | ||
} | ||
var total = user + nice + sys + idle + irq; | ||
return {'idle': idle, 'total': total}; | ||
} | ||
{ | ||
"name" : "os-utils", | ||
"version" : "0.0.6", | ||
"version" : "0.0.9", | ||
"description" : "an operating-system utility library", | ||
@@ -26,3 +26,3 @@ "url" : "http://oscar-mejia.com", | ||
}, | ||
"license" : "BSD" | ||
"license" : "MIT" | ||
} |
os-utils | ||
======== | ||
an operating-system utility library | ||
an operating-system utility library. Some methods are wrappers of Node libraries | ||
and others are calculation made by the module. | ||
@@ -18,2 +19,11 @@ | ||
os.cpuUsage(function(v){ | ||
console.log( 'CPU Usage (%): ' + v ); | ||
}); | ||
os.cpuFree(function(v){ | ||
## Usage | ||
@@ -23,17 +33,53 @@ | ||
### Get current memory usage | ||
os.getMemory(); | ||
### Calculate CPU usage in the next second. This is not an average of CPU usage like in the "os" module. The callback will receive a parameter with the value | ||
os.cpuUsage( callback ); | ||
### Calculate free CPU in the next second. This is not based on average CPU usage like in the "os" module. The callback will receive a parameter with the value | ||
os.cpuFree( callback ); | ||
### Get the platform name | ||
os.platform(); | ||
### Get number of CPU | ||
os.countCPUs() | ||
os.countCPUs() | ||
### Get current free memory | ||
os.freemem() | ||
### Get total memory | ||
os.totalmem() | ||
### Get a percentage reporesentinf the free memory | ||
os.freememPercentage() | ||
### Get the number of miliseconds that the system has been running for. | ||
os.sysUptime(); | ||
### Get current CPU usage | ||
### Get the number of miliseconds that the process has been running for. | ||
os.getCPU() | ||
os.processUptime() | ||
### Get average load for the 1, 5 or 15 minutes | ||
os.loadavg(1) | ||
os.loadavg(5) | ||
os.loadavg(15) | ||
var os = require('../lib/OSUtils'); | ||
console.log( 'OS Utils' ); | ||
console.log( 'Memory: ' + os.getMemory() ); | ||
console.log('\n'); | ||
console.log( 'OS Utils'); | ||
console.log('\n'); | ||
console.log( 'Platform: ' + os.platform() ); | ||
console.log( 'CPUs: ' + os.cpuCount() ); | ||
console.log('\n'); | ||
console.log( 'System Uptime (s): ' + os.sysUptime() ); | ||
console.log( 'Process Uptime (s): ' + os.processUptime() ); | ||
console.log('\n'); | ||
console.log( 'Free Memory (Kb): ' + os.freemem() ); | ||
console.log( 'total Memory (Kb): ' + os.totalmem() ); | ||
console.log( 'Free Memory (%): ' + os.freememPercentage() ); | ||
console.log('\n'); | ||
console.log( 'Load Usage (%): ' + os.loadavg() ); | ||
console.log( 'Load Usage 1 (%): ' + os.loadavg(1) ); | ||
console.log( 'Load Usage 5 (%): ' + os.loadavg(5) ); | ||
console.log( 'Load Usage 15 (%): ' + os.loadavg(15) ); | ||
console.log('\n'); | ||
os.cpuUsage(function(v){ | ||
console.log( 'CPU Usage (%): ' + v ); | ||
}); | ||
os.cpuFree(function(v){ | ||
console.log( 'CPU Free (%): ' + v ); | ||
}); | ||
console.log('\n'); |
Sorry, the diff of this file is not supported yet
Unidentified License
License(Experimental) Something that seems like a license was found, but its contents could not be matched with a known license
Found 1 instance in 1 package
6299
7
0
100
121
84