From e9fb3dcaad4d46f1e594fd773f6d2327822bdddf Mon Sep 17 00:00:00 2001 From: Florian Forster Date: Thu, 13 Dec 2012 09:52:22 +0100 Subject: [PATCH] format_graphite: Implement the "separate instances" and "always append ds" options. Fixes Github issue #214. --- src/amqp.c | 7 ++++++- src/utils_format_graphite.c | 35 ++++++++++++++++++++--------------- src/utils_format_graphite.h | 6 +++++- src/write_graphite.c | 17 +++++++++-------- 4 files changed, 40 insertions(+), 25 deletions(-) diff --git a/src/amqp.c b/src/amqp.c index 9c8c6e53..767a8776 100644 --- a/src/amqp.c +++ b/src/amqp.c @@ -74,6 +74,7 @@ struct camqp_config_s char *prefix; char *postfix; char escape_char; + unsigned int graphite_flags; /* subscribe only */ char *exchange_type; @@ -794,7 +795,7 @@ static int camqp_write (const data_set_t *ds, const value_list_t *vl, /* {{{ */ { status = format_graphite (buffer, sizeof (buffer), ds, vl, conf->prefix, conf->postfix, conf->escape_char, - conf->store_rates); + conf->graphite_flags); if (status != 0) { ERROR ("amqp plugin: format_graphite failed with status %i.", @@ -934,7 +935,11 @@ static int camqp_config_connection (oconfig_item_t *ci, /* {{{ */ conf->delivery_mode = CAMQP_DM_VOLATILE; } else if ((strcasecmp ("StoreRates", child->key) == 0) && publish) + { status = cf_util_get_boolean (child, &conf->store_rates); + (void) cf_util_get_flag (child, &conf->graphite_flags, + GRAPHITE_STORE_RATES); + } else if ((strcasecmp ("Format", child->key) == 0) && publish) status = camqp_config_set_format (child, conf); else if ((strcasecmp ("GraphitePrefix", child->key) == 0) && publish) diff --git a/src/utils_format_graphite.c b/src/utils_format_graphite.c index b9b906fa..83512015 100644 --- a/src/utils_format_graphite.c +++ b/src/utils_format_graphite.c @@ -25,8 +25,8 @@ #include "plugin.h" #include "common.h" +#include "utils_format_graphite.h" #include "utils_cache.h" -#include "utils_format_json.h" #include "utils_parse_option.h" /* Utils functions to format data sets in graphite format. @@ -108,11 +108,12 @@ static void gr_copy_escape_part (char *dst, const char *src, size_t dst_len, } static int gr_format_name (char *ret, int ret_len, - const value_list_t *vl, - const char *ds_name, - char *prefix, - char *postfix, - char escape_char) + value_list_t const *vl, + char const *ds_name, + char const *prefix, + char const *postfix, + char const escape_char, + unsigned int flags) { char n_host[DATA_MAX_NAME_LEN]; char n_plugin[DATA_MAX_NAME_LEN]; @@ -143,7 +144,7 @@ static int gr_format_name (char *ret, int ret_len, if (n_plugin_instance[0] != '\0') ssnprintf (tmp_plugin, sizeof (tmp_plugin), "%s%c%s", n_plugin, - '-', + (flags & GRAPHITE_SEPARATE_INSTANCES) ? '.' : '-', n_plugin_instance); else sstrncpy (tmp_plugin, n_plugin, sizeof (tmp_plugin)); @@ -151,11 +152,13 @@ static int gr_format_name (char *ret, int ret_len, if (n_type_instance[0] != '\0') ssnprintf (tmp_type, sizeof (tmp_type), "%s%c%s", n_type, - '-', + (flags & GRAPHITE_SEPARATE_INSTANCES) ? '.' : '-', n_type_instance); else sstrncpy (tmp_type, n_type, sizeof (tmp_type)); + /* Assert always_append_ds -> ds_name */ + assert (!(flags & GRAPHITE_ALWAYS_APPEND_DS) || (ds_name != NULL)); if (ds_name != NULL) ssnprintf (ret, ret_len, "%s%s%s.%s.%s.%s", prefix, n_host, postfix, tmp_plugin, tmp_type, ds_name); @@ -167,31 +170,33 @@ static int gr_format_name (char *ret, int ret_len, } int format_graphite (char *buffer, size_t buffer_size, - const data_set_t *ds, const value_list_t *vl, char *prefix, - char *postfix, char escape_char, - _Bool store_rates) + data_set_t const *ds, value_list_t const *vl, + char const *prefix, char const *postfix, char const escape_char, + unsigned int flags) { int status = 0; int i; int buffer_pos = 0; gauge_t *rates = NULL; - if (store_rates) + if (flags & GRAPHITE_STORE_RATES) rates = uc_get_rate (ds, vl); for (i = 0; i < ds->ds_num; i++) { - const char *ds_name = NULL; + char const *ds_name = NULL; char key[10*DATA_MAX_NAME_LEN]; char values[512]; size_t message_len; char message[1024]; - ds_name = ds->ds[i].name; + if ((flags & GRAPHITE_ALWAYS_APPEND_DS) + || (ds->ds_num > 1)) + ds_name = ds->ds[i].name; /* Copy the identifier to `key' and escape it. */ status = gr_format_name (key, sizeof (key), vl, ds_name, - prefix, postfix, escape_char); + prefix, postfix, escape_char, flags); if (status != 0) { ERROR ("format_graphite: error with gr_format_name"); diff --git a/src/utils_format_graphite.h b/src/utils_format_graphite.h index a7a05bbb..398defbe 100644 --- a/src/utils_format_graphite.h +++ b/src/utils_format_graphite.h @@ -25,10 +25,14 @@ #include "collectd.h" #include "plugin.h" +#define GRAPHITE_STORE_RATES 0x01 +#define GRAPHITE_SEPARATE_INSTANCES 0x02 +#define GRAPHITE_ALWAYS_APPEND_DS 0x04 + int format_graphite (char *buffer, size_t buffer_size, const data_set_t *ds, const value_list_t *vl, const char *prefix, const char *postfix, const char escape_char, - _Bool store_rates); + unsigned int flags); #endif /* UTILS_FORMAT_GRAPHITE_H */ diff --git a/src/write_graphite.c b/src/write_graphite.c index 2ae30efe..8ae15ba0 100644 --- a/src/write_graphite.c +++ b/src/write_graphite.c @@ -85,9 +85,7 @@ struct wg_callback char *postfix; char escape_char; - _Bool store_rates; - _Bool separate_instances; - _Bool always_append_ds; + unsigned int format_flags; char send_buf[WG_SEND_BUF_SIZE]; size_t send_buf_free; @@ -352,7 +350,7 @@ static int wg_write_messages (const data_set_t *ds, const value_list_t *vl, memset (buffer, 0, sizeof (buffer)); status = format_graphite (buffer, sizeof (buffer), ds, vl, - cb->prefix, cb->postfix, cb->escape_char, cb->store_rates); + cb->prefix, cb->postfix, cb->escape_char, cb->format_flags); if (status != 0) /* error message has been printed already. */ return (status); @@ -434,7 +432,7 @@ static int wg_config_carbon (oconfig_item_t *ci) cb->prefix = NULL; cb->postfix = NULL; cb->escape_char = WG_DEFAULT_ESCAPE; - cb->store_rates = 1; + cb->format_flags = GRAPHITE_STORE_RATES; pthread_mutex_init (&cb->send_lock, /* attr = */ NULL); @@ -451,11 +449,14 @@ static int wg_config_carbon (oconfig_item_t *ci) else if (strcasecmp ("Postfix", child->key) == 0) cf_util_get_string (child, &cb->postfix); else if (strcasecmp ("StoreRates", child->key) == 0) - cf_util_get_boolean (child, &cb->store_rates); + cf_util_get_flag (child, &cb->format_flags, + GRAPHITE_STORE_RATES); else if (strcasecmp ("SeparateInstances", child->key) == 0) - cf_util_get_boolean (child, &cb->separate_instances); + cf_util_get_flag (child, &cb->format_flags, + GRAPHITE_SEPARATE_INSTANCES); else if (strcasecmp ("AlwaysAppendDS", child->key) == 0) - cf_util_get_boolean (child, &cb->always_append_ds); + cf_util_get_flag (child, &cb->format_flags, + GRAPHITE_ALWAYS_APPEND_DS); else if (strcasecmp ("EscapeCharacter", child->key) == 0) config_set_char (&cb->escape_char, child); else -- 2.11.0