diff --git a/src/write_graphite.c b/src/write_graphite.c
index 827ec3881263a749eeb68d89c50d7a357a185ac4..6c8a977f98770f0a6772c76c153faac667e55a2e 100644 (file)
--- a/src/write_graphite.c
+++ b/src/write_graphite.c
/**
* collectd - src/write_graphite.c
- * Copyright (C) 2011 Scott Sanders
+ * Copyright (C) 2012 Pierre-Yves Ritschard
+ * Copyright (C) 2011 Scott Sanders
+ * Copyright (C) 2009 Paul Sadauskas
+ * Copyright (C) 2009 Doug MacEachern
+ * Copyright (C) 2007-2012 Florian octo Forster
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
- * Author:
- * Scott Sanders <scott@jssjr.com>
+ * Authors:
+ * Florian octo Forster <octo at collectd.org>
+ * Doug MacEachern <dougm at hyperic.com>
+ * Paul Sadauskas <psadauskas at gmail.com>
+ * Scott Sanders <scott at jssjr.com>
+ * Pierre-Yves Ritschard <pyr at spootnik.org>
*
- * based on the excellent write_http plugin
+ * Based on the write_http plugin.
**/
/* write_graphite plugin configuation example
#include <pthread.h>
#include <sys/socket.h>
-#include <sys/stat.h>
-#include <sys/types.h>
-
-#include <netinet/in.h>
#include <netdb.h>
#ifndef WG_FORMAT_NAME
-#define WG_FORMAT_NAME(ret, ret_len, vl, cb, name) \
- wg_format_name (ret, ret_len, (vl)->host, (vl)->plugin, \
- (vl)->plugin_instance, (vl)->type, \
- (vl)->type_instance, (cb)->prefix, (cb)->postfix, \
- name, (cb)->dotchar)
+#define WG_FORMAT_NAME(ret, ret_len, vl, cb, ds_name) \
+ wg_format_name (ret, ret_len, (vl)->host, \
+ (vl)->plugin, (vl)->plugin_instance, \
+ (vl)->type, (vl)->type_instance, \
+ (cb)->prefix, (cb)->postfix, \
+ ds_name, (cb)->escape_char)
#endif
#ifndef WG_DEFAULT_NODE
# define WG_DEFAULT_SERVICE "2003"
#endif
+#ifndef WG_DEFAULT_ESCAPE
+# define WG_DEFAULT_ESCAPE '_'
+#endif
+
#ifndef WG_SEND_BUF_SIZE
# define WG_SEND_BUF_SIZE 4096
#endif
struct wg_callback
{
int sock_fd;
- struct hostent *server;
char *node;
char *service;
char *prefix;
char *postfix;
- char dotchar;
+ char escape_char;
char send_buf[WG_SEND_BUF_SIZE];
size_t send_buf_free;
status,
strerror (errno));
- pthread_mutex_trylock (&cb->send_lock);
-
- DEBUG ("write_graphite plugin: closing socket and restting fd "
- "so reinit will occur");
close (cb->sock_fd);
cb->sock_fd = -1;
- pthread_mutex_unlock (&cb->send_lock);
-
return (-1);
}
return (0);
}
+/* NOTE: You must hold cb->send_lock when calling this function! */
static int wg_flush_nolock (cdtime_t timeout, struct wg_callback *cb)
{
int status;
cb = data;
+ pthread_mutex_lock (&cb->send_lock);
+
wg_flush_nolock (/* timeout = */ 0, cb);
close(cb->sock_fd);
+ cb->sock_fd = -1;
+
sfree(cb->node);
sfree(cb->service);
sfree(cb->prefix);
sfree(cb->postfix);
+ pthread_mutex_destroy (&cb->send_lock);
+
sfree(cb);
}
return (0);
}
-static int swap_chars (char *dst, const char *src,
- const char from, const char to)
+static void wg_copy_escape_part (char *dst, const char *src, size_t dst_len,
+ char escape_char)
{
size_t i;
- int reps = 0;
+ memset (dst, 0, dst_len);
+
+ if (src == NULL)
+ return;
- for (i = 0; i < strlen(src) ; i++)
+ for (i = 0; i < dst_len; i++)
{
- if (src[i] == from)
- {
- dst[i] = to;
- ++reps;
- }
+ if ((src[i] == '.')
+ || isspace ((int) src[i])
+ || iscntrl ((int) src[i]))
+ dst[i] = escape_char;
else
dst[i] = src[i];
- }
- dst[i] = '\0';
- return reps;
+ if (src[i] == 0)
+ break;
+ }
}
static int wg_format_name (char *ret, int ret_len,
const char *plugin, const char *plugin_instance,
const char *type, const char *type_instance,
const char *prefix, const char *postfix,
- const char *ds_name, const char dotchar)
+ const char *ds_name, char escape_char)
{
+ char n_hostname[DATA_MAX_NAME_LEN];
+ char n_plugin[DATA_MAX_NAME_LEN];
+ char n_plugin_instance[DATA_MAX_NAME_LEN];
+ char n_type[DATA_MAX_NAME_LEN];
+ char n_type_instance[DATA_MAX_NAME_LEN];
int status;
- char *n_hostname = 0;
- char *n_type_instance = 0;
+ assert (hostname != NULL);
assert (plugin != NULL);
assert (type != NULL);
+ assert (ds_name != NULL);
if (prefix == NULL)
prefix = "";
if (postfix == NULL)
postfix = "";
- if ((n_hostname = malloc(strlen(hostname)+1)) == NULL)
- {
- ERROR ("Unable to allocate memory for normalized hostname buffer");
- return (-1);
- }
-
- if (swap_chars(n_hostname, hostname, '.', dotchar) == -1)
+ wg_copy_escape_part (n_hostname, hostname,
+ sizeof (n_hostname), escape_char);
+ wg_copy_escape_part (n_plugin, plugin,
+ sizeof (n_plugin), escape_char);
+ wg_copy_escape_part (n_plugin_instance, plugin_instance,
+ sizeof (n_plugin_instance), escape_char);
+ wg_copy_escape_part (n_type, type,
+ sizeof (n_type), escape_char);
+ wg_copy_escape_part (n_type_instance, type_instance,
+ sizeof (n_type_instance), escape_char);
+
+ if (n_plugin_instance[0] == '\0')
{
- ERROR ("Unable to normalize hostname");
- return (-1);
- }
-
- if (type_instance && type_instance[0] != '\0') {
- if ((n_type_instance = malloc(strlen(type_instance)+1)) == NULL)
+ if (n_type_instance[0] == '\0')
{
- ERROR ("Unable to allocate memory for normalized datasource name buffer");
- return (-1);
- }
- if (swap_chars(n_type_instance, type_instance, '.', dotchar) == -1)
- {
- ERROR ("Unable to normalize datasource name");
- return (-1);
- }
- }
-
- if ((plugin_instance == NULL) || (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);
- else
- status = ssnprintf (ret, ret_len, "%s%s%s.%s.%s.%s",
- prefix, n_hostname, postfix, plugin, type, ds_name);
+ status = ssnprintf (ret, ret_len, "%s%s%s.%s.%s.%s",
+ 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,
- n_type_instance);
- else
- status = ssnprintf (ret, ret_len, "%s%s%s.%s.%s-%s.%s",
- prefix, n_hostname, postfix, plugin, type,
- n_type_instance, ds_name);
+ status = ssnprintf (ret, ret_len, "%s%s%s.%s.%s-%s.%s",
+ prefix, n_hostname, postfix, n_plugin, n_type,
+ n_type_instance, ds_name);
}
}
else
{
- if ((n_type_instance == NULL) || (n_type_instance[0] == '\0'))
+ if (n_type_instance[0] == '\0')
{
- 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);
- else
- status = ssnprintf (ret, ret_len, "%s%s%s.%s.%s.%s.%s",
- prefix, n_hostname, postfix, plugin,
- plugin_instance, type, ds_name);
+ status = ssnprintf (ret, ret_len, "%s%s%s.%s.%s.%s.%s",
+ 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);
- 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);
+ status = ssnprintf (ret, ret_len, "%s%s%s.%s.%s.%s-%s.%s",
+ prefix, n_hostname, postfix, n_plugin,
+ n_plugin_instance, n_type, n_type_instance, ds_name);
}
}
- sfree(n_hostname);
- sfree(n_type_instance);
-
if ((status < 1) || (status >= ret_len))
return (-1);
+
return (0);
}
cb->service = NULL;
cb->prefix = NULL;
cb->postfix = NULL;
- cb->server = NULL;
- cb->dotchar = '_';
+ cb->escape_char = WG_DEFAULT_ESCAPE;
pthread_mutex_init (&cb->send_lock, /* attr = */ NULL);
cf_util_get_string (child, &cb->prefix);
else if (strcasecmp ("Postfix", child->key) == 0)
cf_util_get_string (child, &cb->postfix);
- else if (strcasecmp ("DotCharacter", child->key) == 0)
- config_set_char (&cb->dotchar, child);
+ else if (strcasecmp ("EscapeCharacter", child->key) == 0)
+ config_set_char (&cb->escape_char, child);
else
{
ERROR ("write_graphite plugin: Invalid configuration "