2010-12-30 03:15:00 +03:00
|
|
|
var dgram = require('dgram')
|
2012-01-20 21:45:48 +04:00
|
|
|
, util = require('util')
|
2010-12-30 03:15:00 +03:00
|
|
|
, net = require('net')
|
2012-10-19 04:43:55 +04:00
|
|
|
, config = require('./lib/config')
|
2012-02-09 23:40:41 +04:00
|
|
|
, fs = require('fs')
|
2012-04-04 19:19:24 +04:00
|
|
|
, events = require('events')
|
2012-07-11 20:32:52 +04:00
|
|
|
, logger = require('./lib/logger')
|
2012-08-09 02:05:06 +04:00
|
|
|
, set = require('./lib/set')
|
2012-10-13 10:01:03 +04:00
|
|
|
, pm = require('./lib/process_metrics')
|
2010-12-30 03:15:00 +03:00
|
|
|
|
|
|
|
|
2012-07-08 23:11:23 +04:00
|
|
|
// initialize data structures with defaults for statsd stats
|
2012-02-09 23:40:41 +04:00
|
|
|
var keyCounter = {};
|
2012-07-08 23:11:23 +04:00
|
|
|
var counters = {
|
|
|
|
"statsd.packets_received": 0,
|
|
|
|
"statsd.bad_lines_seen": 0
|
|
|
|
};
|
2012-10-13 08:25:44 +04:00
|
|
|
var timers = {};
|
2012-02-19 02:18:25 +04:00
|
|
|
var gauges = {};
|
2012-10-13 08:25:44 +04:00
|
|
|
var sets = {};
|
2012-10-13 08:33:05 +04:00
|
|
|
var counter_rates = {};
|
|
|
|
var timer_data = {};
|
2012-03-30 00:57:30 +04:00
|
|
|
var pctThreshold = null;
|
2012-03-31 01:02:39 +04:00
|
|
|
var debugInt, flushInterval, keyFlushInt, server, mgmtServer;
|
2011-10-11 19:34:14 +04:00
|
|
|
var startup_time = Math.round(new Date().getTime() / 1000);
|
2012-04-04 19:19:24 +04:00
|
|
|
var backendEvents = new events.EventEmitter();
|
2011-10-11 19:34:14 +04:00
|
|
|
|
2012-03-31 01:02:39 +04:00
|
|
|
// Load and init the backend from the backends/ directory.
|
2012-04-03 23:49:02 +04:00
|
|
|
function loadBackend(config, name) {
|
2012-05-15 07:07:58 +04:00
|
|
|
var backendmod = require(name);
|
2012-03-31 01:02:39 +04:00
|
|
|
|
|
|
|
if (config.debug) {
|
2012-07-11 22:02:45 +04:00
|
|
|
l.log("Loading backend: " + name, 'debug');
|
2012-03-31 01:02:39 +04:00
|
|
|
}
|
|
|
|
|
2012-04-04 19:19:24 +04:00
|
|
|
var ret = backendmod.init(startup_time, config, backendEvents);
|
2012-04-03 20:26:48 +04:00
|
|
|
if (!ret) {
|
2012-07-11 20:32:52 +04:00
|
|
|
l.log("Failed to load backend: " + name);
|
2012-03-31 01:02:39 +04:00
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-11-13 20:59:37 +04:00
|
|
|
// global for conf
|
|
|
|
var conf;
|
|
|
|
|
2012-03-31 01:02:39 +04:00
|
|
|
// Flush metrics to each backend.
|
2012-04-03 23:49:02 +04:00
|
|
|
function flushMetrics() {
|
2012-04-04 19:19:24 +04:00
|
|
|
var time_stamp = Math.round(new Date().getTime() / 1000);
|
2012-03-30 00:57:30 +04:00
|
|
|
|
2012-04-04 19:19:24 +04:00
|
|
|
var metrics_hash = {
|
2012-03-30 00:57:30 +04:00
|
|
|
counters: counters,
|
|
|
|
gauges: gauges,
|
|
|
|
timers: timers,
|
2012-08-09 02:05:06 +04:00
|
|
|
sets: sets,
|
2012-10-13 08:33:05 +04:00
|
|
|
counter_rates: counter_rates,
|
|
|
|
timer_data: timer_data,
|
2012-03-30 00:57:30 +04:00
|
|
|
pctThreshold: pctThreshold
|
|
|
|
}
|
|
|
|
|
2012-04-04 19:19:24 +04:00
|
|
|
// After all listeners, reset the stats
|
|
|
|
backendEvents.once('flush', function clear_metrics(ts, metrics) {
|
|
|
|
// Clear the counters
|
2012-11-13 20:59:37 +04:00
|
|
|
conf.deleteCounters = conf.deleteCounters || false;
|
2012-04-04 19:19:24 +04:00
|
|
|
for (key in metrics.counters) {
|
2012-11-13 20:59:37 +04:00
|
|
|
if (conf.deleteCounters) {
|
2012-11-26 23:56:29 +04:00
|
|
|
delete(metrics.counters[key]);
|
2012-11-01 22:59:12 +04:00
|
|
|
} else {
|
|
|
|
metrics.counters[key] = 0;
|
|
|
|
}
|
2012-04-04 19:19:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Clear the timers
|
|
|
|
for (key in metrics.timers) {
|
|
|
|
metrics.timers[key] = [];
|
|
|
|
}
|
2012-08-09 02:05:06 +04:00
|
|
|
|
|
|
|
// Clear the sets
|
|
|
|
for (key in metrics.sets) {
|
|
|
|
metrics.sets[key] = new set.Set();
|
|
|
|
}
|
2012-04-04 19:19:24 +04:00
|
|
|
});
|
|
|
|
|
2012-11-02 06:44:39 +04:00
|
|
|
pm.process_metrics(metrics_hash, flushInterval, time_stamp, function emitFlush(metrics) {
|
2012-11-02 04:32:45 +04:00
|
|
|
backendEvents.emit('flush', time_stamp, metrics);
|
2012-10-15 08:20:56 +04:00
|
|
|
});
|
2012-10-13 08:33:05 +04:00
|
|
|
|
2012-03-30 00:57:30 +04:00
|
|
|
};
|
|
|
|
|
2011-10-11 19:34:14 +04:00
|
|
|
var stats = {
|
|
|
|
messages: {
|
|
|
|
last_msg_seen: startup_time,
|
2012-11-02 06:44:39 +04:00
|
|
|
bad_lines_seen: 0
|
2011-10-11 19:34:14 +04:00
|
|
|
}
|
|
|
|
};
|
2010-12-30 03:15:00 +03:00
|
|
|
|
2012-07-11 20:32:52 +04:00
|
|
|
// Global for the logger
|
|
|
|
var l;
|
|
|
|
|
2010-12-30 03:15:00 +03:00
|
|
|
config.configFile(process.argv[2], function (config, oldConfig) {
|
2012-11-13 20:59:37 +04:00
|
|
|
conf = config;
|
2010-12-30 03:15:00 +03:00
|
|
|
if (! config.debug && debugInt) {
|
2012-02-19 02:18:25 +04:00
|
|
|
clearInterval(debugInt);
|
2010-12-30 03:15:00 +03:00
|
|
|
debugInt = false;
|
|
|
|
}
|
|
|
|
|
2012-07-11 20:32:52 +04:00
|
|
|
l = new logger.Logger(config.log || {});
|
|
|
|
|
2010-12-30 03:15:00 +03:00
|
|
|
if (config.debug) {
|
2012-07-11 22:02:45 +04:00
|
|
|
if (debugInt !== undefined) {
|
|
|
|
clearInterval(debugInt);
|
|
|
|
}
|
2012-02-19 02:18:25 +04:00
|
|
|
debugInt = setInterval(function () {
|
2012-07-11 20:32:52 +04:00
|
|
|
l.log("Counters:\n" + util.inspect(counters) +
|
2012-02-19 02:18:25 +04:00
|
|
|
"\nTimers:\n" + util.inspect(timers) +
|
2012-07-11 22:02:45 +04:00
|
|
|
"\nGauges:\n" + util.inspect(gauges), 'debug');
|
2010-12-30 03:15:00 +03:00
|
|
|
}, config.debugInterval || 10000);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (server === undefined) {
|
2012-02-09 23:40:41 +04:00
|
|
|
|
|
|
|
// key counting
|
|
|
|
var keyFlushInterval = Number((config.keyFlush && config.keyFlush.interval) || 0);
|
|
|
|
|
2010-12-30 03:15:00 +03:00
|
|
|
server = dgram.createSocket('udp4', function (msg, rinfo) {
|
2012-09-07 22:05:59 +04:00
|
|
|
backendEvents.emit('packet', msg, rinfo);
|
2012-07-08 23:11:23 +04:00
|
|
|
counters["statsd.packets_received"]++;
|
2012-06-04 21:14:05 +04:00
|
|
|
var metrics = msg.toString().split("\n");
|
|
|
|
|
|
|
|
for (midx in metrics) {
|
2012-07-11 20:32:52 +04:00
|
|
|
if (config.dumpMessages) {
|
|
|
|
l.log(metrics[midx].toString());
|
|
|
|
}
|
2012-06-04 21:14:05 +04:00
|
|
|
var bits = metrics[midx].toString().split(':');
|
2012-06-04 21:15:33 +04:00
|
|
|
var key = bits.shift()
|
|
|
|
.replace(/\s+/g, '_')
|
|
|
|
.replace(/\//g, '-')
|
|
|
|
.replace(/[^a-zA-Z_\-0-9\.]/g, '');
|
|
|
|
|
|
|
|
if (keyFlushInterval > 0) {
|
|
|
|
if (! keyCounter[key]) {
|
|
|
|
keyCounter[key] = 0;
|
|
|
|
}
|
|
|
|
keyCounter[key] += 1;
|
2012-02-09 23:40:41 +04:00
|
|
|
}
|
2010-12-30 03:15:00 +03:00
|
|
|
|
2012-06-04 21:15:33 +04:00
|
|
|
if (bits.length == 0) {
|
|
|
|
bits.push("1");
|
2011-03-07 21:35:20 +03:00
|
|
|
}
|
2012-06-04 21:15:33 +04:00
|
|
|
|
|
|
|
for (var i = 0; i < bits.length; i++) {
|
|
|
|
var sampleRate = 1;
|
|
|
|
var fields = bits[i].split("|");
|
|
|
|
if (fields[1] === undefined) {
|
2012-08-18 08:24:00 +04:00
|
|
|
l.log('Bad line: ' + fields + ' in msg "' + metrics[midx] +'"');
|
2012-07-08 23:11:23 +04:00
|
|
|
counters["statsd.bad_lines_seen"]++;
|
2012-06-04 21:15:33 +04:00
|
|
|
stats['messages']['bad_lines_seen']++;
|
|
|
|
continue;
|
2010-12-30 03:15:00 +03:00
|
|
|
}
|
2012-06-04 21:15:33 +04:00
|
|
|
if (fields[1].trim() == "ms") {
|
|
|
|
if (! timers[key]) {
|
|
|
|
timers[key] = [];
|
|
|
|
}
|
|
|
|
timers[key].push(Number(fields[0] || 0));
|
|
|
|
} else if (fields[1].trim() == "g") {
|
|
|
|
gauges[key] = Number(fields[0] || 0);
|
2012-08-09 02:05:06 +04:00
|
|
|
} else if (fields[1].trim() == "s") {
|
|
|
|
if (! sets[key]) {
|
|
|
|
sets[key] = new set.Set();
|
|
|
|
}
|
|
|
|
sets[key].insert(fields[0] || '0');
|
2012-06-04 21:15:33 +04:00
|
|
|
} else {
|
2012-08-28 00:52:52 +04:00
|
|
|
if (fields[2]) {
|
|
|
|
if (fields[2].match(/^@([\d\.]+)/)) {
|
|
|
|
sampleRate = Number(fields[2].match(/^@([\d\.]+)/)[1]);
|
|
|
|
} else {
|
|
|
|
l.log('Bad line: ' + fields + ' in msg "' + metrics[midx] +'"; has invalid sample rate');
|
|
|
|
counters["statsd.bad_lines_seen"]++;
|
|
|
|
stats['messages']['bad_lines_seen']++;
|
|
|
|
continue;
|
|
|
|
}
|
2012-06-04 21:15:33 +04:00
|
|
|
}
|
|
|
|
if (! counters[key]) {
|
|
|
|
counters[key] = 0;
|
|
|
|
}
|
|
|
|
counters[key] += Number(fields[0] || 1) * (1 / sampleRate);
|
2010-12-30 03:15:00 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-10-11 19:34:14 +04:00
|
|
|
|
|
|
|
stats['messages']['last_msg_seen'] = Math.round(new Date().getTime() / 1000);
|
|
|
|
});
|
|
|
|
|
2011-10-11 20:21:04 +04:00
|
|
|
mgmtServer = net.createServer(function(stream) {
|
2011-10-11 19:34:14 +04:00
|
|
|
stream.setEncoding('ascii');
|
|
|
|
|
|
|
|
stream.on('data', function(data) {
|
2012-02-08 19:19:24 +04:00
|
|
|
var cmdline = data.trim().split(" ");
|
|
|
|
var cmd = cmdline.shift();
|
2011-10-11 19:34:14 +04:00
|
|
|
|
|
|
|
switch(cmd) {
|
|
|
|
case "help":
|
2012-02-19 02:18:25 +04:00
|
|
|
stream.write("Commands: stats, counters, timers, gauges, delcounters, deltimers, delgauges, quit\n\n");
|
2011-10-11 19:34:14 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case "stats":
|
|
|
|
var now = Math.round(new Date().getTime() / 1000);
|
|
|
|
var uptime = now - startup_time;
|
|
|
|
|
|
|
|
stream.write("uptime: " + uptime + "\n");
|
|
|
|
|
2012-03-30 00:57:30 +04:00
|
|
|
var stat_writer = function(group, metric, val) {
|
|
|
|
var delta;
|
|
|
|
|
|
|
|
if (metric.match("^last_")) {
|
|
|
|
delta = now - val;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
delta = val;
|
|
|
|
}
|
2011-10-11 19:34:14 +04:00
|
|
|
|
2012-03-30 00:57:30 +04:00
|
|
|
stream.write(group + "." + metric + ": " + delta + "\n");
|
|
|
|
};
|
2011-10-11 19:34:14 +04:00
|
|
|
|
2012-03-30 00:57:30 +04:00
|
|
|
// Loop through the base stats
|
|
|
|
for (group in stats) {
|
|
|
|
for (metric in stats[group]) {
|
|
|
|
stat_writer(group, metric, stats[group][metric]);
|
2011-10-11 19:34:14 +04:00
|
|
|
}
|
|
|
|
}
|
2012-03-30 00:57:30 +04:00
|
|
|
|
2012-04-04 19:19:24 +04:00
|
|
|
backendEvents.once('status', function(writeCb) {
|
|
|
|
stream.write("END\n\n");
|
|
|
|
});
|
|
|
|
|
|
|
|
// Let each backend contribute its status
|
|
|
|
backendEvents.emit('status', function(err, name, stat, val) {
|
|
|
|
if (err) {
|
2012-07-11 20:32:52 +04:00
|
|
|
l.log("Failed to read stats for backend " +
|
2012-04-04 19:19:24 +04:00
|
|
|
name + ": " + err);
|
|
|
|
} else {
|
|
|
|
stat_writer(name, stat, val);
|
|
|
|
}
|
|
|
|
});
|
2012-03-30 00:57:30 +04:00
|
|
|
|
2011-10-11 19:34:14 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case "counters":
|
2012-01-20 21:45:48 +04:00
|
|
|
stream.write(util.inspect(counters) + "\n");
|
2011-10-11 19:34:14 +04:00
|
|
|
stream.write("END\n\n");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "timers":
|
2012-01-20 21:45:48 +04:00
|
|
|
stream.write(util.inspect(timers) + "\n");
|
2011-10-11 19:34:14 +04:00
|
|
|
stream.write("END\n\n");
|
|
|
|
break;
|
|
|
|
|
2012-02-19 02:18:25 +04:00
|
|
|
case "gauges":
|
|
|
|
stream.write(util.inspect(gauges) + "\n");
|
|
|
|
stream.write("END\n\n");
|
|
|
|
break;
|
|
|
|
|
2012-02-08 19:19:24 +04:00
|
|
|
case "delcounters":
|
|
|
|
for (index in cmdline) {
|
|
|
|
delete counters[cmdline[index]];
|
|
|
|
stream.write("deleted: " + cmdline[index] + "\n");
|
|
|
|
}
|
|
|
|
stream.write("END\n\n");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "deltimers":
|
|
|
|
for (index in cmdline) {
|
|
|
|
delete timers[cmdline[index]];
|
|
|
|
stream.write("deleted: " + cmdline[index] + "\n");
|
|
|
|
}
|
|
|
|
stream.write("END\n\n");
|
|
|
|
break;
|
|
|
|
|
2012-02-19 02:18:25 +04:00
|
|
|
case "delgauges":
|
|
|
|
for (index in cmdline) {
|
|
|
|
delete gauges[cmdline[index]];
|
|
|
|
stream.write("deleted: " + cmdline[index] + "\n");
|
|
|
|
}
|
|
|
|
stream.write("END\n\n");
|
|
|
|
break;
|
|
|
|
|
2011-10-11 19:34:14 +04:00
|
|
|
case "quit":
|
|
|
|
stream.end();
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
stream.write("ERROR\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
2010-12-30 03:15:00 +03:00
|
|
|
});
|
|
|
|
|
2011-10-29 02:42:36 +04:00
|
|
|
server.bind(config.port || 8125, config.address || undefined);
|
|
|
|
mgmtServer.listen(config.mgmt_port || 8126, config.mgmt_address || undefined);
|
2010-12-30 03:15:00 +03:00
|
|
|
|
2012-01-20 21:45:48 +04:00
|
|
|
util.log("server is up");
|
2012-01-10 19:03:49 +04:00
|
|
|
|
2012-03-30 00:57:30 +04:00
|
|
|
pctThreshold = config.percentThreshold || 90;
|
2012-03-08 02:13:04 +04:00
|
|
|
if (!Array.isArray(pctThreshold)) {
|
|
|
|
pctThreshold = [ pctThreshold ]; // listify percentiles so single values work the same
|
|
|
|
}
|
|
|
|
|
2012-04-03 20:26:48 +04:00
|
|
|
flushInterval = Number(config.flushInterval || 10000);
|
|
|
|
config.flushInterval = flushInterval;
|
|
|
|
|
2012-03-30 00:57:30 +04:00
|
|
|
if (config.backends) {
|
|
|
|
for (var i = 0; i < config.backends.length; i++) {
|
|
|
|
loadBackend(config, config.backends[i]);
|
2010-12-30 03:15:00 +03:00
|
|
|
}
|
2012-03-30 00:57:30 +04:00
|
|
|
} else {
|
|
|
|
// The default backend is graphite
|
2012-05-15 07:07:58 +04:00
|
|
|
loadBackend(config, './backends/graphite');
|
2012-03-30 00:57:30 +04:00
|
|
|
}
|
2010-12-30 03:15:00 +03:00
|
|
|
|
2012-03-31 01:02:39 +04:00
|
|
|
// Setup the flush timer
|
|
|
|
var flushInt = setInterval(flushMetrics, flushInterval);
|
|
|
|
|
2012-02-09 23:40:41 +04:00
|
|
|
if (keyFlushInterval > 0) {
|
|
|
|
var keyFlushPercent = Number((config.keyFlush && config.keyFlush.percent) || 100);
|
2012-10-10 18:05:14 +04:00
|
|
|
var keyFlushLog = config.keyFlush && config.keyFlush.log;
|
2012-02-09 23:40:41 +04:00
|
|
|
|
|
|
|
keyFlushInt = setInterval(function () {
|
|
|
|
var key;
|
|
|
|
var sortedKeys = [];
|
|
|
|
|
|
|
|
for (key in keyCounter) {
|
|
|
|
sortedKeys.push([key, keyCounter[key]]);
|
|
|
|
}
|
|
|
|
|
|
|
|
sortedKeys.sort(function(a, b) { return b[1] - a[1]; });
|
|
|
|
|
|
|
|
var logMessage = "";
|
|
|
|
var timeString = (new Date()) + "";
|
|
|
|
|
|
|
|
// only show the top "keyFlushPercent" keys
|
|
|
|
for (var i = 0, e = sortedKeys.length * (keyFlushPercent / 100); i < e; i++) {
|
2012-05-15 23:06:44 +04:00
|
|
|
logMessage += timeString + " count=" + sortedKeys[i][1] + " key=" + sortedKeys[i][0] + "\n";
|
2012-02-09 23:40:41 +04:00
|
|
|
}
|
|
|
|
|
2012-10-10 18:05:14 +04:00
|
|
|
if (keyFlushLog) {
|
|
|
|
var logFile = fs.createWriteStream(keyFlushLog, {flags: 'a+'});
|
|
|
|
logFile.write(logMessage);
|
|
|
|
logFile.end();
|
|
|
|
} else {
|
|
|
|
process.stdout.write(logMessage);
|
|
|
|
}
|
2012-02-09 23:40:41 +04:00
|
|
|
|
|
|
|
// clear the counter
|
|
|
|
keyCounter = {};
|
|
|
|
}, keyFlushInterval);
|
|
|
|
}
|
|
|
|
|
2012-05-15 07:07:58 +04:00
|
|
|
|
2012-03-30 00:57:30 +04:00
|
|
|
;
|
2010-12-30 03:15:00 +03:00
|
|
|
|
2012-03-30 00:57:30 +04:00
|
|
|
}
|
|
|
|
})
|