From 5e7f546bbba11a1df71579ea712ec1c818340a69 Mon Sep 17 00:00:00 2001 From: Nicolas Gillen Date: Mon, 10 Aug 2026 09:43:46 +0200 Subject: [PATCH] in_node_exporter_metrics: fix diskstats metric cache pointer bug ctx->dt_metrics is a flat array of 'struct dt_metric', but metric_cache_set()/metric_cache_update() indexed it as an array of pointers, causing every entry past id=0 to read/write at the wrong offset. This corrupts both the scaling factor and the metric pointer, producing wildly incorrect node_disk_* values on Linux and a corrupted counter target on Darwin. Also extract the repeated .001 ms-to-seconds factor into a named MS_TO_SECONDS constant. Signed-off-by: Nicolas Gillen --- .../ne_diskstats_darwin.c | 16 +++++----- .../ne_diskstats_linux.c | 29 +++++++++---------- 2 files changed, 21 insertions(+), 24 deletions(-) diff --git a/plugins/in_node_exporter_metrics/ne_diskstats_darwin.c b/plugins/in_node_exporter_metrics/ne_diskstats_darwin.c index e540c2fcfa5..bf019c9ed98 100644 --- a/plugins/in_node_exporter_metrics/ne_diskstats_darwin.c +++ b/plugins/in_node_exporter_metrics/ne_diskstats_darwin.c @@ -62,15 +62,13 @@ struct dt_metric { static void metric_cache_set(struct flb_ne *ctx, void *metric, double factor, int *offset) { int id; - struct dt_metric *m; - struct dt_metric **cache; + struct dt_metric *cache; id = *offset; - cache = (struct dt_metric **) ctx->dt_metrics; - m = (struct dt_metric *) &cache[id]; - m->metric = metric; - m->factor = factor; + cache = (struct dt_metric *) ctx->dt_metrics; + cache[id].metric = metric; + cache[id].factor = factor; (*offset)++; } @@ -80,11 +78,11 @@ static void metric_cache_update(struct flb_ne *ctx, int id, flb_sds_t device, int ret = -1; uint64_t ts; struct dt_metric *m; - struct dt_metric **cache; + struct dt_metric *cache; struct cmt_counter *c; - cache = (struct dt_metric **) ctx->dt_metrics; - m = (struct dt_metric *) &cache[id]; + cache = (struct dt_metric *) ctx->dt_metrics; + m = &cache[id]; ts = cfl_time_now(); diff --git a/plugins/in_node_exporter_metrics/ne_diskstats_linux.c b/plugins/in_node_exporter_metrics/ne_diskstats_linux.c index f004bf7360b..d5978e533fd 100644 --- a/plugins/in_node_exporter_metrics/ne_diskstats_linux.c +++ b/plugins/in_node_exporter_metrics/ne_diskstats_linux.c @@ -72,6 +72,7 @@ #define KNOWN_FIELDS 17 #define SECTOR_SIZE 512 +#define MS_TO_SECONDS .001 struct dt_metric { void *metric; @@ -81,15 +82,13 @@ struct dt_metric { static void metric_cache_set(struct flb_ne *ctx, void *metric, double factor, int *offset) { int id; - struct dt_metric *m; - struct dt_metric **cache; + struct dt_metric *cache; id = *offset; - cache = (struct dt_metric **) ctx->dt_metrics; - m = (struct dt_metric *) &cache[id]; - m->metric = metric; - m->factor = factor; + cache = (struct dt_metric *) ctx->dt_metrics; + cache[id].metric = metric; + cache[id].factor = factor; (*offset)++; } @@ -100,12 +99,12 @@ static void metric_cache_update(struct flb_ne *ctx, int id, flb_sds_t device, uint64_t ts; double val; struct dt_metric *m; - struct dt_metric **cache; + struct dt_metric *cache; struct cmt_gauge *g; struct cmt_counter *c; - cache = (struct dt_metric **) ctx->dt_metrics; - m = (struct dt_metric *) &cache[id]; + cache = (struct dt_metric *) ctx->dt_metrics; + m = &cache[id]; ret = ne_utils_str_to_double(str_val, &val); if (ret == -1) { @@ -197,7 +196,7 @@ static int ne_diskstats_configure(struct flb_ne *ctx) if (!c) { return -1; } - metric_cache_set(ctx, c, .001, &offset); + metric_cache_set(ctx, c, MS_TO_SECONDS, &offset); /* node_disk_writes_completed_total */ c = cmt_counter_create(ctx->cmt, "node", "disk", "writes_completed_total", @@ -233,7 +232,7 @@ static int ne_diskstats_configure(struct flb_ne *ctx) if (!c) { return -1; } - metric_cache_set(ctx, c, .001, &offset); + metric_cache_set(ctx, c, MS_TO_SECONDS, &offset); /* node_disk_io_now */ g = cmt_gauge_create(ctx->cmt, "node", "disk", "io_now", @@ -251,7 +250,7 @@ static int ne_diskstats_configure(struct flb_ne *ctx) if (!c) { return -1; } - metric_cache_set(ctx, c, .001, &offset); + metric_cache_set(ctx, c, MS_TO_SECONDS, &offset); /* node_disk_io_time_weighted_seconds */ c = cmt_counter_create(ctx->cmt, "node", "disk", "io_time_weighted_seconds_total", @@ -260,7 +259,7 @@ static int ne_diskstats_configure(struct flb_ne *ctx) if (!c) { return -1; } - metric_cache_set(ctx, c, .001, &offset); + metric_cache_set(ctx, c, MS_TO_SECONDS, &offset); /* * Linux Kernel >= 4.18 @@ -301,7 +300,7 @@ static int ne_diskstats_configure(struct flb_ne *ctx) if (!c) { return -1; } - metric_cache_set(ctx, c, .001, &offset); + metric_cache_set(ctx, c, MS_TO_SECONDS, &offset); /* * Linux Kernel >= 5.5 @@ -325,7 +324,7 @@ static int ne_diskstats_configure(struct flb_ne *ctx) if (!c) { return -1; } - metric_cache_set(ctx, c, .001, &offset); + metric_cache_set(ctx, c, MS_TO_SECONDS, &offset); return 0; }