diff --git a/src/write_graphite.c b/src/write_graphite.c
index c658e207bde001a529b32e1f0f50cf36764573a8..7a0bb12d43e6533388cecc1eb7700a4bf38571f9 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)
-#endif
-
#ifndef WG_DEFAULT_NODE
# define WG_DEFAULT_NODE "localhost"
#endif
# define WG_DEFAULT_SERVICE "2003"
#endif
+#ifndef WG_DEFAULT_ESCAPE
+# define WG_DEFAULT_ESCAPE '_'
+#endif
+
+/* Ethernet - (IPv6 + TCP) = 1500 - (40 + 32) = 1428 */
#ifndef WG_SEND_BUF_SIZE
-# define WG_SEND_BUF_SIZE 4096
+# define WG_SEND_BUF_SIZE 1428
#endif
/*
struct wg_callback
{
int sock_fd;
- struct hostent *server;
char *node;
char *service;
char *prefix;
char *postfix;
- char dotchar;
+ char escape_char;
+
+ _Bool store_rates;
+ _Bool always_append_ds;
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)
+ if (src[i] == 0)
{
- dst[i] = to;
- ++reps;
+ dst[i] = 0;
+ break;
}
+
+ 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;
}
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 *postfix,
- const char *ds_name, const char dotchar)
+ const value_list_t *vl,
+ const struct wg_callback *cb,
+ const char *ds_name)
{
- int status;
- char *n_hostname = 0;
- char *n_type_instance = 0;
-
- assert (plugin != NULL);
- assert (type != NULL);
-
- 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)
- {
- ERROR ("Unable to normalize hostname");
- return (-1);
- }
-
- if (type_instance && 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)
- {
- ERROR ("Unable to normalize datasource name");
- return (-1);
- }
- }
+ char n_host[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];
+
+ char *prefix;
+ char *postfix;
+
+ char tmp_plugin[2 * DATA_MAX_NAME_LEN + 1];
+ char tmp_type[2 * DATA_MAX_NAME_LEN + 1];
+
+ prefix = cb->prefix;
+ if (prefix == NULL)
+ prefix = "";
+
+ postfix = cb->postfix;
+ if (postfix == NULL)
+ postfix = "";
+
+ wg_copy_escape_part (n_host, vl->host,
+ sizeof (n_host), cb->escape_char);
+ wg_copy_escape_part (n_plugin, vl->plugin,
+ sizeof (n_plugin), cb->escape_char);
+ wg_copy_escape_part (n_plugin_instance, vl->plugin_instance,
+ sizeof (n_plugin_instance), cb->escape_char);
+ wg_copy_escape_part (n_type, vl->type,
+ sizeof (n_type), cb->escape_char);
+ wg_copy_escape_part (n_type_instance, vl->type_instance,
+ sizeof (n_type_instance), cb->escape_char);
+
+ if (n_plugin_instance[0] != '\0')
+ ssnprintf (tmp_plugin, sizeof (tmp_plugin), "%s-%s",
+ n_plugin, n_plugin_instance);
+ else
+ sstrncpy (tmp_plugin, n_plugin, sizeof (tmp_plugin));
- 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);
- }
- 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);
- }
- }
+ if (n_type_instance[0] != '\0')
+ ssnprintf (tmp_type, sizeof (tmp_type), "%s-%s",
+ n_type, n_type_instance);
else
- {
- 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.%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);
- }
- 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);
- }
- }
+ sstrncpy (tmp_type, n_type, sizeof (tmp_type));
- sfree(n_hostname);
- sfree(n_type_instance);
+ if (ds_name != NULL)
+ ssnprintf (ret, ret_len, "%s%s%s.%s.%s.%s",
+ prefix, n_host, postfix, tmp_plugin, tmp_type, ds_name);
+ else
+ ssnprintf (ret, ret_len, "%s%s%s.%s.%s",
+ prefix, n_host, postfix, tmp_plugin, tmp_type);
- if ((status < 1) || (status >= ret_len))
- return (-1);
return (0);
}
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, ds->ds[i].name);
+ status = wg_format_name (key, sizeof (key), vl, cb, ds->ds[i].name);
if (status != 0)
{
ERROR ("write_graphite plugin: error with format_name");
escape_string (key, sizeof (key));
/* Convert the values to an ASCII representation and put that
* into `values'. */
- status = wg_format_values (values, sizeof (values), i, ds, vl, 0);
+ status = wg_format_values (values, sizeof (values), i, ds, vl,
+ cb->store_rates);
if (status != 0)
{
ERROR ("write_graphite plugin: error with "
else
{
/* Copy the identifier to `key' and escape it. */
- status = WG_FORMAT_NAME (key, sizeof (key), vl, cb, NULL);
+ status = wg_format_name (key, sizeof (key), vl, cb,
+ cb->always_append_ds ? ds->ds[0].name : NULL);
if (status != 0)
{
ERROR ("write_graphite plugin: error with format_name");
escape_string (key, sizeof (key));
/* Convert the values to an ASCII representation and put that into
* `values'. */
- status = wg_format_values (values, sizeof (values), 0, ds, vl, 0);
+ status = wg_format_values (values, sizeof (values), 0, ds, vl,
+ cb->store_rates);
if (status != 0)
{
ERROR ("write_graphite plugin: error with "
int status;
if (user_data == NULL)
- return (-EINVAL);
+ return (EINVAL);
cb = user_data->data;
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);
if (strcasecmp ("Host", child->key) == 0)
cf_util_get_string (child, &cb->node);
else if (strcasecmp ("Port", child->key) == 0)
- cf_util_get_string (child, &cb->service);
+ cf_util_get_service (child, &cb->service);
else if (strcasecmp ("Prefix", child->key) == 0)
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 ("StoreRates", child->key) == 0)
+ cf_util_get_boolean (child, &cb->store_rates);
+ else if (strcasecmp ("AlwaysAppendDS", child->key) == 0)
+ cf_util_get_boolean (child, &cb->always_append_ds);
+ else if (strcasecmp ("EscapeCharacter", child->key) == 0)
+ config_set_char (&cb->escape_char, child);
else
{
ERROR ("write_graphite plugin: Invalid configuration "
}
}
- if (cb->prefix == NULL) {
- if ((cb->prefix = malloc((int)sizeof(char))) == NULL)
- {
- ERROR ("Unable to allocate memory for hostname prefix buffer");
- return (-1);
- }
- cb->prefix[0] = '\0';
- }
-
- if (cb->postfix == NULL) {
- if ((cb->postfix = malloc((int)sizeof(char))) == NULL)
- {
- ERROR ("Unable to allocate memory for hostname postfix buffer");
- return (-1);
- }
- cb->postfix[0] = '\0';
- }
-
DEBUG ("write_graphite: Registering write callback to carbon agent %s:%s",
cb->node ? cb->node : WG_DEFAULT_NODE,
cb->service ? cb->service : WG_DEFAULT_SERVICE);