Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

[stats] Added stats emitter

  • Loading branch information...
commit 897e8a72af6271510cf58d8b2ee4634487cdd43c 1 parent 05b7bd9
@alejandro alejandro authored
Showing with 168 additions and 5 deletions.
  1. +65 −5 app.js
  2. +97 −0 lib/stats.js
  3. +6 −0 var/limits.json
View
70 app.js
@@ -13,12 +13,10 @@ require('coffee-script');
var express = require('express'),
url = require('url'),
sys = require('sys'),
+ path = require('path'),
config = require('./config'),
- middle = require('./lib/middle');
-
-process.on('uncaughtException', function (err) {
- console.log(err.stack);
-});
+ middle = require('./lib/middle'),
+ stats = require('./lib/stats');
var daemon = require('daemon');
// daemon.setreuid(config.opt.userid);
@@ -44,6 +42,68 @@ myapp.error(function (err, req, res, next) {
});
+/*
+ * dev dashboard emitter
+*/
+var bolt = require('bolt');
+var mesh = new bolt.Node({
+ delimiter : '::',
+ host : config.opt.redis.host,
+ port : config.opt.redis.port,
+ user : config.opt.redis.user,
+ auth : config.opt.redis.auth,
+ silent : true
+});
+mesh.start();
+
+myapp.all('*',function(req,res,next){
+ if (!path.extname(req.url)){
+ var ip = req.connection.remoteAddress || req.socket.remoteAddress;
+ if (req.headers["x-real-ip"]) ip =req.headers["x-real-ip"];
+ var toEmit = {
+ ip : ip,
+ url : req.url ,
+ time : new Date,
+ method : req.method,
+ ua : req.headers['user-agent'] || 'nodester',
+ host : req.headers.host
+ }
+ mesh.emit('nodester::incomingRequest', toEmit);
+ }
+ next();
+})
+
+function getStats(){
+ var statistics ={}
+ for (var stat in stats){
+ if (stat != 'getDiskUsage' && stat != 'getProcesses'){
+ statistics[stat] = stats[stat]()
+ } else {
+ stats[stat](function(error,resp){
+ if (!error)
+ statistics[stat] = resp;
+ else
+ statistics[stat] = '0'
+ });
+ }
+ }
+ return statistics;
+}
+
+setInterval(function(){
+ mesh.emit('nodester::ping',{date:new Date})
+ mesh.emit('nodester::stats',getStats());
+},3000);
+
+
+
+process.on('uncaughtException', function (err) {
+ mesh.emit('nodester::uE', err);
+ console.log(err.stack);
+});
+
+
+
/* Routes */
// Homepage
View
97 lib/stats.js
@@ -0,0 +1,97 @@
+var sys = require('util'),
+exec = require('child_process').exec,
+fs = require('fs'),
+os = require('os');
+
+process.startTime = new Date().getTime();
+
+module.exports = {
+ getMemoryUsage: function (cb) {
+ var memPerc = 100 - ((os.freemem() / os.totalmem()) * 100);
+ return {usedRatio: memPerc}
+ },
+ getMemoryinUse :function(cb){
+ return (os.totalmem()-os.freemem())/1024/1024 + 'Mbs';
+ },
+ getUptime: function (cb) {
+ return os.uptime();
+ },
+ getDiskUsage: function(cb) {
+ exec("df -h | awk '{print $1,$3,$4,$5}'", function (err, resp){
+ if (err)
+ cb(err,null);
+ else
+ var out = {};
+ resp = resp.split('\n')[1];
+ args = resp.split(' ');
+ out.disk = args[0];
+ out.used = Math.round(args[2].replace( 'G', ''));
+ out.total = Math.round(args[1].replace('G', ''));
+ out.free = Math.round(out.total - out.used);
+ out.usedPercent = Math.round(args[3].replace( '%', ''));
+ cb(null,out)
+ });
+ },
+ getCPUs: function (cb) {
+ var cpus = os.cpus()
+ return {count: cpus.length, name: cpus[0].model}
+ },
+ getCPUUsage: function(cb) {
+ var total = 0,
+ idle = 0;
+ var cpus = os.cpus();
+ for (t in cpus)
+ total += cpus[t].times.user + cpus[t].times.nice + cpus[t].times.sys + cpus[t].times.idle
+ idle += cpus[t].times.idle
+ total = total / cpus.length
+ idle = idle / cpus.length
+ var cpuPerc = 100 - ((idle / total) * 100)
+ return {usedRatio: cpuPerc}
+ },
+ getRAM: function(cb){
+ var size = Math.round(os.totalmem() / 1000000000) + 'GB'
+ return size;
+ },
+ getLoad: function(cb){
+ var load = os.loadavg()[0]*100;
+ if (load > 100)
+ load = load / 10
+ return load
+ },
+ getProcesses:function(cb) {
+ exec("ps aux | awk '!/PID/ {print $1,$2,$3,$4,$10,$11}'", function(err, resp) {
+ if (err)
+ cb(err,null)
+ else {
+ var out = [];
+ resp = resp.split('\n')
+ for (proc in resp) {
+ var args = proc.split(' ')
+ out.push([args[1], args[2], args[3], args[5], args[4], args[0]])
+ }
+ cb(null, out)
+ }
+ });
+ },
+ getPlatform: function(cb){
+ return os.type() + ' ' + os.release()
+ },
+ getVersion:function(){
+ return process.version.replace('v', '')
+ },
+ getEnvironment: function(){
+ return process.env.NODE_ENV || "None"
+ },
+ getPrefix: function(){
+ return process.installPrefix || '/usr/lib/'
+ },
+ getProcessMemUsage : function(){
+ return process.memoryUsage().rss/1024/1024 +'Mb';
+ },
+ getProcessUptime: function(){
+ return (Date.now() - process.startTime)/1000 +'sec';
+ },
+ currentTime : function(){
+ return new Date;
+ }
+}
View
6 var/limits.json
@@ -0,0 +1,6 @@
+{
+ "appname":{
+ "mem":"25",
+ "disk":"25"
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.