Skip to content

Commit

Permalink
Refactored agents, added simple test
Browse files Browse the repository at this point in the history
  • Loading branch information
DamonOehlman committed Aug 31, 2011
1 parent 3bcadd3 commit 4372fe7
Show file tree
Hide file tree
Showing 10 changed files with 220 additions and 190 deletions.
3 changes: 2 additions & 1 deletion .gitignore
@@ -1 +1,2 @@
node_modules
node_modules
samples
12 changes: 12 additions & 0 deletions lib/agent.js
@@ -0,0 +1,12 @@
var DEFAULT_AMOUNT = Math.pow(10, 5);

// define the base agent
var BaseAgent = exports.BaseAgent = function() {
};

BaseAgent.prototype.roundVal = function(value, decimals) {
// initialise the amount, avoid math if we can
var amount = decimals ? Math.pow(10, decimals) : DEFAULT_AMOUNT;

return Math.round(value * amount) / amount;
};
94 changes: 42 additions & 52 deletions lib/agents/cpu.js
@@ -1,61 +1,51 @@
var os = require('os');

module.exports = (function() {
var cpuCores = os.cpus().length,
cpuOld;
/* internals */
var os = require('os'),
util = require('util'),
BaseAgent = require('../agent').BaseAgent,
coreCount = os.cpus().length;


function collect() {
var raw = os.cpus(),
cpu = [];

for (var ii=0; ii < cpuCores; ii++) {
var times = raw[ii].times,
total = 0;

for (var key in times) {
total += times[key];
} // for
function readCpu() {
var raw = os.cpus(),
cpu = [];

for (var ii = coreCount; ii--; ) {
var times = raw[ii].times,
total = 0;

cpu[ii] = {
total: total,
idle: times.idle
};
for (var key in times) {
total += times[key];
} // for

return cpu;
} // collect

function calcLoad(cpuNew) {
var idleDelta,
tickDelta,
totalTick,
load = [];
cpu[ii] = {
total: total,
idle: times.idle
};
} // for

return cpu;
};

for (var ii=0; ii < cpuCores; ii++) {
tickDelta = cpuNew[ii].total - cpuOld[ii].total;
idleDelta = cpuNew[ii].idle - cpuOld[ii].idle;

load[ii] = 1 - idleDelta / tickDelta;
cpuOld[ii] = cpuNew[ii];
} // for
var Agent = exports.Agent = function() {
// set the default frequency to 100ms
this.frequency = 100;
this.lastReading = readCpu();
};

return load;
} // calcLoad
util.inherits(Agent, BaseAgent);

cpuOld = collect();
Agent.prototype.run = function(callback) {
var idleDelta, tickDelta, load = [],
reading = readCpu();

function run(callback) {
callback(calcLoad(collect()));
} // callback

/* exports */
for (var ii = coreCount; ii--; ) {
tickDelta = reading[ii].total - this.lastReading[ii].total;
idleDelta = reading[ii].idle - this.lastReading[ii].idle;

// calculate the load for the core
load[ii] = this.roundVal(1 - (tickDelta ? idleDelta / tickDelta : 1));

// update the last reading
this.lastReading[ii] = reading[ii];
} // for

return {
// define the frequency for
frequency: 100, // '* * * * * *',
run: run
};

})();
callback(load);
};
40 changes: 16 additions & 24 deletions lib/agents/mem.js
@@ -1,27 +1,19 @@
var os = require('os');

module.exports = (function() {
var data = {
"total" : os.totalmem()
};

/* internals */
function collect() {
data.free = os.freemem();

return data;
}
var os = require('os'),
util = require('util'),
BaseAgent = require('../agent').BaseAgent,
totalMem = os.totalmem();

var Agent = exports.Agent = function() {
};

util.inherits(Agent, BaseAgent);

function run(callback) {
callback(collect());
} // callback

/* exports */
Agent.prototype.run = function(callback) {
// read the free memory
var free = os.freemem();

return {
// define the frequency for
frequency: '* * * * * *',
run: run
};
})();
callback(this.roundVal((totalMem - free) / totalMem), {
free: free,
total: totalMem
});
};
52 changes: 0 additions & 52 deletions lib/agents/net.js

This file was deleted.

56 changes: 50 additions & 6 deletions lib/agents/proc.js
@@ -1,13 +1,57 @@
var os = require('os'),
exec = require('child_process').exec;
util = require('util'),
_ = require('underscore'),
exec = require('child_process').exec,
BaseAgent = require('../agent').BaseAgent;

function readProcesses(command, callback) {
var ps = exec(command, function(err, stdout, stderr) {
var lines = (stdout || '').split('\n').slice(1),
processes = [],
sortedProcesses;

lines.forEach(function(line) {
var data = line.split(/\s+/).slice(1),
psData = {
cpu: parseFloat(data[0]),
mem: parseFloat(data[1]),
process: data.slice(2).join(' ')
};

if (psData.cpu >= 0.5 || psData.mem >= 0.5) {
processes.push(psData);
} // if
});

callback(null, processes.sort(function(a, b) {
return b.cpu - a.cpu;
}));
});
};

var Agent = exports.Agent = function() {
this.frequency = '*/2 * * * * *';
};

util.inherits(Agent, BaseAgent);

Agent.prototype.run = function(callback) {
var command = this.opts.command || 'ps -A -o %cpu,%mem,fname,command';

readProcesses(command, function(err, processes) {
if (! err) {
callback(processes.slice(0, 5), processes.slice(5));
} // if
});
};

module.exports = (function() {

/* internals */
/*
module.exports = (function() {
function run(callback) {
// The following code is taken from https://github.com/codejoust/node-sysmon/blob/master/core.js line 36 - 50
var ps = exec('ps -e --sort=%cpu -o %cpu,%mem,pid,user,fname | tail --lines 5', function(err, stdout, stderr) {
var ps = exec('ps -A -o %cpu,%mem,pid,user,fname,command', function(err, stdout, stderr) {
var ps_raw = (' ' + stdout).split("\n").slice(1, 5).reverse();
var processes = [];
Expand All @@ -31,11 +75,11 @@ module.exports = (function() {
}); // ps
} // callback
/* exports */

return {
// define the frequency for
frequency: '* * * * * *',
run: run
};
})();
*/

0 comments on commit 4372fe7

Please sign in to comment.