var os = require('os'); var ps = require('portscanner'); var nl = require('./numberlogger.js'); var config = require('./config.js'); var express = require('express'); var compression = require('compression'); var PORT = 3369; var serverstat = { "uptime": os.uptime(), "load": [0, 0, 0], "services": {}, "stats" : {} }; var serverinfo = { "hostname": os.hostname(), "release": os.release(), "cpuname": os.cpus()[0].model, "cpunum": os.cpus().length, "totmem": Math.round(os.totalmem()/1024/1024), "desc" : config.description, "stats": {} }; var statResponse = {}; function checkp(key) { ps.checkPortStatus(config.services[key], 'localhost', function (error, status) { serverstat.services[key] = (status == 'open'); }) } 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; }; logger.prototype.setupResponse = function(res){ var host = this; var stat = []; function setupSingleStat(id){ stat.push(host[id].name); res[host[id].name] = function (stat) { stat.stats[host.name] = host[id].stringify(); }; } console.log("Setting up response for "+this.name); setupSingleStat("sec"); setupSingleStat("min"); setupSingleStat("hrs"); setupSingleStat("day"); serverinfo.stats[this.name] = stat; }; logger.prototype.stringify = function () { return [this.sec.stringify(), this.min.stringify(), this.hrs.stringify(), this.day.stringify()]; }; var cpustat = new logger("cpu", 100); var memstat = new logger("mem", 100); cpustat.setupResponse(statResponse); memstat.setupResponse(statResponse); function refreshStat() { for (var key in config.services) { if (config.services.hasOwnProperty(key)) { checkp(key); } } serverstat.uptime = os.uptime(); serverstat.load = os.loadavg(); } refreshStat(); setInterval(refreshStat, 1000); function getCPUInfo() { var cpus = os.cpus(); var user = 0; var nice = 0; var sys = 0; var idle = 0; var irq = 0; for (var cpu in cpus) if (cpus.hasOwnProperty(cpu)) { 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() { var stats = getCPUInfo(); setInterval(function () { var nstats = getCPUInfo(); 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()); }, 1000); } function memPercentage() { return 1 - os.freemem() / os.totalmem(); } getUsage(); var app = express(); app.use(compression()); app.get('/', function (req, res) { res.append('Content-Type', 'application/json'); res.append('Access-Control-Allow-Origin', '*'); res.send(JSON.stringify(serverinfo)); }); app.get('/stat', function (req, res) { res.append('Content-Type', 'application/json'); res.append('Access-Control-Allow-Origin', '*'); var stat = JSON.parse(JSON.stringify(serverstat)); for (var query in req.query) if (req.query.hasOwnProperty(query)) statResponse[req.query[query]](stat); res.send(JSON.stringify(stat)); }); app.listen(PORT); console.log("Server listening on: %s", PORT);