Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions plugins/in_node_exporter_metrics/ne_cpu_linux.c
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
87 changes: 62 additions & 25 deletions plugins/in_node_exporter_metrics/ne_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#include <fluent-bit/flb_info.h>
#include <fluent-bit/flb_input_plugin.h>
#include <fluent-bit/flb_sds.h>
#include "ne.h"
#include "ne_utils.h"

/* required by stat(2), open(2) */
#include <sys/types.h>
Expand All @@ -30,6 +30,39 @@

#include <glob.h>

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;
Expand Down Expand Up @@ -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)
Comment thread
cosmo0920 marked this conversation as resolved.
{
int fd;
int len;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
16 changes: 16 additions & 0 deletions plugins/in_node_exporter_metrics/ne_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,29 @@ 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,
const char *join_a,
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,
Expand Down
Loading