Skip to content

Commit

Permalink
added a little tcp management interface for pulling stats from runnin…
Browse files Browse the repository at this point in the history
…g statsd instance
  • Loading branch information
Marcus Barczak committed Oct 11, 2011
1 parent 116dfe3 commit b7b5c17
Showing 1 changed file with 75 additions and 1 deletion.
76 changes: 75 additions & 1 deletion stats.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,19 @@ var dgram = require('dgram')

var counters = {};
var timers = {};
var debugInt, flushInt, server;
var debugInt, flushInt, server, mgmt_server;
var startup_time = Math.round(new Date().getTime() / 1000);

var stats = {
graphite: {
last_flush: startup_time,
last_exception: startup_time
},
messages: {
last_msg_seen: startup_time,
bad_lines_seen: 0,
}
};

config.configFile(process.argv[2], function (config, oldConfig) {
if (! config.debug && debugInt) {
Expand Down Expand Up @@ -38,6 +50,7 @@ config.configFile(process.argv[2], function (config, oldConfig) {
var fields = bits[i].split("|");
if (fields[1] === undefined) {
sys.log('Bad line: ' + fields);
stats['messages']['bad_lines_seen']++;
continue;
}
if (fields[1].trim() == "ms") {
Expand All @@ -55,9 +68,68 @@ config.configFile(process.argv[2], function (config, oldConfig) {
counters[key] += Number(fields[0] || 1) * (1 / sampleRate);
}
}

stats['messages']['last_msg_seen'] = Math.round(new Date().getTime() / 1000);
});

mgmt_server = net.createServer(function(stream) {
stream.setEncoding('ascii');

stream.on('data', function(data) {
var cmd = data.trim();

switch(cmd) {
case "help":
stream.write("Commands: stats, counters, timers, quit\n\n");
break;

case "stats":
var now = Math.round(new Date().getTime() / 1000);
var uptime = now - startup_time;

stream.write("uptime: " + uptime + "\n");

for (group in stats) {
for (metric in stats[group]) {
var val;

if (metric.match("^last_")) {
val = now - stats[group][metric];
}
else {
val = stats[group][metric];
}

stream.write(group + "." + metric + ": " + val + "\n");
}
}
stream.write("END\n\n");
break;

case "counters":
stream.write(sys.inspect(counters) + "\n");
stream.write("END\n\n");
break;

case "timers":
stream.write(sys.inspect(timers) + "\n");
stream.write("END\n\n");
break;

case "quit":
stream.end();
break;

default:
stream.write("ERROR\n");
break;
}

});
});

server.bind(config.port || 8125);
mgmt_server.listen(config.mgmt_port || 8126);

var flushInterval = Number(config.flushInterval || 10000);

Expand Down Expand Up @@ -129,11 +201,13 @@ config.configFile(process.argv[2], function (config, oldConfig) {
graphite.on('connect', function() {
this.write(statString);
this.end();
stats['graphite']['last_flush'] = Math.round(new Date().getTime() / 1000);
});
} catch(e){
if (config.debug) {
sys.log(e);
}
stats['graphite']['last_execption'] = Math.round(new Date().getTime() / 1000);
}

}, flushInterval);
Expand Down

0 comments on commit b7b5c17

Please sign in to comment.