Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #3 from tabletcorry/master

Fix collectd metric processing
  • Loading branch information...
commit 4e5188c81e9c423a1ed6b113faa2fabcb6bf5343 2 parents ca35567 + e64ccd1
@jordansissel jordansissel authored
Showing with 46 additions and 5 deletions.
  1. +46 −5 collectd-graphite-proxy.js
View
51 collectd-graphite-proxy.js
@@ -20,7 +20,7 @@ var net = require("net");
var assert = require("assert");
var fs = require('fs');
-var types = fs.readFileSync('./types.db', encoding='utf8').split("\n");
+var types = fs.readFileSync('/usr/share/collectd/types.db', encoding='utf8').split("\n");
var typesObj = new Object;
@@ -44,7 +44,6 @@ for (var i in types) {
}
-
try {
var graphite_connection = net.createConnection(2003, host=process.argv[2]);
} catch (error) {
@@ -78,10 +77,52 @@ var request_handler = function(request, response) {
continue;
}
- name = "agents." + name.replace(/\./g, "_").replace(/\//g, ".");
+ // Replace some chars for graphite, split into parts
+ var name_parts = name.replace(/\./g, "_").replace(/\//g, ".").split(".");
+
+ // Start to construct the new name
+ var rebuild = ["agents"]
+
+ var host = name_parts[0].split(/_/)[0]
+ rebuild = rebuild.concat(host)
+
+ // Pluigin names can contain an "instance" which is set apart by a dash
+ var plugin = name_parts[1].split("-")
+ rebuild = rebuild.concat(plugin[0])
+ if (plugin.length > 1) {
+ var plugin_instance = plugin.slice(1).join("-")
+ rebuild = rebuild.concat(plugin_instance)
+ }
+ plugin = plugin[0]
+
+ // Type names can also contain an "instance"
+ var type = name_parts[2].split("-")
+ if (type[0] != plugin) {
+ // If type and plugin are equal, delete one to clean up a bit
+ rebuild = rebuild.concat(type[0])
+ }
+ if (type.length > 1) {
+ var type_instance = type.slice(1).join("-")
+ rebuild = rebuild.concat(type_instance)
+ }
+ type = type[0]
+
+ // Put the name back together
+ name = rebuild.join(".")
+
if ( values.length > 2 ) {
- var metric = name.split(".")[3];
- name = name + "_" + typesObj[metric][v - 1];
+ var metric = name_parts[2];
+
+ // If the metric contains a '-' (after removing the instance name)
+ // then we want to remove it before looking up in the types.db
+ index = metric.search(/-/)
+ if (index > -1) {
+ metric = /^([\w]+)-(.*)$/.exec(metric);
+ } else {
+ // Kinda a hack
+ metric = [ "", metric]
+ }
+ name = name + "." + typesObj[metric[1]][v - 1];
}
message = [name, values[v], time].join(" ");
graphite_connection.write(message + "\n");
Please sign in to comment.
Something went wrong with that request. Please try again.