forked from loggly/collectd-to-graphite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
collectd-graphite-proxy.js
103 lines (87 loc) · 2.64 KB
/
collectd-graphite-proxy.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/* TODO(sissel): make connections retry/etc
* TODO(sissel): make graphite target configurable via command line
*
* This code is a work in progress.
*
* To use this, put the following in your collectd config:
*
* LoadPlugin write_http
* <Plugin write_http>
* <URL "http://monitor:3012/post-collectd">
* </URL>
* </Plugin>
*
* This will make collectd write 'PUTVAL' statements over HTTP to the above URL.
* This code below will then convert the PUTVAL statements to graphite metrics
* and ship them to 'monitor:2003'
*/
var http = require("http");
var net = require("net");
var assert = require("assert");
var fs = require('fs');
var types = fs.readFileSync('./types.db', encoding='utf8').split("\n");
var typesObj = new Object;
var type_comments_re = /^#/;
var type_cut_re = /^([^\s]+)\s+(.*)/;
for (var i in types) {
if (!type_comments_re.exec(types[i])) {
typeSet = type_cut_re.exec(types[i])
if (!typeSet) { continue; }
for (var t=0;t < typeSet.length;t++) {
var name = typeSet[1];
typesObj[name] = new Array();
var eachType = typeSet[2].split(", ")
for (var u=0; u < eachType.length; u++){
var theName = eachType[u].split(":")[0];
typesObj[name].push(theName);
}
}
}
}
try {
var graphite_connection = net.createConnection(2003, host=process.argv[2]);
} catch (error) {
throw error;
}
graphite_connection.on("close", function() {
throw new Error("Connection closed");
});
graphite_connection.on("error", function() {
throw new Error("Connection error");
});
var request_handler = function(request, response) {
var putval_re = /^PUTVAL ([^ ]+)(?: ([^ ]+=[^ ]+)?) ([0-9.]+)(:.*)/;
request.addListener("data", function(chunk) {
metrics = chunk.toString().split("\n");
for (var i in metrics) {
var m = putval_re.exec(metrics[i]);
if (!m) {
continue;
}
var values = m[4].split(":");
for (var v in values) {
var name = m[1];
var options = m[2];
var time = m[3];
if ( v == 0 ) {
continue;
}
name = "agents." + name.replace(/\./g, "_").replace(/\//g, ".");
if ( values.length > 2 ) {
var metric = name.split(".")[3];
name = name + "_" + typesObj[metric][v - 1];
}
message = [name, values[v], time].join(" ");
graphite_connection.write(message + "\n");
}
}
});
request.addListener("end", function() {
response.writeHead(200, {"Content-Type": "text/plain"});
response.write("OK");
response.end();
});
}
var server = http.createServer()
server.addListener("request", request_handler)
server.listen(3012);