Skip to content

Commit

Permalink
Add reporting/process.spawnAndMonitor() and chart.updateFromEventEmit…
Browse files Browse the repository at this point in the history
…ter() to allow updating a chart from a child process.
  • Loading branch information
jonjlee committed Feb 7, 2011
1 parent b05286b commit 579f27c
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
.PHONY: clean templates compile
PROCESS_TPL = scripts/process_tpl.js
SOURCES = lib/header.js lib/config.js lib/util.js lib/stats.js lib/loop/loop.js lib/loop/multiloop.js lib/monitoring/collectors.js lib/monitoring/statslogger.js lib/monitoring/monitor.js lib/monitoring/monitorgroup.js lib/http.js lib/reporting/*.tpl.js lib/reporting/template.js lib/reporting/index.js lib/loadtesting.js lib/remote/endpoint.js lib/remote/endpointclient.js lib/remote/slave.js lib/remote/slaves.js lib/remote/slavenode.js lib/remote/cluster.js lib/remote/httphandler.js lib/remote/remotetesting.js
SOURCES = lib/header.js lib/config.js lib/util.js lib/stats.js lib/loop/loop.js lib/loop/multiloop.js lib/monitoring/collectors.js lib/monitoring/statslogger.js lib/monitoring/monitor.js lib/monitoring/monitorgroup.js lib/http.js lib/reporting/*.tpl.js lib/reporting/template.js lib/reporting/index.js lib/reporting/process.js lib/loadtesting.js lib/remote/endpoint.js lib/remote/endpointclient.js lib/remote/slave.js lib/remote/slaves.js lib/remote/slavenode.js lib/remote/cluster.js lib/remote/httphandler.js lib/remote/remotetesting.js

all: compile

Expand Down
Binary file added jmxstat/jmxstat.jar
Binary file not shown.
3 changes: 2 additions & 1 deletion lib/header.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ var util = require('util'),
url = require('url'),
fs = require('fs'),
events = require('events'),
querystring = require('querystring');
querystring = require('querystring'),
child_process = require('child_process');

var EventEmitter = events.EventEmitter;

Expand Down
16 changes: 16 additions & 0 deletions lib/reporting/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,22 @@ Chart.prototype = {
row[col] = val;
});
self.rows.push(row);
},
/** Update chart using data from event emitter each time it emits an event. 'eventEmitter' should
emit the given 'event' (defaults to 'data') with a single object. 'fields' are read from the object
and added to the chart. For example, a chart can track the output form a child process output using
chart.updateFromEventEmitter(spawnAndMonitor('cmd', ['args'], /val: (.*)/, ['val']), ['val'])
*/
updateFromEventEmitter: function(eventEmitter, fields, event) {
eventEmitter.on(event || 'data', function(data) {
var row = {};
fields.forEach(function(i) {
if (data[i] !== undefined) { row[i] = data[i]; }
});
this.put(row);
});
}
};

Expand Down
50 changes: 50 additions & 0 deletions lib/reporting/process.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
var BUILD_AS_SINGLE_FILE;
if (!BUILD_AS_SINGLE_FILE) {
var child_process = require('child_process');
}

/** Spawn a child process and extract data using a regex. Each line is compared to the regex. If a match
is found, the groups are written to an object using the given field names, and the 'data' event is
emitted.
Returns a standard ChildProcess object, extended to emit('data', object).
For example, if 'ls -al' prints:
drwxr-xr-x 22 user staff 748 Feb 7 10:54 ./
drwxr-xr-x 23 user staff 782 Feb 7 09:10 ../
-rw-r----- 1 user staff 68 Jan 12 17:03 .gitignore
then:
spawnAndMonitor(
/-(.)..(.)..(.)../, ['userReadable', 'groupReadable', 'worldReadable'],
'ls', ['-al']);
will emit('data', {userReadable: 'r', groupReadable: 'r', worldReadable: '-'});
*/
var spawnAndMonitor = exports.spawnAndMonitor = function(regex, fields, spawnArguments) {
var buf = '', proc = child_process.spawn.apply(child_process, Array.prototype.slice.call(arguments, 2));
proc.stdout.on('data', function (data) {
buf += data.toString();

var lines = buf.split('\n');
buf = lines.pop();

lines.forEach(function(line) {
var vals = line.match(regex);
if (vals) {
if (fields) {
var obj = {};
for (var i = 1; i < vals.length; i++) {
obj[fields[i-1]] = vals[i];
}
proc.emit('data', obj);
} else {
proc.emit('data', vals);
}
}
});
});
return proc;
};
Loading

0 comments on commit 579f27c

Please sign in to comment.