Skip to content

Commit

Permalink
in_node_exporter_metrics: Implement NVMe metrics
Browse files Browse the repository at this point in the history
Signed-off-by: Hiroshi Hatake <[email protected]>
  • Loading branch information
cosmo0920 committed Oct 17, 2023
1 parent b5c18d9 commit 6070107
Show file tree
Hide file tree
Showing 6 changed files with 357 additions and 1 deletion.
1 change: 1 addition & 0 deletions plugins/in_node_exporter_metrics/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ set(src
ne_filefd_linux.c
ne_textfile.c
ne_processes.c
ne_nvme.c
ne_utils.c
ne_config.c
ne.c
Expand Down
59 changes: 59 additions & 0 deletions plugins/in_node_exporter_metrics/ne.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
#include "ne_textfile.h"
#include "ne_systemd.h"
#include "ne_processes.h"
#include "ne_nvme.h"

static int ne_timer_cpu_metrics_cb(struct flb_input_instance *ins,
struct flb_config *config, void *in_context)
Expand Down Expand Up @@ -192,6 +193,17 @@ static int ne_timer_processes_metrics_cb(struct flb_input_instance *ins,

return 0;
}

static int ne_timer_nvme_metrics_cb(struct flb_input_instance *ins,
struct flb_config *config, void *in_context)
{
struct flb_ne *ctx = in_context;

ne_nvme_update(ctx);

return 0;
}

struct flb_ne_callback {
char *name;
void (*func)(char *, void *, void *);
Expand Down Expand Up @@ -346,6 +358,13 @@ static void ne_processes_update_cb(char *name, void *p1, void *p2)
ne_processes_update(ctx);
}

static void ne_nvme_update_cb(char *name, void *p1, void *p2)
{
struct flb_ne *ctx = p1;

ne_nvme_update(ctx);
}

static int ne_update_cb(struct flb_ne *ctx, char *name)
{
int ret;
Expand Down Expand Up @@ -374,6 +393,7 @@ struct flb_ne_callback ne_callbacks[] = {
{ "textfile", ne_textfile_update_cb },
{ "systemd", ne_systemd_update_cb },
{ "processes", ne_processes_update_cb },
{ "nvme", ne_nvme_update_cb },
{ 0 }
};

Expand Down Expand Up @@ -411,6 +431,7 @@ static int in_ne_init(struct flb_input_instance *in,
ctx->coll_textfile_fd = -1;
ctx->coll_systemd_fd = -1;
ctx->coll_processes_fd = -1;
ctx->coll_nvme_fd = -1;

ctx->callback = flb_callback_create(in->name);
if (!ctx->callback) {
Expand Down Expand Up @@ -740,6 +761,26 @@ static int in_ne_init(struct flb_input_instance *in,
}
ne_processes_init(ctx);
}
else if (strncmp(entry->str, "nvme", 4) == 0) {
if (ctx->nvme_scrape_interval == 0) {
flb_plg_debug(ctx->ins, "enabled metrics %s", entry->str);
metric_idx = 15;
}
else if (ctx->systemd_scrape_interval > 0) {
/* Create the filefd collector */
ret = flb_input_set_collector_time(in,
ne_timer_nvme_metrics_cb,
ctx->nvme_scrape_interval, 0,
config);
if (ret == -1) {
flb_plg_error(ctx->ins,
"could not set nvme collector for Node Exporter Metrics plugin");
return -1;
}
ctx->coll_nvme_fd = ret;
}
ne_nvme_init(ctx);
}
else {
flb_plg_warn(ctx->ins, "Unknown metrics: %s", entry->str);
metric_idx = -1;
Expand Down Expand Up @@ -828,6 +869,9 @@ static int in_ne_exit(void *data, struct flb_config *config)
else if (strncmp(entry->str, "processes", 9) == 0) {
ne_processes_exit(ctx);
}
else if (strncmp(entry->str, "nvme", 7) == 0) {
ne_nvme_exit(ctx);
}
else {
flb_plg_warn(ctx->ins, "Unknown metrics: %s", entry->str);
}
Expand Down Expand Up @@ -862,6 +906,9 @@ static int in_ne_exit(void *data, struct flb_config *config)
if (ctx->coll_processes_fd != -1) {
ne_processes_exit(ctx);
}
if (ctx->coll_nvme_fd != -1) {
ne_nvme_exit(ctx);
}

flb_ne_config_destroy(ctx);

Expand Down Expand Up @@ -918,6 +965,9 @@ static void in_ne_pause(void *data, struct flb_config *config)
if (ctx->coll_processes_fd != -1) {
flb_input_collector_pause(ctx->coll_processes_fd, ctx->ins);
}
if (ctx->coll_nvme_fd != -1) {
flb_input_collector_pause(ctx->coll_nvme_fd, ctx->ins);
}
}

static void in_ne_resume(void *data, struct flb_config *config)
Expand Down Expand Up @@ -970,6 +1020,9 @@ static void in_ne_resume(void *data, struct flb_config *config)
if (ctx->coll_processes_fd != -1) {
flb_input_collector_resume(ctx->coll_processes_fd, ctx->ins);
}
if (ctx->coll_nvme_fd != -1) {
flb_input_collector_resume(ctx->coll_nvme_fd, ctx->ins);
}
}

/* Configuration properties map */
Expand Down Expand Up @@ -1070,6 +1123,12 @@ static struct flb_config_map config_map[] = {
"scrape interval to collect processes metrics from the node."
},

{
FLB_CONFIG_MAP_TIME, "collector.nvme.scrape_interval", "0",
0, FLB_TRUE, offsetof(struct flb_ne, nvme_scrape_interval),
"scrape interval to collect nvme metrics from the node."
},

{
FLB_CONFIG_MAP_CLIST, "metrics",
NE_DEFAULT_ENABLED_METRICS,
Expand Down
7 changes: 6 additions & 1 deletion plugins/in_node_exporter_metrics/ne.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
/* Default enabled metrics */

#ifdef __linux__
#define NE_DEFAULT_ENABLED_METRICS "cpu,cpufreq,meminfo,diskstats,filesystem,uname,stat,time,loadavg,vmstat,netdev,filefd,systemd"
#define NE_DEFAULT_ENABLED_METRICS "cpu,cpufreq,meminfo,diskstats,filesystem,uname,stat,time,loadavg,vmstat,netdev,filefd,systemd,nvme"
#elif __APPLE__
#define NE_DEFAULT_ENABLED_METRICS "cpu,loadavg,meminfo,diskstats,uname,netdev"
#endif
Expand Down Expand Up @@ -75,6 +75,7 @@ struct flb_ne {
int textfile_scrape_interval;
int systemd_scrape_interval;
int processes_scrape_interval;
int nvme_scrape_interval;

int coll_cpu_fd; /* collector fd (cpu) */
int coll_cpufreq_fd; /* collector fd (cpufreq) */
Expand All @@ -91,6 +92,7 @@ struct flb_ne {
int coll_textfile_fd; /* collector fd (textfile) */
int coll_systemd_fd ; /* collector fd (systemd) */
int coll_processes_fd ; /* collector fd (processes) */
int coll_nvme_fd; /* collector fd (nvme) */

/*
* Metrics Contexts
Expand Down Expand Up @@ -235,6 +237,9 @@ struct flb_ne {
struct cmt_gauge *processes_procs_state;
struct cmt_gauge *processes_pid_used;
struct cmt_gauge *processes_pid_max;

/* nvme */
struct cmt_gauge *nvme_info;
};

#endif
22 changes: 22 additions & 0 deletions plugins/in_node_exporter_metrics/ne_nvme.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */

/* Fluent Bit
* ==========
* Copyright (C) 2023 The Fluent Bit Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifdef __linux__
#include "ne_nvme_linux.c"
#endif
47 changes: 47 additions & 0 deletions plugins/in_node_exporter_metrics/ne_nvme.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */

/* Fluent Bit
* ==========
* Copyright (C) 2023 The Fluent Bit Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef FLB_IN_NE_NVME_H
#define FLB_IN_NE_NVME_H

#include "ne.h"

#ifdef __linux__
int ne_nvme_init(struct flb_ne *ctx);
int ne_nvme_update(struct flb_ne *ctx);
int ne_nvme_exit(struct flb_ne *ctx);
#else
static int ne_nvme_init(struct flb_ne *ctx)
{
return 0;
}

static int ne_nvme_update(struct flb_ne *ctx)
{
return 0;
}

static int ne_nvme_exit(struct flb_ne *ctx)
{
return 0;
}

#endif

#endif
Loading

0 comments on commit 6070107

Please sign in to comment.