diff options
-rw-r--r-- | app.js | 138 | ||||
-rw-r--r-- | numberlogger.js | 18 | ||||
-rw-r--r-- | services.js | 1 |
3 files changed, 120 insertions, 37 deletions
@@ -3,38 +3,52 @@ var http = require('http'); var ps = require('portscanner'); var dispatcher = require('httpdispatcher'); var spawn = require('child_process').spawn; +var nl = require('./numberlogger.js'); +var services = require('./services.js'); -function getmem(){ - var prc = spawn('free', []); +var PORT = 3369; - 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]; - }); +var logger = function(name,length) { + this.name = name; + this.sec = new nl(name+":sec",length); + this.min = new nl(name+":min",length); + this.hrs = new nl(name+":hrs",length); + this.day = new nl(name+":day",length); + this.time = new Date(); +} + +logger.prototype.add = function(time,x) { + function average(l,n) { + var sum = 0; + for (var i=0;i<n;++i) + sum += l.ns[l.len - 1 - i]; + return sum/n; + } + if (time.getDay()!=this.time.getDay()) + this.day.add(average(this.hrs,24)); + if (time.getHours() != this.time.getHours()) + this.hrs.add(average(this.min,60)); + if (time.getMinutes() != this.time.getMinutes()) + this.min.add(average(this.sec,60)); + this.sec.add(x); + this.time = time; +} - prc.on('close', function (code) { - }); +logger.prototype.stringify = function() { + return [this.sec.stringify(),this.min.stringify(),this.hrs.stringify(),this.day.stringify()]; } -var PORT = 3369; +var cpustat = new logger("cpu",100); +var memstat = new logger("mem",100); var serverstat={ + "hostname":os.hostname(), "release":"undef", "uptime":"", "load":[0,0,0], - "mem":0, - "mc":false, - "pcs":{ - "2222":false, - "2223":false, - "2224":false, - "2225":false - } + "cpu":cpustat.stringify(), + "mem":memstat.stringify(), + "services":{} } serverstat.release = os.release(); @@ -52,30 +66,22 @@ function secondsToString(seconds) } function checkp(key){ - ps.checkPortStatus(key, 'cnjoe.info', function(error, status) { + ps.checkPortStatus(services[key], 'localhost', function(error, status) { // Status is 'open' if currently in use or 'closed' if available - console.log(key,status); + // console.log(key,status); if (status=='open') - serverstat.pcs[key]=true; + serverstat.services[key]=true; else - serverstat.pcs[key]=false; + serverstat.services[key]=false; }) } 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 (key in serverstat.pcs) { - if (serverstat.pcs.hasOwnProperty(key)) { + for (key in services) { + if (services.hasOwnProperty(key)) { checkp(key); } } - getmem(); serverstat.uptime = os.uptime(); serverstat.load = os.loadavg(); } @@ -96,7 +102,7 @@ dispatcher.setStatic('resources'); //A sample GET request dispatcher.onGet("/", function(req, res) { - res.writeHead(200, {'Content-Type': 'application/json'}); + res.writeHead(200, {'Content-Type': 'application/json','Access-Control-Allow-Origin': '*'}); res.end(JSON.stringify(serverstat)); }); @@ -118,3 +124,61 @@ server.listen(PORT, function(){ refreshStat(); setInterval(refreshStat,1000); + +function getCPUInfo(callback){ + var cpus = os.cpus(); + + var user = 0; + var nice = 0; + var sys = 0; + var idle = 0; + var irq = 0; + var total = 0; + + for(var cpu in cpus){ + + user += cpus[cpu].times.user; + nice += cpus[cpu].times.nice; + sys += cpus[cpu].times.sys; + irq += cpus[cpu].times.irq; + idle += cpus[cpu].times.idle; + } + + var total = user + nice + sys + idle + irq; + + return { + 'idle': idle, + 'total': total + }; +} + +function getUsage(callback, free){ + + var stats = getCPUInfo(); + + setInterval(function() { + var nstats = getCPUInfo(); + var endIdle = nstats.idle; + var endTotal = nstats.total; + + var idle = nstats.idle - stats.idle; + var total = nstats.total - stats.total; + var perc = 1 - idle / total; + + stats = nstats; + + var time = new Date(); + + cpustat.add(time,perc); + memstat.add(time,memPercentage()); + serverstat.cpu=cpustat.stringify(); + serverstat.mem=memstat.stringify(); + + }, 1000 ); +} + +function memPercentage(){ + return 1 - os.freemem() / os.totalmem(); +} + +getUsage(); diff --git a/numberlogger.js b/numberlogger.js new file mode 100644 index 0000000..764027e --- /dev/null +++ b/numberlogger.js @@ -0,0 +1,18 @@ +var numberLogger = function(name,length){ + this.name=name; + this.ns = new Array(length); + this.len = length; + for (i=0;i<length;++i) + this.ns[i] = 0; +} + +numberLogger.prototype.add = function(x) { + this.ns.splice(0,1); + this.ns.push(x); +} + +numberLogger.prototype.stringify = function() { + return {"name":this.name,"dat":this.ns,"len":this.length}; +} + +module.exports = numberLogger; diff --git a/services.js b/services.js new file mode 100644 index 0000000..e812e50 --- /dev/null +++ b/services.js @@ -0,0 +1 @@ +module.exports = {"ssh":22,"minecraft":25565}; |