From 7b1d658c9e330eabaeb98777d3461e82cb866037 Mon Sep 17 00:00:00 2001 From: Eduardo Silva Date: Tue, 6 Jul 2021 13:24:10 -0600 Subject: [PATCH] in_node_exporter_metrics: sanitize meminfo metric names Signed-off-by: Eduardo Silva --- .../ne_meminfo_linux.c | 25 ++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/plugins/in_node_exporter_metrics/ne_meminfo_linux.c b/plugins/in_node_exporter_metrics/ne_meminfo_linux.c index 740f9c3c7a0..8737f7115e0 100644 --- a/plugins/in_node_exporter_metrics/ne_meminfo_linux.c +++ b/plugins/in_node_exporter_metrics/ne_meminfo_linux.c @@ -32,6 +32,8 @@ static int meminfo_configure(struct flb_ne *ctx) { int ret; int parts; + int len; + char *p; char desc[] = "Memory information field "; struct cmt_gauge *g; struct mk_list *head; @@ -82,8 +84,20 @@ static int meminfo_configure(struct flb_ne *ctx) /* set metric name */ entry = mk_list_entry_first(&split_list, struct flb_slist_entry, _head); + if ((p = strstr(entry->str, "(anon)")) || + (p = strstr(entry->str, "(file)"))) { + *p = '_'; + len = flb_sds_len(entry->str) - 2; + flb_sds_len_set(entry->str, len); + } + else { + len = flb_sds_len(entry->str) - 1; + flb_sds_len_set(entry->str, len); + } + entry->str[len] = '\0'; + flb_sds_len_set(metric_name, 0); - flb_sds_cat(metric_name, entry->str, flb_sds_len(entry->str) - 1); + flb_sds_cat(metric_name, entry->str, flb_sds_len(entry->str)); /* Metric description */ flb_sds_len_set(metric_desc, 0); @@ -151,10 +165,12 @@ static int meminfo_update(struct flb_ne *ctx) { int i = 0; int ret; + int len; int parts; uint64_t ts; double val; size_t out_size; + char *p; flb_sds_t tmp; flb_sds_t metric_name = NULL; struct cmt_gauge *g; @@ -190,6 +206,13 @@ static int meminfo_update(struct flb_ne *ctx) entry = mk_list_entry_first(&split_list, struct flb_slist_entry, _head); metric_name = entry->str; + if ((p = strstr(entry->str, "(anon)")) || + (p = strstr(entry->str, "(file)"))) { + *p = '_'; + len = flb_sds_len(metric_name) - 1; + flb_sds_len_set(metric_name, len); + } + /* Metric value */ entry = mk_list_entry_next(&split_list, struct flb_slist_entry, _head, &entry->_head);