allLoadavg = function (){
var loads = _os.loadavg();
return loads[0].toFixed(4)+','+loads[1].toFixed(4)+','+loads[2].toFixed(4);
}n/a
cpuCount = function (){
return _os.cpus().length;
}...
var os = require('../lib/OSUtils');
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() );
...cpuFree = function (callback){
getCPUUsage(callback, true);
}...
var os = require('os-utils');
os.cpuUsage(function(v){
console.log( 'CPU Usage (%): ' + v );
});
os.cpuFree(function(v){
## Usage
The follwoing methods are available:
...cpuUsage = function (callback){
getCPUUsage(callback, false);
}...
npm install os-utils
Then in your code
var os = require('os-utils');
os.cpuUsage(function(v){
console.log( 'CPU Usage (%): ' + v );
});
os.cpuFree(function(v){
...freeCommand = function (callback){
// Only Linux
require('child_process').exec('free -m', function(error, stdout, stderr) {
var lines = stdout.split("\n");
var str_mem_info = lines[1].replace( /[\s\n\r]+/g,' ');
var mem_info = str_mem_info.split(' ')
total_mem = parseFloat(mem_info[1])
free_mem = parseFloat(mem_info[3])
buffers_mem = parseFloat(mem_info[5])
cached_mem = parseFloat(mem_info[6])
used_mem = total_mem - (free_mem + buffers_mem + cached_mem)
callback(used_mem -2);
});
}n/a
freemem = function (){
return _os.freemem() / ( 1024 * 1024 );
}...
### Get number of CPU
os.countCPUs()
### Get current free memory
os.freemem()
### Get total memory
os.totalmem()
...freememPercentage = function (){
return _os.freemem() / _os.totalmem();
}...
### 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();
...getProcesses = function (nProcess, callback){
// if nprocess is undefined then is function
if(typeof nProcess === 'function'){
callback =nProcess;
nProcess = 0
}
command = 'ps -eo pcpu,pmem,time,args | sort -k 1 -r | head -n'+10
//command = 'ps aux | head -n '+ 11
//command = 'ps aux | head -n '+ (nProcess + 1)
if (nProcess > 0)
command = 'ps -eo pcpu,pmem,time,args | sort -k 1 -r | head -n'+(nProcess + 1)
require('child_process').exec(command, function(error, stdout, stderr) {
var that = this
var lines = stdout.split("\n");
lines.shift()
lines.pop()
var result = ''
lines.forEach(function(_item,_i){
var _str = _item.replace( /[\s\n\r]+/g,' ');
_str = _str.split(' ')
// result += _str[10]+" "+_str[9]+" "+_str[2]+" "+_str[3]+"\n"; // process
result += _str[1]+" "+_str[2]+" "+_str[3]+" "+_str[4].substring((_str[4].length - 25))+"\n"; // process
});
callback(result);
});
}n/a
harddrive = function (callback){
require('child_process').exec('df -k', function(error, stdout, stderr) {
var total = 0;
var used = 0;
var free = 0;
var lines = stdout.split("\n");
var str_disk_info = lines[1].replace( /[\s\n\r]+/g,' ');
var disk_info = str_disk_info.split(' ');
total = Math.ceil((disk_info[1] * 1024)/ Math.pow(1024,2));
used = Math.ceil(disk_info[2] * 1024 / Math.pow(1024,2)) ;
free = Math.ceil(disk_info[3] * 1024 / Math.pow(1024,2)) ;
callback(total, free, used);
});
}n/a
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;
}...
### Get the number of miliseconds that the process has been running for.
os.processUptime()
### Get average load for the 1, 5 or 15 minutes
os.loadavg(1)
os.loadavg(5)
os.loadavg(15)
...platform = function (){
return process.platform;
}...
### 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()
...processUptime = function (){
//seconds
return process.uptime();
}...
### Get the number of miliseconds that the system has been running for.
os.sysUptime();
### Get the number of miliseconds that the process has been running for.
os.processUptime()
### Get average load for the 1, 5 or 15 minutes
os.loadavg(1)
os.loadavg(5)
os.loadavg(15)
...sysUptime = function (){
//seconds
return _os.uptime();
}...
### Get a percentage reporesentinf the free memory
os.freememPercentage()
### Get the number of miliseconds that the system has been running for.
os.sysUptime();
### Get the number of miliseconds that the process has been running for.
os.processUptime()
...totalmem = function (){
return _os.totalmem() / ( 1024 * 1024 );
}...
### Get current free memory
os.freemem()
### Get total memory
os.totalmem()
### Get a percentage reporesentinf the free memory
os.freememPercentage()
...