diff --git a/plugins/in_node_exporter_metrics/ne_cpu_linux.c b/plugins/in_node_exporter_metrics/ne_cpu_linux.c index bc7c8a9feec..67f85b7cb53 100644 --- a/plugins/in_node_exporter_metrics/ne_cpu_linux.c +++ b/plugins/in_node_exporter_metrics/ne_cpu_linux.c @@ -130,10 +130,12 @@ static int cpu_thermal_update(struct flb_ne *ctx, uint64_t ts) } /* Package Metric: node_cpu_core_throttles_total */ - ret = ne_utils_file_read_uint64(ctx, ctx->path_sysfs, - entry->str, - "thermal_throttle", "core_throttle_count", - &core_throttle_count); + ret = ne_utils_file_read_uint64_at_level(ctx, ctx->path_sysfs, + entry->str, + "thermal_throttle", + "core_throttle_count", + &core_throttle_count, + FLB_LOG_DEBUG); if (ret != 0) { flb_plg_debug(ctx->ins, "CPU is missing core_throttle_count: %s", @@ -150,10 +152,12 @@ static int cpu_thermal_update(struct flb_ne *ctx, uint64_t ts) } /* Package Metric: node_cpu_package_throttles_total */ - ret = ne_utils_file_read_uint64(ctx, ctx->path_sysfs, - entry->str, - "thermal_throttle", "package_throttle_count", - &package_throttle_count); + ret = ne_utils_file_read_uint64_at_level(ctx, ctx->path_sysfs, + entry->str, + "thermal_throttle", + "package_throttle_count", + &package_throttle_count, + FLB_LOG_DEBUG); if (ret != 0) { flb_plg_debug(ctx->ins, "CPU is missing package_throttle_count: %s", diff --git a/plugins/in_node_exporter_metrics/ne_utils.c b/plugins/in_node_exporter_metrics/ne_utils.c index 591c856b54f..543de19498e 100644 --- a/plugins/in_node_exporter_metrics/ne_utils.c +++ b/plugins/in_node_exporter_metrics/ne_utils.c @@ -20,7 +20,7 @@ #include #include #include -#include "ne.h" +#include "ne_utils.h" /* required by stat(2), open(2) */ #include @@ -30,6 +30,39 @@ #include +static void ne_utils_file_log(struct flb_ne *ctx, int log_level, int err_num, + const char *operation, const char *path) +{ + if (!ctx) { + errno = err_num; + flb_errno(); + return; + } + + if (err_num != ENOENT) { + log_level = FLB_LOG_ERROR; + } + + switch (log_level) { + case FLB_LOG_TRACE: + flb_plg_trace(ctx->ins, "could not %s '%s'", operation, path); + break; + case FLB_LOG_DEBUG: + flb_plg_debug(ctx->ins, "could not %s '%s'", operation, path); + break; + case FLB_LOG_INFO: + flb_plg_info(ctx->ins, "could not %s '%s'", operation, path); + break; + case FLB_LOG_WARN: + flb_plg_warn(ctx->ins, "could not %s '%s'", operation, path); + break; + case FLB_LOG_ERROR: + default: + flb_plg_error(ctx->ins, "could not %s '%s'", operation, path); + break; + } +} + int ne_utils_str_to_double(char *str, double *out_val) { double val; @@ -70,6 +103,18 @@ int ne_utils_file_read_uint64(struct flb_ne *ctx, const char *path, const char *join_a, const char *join_b, uint64_t *out_val) +{ + return ne_utils_file_read_uint64_at_level(ctx, mount, path, join_a, join_b, + out_val, FLB_LOG_ERROR); +} + +int ne_utils_file_read_uint64_at_level(struct flb_ne *ctx, + const char *mount, + const char *path, + const char *join_a, + const char *join_b, + uint64_t *out_val, + int log_level) { int fd; int len; @@ -123,24 +168,14 @@ int ne_utils_file_read_uint64(struct flb_ne *ctx, fd = open(p, O_RDONLY); if (fd == -1) { - if (ctx) { - flb_plg_error(ctx->ins, "could not open '%s'", p); - } - else { - flb_errno(); - } + ne_utils_file_log(ctx, log_level, errno, "open", p); flb_sds_destroy(p); return -1; } bytes = read(fd, &tmp, sizeof(tmp)); if (bytes == -1) { - if (ctx) { - flb_plg_error(ctx->ins, "could not read from '%s'", p); - } - else { - flb_errno(); - } + ne_utils_file_log(ctx, log_level, errno, "read from", p); close(fd); flb_sds_destroy(p); return -1; @@ -220,6 +255,18 @@ int ne_utils_file_read_sds(struct flb_ne *ctx, const char *join_a, const char *join_b, flb_sds_t *str) +{ + return ne_utils_file_read_sds_at_level(ctx, mount, path, join_a, join_b, + str, FLB_LOG_ERROR); +} + +int ne_utils_file_read_sds_at_level(struct flb_ne *ctx, + const char *mount, + const char *path, + const char *join_a, + const char *join_b, + flb_sds_t *str, + int log_level) { int fd; int len; @@ -269,24 +316,14 @@ int ne_utils_file_read_sds(struct flb_ne *ctx, fd = open(p, O_RDONLY); if (fd == -1) { - if (ctx) { - flb_plg_error(ctx->ins, "could not open '%s'", p); - } - else { - flb_errno(); - } + ne_utils_file_log(ctx, log_level, errno, "open", p); flb_sds_destroy(p); return -1; } bytes = read(fd, &tmp, sizeof(tmp)); if (bytes == -1) { - if (ctx) { - flb_plg_error(ctx->ins, "could not read from '%s'", p); - } - else { - flb_errno(); - } + ne_utils_file_log(ctx, log_level, errno, "read from", p); close(fd); flb_sds_destroy(p); return -1; diff --git a/plugins/in_node_exporter_metrics/ne_utils.h b/plugins/in_node_exporter_metrics/ne_utils.h index a365fbd4937..70089316e99 100644 --- a/plugins/in_node_exporter_metrics/ne_utils.h +++ b/plugins/in_node_exporter_metrics/ne_utils.h @@ -34,6 +34,14 @@ int ne_utils_file_read_uint64(struct flb_ne *ctx, const char *join_a, const char *join_b, uint64_t *out_val); +int ne_utils_file_read_uint64_at_level(struct flb_ne *ctx, + const char *mount, + const char *path, + const char *join_a, + const char *join_b, + uint64_t *out_val, + int log_level); + int ne_utils_file_read_sds(struct flb_ne *ctx, const char *mount, const char *path, @@ -41,6 +49,14 @@ int ne_utils_file_read_sds(struct flb_ne *ctx, const char *join_b, flb_sds_t *str); +int ne_utils_file_read_sds_at_level(struct flb_ne *ctx, + const char *mount, + const char *path, + const char *join_a, + const char *join_b, + flb_sds_t *str, + int log_level); + int ne_utils_file_read_lines(struct flb_ne *ctx, const char *mount, const char *path,