From 836598b100f9acec988fcfe9a14e349e4186783b Mon Sep 17 00:00:00 2001 From: Michael Leinartas Date: Thu, 8 Sep 2016 13:13:26 -0500 Subject: [PATCH] Add option to write_graphite: DropDuplicateFields This option drops duplicate type and type_instance fields or duplicate type and plugin fields. example_host.memory.memory.cached becomes example_host.memory.cached example_host.cpu.0.cpu.idle becomes example_host.cpu.0.idle --- src/collectd.conf.in | 1 + src/utils_format_graphite.c | 19 +++++++++++++------ src/utils_format_graphite.h | 1 + src/write_graphite.c | 3 +++ 4 files changed, 18 insertions(+), 6 deletions(-) diff --git a/src/collectd.conf.in b/src/collectd.conf.in index 0fc13dcc..7ac9f2d2 100644 --- a/src/collectd.conf.in +++ b/src/collectd.conf.in @@ -1415,6 +1415,7 @@ # StoreRates true # AlwaysAppendDS false # EscapeCharacter "_" +# DropDuplicateFields false # # diff --git a/src/utils_format_graphite.c b/src/utils_format_graphite.c index e5234204..c7262655 100644 --- a/src/utils_format_graphite.c +++ b/src/utils_format_graphite.c @@ -150,18 +150,25 @@ static int gr_format_name (char *ret, int ret_len, sstrncpy (tmp_plugin, n_plugin, sizeof (tmp_plugin)); if (n_type_instance[0] != '\0') - ssnprintf (tmp_type, sizeof (tmp_type), "%s%c%s", - n_type, - (flags & GRAPHITE_SEPARATE_INSTANCES) ? '.' : '-', - n_type_instance); + if ((flags & GRAPHITE_DROP_DUPE_FIELDS) && strcmp(n_plugin, n_type) == 0) + sstrncpy (tmp_type, n_type_instance, sizeof (tmp_type)); + else + 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); + if ((flags & GRAPHITE_DROP_DUPE_FIELDS) && strcmp(tmp_plugin, tmp_type) == 0) + ssnprintf (ret, ret_len, "%s%s%s.%s.%s", + prefix, n_host, postfix, tmp_plugin, ds_name); + else + 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); diff --git a/src/utils_format_graphite.h b/src/utils_format_graphite.h index 60576dac..5165f9e6 100644 --- a/src/utils_format_graphite.h +++ b/src/utils_format_graphite.h @@ -29,6 +29,7 @@ #define GRAPHITE_STORE_RATES 0x01 #define GRAPHITE_SEPARATE_INSTANCES 0x02 #define GRAPHITE_ALWAYS_APPEND_DS 0x04 +#define GRAPHITE_DROP_DUPE_FIELDS 0x08 int format_graphite (char *buffer, size_t buffer_size, const data_set_t *ds, diff --git a/src/write_graphite.c b/src/write_graphite.c index fe2376ad..dd7f966c 100644 --- a/src/write_graphite.c +++ b/src/write_graphite.c @@ -548,6 +548,9 @@ static int wg_config_node (oconfig_item_t *ci) else if (strcasecmp ("AlwaysAppendDS", child->key) == 0) cf_util_get_flag (child, &cb->format_flags, GRAPHITE_ALWAYS_APPEND_DS); + else if (strcasecmp ("DropDuplicateFields", child->key) == 0) + cf_util_get_flag (child, &cb->format_flags, + GRAPHITE_DROP_DUPE_FIELDS); else if (strcasecmp ("EscapeCharacter", child->key) == 0) config_set_char (&cb->escape_char, child); else -- 2.30.2