var os = require('os'); var http = require('http'); var ps = require('portscanner'); var dispatcher = require('httpdispatcher'); var spawn = require('child_process').spawn; function getmem(){ var prc = spawn('free', []); prc.stdout.setEncoding('utf8'); prc.stdout.on('data', function (data) { var str = data.toString() var lines = str.split(/\n/g); for(var i = 0; i < lines.length; i++) { lines[i] = lines[i].split(/\s+/); } serverstat.mem = lines[1][2]/lines[1][1]; }); prc.on('close', function (code) { }); } var PORT = 3369; var serverstat={ "release":"undef", "uptime":"", "load":[0,0,0], "mem":0, "mc":false, "pcs":{ "2222":false, "2223":false, "2224":false, "2225":false } } serverstat.release = os.release(); function secondsToString(seconds) { function adds(x,a){ return x + " " + a + " "; } var numdays = Math.floor(seconds / 86400); seconds%=86400; var numhours = Math.floor(seconds / 3600); seconds%=3600; var numminutes = Math.floor(seconds / 60); seconds%=60; return adds(numdays,"day") + adds(numhours,"hrs")+ adds(numminutes,"min") + adds(seconds,"sec"); } function refreshStat(){ ps.checkPortStatus(25565, 'cnjoe.info', function(error, status) { // Status is 'open' if currently in use or 'closed' if available if (status=='open') serverstat.mc=true; else serverstat.mc=false; }) for (var key in p) { if (p.hasOwnProperty(key)) { alert(key + " -> " + p[key]); } getmem(); serverstat.uptime = os.uptime(); serverstat.load = os.loadavg(); } function handleRequest(request, response){ try { //log the request on console // console.log(request.url); //Disptach dispatcher.dispatch(request, response); } catch(err) { console.log(err); } } //For all your static (js/css/images/etc.) set the directory name (relative path). dispatcher.setStatic('resources'); //A sample GET request dispatcher.onGet("/", function(req, res) { res.writeHead(200, {'Content-Type': 'application/json'}); res.end(JSON.stringify(serverstat)); }); //A sample POST request //dispatcher.onPost("/post1", function(req, res) { // res.writeHead(200, {'Content-Type': 'text/plain'}); // res.end('Got Post Data'); //}); //Create a server var server = http.createServer(handleRequest); //Lets start our server server.listen(PORT, function(){ //Callback triggered when server is successfully listening. Hurray! console.log("Server listening on: http://localhost:%s", PORT); }); refreshStat(); setInterval(refreshStat,5000);