Skip to content

Instantly share code, notes, and snippets.

@coolaj86
Created October 23, 2010 14:09
Show Gist options
  • Save coolaj86/642253 to your computer and use it in GitHub Desktop.
Save coolaj86/642253 to your computer and use it in GitHub Desktop.
var exec = require('child_process').exec,
Futures = require('futures'),
pload = Futures.promise(),
pdisk = Futures.promise(),
pusage = Futures.promise();
function log() {
console.log("event happened");
console.dir(arguments);
}
exec('uptime', pload.fulfill);
exec('df -l -k |grep md0 |awk \'{print $3,$4}\'', pdisk.fulfill);
exec('vnstat --dumpdb |grep "m;0"', pusage.fulfill);
pload.when(log);
pdisk.when(log);
pusage.when(log);
function parseLoad(err, stdout, stderr) {
var load = stdout.split('load average:');
load = load[1].split(', ');
return load;
}
function parseDisk(err, stdout, stderr) {
return stdout;
}
function parseUsage(err, stdout, stderr) {
return stdout.split(';');
}
Futures.join(pload, pdisk, pusage).when(function(loadArgs, diskArgs, usageArgs) {
var result = [
parseLoad.apply(null, loadArgs),
parseDisk.apply(null, diskArgs),
parseUsage.apply(null, usageArgs)
];
console.log("\n\nall complete");
console.dir(result);
});
var exec = require('child_process').exec,
Futures = require('futures');
var getLoad = function() {
var p = Futures.promise();
console.log('load');
exec('uptime', function(err, stdout, stderr) {
var load = stdout.split('load average:');
//load = load[1].slice(1,load[1].length-1);
load = load[1].split(', ');
//console.log(load);
p.fulfill(err, load);
});
return p;
};
var getDiskUsage = function() {
var p = Futures.promise();
console.log('disk');
var data = {};
exec('df -l -k |grep md0 |awk \'{print $3,$4}\'', function(err, stdout, stderr) {
var diskUsage = stdout.split(' ');
p.fulfill(err, diskUsage);
});
return p;
};
var getIfUsage = function() {
var p = Futures.promise();
console.log('ifUsage');
var data = {};
exec('vnstat --dumpdb |grep "m;0"', function(err, stdout, stderr) {
var ifUsage = stdout.split(';');
//console.log(ifUsage);
p.fulfill(err, ifUsage);
});
return p;
};
test = getLoad;
test2 = getDiskUsage;
test3 = getIfUsage;
var p = test().when(function(err, data) {
console.log('test-callback');
console.log(data);
});
var p2 = test2().when(function(err, data) {
console.log('test2-callback');
console.log(data);
});
var p3 = test3().when(function(err, data) {
console.log('test3-callback');
console.log(data);
});
Futures.join(p, p2, p3).when(function(arr) {
console.log(arr);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment