X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fwrite_graphite.c;h=de460d7cfc0ba39e94ba4ae94fad4e45b25d34de;hb=917f8784060b467e67b4b05429ddd120be4bb845;hp=c658e207bde001a529b32e1f0f50cf36764573a8;hpb=75754cebc8d16dc4246d83c61dc5adba610ddfa0;p=collectd.git diff --git a/src/write_graphite.c b/src/write_graphite.c index c658e207..de460d7c 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 @@ -76,13 +81,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; @@ -341,26 +345,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,94 +374,70 @@ 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); - - 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); - } - } - - if ((plugin_instance == NULL) || (plugin_instance[0] == '\0')) + assert (ds_name != NULL); + + if (prefix == NULL) + prefix = ""; + + if (postfix == NULL) + postfix = ""; + + 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') { - 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", - 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); } @@ -653,8 +635,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 = '_'; pthread_mutex_init (&cb->send_lock, /* attr = */ NULL); @@ -670,8 +651,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 " @@ -679,24 +660,6 @@ static int wg_config_carbon (oconfig_item_t *ci) } } - 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);