Skip to content

Commit 0f8b535

Browse files
committed
in_node_exporter_metrics: Use util function to read numeric long values
Signed-off-by: Hiroshi Hatake <hiroshi@chronosphere.io>
1 parent 45062be commit 0f8b535

1 file changed

Lines changed: 13 additions & 20 deletions

File tree

plugins/in_node_exporter_metrics/ne_powersupply_linux.c

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@
1818
*/
1919

2020
#include <dirent.h>
21-
#include <stdio.h>
21+
#include <stdlib.h>
2222
#include <string.h>
2323

2424
#include "ne.h"
25+
#include "ne_utils.h"
2526

2627
struct ps_metric_spec {
2728
const char *metric_name;
@@ -109,24 +110,6 @@ static struct ps_metric_spec ps_numeric_metrics[] = {
109110
{"temp_min_celsius", "temp_min", 10.0},
110111
};
111112

112-
static int read_long_file(const char *path, long *out)
113-
{
114-
FILE *fp;
115-
116-
fp = fopen(path, "r");
117-
if (fp == NULL) {
118-
return -1;
119-
}
120-
121-
if (fscanf(fp, "%ld", out) != 1) {
122-
fclose(fp);
123-
return -1;
124-
}
125-
126-
fclose(fp);
127-
return 0;
128-
}
129-
130113
static struct cmt_gauge *ps_metric_get(struct flb_ne *ctx, const char *name)
131114
{
132115
struct mk_list *head;
@@ -176,6 +159,9 @@ static int update_one(struct flb_ne *ctx, const char *name)
176159
long v;
177160
uint64_t ts;
178161
char path[1024];
162+
char *buf;
163+
char *end;
164+
size_t buf_size;
179165
char *labels[] = {(char *) name};
180166
struct cmt_gauge *gauge;
181167

@@ -185,7 +171,14 @@ static int update_one(struct flb_ne *ctx, const char *name)
185171
snprintf(path, sizeof(path) - 1, "%s/class/power_supply/%s/%s",
186172
ctx->path_sysfs, name, ps_numeric_metrics[i].file_name);
187173

188-
if (read_long_file(path, &v) == 0) {
174+
if (flb_utils_read_file(path, &buf, &buf_size) == 0 && buf != NULL && buf_size > 0) {
175+
v = strtol(buf, &end, 10);
176+
if (end == buf) {
177+
flb_free(buf);
178+
continue;
179+
}
180+
flb_free(buf);
181+
189182
gauge = ps_metric_get(ctx, ps_numeric_metrics[i].metric_name);
190183
if (gauge != NULL) {
191184
cmt_gauge_set(gauge, ts, ((double) v) / ps_numeric_metrics[i].divisor,

0 commit comments

Comments
 (0)