Created
October 4, 2011 06:56
-
-
Save benwtr/1261045 to your computer and use it in GitHub Desktop.
ghetto carbon/graphite amqp proxy in node.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Listens on localhost port 2003 for graphite metrics and publish them to carbon via amqp | |
var sys = require('sys') | |
, net = require('net') | |
, amqp = require('amqp') | |
var payload = ""; | |
var server = net.createServer(function (s) { | |
s.on('data', function(data) { | |
d = data.toString().replace(/(\n|\r)+$/, ''); | |
if (d.match(/^[a-zA-Z\._]+ \d*\.?\d* \d+\s*$/)) { | |
payload += d + "\n"; | |
} else if (d.match(/^[a-zA-Z\._]+ \d*\.?\d*\s*$/)) { | |
var datetime = new Date(); | |
payload += d + " " + datetime.getTime() + "\n"; | |
} else { | |
sys.puts("bad metric line received"); | |
} | |
}) | |
}); | |
server.listen(2003, 'localhost'); | |
connection = amqp.createConnection({url:'amqp://guest:guest@amqpserver:5672/'}); | |
connection.on('ready', function() { | |
connection.exchange('graphite', { passive: 'true' }, function(exchange) { | |
setInterval(function() { | |
if (payload != "") { | |
//assuming carbon is configured with AMQP_METRIC_NAME_IN_BODY = True | |
exchange.publish("", payload); | |
payload = ""; | |
} | |
}, 2000); // How often to flush | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment