Created
September 3, 2010 01:50
-
-
Save moos3/563279 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var http = require('http'), | |
url = require('url'), | |
fs = require('fs'), | |
io = require('./lib/'), | |
sys = require('sys'); | |
var iostat = require('child_process').spawn("iostat",["-w 1"]); | |
send404 = function(res){ | |
res.writeHead(404); | |
res.write('404'); | |
res.end(); | |
}; | |
// Format iostat date | |
function format (data){ | |
var output_data = data.toString(); | |
sys.log(output_data); | |
header = 'disk0 cpu load average'; | |
if (output_data.match(header)){ | |
sys.log("ignore header"); | |
}else{ | |
var output_array = output_data.replace(/^\s+|\s+$/g,"").split(/\s+/); | |
for (var i=0; i < output_array.length; i++){ | |
output_array[i] = parseFloat( output_array[i]); | |
}; | |
output_hash = { | |
date:new Date(), | |
disk:{ | |
kpt:output_array[0], | |
tps:output_array[1], | |
mbs:output_array[2] | |
}, | |
cpu:{ | |
us:output_array[3], | |
sy:output_array[4], | |
id:output_array[5] | |
}, | |
load_average:{ | |
m1:output_array[6], | |
m5:output_array[7], | |
m15:output_array[8] | |
} | |
} | |
return JSON.stringify(output_hash); | |
} | |
} | |
// HTTP SERVER SETUP | |
/*server = http.createServer(function(req,res){ | |
var path = url.parse(req.url).pahtname; | |
switch (path){ | |
case '/': | |
res.writeHead(200, {'Content-Type': 'text/html'}); | |
res.write('<h1>Welcome to System Monitor</h1>'); | |
res.end(); | |
break; | |
default: | |
if (/\.(js|html|swf)$/.test(path)){ | |
try { | |
var swf = path.substr(-4) === '.swf'; | |
res.writeHead(200, {'Content-Type': swf ? 'application/x-shockwave-flash' : ('text/' + (path.subsr(-3) === '.js' ? 'javascript' : 'html'))}); | |
fs.readFile(__dirname + path, swf ? 'binary' : 'utf8', function(err, data){ | |
if (!err) res.write(data, swf ? 'binary' : 'utf8'); | |
res.end(); | |
}); | |
}catch(e){ | |
send404(res); | |
} | |
break; | |
} | |
send404(res); | |
break; | |
} | |
});*/ | |
var server = http.createServer(function(req,res){ | |
res.writeHead(200,{'Content-Type':'text/plain'}); | |
}); | |
// set port for server | |
server.listen(9000); | |
// configure socket.io | |
var io = io.listen(server), | |
buffer = []; | |
io.on('connection',function(client){ | |
console.log("opened connection: "+client.id); | |
client.send(client.id, "Connected as: " +conn.id); | |
iostat.stdout.on('data',function(data){ | |
console.log(typeof(data)); | |
console.log('stdout: '+data); | |
console.log('stdout: '+format(data)); | |
client.send(client.id, format(data)); | |
}); | |
client.send(client.id, "Connected as: "+client.id); | |
client.on('disconnect',function(client){ | |
console.log("closed connection: "+client.id); | |
client.broadcast("<"+client.id+"> disconnected"); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment