Converts . to _ in datasource names
[collectd.git] / src / write_graphite.c
index 8090c8b..2d80dbb 100644 (file)
@@ -24,7 +24,7 @@
  /* write_graphite plugin configuation example
   *
   * <Plugin write_graphite>
-  *   <Carbon "local-agent">
+  *   <Carbon>
   *     Host "localhost"
   *     Port 2003
   *     Prefix "collectd"
 
 #ifndef WG_FORMAT_NAME
 #define WG_FORMAT_NAME(ret, ret_len, vl, prefix, name) \
-        wg_format_name (ret, ret_len, (vl)->host, (vl)->plugin, (vl)->plugin_instance, \
-                        (vl)->type, (vl)->type_instance, prefix, name)
+        wg_format_name (ret, ret_len, (vl)->host, (vl)->plugin, \
+                         (vl)->plugin_instance, (vl)->type, \
+                         (vl)->type_instance, prefix, name)
+#endif
+
+#ifndef WG_SEND_BUF_SIZE
+#define WG_SEND_BUF_SIZE 4096
 #endif
 
 /*
@@ -61,8 +66,6 @@
  */
 struct wg_callback
 {
-    char    *name;
-
     int      sock_fd;
     struct hostent *server;
 
@@ -70,7 +73,7 @@ struct wg_callback
     int      port;
     char    *prefix;
 
-    char     send_buf[4096];
+    char     send_buf[WG_SEND_BUF_SIZE];
     size_t   send_buf_free;
     size_t   send_buf_fill;
     cdtime_t send_buf_init_time;
@@ -171,11 +174,13 @@ static int wg_callback_init (struct wg_callback *cb)
     memset (&serv_addr, 0, sizeof (serv_addr));
     serv_addr.sin_family = AF_INET;
     memcpy (&serv_addr.sin_addr.s_addr,
-                cb->server->h_addr,
-                cb->server->h_length);
+             cb->server->h_addr,
+             cb->server->h_length);
     serv_addr.sin_port = htons(cb->port);
 
-    status = connect(cb->sock_fd, (struct sockaddr *) &serv_addr, sizeof(serv_addr));
+    status = connect(cb->sock_fd,
+                      (struct sockaddr *) &serv_addr,
+                      sizeof(serv_addr));
     if (status < 0)
     {
         char errbuf[1024];
@@ -203,7 +208,6 @@ static void wg_callback_free (void *data)
     wg_flush_nolock (/* timeout = */ 0, cb);
 
     close(cb->sock_fd);
-    sfree(cb->name);
     sfree(cb->host);
     sfree(cb->prefix);
 
@@ -304,7 +308,7 @@ static int wg_format_values (char *ret, size_t ret_len,
     return (0);
 }
 
-static int normalize_hostname (char *dst, const char *src)
+static int mangle_dots (char *dst, const char *src)
 {
     size_t i;
 
@@ -326,13 +330,14 @@ static int normalize_hostname (char *dst, const char *src)
 }
 
 static int wg_format_name (char *ret, int ret_len,
-                const char *hostname,
-                const char *plugin, const char *plugin_instance,
-                const char *type, const char *type_instance,
-                const char *prefix, const char *ds_name)
+        const char *hostname,
+        const char *plugin, const char *plugin_instance,
+        const char *type, const char *type_instance,
+        const char *prefix, const char *ds_name)
 {
     int  status;
-    char *n_hostname;
+    char *n_hostname = 0;
+    char *n_ds_name = 0;
 
     assert (plugin != NULL);
     assert (type != NULL);
@@ -343,17 +348,25 @@ static int wg_format_name (char *ret, int ret_len,
         return (-1);
     }
 
-    if (normalize_hostname(n_hostname, hostname) == -1)
+    if (mangle_dots(n_hostname, hostname) == -1)
     {
         ERROR ("Unable to normalize hostname");
         return (-1);
     }
 
-    if ((plugin_instance == NULL) || (strlen (plugin_instance) == 0))
+    if (ds_name && ds_name[0] != '\0') {
+        if (mangle_dots(n_ds_name, ds_name) == -1)
+        {
+            ERROR ("Unable to normalize datasource name");
+            return (-1);
+        }
+    }
+
+    if ((plugin_instance == NULL) || (plugin_instance[0] == '\0'))
     {
-        if ((type_instance == NULL) || (strlen (type_instance) == 0))
+        if ((type_instance == NULL) || (type_instance[0] == '\0'))
         {
-            if ((ds_name == NULL) || (strlen (ds_name) == 0))
+            if ((ds_name == NULL) || (ds_name[0] == '\0'))
                 status = ssnprintf (ret, ret_len, "%s.%s.%s.%s",
                         prefix, n_hostname, plugin, type);
             else
@@ -362,7 +375,7 @@ static int wg_format_name (char *ret, int ret_len,
         }
         else
         {
-            if ((ds_name == NULL) || (strlen (ds_name) == 0))
+            if ((ds_name == NULL) || (ds_name[0] == '\0'))
                 status = ssnprintf (ret, ret_len, "%s.%s.%s.%s-%s",
                         prefix, n_hostname, plugin, type,
                         type_instance);
@@ -374,9 +387,9 @@ static int wg_format_name (char *ret, int ret_len,
     }
     else
     {
-        if ((type_instance == NULL) || (strlen (type_instance) == 0))
+        if ((type_instance == NULL) || (type_instance[0] == '\0'))
         {
-            if ((ds_name == NULL) || (strlen (ds_name) == 0))
+            if ((ds_name == NULL) || (ds_name[0] == '\0'))
                 status = ssnprintf (ret, ret_len, "%s.%s.%s.%s.%s",
                         prefix, n_hostname, plugin,
                         plugin_instance, type);
@@ -387,7 +400,7 @@ static int wg_format_name (char *ret, int ret_len,
         }
         else
         {
-            if ((ds_name == NULL) || (strlen (ds_name) == 0))
+            if ((ds_name == NULL) || (ds_name[0] == '\0'))
                 status = ssnprintf (ret, ret_len, "%s.%s.%s.%s.%s-%s",
                         prefix, n_hostname, plugin,
                         plugin_instance, type, type_instance);
@@ -405,7 +418,8 @@ static int wg_format_name (char *ret, int ret_len,
     return (0);
 }
 
-static int wg_send_message (const char* key, const char* value, cdtime_t time, struct wg_callback *cb)
+static int wg_send_message (const char* key, const char* value,
+        cdtime_t time, struct wg_callback *cb)
 {
     int status;
     size_t message_len;
@@ -468,7 +482,7 @@ static int wg_send_message (const char* key, const char* value, cdtime_t time, s
 }
 
 static int wg_write_messages (const data_set_t *ds, const value_list_t *vl,
-                        struct wg_callback *cb)
+        struct wg_callback *cb)
 {
     char key[10*DATA_MAX_NAME_LEN];
     char values[512];
@@ -487,7 +501,8 @@ static int wg_write_messages (const data_set_t *ds, const value_list_t *vl,
         for (i = 0; i < ds->ds_num; i++)
         {
             /* Copy the identifier to `key' and escape it. */
-            status = WG_FORMAT_NAME (key, sizeof (key), vl, cb->prefix, ds->ds[i].name);
+            status = WG_FORMAT_NAME (key, sizeof (key), vl, cb->prefix, \
+                    ds->ds[i].name);
             if (status != 0)
             {
                 ERROR ("write_graphite plugin: error with format_name");
@@ -495,8 +510,8 @@ static int wg_write_messages (const data_set_t *ds, const value_list_t *vl,
             }
 
             escape_string (key, sizeof (key));
-            /* Convert the values to an ASCII representation and put that into
-             * `values'. */
+            /* Convert the values to an ASCII representation and put that
+             * into `values'. */
             status = wg_format_values (values, sizeof (values), i, ds, vl, 0);
             if (status != 0)
             {
@@ -622,17 +637,12 @@ static int wg_config_carbon (oconfig_item_t *ci)
     memset (cb, 0, sizeof (*cb));
     cb->sock_fd = -1;
     cb->host = NULL;
-    cb->name = NULL;
     cb->port = 2003;
     cb->prefix = NULL;
     cb->server = NULL;
 
     pthread_mutex_init (&cb->send_lock, /* attr = */ NULL);
 
-    config_set_string (&cb->name, ci);
-    if (cb->name == NULL)
-        return (-1);
-
     for (i = 0; i < ci->children_num; i++)
     {
         oconfig_item_t *child = ci->children + i;
@@ -677,7 +687,7 @@ static int wg_config (oconfig_item_t *ci)
         else
         {
             ERROR ("write_graphite plugin: Invalid configuration "
-                        "option: %s.", child->key);
+                    "option: %s.", child->key);
         }
     }