X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;f=src%2Fwrite_graphite.c;h=6c8a977f98770f0a6772c76c153faac667e55a2e;hb=5e1d7902ffae810f851f4298683f675fc538f5bd;hp=827ec3881263a749eeb68d89c50d7a357a185ac4;hpb=cbe5d0b3deec5978a5b0c61371d5c87b9285a0ae;p=collectd.git diff --git a/src/write_graphite.c b/src/write_graphite.c index 827ec388..6c8a977f 100644 --- a/src/write_graphite.c +++ b/src/write_graphite.c @@ -1,6 +1,10 @@ /** * 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 @@ -15,10 +19,14 @@ * 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 + * Authors: + * Florian octo Forster + * Doug MacEachern + * Paul Sadauskas + * Scott Sanders + * Pierre-Yves Ritschard * - * based on the excellent write_http plugin + * Based on the write_http plugin. **/ /* write_graphite plugin configuation example @@ -44,18 +52,15 @@ #include #include -#include -#include - -#include #include #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 @@ -66,6 +71,10 @@ # 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 @@ -76,13 +85,12 @@ 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; @@ -116,20 +124,15 @@ static int wg_send_buffer (struct wg_callback *cb) 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; @@ -236,14 +239,20 @@ static void wg_callback_free (void *data) 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); } @@ -341,26 +350,28 @@ static int wg_format_values (char *ret, size_t ret_len, 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, @@ -368,14 +379,19 @@ 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 = ""; @@ -383,85 +399,50 @@ static int wg_format_name (char *ret, int ret_len, 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); } @@ -659,8 +640,7 @@ static int wg_config_carbon (oconfig_item_t *ci) 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); @@ -676,8 +656,8 @@ static int wg_config_carbon (oconfig_item_t *ci) 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 "