forked from loggly/collectd-to-graphite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
json-to-graphite.js
54 lines (47 loc) · 1.48 KB
/
json-to-graphite.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
var net = require("net");
function to_dotted_notation(obj, parent_key, result) {
if (typeof(parent_key) == 'undefined') parent_key = "";
if (typeof(result) == 'undefined') result = {};
if (typeof(obj) == 'object') {
for (var i in obj) {
var key = parent_key ? (parent_key + "." + i) : i;
to_dotted_notation(obj[i], key, result);
}
} else if (typeof(obj) == 'number') {
result[parent_key] = obj;
}
return result;
}
var stdin = process.openStdin();
var input = "";
stdin.on("data", function(chunk) {
input += chunk;
});
stdin.on("end", function() {
data = JSON.parse(input);
results = to_dotted_notation(data);
/* TODO(sissel): validate args */
var address = process.argv[2].split(":");
address.push(2003); /* default port */
var host = address[0];
var port = address[1];
/* Only fetch matching keys */
var args = process.argv.slice(3); /* argv[0] == 'node', argv[1] is script name */
/* Create a regexp of (arg)|(arg)|(arg)... */
var pattern = args.map(function(arg) { return "(" + arg + ")" }).join("|");
var re = new RegExp(pattern);
var now = Math.floor((new Date()).getTime() / 1000);
var messages = []
for (var key in results) {
if (re.test(key)) {
messages.push([key, results[key], now].join(" "));
}
}
var graphite = net.createConnection(port, host);
graphite.on('connect', function() {
for (var i in messages) {
graphite.write(messages[i].toLowerCase() + "\n");
}
graphite.end();
});
});