Merge pull request #20 from pyr/ss/graphite
[collectd.git] / src / write_graphite.c
index fab8f05..3842e4b 100644 (file)
@@ -337,26 +337,19 @@ static int wg_format_values (char *ret, size_t ret_len,
     return (0);
 }
 
-static int swap_chars (char *dst, const char *src,
+static void swap_chars (char *dst, const char *src,
         const char from, const char to)
 {
     size_t i;
 
-    int reps = 0;
-
     for (i = 0; i < strlen(src) ; i++)
     {
         if (src[i] == from)
-        {
             dst[i] = to;
-            ++reps;
-        }
         else
             dst[i] = src[i];
     }
     dst[i] = '\0';
-
-    return reps;
 }
 
 static int wg_format_name (char *ret, int ret_len,
@@ -367,8 +360,11 @@ static int wg_format_name (char *ret, int ret_len,
         const char *ds_name, const char dotchar)
 {
     int  status;
-    char *n_hostname = 0;
-    char *n_type_instance = 0;
+    char *n_hostname = NULL;
+    char *n_plugin = NULL;
+    char *n_plugin_instance = NULL;
+    char *n_type = NULL;
+    char *n_type_instance = NULL;
 
     assert (plugin != NULL);
     assert (type != NULL);
@@ -385,45 +381,63 @@ static int wg_format_name (char *ret, int ret_len,
         return (-1);
     }
 
-    if (swap_chars(n_hostname, hostname, '.', dotchar) == -1)
+    if ((n_plugin = malloc(strlen(plugin)+1)) == NULL)
     {
-        ERROR ("Unable to normalize hostname");
+        ERROR ("Unable to allocate memory for normalized plugin buffer");
         return (-1);
     }
 
-    if (type_instance && type_instance[0] != '\0') {
+    if ((n_type = malloc(strlen(type)+1)) == NULL)
+    {
+        ERROR ("Unable to allocate memory for normalized type buffer");
+        return (-1);
+    }
+
+    swap_chars(n_hostname, hostname, '.', dotchar);
+    swap_chars(n_plugin, plugin, ' ', '_');
+    swap_chars(n_type, type, ' ', '_');
+
+    if (type_instance != NULL && type_instance[0] != '\0')
+    {
         if ((n_type_instance = malloc(strlen(type_instance)+1)) == NULL)
         {
             ERROR ("Unable to allocate memory for normalized datasource name buffer");
             return (-1);
         }
-        if (swap_chars(n_type_instance, type_instance, '.', dotchar) == -1)
+        swap_chars(n_type_instance, type_instance, '.', dotchar);
+        swap_chars(n_type_instance, type_instance, ' ', '_');
+    }
+
+    if (plugin_instance != NULL && plugin_instance[0] != '\0')
+    {
+        if ((n_plugin_instance = malloc(strlen(plugin_instance)+1)) == NULL)
         {
-            ERROR ("Unable to normalize datasource name");
+            ERROR ("Unable to allocate memory for normalized plugin instance buffer");
             return (-1);
         }
+        swap_chars(n_plugin_instance, plugin_instance, ' ', '_');
     }
 
-    if ((plugin_instance == NULL) || (plugin_instance[0] == '\0'))
+    if ((n_plugin_instance == NULL) || (n_plugin_instance[0] == '\0'))
     {
         if ((n_type_instance == NULL) || (n_type_instance[0] == '\0'))
         {
             if ((ds_name == NULL) || (ds_name[0] == '\0'))
                 status = ssnprintf (ret, ret_len, "%s%s%s.%s.%s",
-                        prefix, n_hostname, postfix, plugin, type);
+                        prefix, n_hostname, postfix, n_plugin, n_type);
             else
                 status = ssnprintf (ret, ret_len, "%s%s%s.%s.%s.%s",
-                        prefix, n_hostname, postfix, plugin, type, ds_name);
+                        prefix, n_hostname, postfix, n_plugin, n_type, ds_name);
         }
         else
         {
             if ((ds_name == NULL) || (ds_name[0] == '\0'))
                 status = ssnprintf (ret, ret_len, "%s%s%s.%s.%s-%s",
-                        prefix, n_hostname, postfix, plugin, type,
+                        prefix, n_hostname, postfix, n_plugin, n_type,
                         n_type_instance);
             else
                 status = ssnprintf (ret, ret_len, "%s%s%s.%s.%s-%s.%s",
-                        prefix, n_hostname, postfix, plugin, type,
+                        prefix, n_hostname, postfix, n_plugin, n_type,
                         n_type_instance, ds_name);
         }
     }
@@ -433,28 +447,31 @@ static int wg_format_name (char *ret, int ret_len,
         {
             if ((ds_name == NULL) || (ds_name[0] == '\0'))
                 status = ssnprintf (ret, ret_len, "%s%s%s.%s.%s.%s",
-                        prefix, n_hostname, postfix, plugin,
-                        plugin_instance, type);
+                        prefix, n_hostname, postfix, n_plugin,
+                        n_plugin_instance, n_type);
             else
                 status = ssnprintf (ret, ret_len, "%s%s%s.%s.%s.%s.%s",
-                        prefix, n_hostname, postfix, plugin,
-                        plugin_instance, type, ds_name);
+                        prefix, n_hostname, postfix, n_plugin,
+                        n_plugin_instance, n_type, ds_name);
         }
         else
         {
             if ((ds_name == NULL) || (ds_name[0] == '\0'))
                 status = ssnprintf (ret, ret_len, "%s%s%s.%s.%s.%s-%s",
-                        prefix, n_hostname, postfix, plugin,
-                        plugin_instance, type, n_type_instance);
+                        prefix, n_hostname, postfix, n_plugin,
+                        n_plugin_instance, n_type, n_type_instance);
             else
                 status = ssnprintf (ret, ret_len, "%s%s%s.%s.%s.%s-%s.%s",
-                        prefix, n_hostname, postfix, plugin,
-                        plugin_instance, type, n_type_instance, ds_name);
+                        prefix, n_hostname, postfix, n_plugin,
+                        n_plugin_instance, n_type, n_type_instance, ds_name);
         }
     }
 
     sfree(n_hostname);
     sfree(n_type_instance);
+    sfree(n_type);
+    sfree(n_plugin);
+    sfree(n_plugin_instance);
 
     if ((status < 1) || (status >= ret_len))
         return (-1);