diff --git a/plugins/in_node_exporter_metrics/CMakeLists.txt b/plugins/in_node_exporter_metrics/CMakeLists.txt index 9406ebc1751..d8cd7cd359a 100644 --- a/plugins/in_node_exporter_metrics/CMakeLists.txt +++ b/plugins/in_node_exporter_metrics/CMakeLists.txt @@ -4,26 +4,20 @@ set(src ne_diskstats.c ne_filesystem.c ne_uname.c - ne_stat_linux.c - ne_vmstat_linux.c + ne_stat.c + ne_vmstat.c ne_netdev.c ne_time.c ne_loadavg.c - ne_filefd_linux.c + ne_filefd.c ne_textfile.c ne_processes.c ne_utils.c ne_config.c + ne_systemd.c ne.c ) -if(FLB_HAVE_SYSTEMD_SDBUS) -set(src - ${src} - ne_systemd.c - ) -endif() - if (APPLE) FLB_PLUGIN(in_node_exporter_metrics "${src}" "-framework Foundation -framework IOKit") else() diff --git a/plugins/in_node_exporter_metrics/ne.c b/plugins/in_node_exporter_metrics/ne.c index 81d71384426..b7bf137a4b9 100644 --- a/plugins/in_node_exporter_metrics/ne.c +++ b/plugins/in_node_exporter_metrics/ne.c @@ -25,7 +25,7 @@ #include "ne.h" #include "ne_config.h" -#include "ne_filefd_linux.h" +#include "ne_filefd.h" /* collectors */ #include "ne_cpu.h" @@ -34,192 +34,15 @@ #include "ne_diskstats.h" #include "ne_filesystem.h" #include "ne_uname.h" -#include "ne_stat_linux.h" +#include "ne_stat.h" #include "ne_time.h" #include "ne_loadavg.h" -#include "ne_vmstat_linux.h" +#include "ne_vmstat.h" #include "ne_netdev.h" #include "ne_textfile.h" #include "ne_systemd.h" #include "ne_processes.h" -static int ne_timer_cpu_metrics_cb(struct flb_input_instance *ins, - struct flb_config *config, void *in_context) -{ - struct flb_ne *ctx = in_context; - - ne_cpu_update(ctx); - - return 0; -} - -static int ne_timer_cpufreq_metrics_cb(struct flb_input_instance *ins, - struct flb_config *config, void *in_context) -{ - struct flb_ne *ctx = in_context; - - ne_cpufreq_update(ctx); - - return 0; -} - -static int ne_timer_meminfo_metrics_cb(struct flb_input_instance *ins, - struct flb_config *config, void *in_context) -{ - struct flb_ne *ctx = in_context; - - ne_meminfo_update(ctx); - - return 0; -} - -static int ne_timer_diskstats_metrics_cb(struct flb_input_instance *ins, - struct flb_config *config, void *in_context) -{ - struct flb_ne *ctx = in_context; - - ne_diskstats_update(ctx); - - return 0; -} - -static int ne_timer_filesystem_metrics_cb(struct flb_input_instance *ins, - struct flb_config *config, void *in_context) -{ - struct flb_ne *ctx = in_context; - - ne_filesystem_update(ctx); - - return 0; -} - -static int ne_timer_uname_metrics_cb(struct flb_input_instance *ins, - struct flb_config *config, void *in_context) -{ - struct flb_ne *ctx = in_context; - - ne_uname_update(ctx); - - return 0; -} - -static int ne_timer_stat_metrics_cb(struct flb_input_instance *ins, - struct flb_config *config, void *in_context) -{ - struct flb_ne *ctx = in_context; - - ne_stat_update(ctx); - - return 0; -} - -static int ne_timer_time_metrics_cb(struct flb_input_instance *ins, - struct flb_config *config, void *in_context) -{ - struct flb_ne *ctx = in_context; - - ne_time_update(ctx); - - return 0; -} - -static int ne_timer_loadavg_metrics_cb(struct flb_input_instance *ins, - struct flb_config *config, void *in_context) -{ - struct flb_ne *ctx = in_context; - - ne_loadavg_update(ctx); - - return 0; -} - -static int ne_timer_vmstat_metrics_cb(struct flb_input_instance *ins, - struct flb_config *config, void *in_context) -{ - struct flb_ne *ctx = in_context; - - ne_vmstat_update(ctx); - - return 0; -} - -static int ne_timer_netdev_metrics_cb(struct flb_input_instance *ins, - struct flb_config *config, void *in_context) -{ - struct flb_ne *ctx = in_context; - - ne_netdev_update(ctx); - - return 0; -} - -static int ne_timer_filefd_metrics_cb(struct flb_input_instance *ins, - struct flb_config *config, void *in_context) -{ - struct flb_ne *ctx = in_context; - - ne_filefd_update(ctx); - - return 0; -} - -static int ne_timer_textfile_metrics_cb(struct flb_input_instance *ins, - struct flb_config *config, void *in_context) -{ - struct flb_ne *ctx = in_context; - - ne_textfile_update(ctx); - - return 0; -} - -static int ne_timer_systemd_metrics_cb(struct flb_input_instance *ins, - struct flb_config *config, void *in_context) -{ - struct flb_ne *ctx = in_context; - - ne_systemd_update(ctx); - - return 0; -} - -static int ne_timer_processes_metrics_cb(struct flb_input_instance *ins, - struct flb_config *config, void *in_context) -{ - struct flb_ne *ctx = in_context; - - ne_processes_update(ctx); - - return 0; -} -struct flb_ne_callback { - char *name; - void (*func)(char *, void *, void *); -}; - -static int ne_update_cb(struct flb_ne *ctx, char *name); - -static void update_metrics(struct flb_input_instance *ins, struct flb_ne *ctx) -{ - int ret; - struct mk_list *head; - struct flb_slist_entry *entry; - - /* Update our metrics */ - if (ctx->metrics) { - mk_list_foreach(head, ctx->metrics) { - entry = mk_list_entry(head, struct flb_slist_entry, _head); - ret = flb_callback_exists(ctx->callback, entry->str); - if (ret == FLB_TRUE) { - ne_update_cb(ctx, entry->str); - } - else { - flb_plg_debug(ctx->ins, "Callback for metrics '%s' is not registered", entry->str); - } - } - } -} - /* * Update the metrics, this function is invoked every time 'scrape_interval' * expires. @@ -230,8 +53,6 @@ static int cb_ne_collect(struct flb_input_instance *ins, int ret; struct flb_ne *ctx = in_context; - update_metrics(ins, ctx); - /* Append the updated metrics */ ret = flb_input_metrics_append(ins, NULL, 0, ctx->cmt); if (ret != 0) { @@ -241,151 +62,110 @@ static int cb_ne_collect(struct flb_input_instance *ins, return 0; } -static void ne_cpu_update_cb(char *name, void *p1, void *p2) -{ - struct flb_ne *ctx = p1; - - ne_cpu_update(ctx); -} - -static void ne_cpufreq_update_cb(char *name, void *p1, void *p2) +static int collectors_common_init(struct flb_ne_collector *coll) { - struct flb_ne *ctx = p1; - - ne_cpufreq_update(ctx); -} - -static void ne_meminfo_update_cb(char *name, void *p1, void *p2) -{ - struct flb_ne *ctx = p1; - - ne_meminfo_update(ctx); -} - -static void ne_diskstats_update_cb(char *name, void *p1, void *p2) -{ - struct flb_ne *ctx = p1; - - ne_diskstats_update(ctx); -} - -static void ne_filesystem_update_cb(char *name, void *p1, void *p2) -{ - struct flb_ne *ctx = p1; - - ne_filesystem_update(ctx); -} - -static void ne_uname_update_cb(char *name, void *p1, void *p2) -{ - struct flb_ne *ctx = p1; - - ne_uname_update(ctx); -} - -static void ne_stat_update_cb(char *name, void *p1, void *p2) -{ - struct flb_ne *ctx = p1; - - ne_stat_update(ctx); -} - -static void ne_time_update_cb(char *name, void *p1, void *p2) -{ - struct flb_ne *ctx = p1; - - ne_time_update(ctx); -} - -static void ne_loadavg_update_cb(char *name, void *p1, void *p2) -{ - struct flb_ne *ctx = p1; - - ne_loadavg_update(ctx); -} - -static void ne_vmstat_update_cb(char *name, void *p1, void *p2) -{ - struct flb_ne *ctx = p1; - - ne_vmstat_update(ctx); -} - -static void ne_netdev_update_cb(char *name, void *p1, void *p2) -{ - struct flb_ne *ctx = p1; + if (coll == NULL) { + return -1; + } + coll->coll_fd = -1; + coll->interval = 0; + coll->activated = FLB_FALSE; - ne_netdev_update(ctx); + return 0; } -static void ne_filefd_update_cb(char *name, void *p1, void *p2) +static int get_interval_property(struct flb_ne *ctx, flb_sds_t name) { - struct flb_ne *ctx = p1; + flb_sds_t interval_conf_name; + size_t conf_len = 1024; + const char *interval_str; + int ret; + int interval; - ne_filefd_update(ctx); -} + interval_conf_name = flb_sds_create_size(conf_len); + if (interval_conf_name == NULL) { + flb_errno(); + return -1; + } + ret = flb_sds_snprintf(&interval_conf_name, conf_len, "collector.%s.scrape_interval", name); + if (ret < 0) { + flb_errno(); + flb_sds_destroy(interval_conf_name); + return -1; + } + else if (ret > conf_len) { + flb_plg_error(ctx->ins, "buffer is small for %s interval config", name); + flb_sds_destroy(interval_conf_name); + return -1; + } -static void ne_textfile_update_cb(char *name, void *p1, void *p2) -{ - struct flb_ne *ctx = p1; + interval_str = flb_input_get_property(interval_conf_name, ctx->ins); + if (interval_str == NULL) { + interval = ctx->scrape_interval; + } + else { + interval = atoi(interval_str); + if (interval == 0) { + interval = ctx->scrape_interval; + } + } + flb_sds_destroy(interval_conf_name); - ne_textfile_update(ctx); + return interval; } -static void ne_systemd_update_cb(char *name, void *p1, void *p2) +static int activate_collector(struct flb_ne *ctx, struct flb_config *config, + struct flb_ne_collector *coll, flb_sds_t name) { - struct flb_ne *ctx = p1; - - ne_systemd_update(ctx); -} + int interval; + int ret; -static void ne_processes_update_cb(char *name, void *p1, void *p2) -{ - struct flb_ne *ctx = p1; + if (coll == NULL) { + return -1; + } + if (coll->activated == FLB_TRUE) { + flb_plg_warn(ctx->ins, "%s is already activated", name); + return 0; + } + if (coll->cb_init == NULL) { + flb_plg_warn(ctx->ins, "%s is not supported", name); + return 0; + } - ne_processes_update(ctx); -} + if (coll->cb_update) { + interval = get_interval_property(ctx, name); + if (interval < 0) { + return -1; + } + ret = flb_input_set_collector_time(ctx->ins, + coll->cb_update, + interval, 0, config); + if (ret < 0) { + flb_plg_error(ctx->ins, "flb_input_set_collector_time failed"); + return -1; + } + coll->coll_fd = ret; + } -static int ne_update_cb(struct flb_ne *ctx, char *name) -{ - int ret; + ret = coll->cb_init(ctx); + if (ret != 0) { + flb_plg_error(ctx->ins, "%s init failed", name); + return -1; + } + coll->activated = FLB_TRUE; - ret = flb_callback_do(ctx->callback, name, ctx, NULL); - return ret; + return 0; } -/* - * Callbacks Table - */ -struct flb_ne_callback ne_callbacks[] = { - /* metrics */ - { "cpufreq", ne_cpufreq_update_cb }, - { "cpu", ne_cpu_update_cb }, - { "meminfo", ne_meminfo_update_cb }, - { "diskstats", ne_diskstats_update_cb }, - { "filesystem", ne_filesystem_update_cb }, - { "uname", ne_uname_update_cb }, - { "stat", ne_stat_update_cb }, - { "time", ne_time_update_cb }, - { "loadavg", ne_loadavg_update_cb }, - { "vmstat", ne_vmstat_update_cb }, - { "netdev", ne_netdev_update_cb }, - { "filefd", ne_filefd_update_cb }, - { "textfile", ne_textfile_update_cb }, - { "systemd", ne_systemd_update_cb }, - { "processes", ne_processes_update_cb }, - { 0 } -}; - static int in_ne_init(struct flb_input_instance *in, struct flb_config *config, void *data) { int ret; - int metric_idx = -1; struct flb_ne *ctx; struct mk_list *head; + struct mk_list *coll_head; + struct flb_ne_collector *coll; struct flb_slist_entry *entry; - struct flb_ne_callback *cb; /* Create plugin context */ ctx = flb_ne_config_create(in, config); @@ -394,29 +174,30 @@ static int in_ne_init(struct flb_input_instance *in, return -1; } + mk_list_init(&ctx->collectors); + mk_list_add(&cpu_collector._head, &ctx->collectors); + mk_list_add(&cpufreq_collector._head, &ctx->collectors); + mk_list_add(&meminfo_collector._head, &ctx->collectors); + mk_list_add(&diskstats_collector._head, &ctx->collectors); + mk_list_add(&filesystem_collector._head, &ctx->collectors); + mk_list_add(&uname_collector._head, &ctx->collectors); + mk_list_add(&stat_collector._head, &ctx->collectors); + mk_list_add(&time_collector._head, &ctx->collectors); + mk_list_add(&loadavg_collector._head, &ctx->collectors); + mk_list_add(&vmstat_collector._head, &ctx->collectors); + mk_list_add(&netdev_collector._head, &ctx->collectors); + mk_list_add(&filefd_collector._head, &ctx->collectors); + mk_list_add(&textfile_collector._head, &ctx->collectors); + mk_list_add(&systemd_collector._head, &ctx->collectors); + mk_list_add(&processes_collector._head, &ctx->collectors); + + mk_list_foreach(head, &ctx->collectors) { + coll = mk_list_entry(head, struct flb_ne_collector, _head); + collectors_common_init(coll); + } + /* Initialize fds */ ctx->coll_fd = -1; - ctx->coll_cpu_fd = -1; - ctx->coll_cpufreq_fd = -1; - ctx->coll_meminfo_fd = -1; - ctx->coll_diskstats_fd = -1; - ctx->coll_filesystem_fd = -1; - ctx->coll_uname_fd = -1; - ctx->coll_stat_fd = -1; - ctx->coll_time_fd = -1; - ctx->coll_loadavg_fd = -1; - ctx->coll_vmstat_fd = -1; - ctx->coll_netdev_fd = -1; - ctx->coll_filefd_fd = -1; - ctx->coll_textfile_fd = -1; - ctx->coll_systemd_fd = -1; - ctx->coll_processes_fd = -1; - - ctx->callback = flb_callback_create(in->name); - if (!ctx->callback) { - flb_plg_error(ctx->ins, "Create callback failed"); - return -1; - } /* Associate context with the instance */ flb_input_set_context(in, ctx); @@ -437,321 +218,21 @@ static int in_ne_init(struct flb_input_instance *in, if (ctx->metrics) { mk_list_foreach(head, ctx->metrics) { entry = mk_list_entry(head, struct flb_slist_entry, _head); - ret = flb_callback_exists(ctx->callback, entry->str); - if (ret == FLB_FALSE) { - if (strncmp(entry->str, "cpufreq", 7) == 0) { - if (ctx->cpufreq_scrape_interval == 0) { - flb_plg_debug(ctx->ins, "enabled metrics %s", entry->str); - metric_idx = 0; + mk_list_foreach(coll_head, &ctx->collectors) { + coll = mk_list_entry(coll_head, struct flb_ne_collector, _head); + if (coll->activated == FLB_FALSE && + flb_sds_len(entry->str) == strlen(coll->name) && + strncmp(entry->str, coll->name, strlen(coll->name)) == 0) { + ret = activate_collector(ctx, config, coll, entry->str); + if (ret < 0) { + flb_plg_error(ctx->ins, + "could not set %s collector for Node Exporter Metrics plugin", entry->str); } - else if (ctx->cpufreq_scrape_interval > 0) { - /* Create the cpufreq collector */ - ret = flb_input_set_collector_time(in, - ne_timer_cpufreq_metrics_cb, - ctx->cpufreq_scrape_interval, 0, - config); - if (ret == -1) { - flb_plg_error(ctx->ins, - "could not set cpufreq collector for Node Exporter Metrics plugin"); - return -1; - } - ctx->coll_cpufreq_fd = ret; - } - ne_cpufreq_init(ctx); - } - else if (strncmp(entry->str, "cpu", 3) == 0) { - if (ctx->cpu_scrape_interval == 0) { - flb_plg_debug(ctx->ins, "enabled metrics %s", entry->str); - metric_idx = 1; - } - else if (ctx->cpu_scrape_interval > 0) { - /* Create the cpu collector */ - ret = flb_input_set_collector_time(in, - ne_timer_cpu_metrics_cb, - ctx->cpu_scrape_interval, 0, - config); - if (ret == -1) { - flb_plg_error(ctx->ins, - "could not set cpu collector for Node Exporter Metrics plugin"); - return -1; - } - ctx->coll_cpu_fd = ret; - } - ne_cpu_init(ctx); - } - else if (strncmp(entry->str, "meminfo", 7) == 0) { - if (ctx->meminfo_scrape_interval == 0) { - flb_plg_debug(ctx->ins, "enabled metrics %s", entry->str); - metric_idx = 2; - } - else if (ctx->meminfo_scrape_interval > 0) { - /* Create the meminfo collector */ - ret = flb_input_set_collector_time(in, - ne_timer_meminfo_metrics_cb, - ctx->meminfo_scrape_interval, 0, - config); - if (ret == -1) { - flb_plg_error(ctx->ins, - "could not set meminfo collector for Node Exporter Metrics plugin"); - return -1; - } - ctx->coll_meminfo_fd = ret; - } - ne_meminfo_init(ctx); - } - else if (strncmp(entry->str, "diskstats", 9) == 0) { - if (ctx->diskstats_scrape_interval == 0) { - flb_plg_debug(ctx->ins, "enabled metrics %s", entry->str); - metric_idx = 3; - } - else if (ctx->diskstats_scrape_interval > 0) { - /* Create the diskstats collector */ - ret = flb_input_set_collector_time(in, - ne_timer_diskstats_metrics_cb, - ctx->diskstats_scrape_interval, 0, - config); - if (ret == -1) { - flb_plg_error(ctx->ins, - "could not set diskstats collector for Node Exporter Metrics plugin"); - return -1; - } - ctx->coll_diskstats_fd = ret; - } - ne_diskstats_init(ctx); - } - else if (strncmp(entry->str, "filesystem", 10) == 0) { - if (ctx->diskstats_scrape_interval == 0) { - flb_plg_debug(ctx->ins, "enabled metrics %s", entry->str); - metric_idx = 4; - } - else if (ctx->filesystem_scrape_interval > 0) { - /* Create the diskstats collector */ - ret = flb_input_set_collector_time(in, - ne_timer_filesystem_metrics_cb, - ctx->filesystem_scrape_interval, 0, - config); - if (ret == -1) { - flb_plg_error(ctx->ins, - "could not set filesystem collector for Node Exporter Metrics plugin"); - return -1; - } - ctx->coll_filesystem_fd = ret; - } - ne_filesystem_init(ctx); - } - else if (strncmp(entry->str, "uname", 5) == 0) { - if (ctx->uname_scrape_interval == 0) { - flb_plg_debug(ctx->ins, "enabled metrics %s", entry->str); - metric_idx = 5; - } - else if (ctx->uname_scrape_interval > 0) { - /* Create the uname collector */ - ret = flb_input_set_collector_time(in, - ne_timer_uname_metrics_cb, - ctx->uname_scrape_interval, 0, - config); - if (ret == -1) { - flb_plg_error(ctx->ins, - "could not set uname collector for Node Exporter Metrics plugin"); - return -1; - } - ctx->coll_uname_fd = ret; - } - ne_uname_init(ctx); - } - else if (strncmp(entry->str, "stat", 4) == 0) { - if (ctx->stat_scrape_interval == 0) { - flb_plg_debug(ctx->ins, "enabled metrics %s", entry->str); - metric_idx = 6; - } - else if (ctx->stat_scrape_interval > 0) { - /* Create the meminfo collector */ - ret = flb_input_set_collector_time(in, - ne_timer_stat_metrics_cb, - ctx->stat_scrape_interval, 0, - config); - if (ret == -1) { - flb_plg_error(ctx->ins, - "could not set meminfo collector for Node Exporter Metrics plugin"); - return -1; - } - ctx->coll_stat_fd = ret; - } - ne_stat_init(ctx); - } - else if (strncmp(entry->str, "time", 4) == 0) { - if (ctx->time_scrape_interval == 0) { + else { flb_plg_debug(ctx->ins, "enabled metrics %s", entry->str); - metric_idx = 7; - } - else if (ctx->time_scrape_interval > 0) { - /* Create the time collector */ - ret = flb_input_set_collector_time(in, - ne_timer_time_metrics_cb, - ctx->time_scrape_interval, 0, - config); - if (ret == -1) { - flb_plg_error(ctx->ins, - "could not set time collector for Node Exporter Metrics plugin"); - return -1; - } - ctx->coll_time_fd = ret; - } - ne_time_init(ctx); - } - else if (strncmp(entry->str, "loadavg", 7) == 0) { - if (ctx->loadavg_scrape_interval == 0) { - flb_plg_debug(ctx->ins, "enabled metrics %s", entry->str); - metric_idx = 8; - } - else if (ctx->loadavg_scrape_interval > 0) { - /* Create the loadavg collector */ - ret = flb_input_set_collector_time(in, - ne_timer_loadavg_metrics_cb, - ctx->loadavg_scrape_interval, 0, - config); - if (ret == -1) { - flb_plg_error(ctx->ins, - "could not set loadavg collector for Node Exporter Metrics plugin"); - return -1; - } - ctx->coll_loadavg_fd = ret; - } - ne_loadavg_init(ctx); - } - else if (strncmp(entry->str, "vmstat", 6) == 0) { - if (ctx->vmstat_scrape_interval == 0) { - flb_plg_debug(ctx->ins, "enabled metrics %s", entry->str); - metric_idx = 9; - } - else if (ctx->vmstat_scrape_interval > 0) { - /* Create the vmstat collector */ - ret = flb_input_set_collector_time(in, - ne_timer_vmstat_metrics_cb, - ctx->vmstat_scrape_interval, 0, - config); - if (ret == -1) { - flb_plg_error(ctx->ins, - "could not set vmstat collector for Node Exporter Metrics plugin"); - return -1; - } - ctx->coll_vmstat_fd = ret; - } - ne_vmstat_init(ctx); - } - else if (strncmp(entry->str, "netdev", 6) == 0) { - if (ctx->netdev_scrape_interval == 0) { - flb_plg_debug(ctx->ins, "enabled metrics %s", entry->str); - metric_idx = 10; - } - else if (ctx->netdev_scrape_interval > 0) { - /* Create the netdev collector */ - ret = flb_input_set_collector_time(in, - ne_timer_netdev_metrics_cb, - ctx->netdev_scrape_interval, 0, - config); - if (ret == -1) { - flb_plg_error(ctx->ins, - "could not set netdev collector for Node Exporter Metrics plugin"); - return -1; - } - ctx->coll_netdev_fd = ret; - } - ne_netdev_init(ctx); - } - else if (strncmp(entry->str, "filefd", 6) == 0) { - if (ctx->filefd_scrape_interval == 0) { - flb_plg_debug(ctx->ins, "enabled metrics %s", entry->str); - metric_idx = 11; - } - else if (ctx->filefd_scrape_interval > 0) { - /* Create the filefd collector */ - ret = flb_input_set_collector_time(in, - ne_timer_filefd_metrics_cb, - ctx->filefd_scrape_interval, 0, - config); - if (ret == -1) { - flb_plg_error(ctx->ins, - "could not set filefd collector for Node Exporter Metrics plugin"); - return -1; - } - ctx->coll_filefd_fd = ret; - } - ne_filefd_init(ctx); - } - else if (strncmp(entry->str, "textfile", 8) == 0) { - if (ctx->textfile_scrape_interval == 0) { - flb_plg_debug(ctx->ins, "enabled metrics %s", entry->str); - metric_idx = 12; - } - else if (ctx->textfile_scrape_interval > 0) { - /* Create the filefd collector */ - ret = flb_input_set_collector_time(in, - ne_timer_textfile_metrics_cb, - ctx->textfile_scrape_interval, 0, - config); - if (ret == -1) { - flb_plg_error(ctx->ins, - "could not set textfile collector for Node Exporter Metrics plugin"); - return -1; - } - ctx->coll_textfile_fd = ret; - } - ne_textfile_init(ctx); - } - else if (strncmp(entry->str, "systemd", 7) == 0) { - if (ctx->systemd_scrape_interval == 0) { - flb_plg_debug(ctx->ins, "enabled metrics %s", entry->str); - metric_idx = 13; - } - else if (ctx->systemd_scrape_interval > 0) { - /* Create the filefd collector */ - ret = flb_input_set_collector_time(in, - ne_timer_systemd_metrics_cb, - ctx->systemd_scrape_interval, 0, - config); - if (ret == -1) { - flb_plg_error(ctx->ins, - "could not set systemd collector for Node Exporter Metrics plugin"); - return -1; - } - ctx->coll_systemd_fd = ret; - } - ne_systemd_init(ctx); - } - else if (strncmp(entry->str, "processes", 9) == 0) { - if (ctx->processes_scrape_interval == 0) { - flb_plg_debug(ctx->ins, "enabled metrics %s", entry->str); - metric_idx = 14; - } - else if (ctx->processes_scrape_interval > 0) { - /* Create the filefd collector */ - ret = flb_input_set_collector_time(in, - ne_timer_processes_metrics_cb, - ctx->processes_scrape_interval, 0, - config); - if (ret == -1) { - flb_plg_error(ctx->ins, - "could not set systemd collector for Node Exporter Metrics plugin"); - return -1; - } - ctx->coll_processes_fd = ret; - } - ne_processes_init(ctx); - } - else { - flb_plg_warn(ctx->ins, "Unknown metrics: %s", entry->str); - metric_idx = -1; - } - - if (metric_idx >= 0) { - cb = &ne_callbacks[metric_idx]; - ret = flb_callback_set(ctx->callback, cb->name, cb->func); - if (ret == -1) { - flb_plg_error(ctx->ins, "error setting up default " - "callback '%s'", cb->name); } + break; } } } @@ -767,101 +248,22 @@ static int in_ne_init(struct flb_input_instance *in, static int in_ne_exit(void *data, struct flb_config *config) { - int ret; struct flb_ne *ctx = data; - struct mk_list *head; - struct flb_slist_entry *entry; + struct mk_list *coll_head; + struct flb_ne_collector *coll; if (!ctx) { return 0; } - /* Teardown for callback tied up resources */ - if (ctx->metrics) { - mk_list_foreach(head, ctx->metrics) { - entry = mk_list_entry(head, struct flb_slist_entry, _head); - ret = flb_callback_exists(ctx->callback, entry->str); - - if (ret == FLB_TRUE) { - if (strncmp(entry->str, "cpufreq", 7) == 0) { - /* nop */ - } - else if (strncmp(entry->str, "cpu", 3) == 0) { - /* nop */ - } - else if (strncmp(entry->str, "meminfo", 7) == 0) { - ne_meminfo_exit(ctx); - } - else if (strncmp(entry->str, "diskstats", 9) == 0) { - ne_diskstats_exit(ctx); - } - else if (strncmp(entry->str, "filesystem", 10) == 0) { - ne_filesystem_exit(ctx); - } - else if (strncmp(entry->str, "uname", 5) == 0) { - /* nop */ - } - else if (strncmp(entry->str, "stat", 4) == 0) { - /* nop */ - } - else if (strncmp(entry->str, "time", 4) == 0) { - /* nop */ - } - else if (strncmp(entry->str, "loadavg", 7) == 0) { - /* nop */ - } - else if (strncmp(entry->str, "vmstat", 6) == 0) { - ne_vmstat_exit(ctx); - } - else if (strncmp(entry->str, "netdev", 6) == 0) { - ne_netdev_exit(ctx); - } - else if (strncmp(entry->str, "filefd", 6) == 0) { - /* nop */ - } - else if (strncmp(entry->str, "textfile", 8) == 0) { - /* nop */ - } - else if (strncmp(entry->str, "systemd", 7) == 0) { - ne_systemd_exit(ctx); - } - else if (strncmp(entry->str, "processes", 9) == 0) { - ne_processes_exit(ctx); - } - else { - flb_plg_warn(ctx->ins, "Unknown metrics: %s", entry->str); - } - } + mk_list_foreach(coll_head, &ctx->collectors) { + coll = mk_list_entry(coll_head, struct flb_ne_collector, _head); + if (coll->activated == FLB_TRUE && coll->cb_exit) { + coll->cb_exit(ctx); } } - /* destroy callback context */ - if (ctx->callback) { - flb_callback_destroy(ctx->callback); - } - /* Teardown for timer tied up resources */ - if (ctx->coll_meminfo_fd != -1) { - ne_meminfo_exit(ctx); - } - if (ctx->coll_diskstats_fd != -1) { - ne_diskstats_exit(ctx); - } - if (ctx->coll_filesystem_fd != -1) { - ne_filesystem_exit(ctx); - } - if (ctx->coll_vmstat_fd != -1) { - ne_vmstat_exit(ctx); - } - if (ctx->coll_netdev_fd != -1) { - ne_netdev_exit(ctx); - } - if (ctx->coll_systemd_fd != -1) { - ne_systemd_exit(ctx); - } - if (ctx->coll_processes_fd != -1) { - ne_processes_exit(ctx); - } flb_ne_config_destroy(ctx); @@ -871,104 +273,33 @@ static int in_ne_exit(void *data, struct flb_config *config) static void in_ne_pause(void *data, struct flb_config *config) { struct flb_ne *ctx = data; + struct flb_ne_collector *coll; + struct mk_list *head; flb_input_collector_pause(ctx->coll_fd, ctx->ins); - if (ctx->coll_cpu_fd != -1) { - flb_input_collector_pause(ctx->coll_cpu_fd, ctx->ins); - } - if (ctx->coll_cpufreq_fd != -1) { - flb_input_collector_pause(ctx->coll_cpufreq_fd, ctx->ins); - } - if (ctx->coll_meminfo_fd != -1) { - flb_input_collector_pause(ctx->coll_meminfo_fd, ctx->ins); - } - if (ctx->coll_diskstats_fd != -1) { - flb_input_collector_pause(ctx->coll_diskstats_fd, ctx->ins); - } - if (ctx->coll_filesystem_fd != -1) { - flb_input_collector_pause(ctx->coll_filesystem_fd, ctx->ins); - } - if (ctx->coll_uname_fd != -1) { - flb_input_collector_pause(ctx->coll_uname_fd, ctx->ins); - } - if (ctx->coll_stat_fd != -1) { - flb_input_collector_pause(ctx->coll_stat_fd, ctx->ins); - } - if (ctx->coll_time_fd != -1) { - flb_input_collector_pause(ctx->coll_time_fd, ctx->ins); - } - if (ctx->coll_loadavg_fd != -1) { - flb_input_collector_pause(ctx->coll_loadavg_fd, ctx->ins); - } - if (ctx->coll_vmstat_fd != -1) { - flb_input_collector_pause(ctx->coll_vmstat_fd, ctx->ins); - } - if (ctx->coll_netdev_fd != -1) { - flb_input_collector_pause(ctx->coll_netdev_fd, ctx->ins); - } - if (ctx->coll_filefd_fd != -1) { - flb_input_collector_pause(ctx->coll_filefd_fd, ctx->ins); - } - if (ctx->coll_textfile_fd != -1) { - flb_input_collector_pause(ctx->coll_textfile_fd, ctx->ins); - } - if (ctx->coll_systemd_fd != -1) { - flb_input_collector_pause(ctx->coll_systemd_fd, ctx->ins); - } - if (ctx->coll_processes_fd != -1) { - flb_input_collector_pause(ctx->coll_processes_fd, ctx->ins); + + mk_list_foreach(head, &ctx->collectors) { + coll = mk_list_entry(head, struct flb_ne_collector, _head); + if (coll->activated == FLB_FALSE) { + continue; + } + flb_input_collector_pause(coll->coll_fd, ctx->ins); } } static void in_ne_resume(void *data, struct flb_config *config) { struct flb_ne *ctx = data; + struct flb_ne_collector *coll; + struct mk_list *head; flb_input_collector_resume(ctx->coll_fd, ctx->ins); - if (ctx->coll_cpu_fd != -1) { - flb_input_collector_resume(ctx->coll_cpu_fd, ctx->ins); - } - if (ctx->coll_cpufreq_fd != -1) { - flb_input_collector_resume(ctx->coll_cpufreq_fd, ctx->ins); - } - if (ctx->coll_meminfo_fd != -1) { - flb_input_collector_resume(ctx->coll_meminfo_fd, ctx->ins); - } - if (ctx->coll_diskstats_fd != -1) { - flb_input_collector_resume(ctx->coll_diskstats_fd, ctx->ins); - } - if (ctx->coll_filesystem_fd != -1) { - flb_input_collector_resume(ctx->coll_filesystem_fd, ctx->ins); - } - if (ctx->coll_uname_fd != -1) { - flb_input_collector_resume(ctx->coll_uname_fd, ctx->ins); - } - if (ctx->coll_stat_fd != -1) { - flb_input_collector_resume(ctx->coll_stat_fd, ctx->ins); - } - if (ctx->coll_time_fd != -1) { - flb_input_collector_resume(ctx->coll_time_fd, ctx->ins); - } - if (ctx->coll_loadavg_fd != -1) { - flb_input_collector_resume(ctx->coll_loadavg_fd, ctx->ins); - } - if (ctx->coll_vmstat_fd != -1) { - flb_input_collector_resume(ctx->coll_vmstat_fd, ctx->ins); - } - if (ctx->coll_netdev_fd != -1) { - flb_input_collector_resume(ctx->coll_netdev_fd, ctx->ins); - } - if (ctx->coll_filefd_fd != -1) { - flb_input_collector_resume(ctx->coll_filefd_fd, ctx->ins); - } - if (ctx->coll_textfile_fd != -1) { - flb_input_collector_resume(ctx->coll_textfile_fd, ctx->ins); - } - if (ctx->coll_systemd_fd != -1) { - flb_input_collector_resume(ctx->coll_systemd_fd, ctx->ins); - } - if (ctx->coll_processes_fd != -1) { - flb_input_collector_resume(ctx->coll_processes_fd, ctx->ins); + mk_list_foreach(head, &ctx->collectors) { + coll = mk_list_entry(head, struct flb_ne_collector, _head); + if (coll->activated == FLB_FALSE) { + continue; + } + flb_input_collector_resume(coll->coll_fd, ctx->ins); } } @@ -982,91 +313,91 @@ static struct flb_config_map config_map[] = { { FLB_CONFIG_MAP_TIME, "collector.cpu.scrape_interval", "0", - 0, FLB_TRUE, offsetof(struct flb_ne, cpu_scrape_interval), + 0, FLB_FALSE, 0, "scrape interval to collect cpu metrics from the node." }, { FLB_CONFIG_MAP_TIME, "collector.cpufreq.scrape_interval", "0", - 0, FLB_TRUE, offsetof(struct flb_ne, cpufreq_scrape_interval), + 0, FLB_FALSE, 0, "scrape interval to collect cpufreq metrics from the node." }, { FLB_CONFIG_MAP_TIME, "collector.meminfo.scrape_interval", "0", - 0, FLB_TRUE, offsetof(struct flb_ne, meminfo_scrape_interval), + 0, FLB_FALSE, 0, "scrape interval to collect meminfo metrics from the node." }, { FLB_CONFIG_MAP_TIME, "collector.diskstats.scrape_interval", "0", - 0, FLB_TRUE, offsetof(struct flb_ne, diskstats_scrape_interval), + 0, FLB_FALSE, 0, "scrape interval to collect diskstats metrics from the node." }, { FLB_CONFIG_MAP_TIME, "collector.filesystem.scrape_interval", "0", - 0, FLB_TRUE, offsetof(struct flb_ne, filesystem_scrape_interval), + 0, FLB_FALSE, 0, "scrape interval to collect filesystem metrics from the node." }, { FLB_CONFIG_MAP_TIME, "collector.uname.scrape_interval", "0", - 0, FLB_TRUE, offsetof(struct flb_ne, uname_scrape_interval), + 0, FLB_FALSE, 0, "scrape interval to collect uname metrics from the node." }, { FLB_CONFIG_MAP_TIME, "collector.stat.scrape_interval", "0", - 0, FLB_TRUE, offsetof(struct flb_ne, stat_scrape_interval), + 0, FLB_FALSE, 0, "scrape interval to collect stat metrics from the node." }, { FLB_CONFIG_MAP_TIME, "collector.time.scrape_interval", "0", - 0, FLB_TRUE, offsetof(struct flb_ne, time_scrape_interval), + 0, FLB_FALSE, 0, "scrape interval to collect time metrics from the node." }, { FLB_CONFIG_MAP_TIME, "collector.loadavg.scrape_interval", "0", - 0, FLB_TRUE, offsetof(struct flb_ne, loadavg_scrape_interval), + 0, FLB_FALSE, 0, "scrape interval to collect loadavg metrics from the node." }, { FLB_CONFIG_MAP_TIME, "collector.vmstat.scrape_interval", "0", - 0, FLB_TRUE, offsetof(struct flb_ne, vmstat_scrape_interval), + 0, FLB_FALSE, 0, "scrape interval to collect vmstat metrics from the node." }, { FLB_CONFIG_MAP_TIME, "collector.netdev.scrape_interval", "0", - 0, FLB_TRUE, offsetof(struct flb_ne, netdev_scrape_interval), + 0, FLB_FALSE, 0, "scrape interval to collect netdev metrics from the node." }, { FLB_CONFIG_MAP_TIME, "collector.filefd.scrape_interval", "0", - 0, FLB_TRUE, offsetof(struct flb_ne, filefd_scrape_interval), + 0, FLB_FALSE, 0, "scrape interval to collect filefd metrics from the node." }, { FLB_CONFIG_MAP_TIME, "collector.textfile.scrape_interval", "0", - 0, FLB_TRUE, offsetof(struct flb_ne, textfile_scrape_interval), + 0, FLB_FALSE, 0, "scrape interval to collect textfile metrics from the node." }, { FLB_CONFIG_MAP_TIME, "collector.systemd.scrape_interval", "0", - 0, FLB_TRUE, offsetof(struct flb_ne, systemd_scrape_interval), + 0, FLB_FALSE, 0, "scrape interval to collect systemd metrics from the node." }, { FLB_CONFIG_MAP_TIME, "collector.processes.scrape_interval", "0", - 0, FLB_TRUE, offsetof(struct flb_ne, processes_scrape_interval), + 0, FLB_FALSE, 0, "scrape interval to collect processes metrics from the node." }, diff --git a/plugins/in_node_exporter_metrics/ne.h b/plugins/in_node_exporter_metrics/ne.h index 2c31f1cdb73..876eb69b509 100644 --- a/plugins/in_node_exporter_metrics/ne.h +++ b/plugins/in_node_exporter_metrics/ne.h @@ -56,41 +56,9 @@ struct flb_ne { int coll_fd; /* collector fd */ struct cmt *cmt; /* cmetrics context */ struct flb_input_instance *ins; /* input instance */ - struct flb_callback *callback; /* metric callback */ struct mk_list *metrics; /* enabled metrics */ - /* Individual intervals for metrics */ - int cpu_scrape_interval; - int cpufreq_scrape_interval; - int meminfo_scrape_interval; - int diskstats_scrape_interval; - int filesystem_scrape_interval; - int uname_scrape_interval; - int stat_scrape_interval; - int time_scrape_interval; - int loadavg_scrape_interval; - int vmstat_scrape_interval; - int netdev_scrape_interval; - int filefd_scrape_interval; - int textfile_scrape_interval; - int systemd_scrape_interval; - int processes_scrape_interval; - - int coll_cpu_fd; /* collector fd (cpu) */ - int coll_cpufreq_fd; /* collector fd (cpufreq) */ - int coll_meminfo_fd; /* collector fd (meminfo) */ - int coll_diskstats_fd; /* collector fd (diskstat) */ - int coll_filesystem_fd; /* collector fd (filesystem) */ - int coll_uname_fd; /* collector fd (uname) */ - int coll_stat_fd; /* collector fd (stat) */ - int coll_time_fd; /* collector fd (time) */ - int coll_loadavg_fd; /* collector fd (loadavg) */ - int coll_vmstat_fd; /* collector fd (vmstat) */ - int coll_netdev_fd; /* collector fd (netdev) */ - int coll_filefd_fd; /* collector fd (filefd) */ - int coll_textfile_fd; /* collector fd (textfile) */ - int coll_systemd_fd ; /* collector fd (systemd) */ - int coll_processes_fd ; /* collector fd (processes) */ + struct mk_list collectors; /* * Metrics Contexts @@ -237,4 +205,18 @@ struct flb_ne { struct cmt_gauge *processes_pid_max; }; +struct flb_ne_collector { + const char *name; + int coll_fd; + int interval; + int activated; + + /* callbacks */ + int (*cb_init) (struct flb_ne *ctx); + int (*cb_update) (struct flb_input_instance *ins, struct flb_config *conf, void *in_context); + int (*cb_exit) (struct flb_ne *ctx); + + struct mk_list _head; +}; + #endif diff --git a/plugins/in_node_exporter_metrics/ne_cpu.c b/plugins/in_node_exporter_metrics/ne_cpu.c index e92c3334359..b36fc40cbaa 100644 --- a/plugins/in_node_exporter_metrics/ne_cpu.c +++ b/plugins/in_node_exporter_metrics/ne_cpu.c @@ -22,4 +22,30 @@ #include "ne_cpufreq_linux.c" #elif __APPLE__ #include "ne_cpu_darwin.c" + +struct flb_ne_collector cpufreq_collector = { + .name = "cpufreq", + .cb_init = NULL, + .cb_update = NULL, + .cb_exit = NULL +}; + +#else + +#include "ne.h" + +struct flb_ne_collector cpu_collector = { + .name = "cpu", + .cb_init = NULL, + .cb_update = NULL, + .cb_exit = NULL +}; + +struct flb_ne_collector cpufreq_collector = { + .name = "cpufreq", + .cb_init = NULL, + .cb_update = NULL, + .cb_exit = NULL +}; + #endif diff --git a/plugins/in_node_exporter_metrics/ne_cpu.h b/plugins/in_node_exporter_metrics/ne_cpu.h index 814652ea6db..a7857fbe219 100644 --- a/plugins/in_node_exporter_metrics/ne_cpu.h +++ b/plugins/in_node_exporter_metrics/ne_cpu.h @@ -22,9 +22,9 @@ #include "ne.h" +extern struct flb_ne_collector cpu_collector; + #ifdef __linux__ -int ne_cpu_init(struct flb_ne *ctx); -int ne_cpu_update(struct flb_ne *ctx); #elif __APPLE__ int ne_cpu_init(struct flb_ne *ctx); int ne_cpu_update(struct flb_ne *ctx); diff --git a/plugins/in_node_exporter_metrics/ne_cpu_darwin.c b/plugins/in_node_exporter_metrics/ne_cpu_darwin.c index 983e763f5ac..0a9b8ba8dcd 100644 --- a/plugins/in_node_exporter_metrics/ne_cpu_darwin.c +++ b/plugins/in_node_exporter_metrics/ne_cpu_darwin.c @@ -59,7 +59,7 @@ static inline int cpu_stat_init(struct flb_ne *ctx) return 0; } -int ne_cpu_init(struct flb_ne *ctx) +static int ne_cpu_init(struct flb_ne *ctx) { int ret; @@ -121,9 +121,10 @@ static int cpu_stat_update(struct flb_ne *ctx, uint64_t ts) return 0; } -int ne_cpu_update(struct flb_ne *ctx) +static int ne_cpu_update(struct flb_input_instance *ins, struct flb_config *config, void *in_context) { uint64_t ts; + struct flb_ne *ctx = (struct flb_ne *)in_context; ts = cfl_time_now(); @@ -131,3 +132,10 @@ int ne_cpu_update(struct flb_ne *ctx) return 0; } + +struct flb_ne_collector cpu_collector = { + .name = "cpu", + .cb_init = ne_cpu_init, + .cb_update = ne_cpu_update, + .cb_exit = NULL +}; diff --git a/plugins/in_node_exporter_metrics/ne_cpu_linux.c b/plugins/in_node_exporter_metrics/ne_cpu_linux.c index 8963f0c5599..7ce31eef924 100644 --- a/plugins/in_node_exporter_metrics/ne_cpu_linux.c +++ b/plugins/in_node_exporter_metrics/ne_cpu_linux.c @@ -362,7 +362,7 @@ static int cpu_stat_update(struct flb_ne *ctx, uint64_t ts) return 0; } -int ne_cpu_init(struct flb_ne *ctx) +static int ne_cpu_init(struct flb_ne *ctx) { int ret; @@ -383,9 +383,10 @@ int ne_cpu_init(struct flb_ne *ctx) return 0; } -int ne_cpu_update(struct flb_ne *ctx) +static int ne_cpu_update(struct flb_input_instance *ins, struct flb_config *config, void *in_context) { uint64_t ts; + struct flb_ne *ctx = (struct flb_ne *)in_context; ts = cfl_time_now(); @@ -394,3 +395,10 @@ int ne_cpu_update(struct flb_ne *ctx) return 0; } + +struct flb_ne_collector cpu_collector = { + .name = "cpu", + .cb_init = ne_cpu_init, + .cb_update = ne_cpu_update, + .cb_exit = NULL +}; diff --git a/plugins/in_node_exporter_metrics/ne_cpufreq.h b/plugins/in_node_exporter_metrics/ne_cpufreq.h index b6712dd5aad..46adcff1885 100644 --- a/plugins/in_node_exporter_metrics/ne_cpufreq.h +++ b/plugins/in_node_exporter_metrics/ne_cpufreq.h @@ -22,7 +22,5 @@ #include "ne.h" -int ne_cpufreq_init(struct flb_ne *ctx); -int ne_cpufreq_update(struct flb_ne *ctx); - +extern struct flb_ne_collector cpufreq_collector; #endif diff --git a/plugins/in_node_exporter_metrics/ne_cpufreq_linux.c b/plugins/in_node_exporter_metrics/ne_cpufreq_linux.c index e31b976d8df..5e4f7591a13 100644 --- a/plugins/in_node_exporter_metrics/ne_cpufreq_linux.c +++ b/plugins/in_node_exporter_metrics/ne_cpufreq_linux.c @@ -183,14 +183,22 @@ static int cpufreq_update(struct flb_ne *ctx) return 0; } -int ne_cpufreq_init(struct flb_ne *ctx) +static int ne_cpufreq_init(struct flb_ne *ctx) { cpufreq_init(ctx); return 0; } -int ne_cpufreq_update(struct flb_ne *ctx) +static int ne_cpufreq_update(struct flb_input_instance *ins, struct flb_config *config, void *in_context) { + struct flb_ne *ctx = (struct flb_ne *)in_context; cpufreq_update(ctx); return 0; } + +struct flb_ne_collector cpufreq_collector = { + .name = "cpufreq", + .cb_init = ne_cpufreq_init, + .cb_update = ne_cpufreq_update, + .cb_exit = NULL +}; diff --git a/plugins/in_node_exporter_metrics/ne_diskstats.c b/plugins/in_node_exporter_metrics/ne_diskstats.c index 5bb246235c9..4f3d406f42c 100644 --- a/plugins/in_node_exporter_metrics/ne_diskstats.c +++ b/plugins/in_node_exporter_metrics/ne_diskstats.c @@ -21,4 +21,15 @@ #include "ne_diskstats_linux.c" #elif __APPLE__ #include "ne_diskstats_darwin.c" +#else + +#include "ne.h" + +struct flb_ne_collector diskstats_collector = { + .name = "diskstats", + .cb_init = NULL, + .cb_update = NULL, + .cb_exit = NULL +}; + #endif diff --git a/plugins/in_node_exporter_metrics/ne_diskstats.h b/plugins/in_node_exporter_metrics/ne_diskstats.h index 34cc111f503..eb76e447bd2 100644 --- a/plugins/in_node_exporter_metrics/ne_diskstats.h +++ b/plugins/in_node_exporter_metrics/ne_diskstats.h @@ -22,25 +22,6 @@ #include "ne.h" -#if defined(__linux__) || defined(__APPLE__) -int ne_diskstats_init(struct flb_ne *ctx); -int ne_diskstats_update(struct flb_ne *ctx); -int ne_diskstats_exit(struct flb_ne *ctx); -#else -static int ne_diskstats_init(struct flb_ne *ctx) -{ - return 0; -} - -static int ne_diskstats_update(struct flb_ne *ctx) -{ - return 0; -} - -static int ne_diskstats_exit(struct flb_ne *ctx) -{ - return 0; -} -#endif +extern struct flb_ne_collector diskstats_collector; #endif diff --git a/plugins/in_node_exporter_metrics/ne_diskstats_darwin.c b/plugins/in_node_exporter_metrics/ne_diskstats_darwin.c index 55c6bafedc6..07099c302e8 100644 --- a/plugins/in_node_exporter_metrics/ne_diskstats_darwin.c +++ b/plugins/in_node_exporter_metrics/ne_diskstats_darwin.c @@ -462,19 +462,20 @@ static int diskstats_update(struct flb_ne *ctx) return 0; } -int ne_diskstats_init(struct flb_ne *ctx) +static int ne_diskstats_init(struct flb_ne *ctx) { ne_diskstats_configure(ctx); return 0; } -int ne_diskstats_update(struct flb_ne *ctx) +static int ne_diskstats_update(struct flb_input_instance *ins, struct flb_config *config, void *in_context) { + struct flb_ne *ctx = (struct flb_ne *)in_context; diskstats_update(ctx); return 0; } -int ne_diskstats_exit(struct flb_ne *ctx) +static int ne_diskstats_exit(struct flb_ne *ctx) { flb_free(ctx->dt_metrics); if (ctx->dt_regex_skip_devices) { @@ -482,3 +483,11 @@ int ne_diskstats_exit(struct flb_ne *ctx) } return 0; } + + +struct flb_ne_collector diskstats_collector = { + .name = "diskstats", + .cb_init = ne_diskstats_init, + .cb_update = ne_diskstats_update, + .cb_exit = ne_diskstats_exit +}; diff --git a/plugins/in_node_exporter_metrics/ne_diskstats_linux.c b/plugins/in_node_exporter_metrics/ne_diskstats_linux.c index 26ba9cb9373..e2f581d5de9 100644 --- a/plugins/in_node_exporter_metrics/ne_diskstats_linux.c +++ b/plugins/in_node_exporter_metrics/ne_diskstats_linux.c @@ -427,19 +427,20 @@ static int diskstats_update(struct flb_ne *ctx) return 0; } -int ne_diskstats_init(struct flb_ne *ctx) +static int ne_diskstats_init(struct flb_ne *ctx) { ne_diskstats_configure(ctx); return 0; } -int ne_diskstats_update(struct flb_ne *ctx) +static int ne_diskstats_update(struct flb_input_instance *ins, struct flb_config *config, void *in_context) { + struct flb_ne *ctx = (struct flb_ne *)in_context; diskstats_update(ctx); return 0; } -int ne_diskstats_exit(struct flb_ne *ctx) +static int ne_diskstats_exit(struct flb_ne *ctx) { flb_free(ctx->dt_metrics); if (ctx->dt_regex_skip_devices) { @@ -447,3 +448,10 @@ int ne_diskstats_exit(struct flb_ne *ctx) } return 0; } + +struct flb_ne_collector diskstats_collector = { + .name = "diskstats", + .cb_init = ne_diskstats_init, + .cb_update = ne_diskstats_update, + .cb_exit = ne_diskstats_exit +}; diff --git a/plugins/in_node_exporter_metrics/ne_filefd.c b/plugins/in_node_exporter_metrics/ne_filefd.c new file mode 100644 index 00000000000..c63a023fb8d --- /dev/null +++ b/plugins/in_node_exporter_metrics/ne_filefd.c @@ -0,0 +1,33 @@ +/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ + +/* Fluent Bit + * ========== + * Copyright (C) 2015-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_filefd_linux.c" +#else + +#include "ne.h" + +struct flb_ne_collector filefd_collector = { + .name = "filefd", + .cb_init = NULL, + .cb_update = NULL, + .cb_exit = NULL +}; + +#endif diff --git a/plugins/in_node_exporter_metrics/ne_vmstat_linux.h b/plugins/in_node_exporter_metrics/ne_filefd.h similarity index 80% rename from plugins/in_node_exporter_metrics/ne_vmstat_linux.h rename to plugins/in_node_exporter_metrics/ne_filefd.h index 08b78f4f476..b91e41d5e62 100644 --- a/plugins/in_node_exporter_metrics/ne_vmstat_linux.h +++ b/plugins/in_node_exporter_metrics/ne_filefd.h @@ -17,13 +17,11 @@ * limitations under the License. */ -#ifndef FLB_IN_NE_VMSTAT_LINUX_H -#define FLB_IN_NE_VMSTAT_LINUX_H +#ifndef FLB_IN_NE_FILEFD_H +#define FLB_IN_NE_FILEFD_H #include "ne.h" -int ne_vmstat_init(struct flb_ne *ctx); -int ne_vmstat_update(struct flb_ne *ctx); -int ne_vmstat_exit(struct flb_ne *ctx); +extern struct flb_ne_collector filefd_collector; #endif diff --git a/plugins/in_node_exporter_metrics/ne_filefd_linux.c b/plugins/in_node_exporter_metrics/ne_filefd_linux.c index 5c0c0166d9a..206892b4852 100644 --- a/plugins/in_node_exporter_metrics/ne_filefd_linux.c +++ b/plugins/in_node_exporter_metrics/ne_filefd_linux.c @@ -102,14 +102,23 @@ static int filefd_update(struct flb_ne *ctx) return 0; } -int ne_filefd_init(struct flb_ne *ctx) +static int ne_filefd_init(struct flb_ne *ctx) { filefd_configure(ctx); return 0; } -int ne_filefd_update(struct flb_ne *ctx) +static int ne_filefd_update(struct flb_input_instance *ins, struct flb_config *config, void *in_context) { + struct flb_ne *ctx = (struct flb_ne *)in_context; + filefd_update(ctx); return 0; } + +struct flb_ne_collector filefd_collector = { + .name = "filefd", + .cb_init = ne_filefd_init, + .cb_update = ne_filefd_update, + .cb_exit = NULL +}; diff --git a/plugins/in_node_exporter_metrics/ne_filesystem.c b/plugins/in_node_exporter_metrics/ne_filesystem.c index 47d13a19b14..08a5773f7e8 100644 --- a/plugins/in_node_exporter_metrics/ne_filesystem.c +++ b/plugins/in_node_exporter_metrics/ne_filesystem.c @@ -19,4 +19,15 @@ #ifdef __linux__ #include "ne_filesystem_linux.c" +#else + +#include "ne.h" + +struct flb_ne_collector filesystem_collector = { + .name = "filesystem", + .cb_init = NULL, + .cb_update = NULL, + .cb_exit = NULL +}; + #endif diff --git a/plugins/in_node_exporter_metrics/ne_filesystem.h b/plugins/in_node_exporter_metrics/ne_filesystem.h index 8bd2bc64876..b480c69fbc8 100644 --- a/plugins/in_node_exporter_metrics/ne_filesystem.h +++ b/plugins/in_node_exporter_metrics/ne_filesystem.h @@ -22,25 +22,6 @@ #include "ne.h" -#ifdef __linux__ -int ne_filesystem_init(struct flb_ne *ctx); -int ne_filesystem_update(struct flb_ne *ctx); -int ne_filesystem_exit(struct flb_ne *ctx); -#else -static int ne_filesystem_init(struct flb_ne *ctx) -{ - return 0; -} - -static int ne_filesystem_update(struct flb_ne *ctx) -{ - return 0; -} - -static int ne_filesystem_exit(struct flb_ne *ctx) -{ - return 0; -} -#endif +extern struct flb_ne_collector filesystem_collector; #endif diff --git a/plugins/in_node_exporter_metrics/ne_filesystem_linux.c b/plugins/in_node_exporter_metrics/ne_filesystem_linux.c index 5f054aa78d3..0ac3a44d2c4 100644 --- a/plugins/in_node_exporter_metrics/ne_filesystem_linux.c +++ b/plugins/in_node_exporter_metrics/ne_filesystem_linux.c @@ -275,7 +275,7 @@ static int filesystem_update(struct flb_ne *ctx, return NE_ERROR_MOUNT_POINT_LIST_FETCH_SUCCESS; } -int ne_filesystem_init(struct flb_ne *ctx) +static int ne_filesystem_init(struct flb_ne *ctx) { ctx->fs_regex_skip_mount = flb_regex_create(ctx->fs_regex_ingore_mount_point_text); ctx->fs_regex_skip_fs_types = flb_regex_create(ctx->fs_regex_ingore_filesystem_type_text); @@ -377,9 +377,10 @@ int ne_filesystem_init(struct flb_ne *ctx) return 0; } -int ne_filesystem_update(struct flb_ne *ctx) +static int ne_filesystem_update(struct flb_input_instance *ins, struct flb_config *config, void *in_context) { int result; + struct flb_ne *ctx = (struct flb_ne *)in_context; result = filesystem_update(ctx, "/proc/1/mounts"); @@ -390,7 +391,7 @@ int ne_filesystem_update(struct flb_ne *ctx) return 0; } -int ne_filesystem_exit(struct flb_ne *ctx) +static int ne_filesystem_exit(struct flb_ne *ctx) { if (ctx->fs_regex_skip_mount != NULL) { flb_regex_destroy(ctx->fs_regex_skip_mount); @@ -402,3 +403,10 @@ int ne_filesystem_exit(struct flb_ne *ctx) return 0; } + +struct flb_ne_collector filesystem_collector = { + .name = "filesystem", + .cb_init = ne_filesystem_init, + .cb_update = ne_filesystem_update, + .cb_exit = ne_filesystem_exit +}; diff --git a/plugins/in_node_exporter_metrics/ne_loadavg.c b/plugins/in_node_exporter_metrics/ne_loadavg.c index efef4f3efc1..464acfbf162 100644 --- a/plugins/in_node_exporter_metrics/ne_loadavg.c +++ b/plugins/in_node_exporter_metrics/ne_loadavg.c @@ -21,4 +21,15 @@ #include "ne_loadavg_linux.c" #elif __APPLE__ #include "ne_loadavg_darwin.c" +#else + +#include "ne.h" + +struct flb_ne_collector loadavg_collector = { + .name = "loadavg", + .cb_init = NULL, + .cb_update = NULL, + .cb_exit = NULL +}; + #endif diff --git a/plugins/in_node_exporter_metrics/ne_loadavg.h b/plugins/in_node_exporter_metrics/ne_loadavg.h index 874f4ab5443..a30163a68af 100644 --- a/plugins/in_node_exporter_metrics/ne_loadavg.h +++ b/plugins/in_node_exporter_metrics/ne_loadavg.h @@ -22,25 +22,6 @@ #include "ne.h" -#if defined(__linux__) || defined(__APPLE__) -int ne_loadavg_init(struct flb_ne *ctx); -int ne_loadavg_update(struct flb_ne *ctx); -int ne_loadavg_exit(struct flb_ne *ctx); -#else -static int ne_loadavg_init(struct flb_ne *ctx) -{ - return 0; -} - -static int ne_loadavg_update(struct flb_ne *ctx) -{ - return 0; -} - -static int ne_loadavg_exit(struct flb_ne *ctx) -{ - return 0; -} -#endif +extern struct flb_ne_collector loadavg_collector; #endif diff --git a/plugins/in_node_exporter_metrics/ne_loadavg_darwin.c b/plugins/in_node_exporter_metrics/ne_loadavg_darwin.c index 8c803787683..4423e34b7dd 100644 --- a/plugins/in_node_exporter_metrics/ne_loadavg_darwin.c +++ b/plugins/in_node_exporter_metrics/ne_loadavg_darwin.c @@ -78,14 +78,23 @@ static int loadavg_update(struct flb_ne *ctx) return 0; } -int ne_loadavg_init(struct flb_ne *ctx) +static int ne_loadavg_init(struct flb_ne *ctx) { ne_loadavg_configure(ctx); return 0; } -int ne_loadavg_update(struct flb_ne *ctx) +static int ne_loadavg_update(struct flb_input_instance *ins, struct flb_config *config, void *in_context) { + struct flb_ne *ctx = (struct flb_ne *)in_context; + loadavg_update(ctx); return 0; } + +struct flb_ne_collector loadavg_collector = { + .name = "loadavg", + .cb_init = ne_loadavg_init, + .cb_update = ne_loadavg_update, + .cb_exit = NULL +}; diff --git a/plugins/in_node_exporter_metrics/ne_loadavg_linux.c b/plugins/in_node_exporter_metrics/ne_loadavg_linux.c index 13d872593c3..d4107362fc6 100644 --- a/plugins/in_node_exporter_metrics/ne_loadavg_linux.c +++ b/plugins/in_node_exporter_metrics/ne_loadavg_linux.c @@ -113,14 +113,23 @@ static int loadavg_update(struct flb_ne *ctx) return 0; } -int ne_loadavg_init(struct flb_ne *ctx) +static int ne_loadavg_init(struct flb_ne *ctx) { ne_loadavg_configure(ctx); return 0; } -int ne_loadavg_update(struct flb_ne *ctx) +static int ne_loadavg_update(struct flb_input_instance *ins, struct flb_config *config, void *in_context) { + struct flb_ne *ctx = (struct flb_ne *)in_context; + loadavg_update(ctx); return 0; } + +struct flb_ne_collector loadavg_collector = { + .name = "loadavg", + .cb_init = ne_loadavg_init, + .cb_update = ne_loadavg_update, + .cb_exit = NULL +}; diff --git a/plugins/in_node_exporter_metrics/ne_meminfo.c b/plugins/in_node_exporter_metrics/ne_meminfo.c index cf7d93dca03..752bc098d58 100644 --- a/plugins/in_node_exporter_metrics/ne_meminfo.c +++ b/plugins/in_node_exporter_metrics/ne_meminfo.c @@ -22,4 +22,15 @@ #include "ne_meminfo_linux.c" #elif __APPLE__ #include "ne_meminfo_darwin.c" +#else + +#include "ne.h" + +struct flb_ne_collector meminfo_collector = { + .name = "meminfo", + .cb_init = NULL, + .cb_update = NULL, + .cb_exit = NULL +}; + #endif diff --git a/plugins/in_node_exporter_metrics/ne_meminfo.h b/plugins/in_node_exporter_metrics/ne_meminfo.h index a038b2c3443..cb1c19fc188 100644 --- a/plugins/in_node_exporter_metrics/ne_meminfo.h +++ b/plugins/in_node_exporter_metrics/ne_meminfo.h @@ -22,25 +22,6 @@ #include "ne.h" -#if defined(__linux__) || defined(__APPLE__) -int ne_meminfo_init(struct flb_ne *ctx); -int ne_meminfo_update(struct flb_ne *ctx); -int ne_meminfo_exit(struct flb_ne *ctx); -#else -static int ne_meminfo_init(struct flb_ne *ctx) -{ - return 0; -} - -static int ne_meminfo_update(struct flb_ne *ctx) -{ - return 0; -} - -static int ne_meminfo_exit(struct flb_ne *ctx) -{ - return 0; -} -#endif +extern struct flb_ne_collector meminfo_collector; #endif diff --git a/plugins/in_node_exporter_metrics/ne_meminfo_darwin.c b/plugins/in_node_exporter_metrics/ne_meminfo_darwin.c index a2d18b0dd59..2ec3c9d8f14 100644 --- a/plugins/in_node_exporter_metrics/ne_meminfo_darwin.c +++ b/plugins/in_node_exporter_metrics/ne_meminfo_darwin.c @@ -215,15 +215,16 @@ static int meminfo_update(struct flb_ne *ctx, uint64_t ts) return 0; } -int ne_meminfo_init(struct flb_ne *ctx) +static int ne_meminfo_init(struct flb_ne *ctx) { meminfo_configure(ctx); return 0; } -int ne_meminfo_update(struct flb_ne *ctx) +static int ne_meminfo_update(struct flb_input_instance *ins, struct flb_config *config, void *in_context) { uint64_t ts; + struct flb_ne *ctx = (struct flb_ne *)in_context; ts = cfl_time_now(); @@ -231,7 +232,14 @@ int ne_meminfo_update(struct flb_ne *ctx) return 0; } -int ne_meminfo_exit(struct flb_ne *ctx) +static int ne_meminfo_exit(struct flb_ne *ctx) { return 0; } + +struct flb_ne_collector meminfo_collector = { + .name = "meminfo", + .cb_init = ne_meminfo_init, + .cb_update = ne_meminfo_update, + .cb_exit = ne_meminfo_exit +}; diff --git a/plugins/in_node_exporter_metrics/ne_meminfo_linux.c b/plugins/in_node_exporter_metrics/ne_meminfo_linux.c index 3189d53c0be..bbcb1af8b10 100644 --- a/plugins/in_node_exporter_metrics/ne_meminfo_linux.c +++ b/plugins/in_node_exporter_metrics/ne_meminfo_linux.c @@ -262,22 +262,30 @@ static int meminfo_update(struct flb_ne *ctx) return 0; } -int ne_meminfo_init(struct flb_ne *ctx) +static int ne_meminfo_init(struct flb_ne *ctx) { meminfo_configure(ctx); return 0; } -int ne_meminfo_update(struct flb_ne *ctx) +static int ne_meminfo_update(struct flb_input_instance *ins, struct flb_config *config, void *in_context) { + struct flb_ne *ctx = (struct flb_ne *)in_context; meminfo_update(ctx); return 0; } -int ne_meminfo_exit(struct flb_ne *ctx) +static int ne_meminfo_exit(struct flb_ne *ctx) { if (ctx->meminfo_ht) { flb_hash_table_destroy(ctx->meminfo_ht); } return 0; } + +struct flb_ne_collector meminfo_collector = { + .name = "meminfo", + .cb_init = ne_meminfo_init, + .cb_update = ne_meminfo_update, + .cb_exit = ne_meminfo_exit +}; diff --git a/plugins/in_node_exporter_metrics/ne_netdev.c b/plugins/in_node_exporter_metrics/ne_netdev.c index 0673ffd86bd..fc17dbb29b5 100644 --- a/plugins/in_node_exporter_metrics/ne_netdev.c +++ b/plugins/in_node_exporter_metrics/ne_netdev.c @@ -21,4 +21,15 @@ #include "ne_netdev_linux.c" #elif __APPLE__ #include "ne_netdev_darwin.c" +#else + +#include "ne.h" + +struct flb_ne_collector netdev_collector = { + .name = "netdev", + .cb_init = NULL, + .cb_update = NULL, + .cb_exit = NULL +}; + #endif diff --git a/plugins/in_node_exporter_metrics/ne_netdev.h b/plugins/in_node_exporter_metrics/ne_netdev.h index cc964ae128a..fbf9af57dde 100644 --- a/plugins/in_node_exporter_metrics/ne_netdev.h +++ b/plugins/in_node_exporter_metrics/ne_netdev.h @@ -22,25 +22,6 @@ #include "ne.h" -#if defined(__linux__) || defined(__APPLE__) -int ne_netdev_init(struct flb_ne *ctx); -int ne_netdev_update(struct flb_ne *ctx); -int ne_netdev_exit(struct flb_ne *ctx); -#else -static int ne_netdev_init(struct flb_ne *ctx) -{ - return 0; -} - -static int ne_netdev_update(struct flb_ne *ctx) -{ - return 0; -} - -static int ne_netdev_exit(struct flb_ne *ctx) -{ - return 0; -} -#endif +extern struct flb_ne_collector netdev_collector; #endif diff --git a/plugins/in_node_exporter_metrics/ne_netdev_darwin.c b/plugins/in_node_exporter_metrics/ne_netdev_darwin.c index 4f3aaef36f8..a344c6313c7 100644 --- a/plugins/in_node_exporter_metrics/ne_netdev_darwin.c +++ b/plugins/in_node_exporter_metrics/ne_netdev_darwin.c @@ -203,19 +203,28 @@ static int netdev_update(struct flb_ne *ctx) return 0; } -int ne_netdev_init(struct flb_ne *ctx) +static int ne_netdev_init(struct flb_ne *ctx) { netdev_configure(ctx); return 0; } -int ne_netdev_update(struct flb_ne *ctx) +static int ne_netdev_update(struct flb_input_instance *ins, struct flb_config *config, void *in_context) { + struct flb_ne *ctx = (struct flb_ne *)in_context; + netdev_update(ctx); return 0; } -int ne_netdev_exit(struct flb_ne *ctx) +static int ne_netdev_exit(struct flb_ne *ctx) { return 0; } + +struct flb_ne_collector netdev_collector = { + .name = "netdev", + .cb_init = ne_netdev_init, + .cb_update = ne_netdev_update, + .cb_exit = ne_netdev_exit +}; diff --git a/plugins/in_node_exporter_metrics/ne_netdev_linux.c b/plugins/in_node_exporter_metrics/ne_netdev_linux.c index 771a6618969..d9e29d7f5be 100644 --- a/plugins/in_node_exporter_metrics/ne_netdev_linux.c +++ b/plugins/in_node_exporter_metrics/ne_netdev_linux.c @@ -342,22 +342,31 @@ static int netdev_update(struct flb_ne *ctx) } -int ne_netdev_init(struct flb_ne *ctx) +static int ne_netdev_init(struct flb_ne *ctx) { netdev_configure(ctx); return 0; } -int ne_netdev_update(struct flb_ne *ctx) +static int ne_netdev_update(struct flb_input_instance *ins, struct flb_config *config, void *in_context) { + struct flb_ne *ctx = (struct flb_ne *)in_context; + netdev_update(ctx); return 0; } -int ne_netdev_exit(struct flb_ne *ctx) +static int ne_netdev_exit(struct flb_ne *ctx) { if (ctx->netdev_ht) { flb_hash_table_destroy(ctx->netdev_ht); } return 0; } + +struct flb_ne_collector netdev_collector = { + .name = "netdev", + .cb_init = ne_netdev_init, + .cb_update = ne_netdev_update, + .cb_exit = ne_netdev_exit +}; diff --git a/plugins/in_node_exporter_metrics/ne_processes.c b/plugins/in_node_exporter_metrics/ne_processes.c index eb8f13d5190..a97f3615c18 100644 --- a/plugins/in_node_exporter_metrics/ne_processes.c +++ b/plugins/in_node_exporter_metrics/ne_processes.c @@ -19,4 +19,15 @@ #ifdef __linux__ #include "ne_processes_linux.c" +#else + +#include "ne.h" + +struct flb_ne_collector processes_collector = { + .name = "processes", + .cb_init = NULL, + .cb_update = NULL, + .cb_exit = NULL +}; + #endif diff --git a/plugins/in_node_exporter_metrics/ne_processes.h b/plugins/in_node_exporter_metrics/ne_processes.h index d5108c868b2..338b74000b6 100644 --- a/plugins/in_node_exporter_metrics/ne_processes.h +++ b/plugins/in_node_exporter_metrics/ne_processes.h @@ -22,26 +22,6 @@ #include "ne.h" -#ifdef __linux__ -int ne_processes_init(struct flb_ne *ctx); -int ne_processes_update(struct flb_ne *ctx); -int ne_processes_exit(struct flb_ne *ctx); -#else -static int ne_processes_init(struct flb_ne *ctx) -{ - return 0; -} - -static int ne_processes_update(struct flb_ne *ctx) -{ - return 0; -} - -static int ne_processes_exit(struct flb_ne *ctx) -{ - return 0; -} - -#endif +extern struct flb_ne_collector processes_collector; #endif diff --git a/plugins/in_node_exporter_metrics/ne_processes_linux.c b/plugins/in_node_exporter_metrics/ne_processes_linux.c index 431c8f268ff..6c046a41b27 100644 --- a/plugins/in_node_exporter_metrics/ne_processes_linux.c +++ b/plugins/in_node_exporter_metrics/ne_processes_linux.c @@ -398,19 +398,28 @@ static int processes_update(struct flb_ne *ctx) return 0; } -int ne_processes_init(struct flb_ne *ctx) +static int ne_processes_init(struct flb_ne *ctx) { processes_configure(ctx); return 0; } -int ne_processes_update(struct flb_ne *ctx) +static int ne_processes_update(struct flb_input_instance *ins, struct flb_config *config, void *in_context) { + struct flb_ne *ctx = (struct flb_ne *)in_context; + processes_update(ctx); return 0; } -int ne_processes_exit(struct flb_ne *ctx) +static int ne_processes_exit(struct flb_ne *ctx) { return 0; } + +struct flb_ne_collector processes_collector = { + .name = "processes", + .cb_init = ne_processes_init, + .cb_update = ne_processes_update, + .cb_exit = ne_processes_exit +}; diff --git a/plugins/in_node_exporter_metrics/ne_stat.c b/plugins/in_node_exporter_metrics/ne_stat.c new file mode 100644 index 00000000000..6f5566f28d6 --- /dev/null +++ b/plugins/in_node_exporter_metrics/ne_stat.c @@ -0,0 +1,33 @@ +/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ + +/* Fluent Bit + * ========== + * Copyright (C) 2015-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_stat_linux.c" +#else + +#include "ne.h" + +struct flb_ne_collector stat_collector = { + .name = "stat", + .cb_init = NULL, + .cb_update = NULL, + .cb_exit = NULL +}; + +#endif diff --git a/plugins/in_node_exporter_metrics/ne_stat_linux.h b/plugins/in_node_exporter_metrics/ne_stat.h similarity index 91% rename from plugins/in_node_exporter_metrics/ne_stat_linux.h rename to plugins/in_node_exporter_metrics/ne_stat.h index 6d92a1492d1..f70fc80a0a3 100644 --- a/plugins/in_node_exporter_metrics/ne_stat_linux.h +++ b/plugins/in_node_exporter_metrics/ne_stat.h @@ -22,7 +22,6 @@ #include "ne.h" -int ne_stat_init(struct flb_ne *ctx); -int ne_stat_update(struct flb_ne *ctx); +extern struct flb_ne_collector stat_collector; #endif diff --git a/plugins/in_node_exporter_metrics/ne_stat_linux.c b/plugins/in_node_exporter_metrics/ne_stat_linux.c index 053fb21816f..b60bbe17556 100644 --- a/plugins/in_node_exporter_metrics/ne_stat_linux.c +++ b/plugins/in_node_exporter_metrics/ne_stat_linux.c @@ -139,14 +139,23 @@ static int stat_update(struct flb_ne *ctx) return 0; } -int ne_stat_init(struct flb_ne *ctx) +static int ne_stat_init(struct flb_ne *ctx) { stat_configure(ctx); return 0; } -int ne_stat_update(struct flb_ne *ctx) +static int ne_stat_update(struct flb_input_instance *ins, struct flb_config *config, void *in_context) { + struct flb_ne *ctx = (struct flb_ne *)in_context; + stat_update(ctx); return 0; } + +struct flb_ne_collector stat_collector = { + .name = "stat", + .cb_init = ne_stat_init, + .cb_update = ne_stat_update, + .cb_exit = NULL +}; diff --git a/plugins/in_node_exporter_metrics/ne_systemd.c b/plugins/in_node_exporter_metrics/ne_systemd.c index ec4df245549..ab950dc0dae 100644 --- a/plugins/in_node_exporter_metrics/ne_systemd.c +++ b/plugins/in_node_exporter_metrics/ne_systemd.c @@ -2,7 +2,7 @@ /* Fluent Bit * ========== - * Copyright (C) 2015-2022 The Fluent Bit Authors + * Copyright (C) 2015-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. @@ -17,791 +17,18 @@ * limitations under the License. */ -#include -#include -#include -#include -#include -#include -#include "ne.h" -#include "ne_utils.h" -#include "ne_systemd.h" - -#include -#include - -static int str_ends_with(char *haystack, char *needle, int caseless) { - size_t haystack_length; - size_t trailer_offset; - size_t needle_length; - int result; - - haystack_length = strlen(haystack); - needle_length = strlen(needle); - - if (needle_length > haystack_length) { - return FLB_FALSE; - } - - trailer_offset = haystack_length - needle_length; - - if (caseless) { - result = strcasecmp(&haystack[trailer_offset], - needle); - } - else { - result = strcmp(&haystack[trailer_offset], - needle); - } - - if (result == 0) { - return FLB_TRUE; - } - - return FLB_FALSE; -} - -static void clear_property_variable(char property_type, void *property_value) -{ - if (property_type == 'y') { - *((uint8_t *) property_value) = 0; - } - else if (property_type == 'b') { - *((int *) property_value) = 0; - } - else if (property_type == 'n') { - *((int16_t *) property_value) = 0; - } - else if (property_type == 'q') { - *((uint16_t *) property_value) = 0; - } - else if (property_type == 'i') { - *((int32_t *) property_value) = 0; - } - else if (property_type == 'u') { - *((uint32_t *) property_value) = 0; - } - else if (property_type == 'x') { - *((int64_t *) property_value) = 0; - } - else if (property_type == 't') { - *((uint64_t *) property_value) = 0; - } - else if (property_type == 'd') { - *((double *) property_value) = 0; - } - else if (property_type == 's') { - *((char **) property_value) = NULL; - } - else if (property_type == 'o') { - *((char **) property_value) = NULL; - } - else if (property_type == 'g') { - *((char **) property_value) = NULL; - } - else if (property_type == 'h') { - *((int32_t *) property_value) = -1; - } -} - -static int get_system_property(struct flb_ne *ctx, - char *interface, - char *property_name, - char property_type, - void *property_value) -{ - int result; - - clear_property_variable(property_type, property_value); - - if (interface == NULL) { - interface = "org.freedesktop.systemd1.Manager"; - } - - if (property_type == 's' || - property_type == 'o' || - property_type == 'g') { - result = sd_bus_get_property_string((sd_bus *) ctx->systemd_dbus_handle, - "org.freedesktop.systemd1", - "/org/freedesktop/systemd1", - interface, - property_name, - NULL, - property_value); - } - else { - result = sd_bus_get_property_trivial((sd_bus *) ctx->systemd_dbus_handle, - "org.freedesktop.systemd1", - "/org/freedesktop/systemd1", - interface, - property_name, - NULL, - property_type, - property_value); - } - - if (result < 0) { - return -1; - } - - return 0; -} - -static int get_unit_property(struct flb_ne *ctx, - struct ne_systemd_unit *unit, - char *interface, - char *property_name, - char property_type, - void *property_value) -{ - int result; - - clear_property_variable(property_type, property_value); - - if (interface == NULL) { - if (unit->unit_type == SYSTEMD_UNIT_TYPE_SERVICE) { - interface = "org.freedesktop.systemd1.Service"; - } - else if (unit->unit_type == SYSTEMD_UNIT_TYPE_MOUNT) { - interface = "org.freedesktop.systemd1.Mount"; - } - else if (unit->unit_type == SYSTEMD_UNIT_TYPE_SOCKET) { - interface = "org.freedesktop.systemd1.Socket"; - } - else if (unit->unit_type == SYSTEMD_UNIT_TYPE_TIMER) { - interface = "org.freedesktop.systemd1.Timer"; - } - else { - interface = unit->name; - } - } - - if (property_type == 's' || - property_type == 'o' || - property_type == 'g') { - result = sd_bus_get_property_string((sd_bus *) ctx->systemd_dbus_handle, - "org.freedesktop.systemd1", - unit->path, - interface, - property_name, - NULL, - property_value); - } - else { - result = sd_bus_get_property_trivial((sd_bus *) ctx->systemd_dbus_handle, - "org.freedesktop.systemd1", - unit->path, - interface, - property_name, - NULL, - property_type, - property_value); - } - - if (result < 0) { - return -1; - } - - return 0; -} - -static int ne_systemd_update_unit_state(struct flb_ne *ctx) -{ - char *unit_states[] = { "activating", "active", - "deactivating", "inactive", - "failed" }; - double timer_trigger_timestamp; - uint64_t deactivating_units; - uint64_t activating_units; - double unit_start_time; - uint64_t inactive_units; - uint64_t active_units; - uint64_t failed_units; - int include_flag; - uint64_t timestamp; - int result; - size_t index; - sd_bus_message *reply; - struct ne_systemd_unit unit; - sd_bus *bus; - - bus = (sd_bus *) ctx->systemd_dbus_handle; - - result = sd_bus_call_method(bus, - "org.freedesktop.systemd1", - "/org/freedesktop/systemd1", - "org.freedesktop.systemd1.Manager", - "ListUnits", - NULL, - &reply, - ""); - - if (result < 0) { - return -1; - } - - result = sd_bus_message_enter_container(reply, 'a', "(ssssssouso)"); - - if (result < 0) { - sd_bus_message_unref(reply); - - return -2; - } - - timestamp = cfl_time_now(); - - deactivating_units = 0; - activating_units = 0; - inactive_units = 0; - active_units = 0; - failed_units = 0; - - do { - result = sd_bus_message_read(reply, - "(ssssssouso)", - &unit.name, - &unit.description, - &unit.load_state, - &unit.active_state, - &unit.sub_state, - &unit.followed, - &unit.path, - &unit.job_id, - &unit.job_type, - &unit.object_path); - - - if (result < 0) { - sd_bus_message_unref(reply); - - return -3; - } - else if(result > 0) { - unit.type = NULL; - - if (strcasecmp(unit.active_state, "activating") == 0) { - activating_units++; - } - else if (strcasecmp(unit.active_state, "deactivating") == 0) { - deactivating_units++; - } - else if (strcasecmp(unit.active_state, "inactive") == 0) { - inactive_units++; - } - else if (strcasecmp(unit.active_state, "active") == 0) { - active_units++; - } - else if (strcasecmp(unit.active_state, "failed") == 0) { - failed_units++; - } - - if (ctx->systemd_regex_include_list != NULL) { - include_flag = flb_regex_match(ctx->systemd_regex_include_list, - (unsigned char *) unit.name, - strlen(unit.name)); - } - else { - include_flag = FLB_TRUE; - } - - if (!include_flag) { - continue; - } - - if (ctx->systemd_regex_exclude_list != NULL) { - include_flag = !flb_regex_match(ctx->systemd_regex_exclude_list, - (unsigned char *) unit.name, - strlen(unit.name)); - } - else { - include_flag = FLB_TRUE; - } - - if (!include_flag) { - continue; - } - - if (strcasecmp(unit.load_state, "loaded") != 0) { - continue; - } - - if (str_ends_with(unit.name, ".service", FLB_TRUE)) { - unit.unit_type = SYSTEMD_UNIT_TYPE_SERVICE; - - result = get_service_type(ctx, - &unit, - &unit.type); - - if (ctx->systemd_include_service_restarts) { - result = get_service_restart_count(ctx, - &unit, - &unit.restart_count); - - cmt_counter_set(ctx->systemd_service_restarts, - timestamp, - unit.restart_count, - 1, - (char *[]){ unit.name }); - - } - - if (ctx->systemd_include_service_task_metrics) { - result = get_service_active_tasks(ctx, - &unit, - &unit.active_tasks); - - if (unit.active_tasks != UINT64_MAX) { - cmt_gauge_set(ctx->systemd_unit_tasks, - timestamp, - unit.active_tasks, - 1, - (char *[]){ unit.name }); - } - - result = get_service_max_tasks(ctx, - &unit, - &unit.max_tasks); - - if (unit.max_tasks != UINT64_MAX) { - cmt_gauge_set(ctx->systemd_unit_tasks_max, - timestamp, - unit.max_tasks, - 1, - (char *[]){ unit.name }); - } - } - - result = 1; - } - else if (str_ends_with(unit.name, ".mount", FLB_TRUE)) { - unit.unit_type = SYSTEMD_UNIT_TYPE_MOUNT; - } - else if (str_ends_with(unit.name, ".socket", FLB_TRUE)) { - unit.unit_type = SYSTEMD_UNIT_TYPE_SOCKET; - - result = get_socket_accepted_connection_count( - ctx, - &unit, - &unit.accepted_connections); - - result = get_socket_active_connection_count( - ctx, - &unit, - &unit.active_connections); - - result = get_socket_refused_connection_count( - ctx, - &unit, - &unit.refused_connections); - - cmt_gauge_set(ctx->systemd_socket_accepted_connections, - timestamp, - unit.accepted_connections, - 1, - (char *[]){ unit.name }); - - cmt_gauge_set(ctx->systemd_socket_active_connections, - timestamp, - unit.active_connections, - 1, - (char *[]){ unit.name }); - - cmt_gauge_set(ctx->systemd_socket_refused_connections, - timestamp, - unit.refused_connections, - 1, - (char *[]){ unit.name }); - - result = 1; - } - else if (str_ends_with(unit.name, ".timer", FLB_TRUE)) { - unit.unit_type = SYSTEMD_UNIT_TYPE_TIMER; - - result = get_timer_last_trigger_timestamp( - ctx, - &unit, - &unit.last_trigger_timestamp); - - timer_trigger_timestamp = (double) unit.last_trigger_timestamp; - timer_trigger_timestamp /= 1000000.0; - - cmt_gauge_set(ctx->systemd_timer_last_trigger_seconds, - timestamp, - timer_trigger_timestamp, - 1, - (char *[]){ unit.name }); - - result = 1; - } - else { - unit.unit_type = SYSTEMD_UNIT_TYPE_UNDEFINED; - } - - if (ctx->systemd_include_unit_start_times) { - if (strcasecmp(unit.active_state, "active") == 0) { - result = get_unit_start_time(ctx, &unit, &unit.start_time); - - unit_start_time = (double) unit.start_time; - unit_start_time /= 1000000.0; - } - else { - unit_start_time = 0; - } - - cmt_gauge_set(ctx->systemd_unit_start_times, - timestamp, - unit_start_time, - 1, - (char *[]){ unit.name }); - - result = 1; - } - - for(index = 0 ; index < 5 ; index++) { - cmt_gauge_add(ctx->systemd_unit_state, - timestamp, - 0, - 3, - (char *[]){ unit.name, - unit_states[index], - unit.type - }); - } +#ifdef FLB_HAVE_SYSTEMD_SDBUS +#include "ne_systemd_linux.c" +#else - cmt_gauge_inc(ctx->systemd_unit_state, - timestamp, - 3, - (char *[]){ unit.name, - unit.active_state, - unit.type - }); - - - if (unit.type != NULL) { - free(unit.type); - } - } - } - while (result > 0); - - sd_bus_message_exit_container(reply); - - sd_bus_message_unref(reply); - - cmt_gauge_set(ctx->systemd_units, - timestamp, - activating_units, - 1, - (char *[]){ "activating" }); - - cmt_gauge_set(ctx->systemd_units, - timestamp, - deactivating_units, - 1, - (char *[]){ "deactivating" }); - - cmt_gauge_set(ctx->systemd_units, - timestamp, - inactive_units, - 1, - (char *[]){ "inactive" }); - - cmt_gauge_set(ctx->systemd_units, - timestamp, - active_units, - 1, - (char *[]){ "active" }); - - cmt_gauge_set(ctx->systemd_units, - timestamp, - failed_units, - 1, - (char *[]){ "failed" }); - - return 0; -} - -static int ne_systemd_update_system_state(struct flb_ne *ctx) -{ - int system_running; - uint64_t timestamp; - char *version; - int result; - char *state; - - timestamp = cfl_time_now(); - - if (!ctx->systemd_initialization_flag) { - result = get_system_version(ctx, &version); - - if (result != 0) { - return -1; - } - - ctx->libsystemd_version_text = version; - ctx->libsystemd_version = strtod(version, NULL); - - cmt_gauge_set(ctx->systemd_version, - timestamp, - ctx->libsystemd_version, - 1, - (char *[]){ ctx->libsystemd_version_text }); - } - else { - cmt_gauge_add(ctx->systemd_version, - timestamp, - 0, - 1, - (char *[]){ ctx->libsystemd_version_text }); - } - - result = get_system_state(ctx, &state); - - if (result != 0) { - return -2; - } - - system_running = 0; - - if (strcasecmp(state, "running") == 0) { - system_running = 1; - } - - cmt_gauge_set(ctx->systemd_system_running, - timestamp, - system_running, - 0, - NULL); - free(state); - - return 0; -} - -int ne_systemd_init(struct flb_ne *ctx) -{ - int result; - - ctx->systemd_dbus_handle = NULL; - - result = sd_bus_open_system((sd_bus **) &ctx->systemd_dbus_handle); - - if (result < 0) { - return -1; - } - - ctx->systemd_socket_accepted_connections = cmt_gauge_create(ctx->cmt, - "node", - "systemd", - "socket_accepted_connections_total", - "Total number of accepted " \ - "socket connections.", - 1, - (char *[]) {"name"}); - - if (ctx->systemd_socket_accepted_connections == NULL) { - return -1; - } - - ctx->systemd_socket_active_connections = cmt_gauge_create(ctx->cmt, - "node", - "systemd", - "socket_current_connections", - "Current number of socket " \ - "connections.", - 1, - (char *[]) {"name"}); - - if (ctx->systemd_socket_active_connections == NULL) { - return -1; - } - - ctx->systemd_socket_refused_connections = cmt_gauge_create(ctx->cmt, - "node", - "systemd", - "socket_refused_connections_total", - "Total number of refused " \ - "socket connections.", - 1, - (char *[]) {"name"}); - - if (ctx->systemd_socket_refused_connections == NULL) { - return -1; - } - - ctx->systemd_system_running = cmt_gauge_create(ctx->cmt, - "node", - "systemd", - "system_running", - "Whether the system is " \ - "operational (see 'systemctl" \ - " is-system-running')", - 0, NULL); - - if (ctx->systemd_system_running == NULL) { - return -1; - } - - ctx->systemd_timer_last_trigger_seconds = cmt_gauge_create(ctx->cmt, - "node", - "systemd", - "timer_last_trigger_seconds", - "Seconds since epoch of " \ - "last trigger.", - 1, - (char *[]) {"name"}); - - if (ctx->systemd_timer_last_trigger_seconds == NULL) { - return -1; - } - - ctx->systemd_service_restarts = cmt_counter_create(ctx->cmt, - "node", - "systemd", - "service_restart_total", - "Service unit count of " \ - "Restart triggers", - 1, (char *[]) {"name"}); - - if (ctx->systemd_service_restarts == NULL) { - return -1; - } - - cmt_counter_allow_reset(ctx->systemd_service_restarts); - - ctx->systemd_unit_tasks = cmt_gauge_create(ctx->cmt, - "node", - "systemd", - "unit_tasks_current", - "Current number of tasks " \ - "per Systemd unit.", - 1, (char *[]) {"name"}); - - if (ctx->systemd_unit_tasks == NULL) { - return -1; - } - - ctx->systemd_unit_tasks_max = cmt_gauge_create(ctx->cmt, - "node", - "systemd", - "unit_tasks_max", - "Maximum number of tasks " \ - "per Systemd unit.", - 1, (char *[]) {"name"}); - - if (ctx->systemd_unit_tasks == NULL) { - return -1; - } - - ctx->systemd_unit_start_times = cmt_gauge_create(ctx->cmt, - "node", - "systemd", - "unit_start_time_seconds", - "Start time of the unit since " \ - "unix epoch in seconds.", - 1, (char *[]) {"name"}); - - if (ctx->systemd_unit_start_times == NULL) { - return -1; - } - - ctx->systemd_unit_state = cmt_gauge_create(ctx->cmt, - "node", - "systemd", - "unit_state", - "Systemd unit", - 3, (char *[]) {"name", - "state", - "type"}); - - if (ctx->systemd_unit_state == NULL) { - return -1; - } - - ctx->systemd_units = cmt_gauge_create(ctx->cmt, - "node", - "systemd", - "units", - "Summary of systemd unit states", - 1, (char *[]) {"state"}); - - if (ctx->systemd_units == NULL) { - return -1; - } - - ctx->systemd_version = cmt_gauge_create(ctx->cmt, - "node", - "systemd", - "version", - "Detected systemd version", - 1, (char *[]) {"version"}); - - if (ctx->systemd_version == NULL) { - return -1; - } - - if (ctx->systemd_regex_include_list_text != NULL) { - ctx->systemd_regex_include_list = \ - flb_regex_create(ctx->systemd_regex_include_list_text); - - if (ctx->systemd_regex_include_list == NULL) { - return -1; - } - } - - if (ctx->systemd_regex_exclude_list_text != NULL) { - ctx->systemd_regex_exclude_list = \ - flb_regex_create(ctx->systemd_regex_exclude_list_text); - - if (ctx->systemd_regex_exclude_list == NULL) { - return -1; - } - } - - return 0; -} - -int ne_systemd_update(struct flb_ne *ctx) -{ - int result; - - result = ne_systemd_update_system_state(ctx); - - if (result != 0) { - return result; - } - - result = ne_systemd_update_unit_state(ctx); - - if (result != 0) { - return result; - } - - if (!ctx->systemd_initialization_flag) { - ctx->systemd_initialization_flag = FLB_TRUE; - } - - return 0; -} - -int ne_systemd_exit(struct flb_ne *ctx) -{ - if (ctx->systemd_dbus_handle != NULL) { - sd_bus_unref((sd_bus *) ctx->systemd_dbus_handle); - - ctx->systemd_dbus_handle = NULL; - } - - if (ctx->systemd_regex_include_list != NULL) { - flb_regex_destroy(ctx->systemd_regex_include_list); - } +#include "ne.h" - if (ctx->systemd_regex_exclude_list != NULL) { - flb_regex_destroy(ctx->systemd_regex_exclude_list); - } +struct flb_ne_collector systemd_collector = { + .name = "systemd", + .cb_init = NULL, + .cb_update = NULL, + .cb_exit = NULL +}; - if (ctx->libsystemd_version_text != NULL) { - flb_free(ctx->libsystemd_version_text); - } - return 0; -} +#endif diff --git a/plugins/in_node_exporter_metrics/ne_systemd.h b/plugins/in_node_exporter_metrics/ne_systemd.h index 8c6fb26c1e9..3359c4cb223 100644 --- a/plugins/in_node_exporter_metrics/ne_systemd.h +++ b/plugins/in_node_exporter_metrics/ne_systemd.h @@ -2,7 +2,7 @@ /* Fluent Bit * ========== - * Copyright (C) 2015-2022 The Fluent Bit Authors + * Copyright (C) 2015-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. @@ -22,106 +22,6 @@ #include "ne.h" -#define SYSTEMD_UNIT_TYPE_UNDEFINED 0 -#define SYSTEMD_UNIT_TYPE_SERVICE 1 -#define SYSTEMD_UNIT_TYPE_SOCKET 2 -#define SYSTEMD_UNIT_TYPE_MOUNT 3 -#define SYSTEMD_UNIT_TYPE_TIMER 4 +extern struct flb_ne_collector systemd_collector; -struct ne_systemd_unit { - char *name; - char *description; - char *load_state; - char *active_state; - char *sub_state; - char *followed; - char *path; - uint32_t job_id; - char *job_type; - char *object_path; - - /* not part of the unit list result */ - uint64_t start_time; - int unit_type; - char *type; - - /* services */ - uint32_t restart_count; - uint64_t active_tasks; - uint64_t max_tasks; - - /* sockets */ - uint32_t accepted_connections; - uint32_t active_connections; - uint32_t refused_connections; - - /* timers */ - uint64_t last_trigger_timestamp; -}; - -#ifdef FLB_HAVE_SYSTEMD_SDBUS -int ne_systemd_init(struct flb_ne *ctx); -int ne_systemd_update(struct flb_ne *ctx); -int ne_systemd_exit(struct flb_ne *ctx); -#else -static int ne_systemd_init(struct flb_ne *ctx) -{ - return 0; -} -static int ne_systemd_update(struct flb_ne *ctx) -{ - return 0; -} -static int ne_systemd_exit(struct flb_ne *ctx) -{ - return 0; -} -#endif - -#define get_system_state(context, output_variable) \ - get_system_property(context, NULL, "SystemState", \ - 's', (void *) (output_variable)) - -#define get_system_version(context, output_variable) \ - get_system_property(context, NULL, "Version", \ - 's', (void *) (output_variable)) - -#define get_service_type(context, unit, output_variable) \ - get_unit_property(context, unit, NULL, "Type", \ - 's', (void *) (output_variable)) - -#define get_service_active_tasks(context, unit, output_variable) \ - get_unit_property(context, unit, NULL, "TasksCurrent", \ - 't', (void *) (output_variable)) - -#define get_service_max_tasks(context, unit, output_variable) \ - get_unit_property(context, unit, NULL, "TasksMax", \ - 't', (void *) (output_variable)) - -#define get_service_restart_count(context, unit, output_variable) \ - get_unit_property(context, unit, NULL, "NRestarts", \ - 'u', (void *) (output_variable)) - -#define get_socket_accepted_connection_count(context, unit, output_variable) \ - get_unit_property(context, unit, NULL, "NAccepted", \ - 'u', (void *) (output_variable)) - -#define get_socket_active_connection_count(context, unit, output_variable) \ - get_unit_property(context, unit, NULL, "NConnections", \ - 'u', (void *) (output_variable)) - -#define get_socket_refused_connection_count(context, unit, output_variable) \ - get_unit_property(context, unit, NULL, "NRefused", \ - 'u', (void *) (output_variable)) - -#define get_timer_last_trigger_timestamp(context, unit, output_variable) \ - get_unit_property(context, unit, NULL, "LastTriggerUSec", \ - 't', (void *) (output_variable)) - -#define get_unit_start_time(context, unit, output_variable) \ - get_unit_property(context, \ - unit, \ - "org.freedesktop.systemd1.Unit", \ - "ActiveEnterTimestamp", \ - 't', (void *) (output_variable)) #endif diff --git a/plugins/in_node_exporter_metrics/ne_systemd_linux.c b/plugins/in_node_exporter_metrics/ne_systemd_linux.c new file mode 100644 index 00000000000..bb0007f5a17 --- /dev/null +++ b/plugins/in_node_exporter_metrics/ne_systemd_linux.c @@ -0,0 +1,815 @@ +/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ + +/* Fluent Bit + * ========== + * Copyright (C) 2015-2022 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. + */ + +#include +#include +#include +#include +#include +#include + +#include "ne.h" +#include "ne_utils.h" +#include "ne_systemd_linux.h" + +#include +#include + +static int str_ends_with(char *haystack, char *needle, int caseless) { + size_t haystack_length; + size_t trailer_offset; + size_t needle_length; + int result; + + haystack_length = strlen(haystack); + needle_length = strlen(needle); + + if (needle_length > haystack_length) { + return FLB_FALSE; + } + + trailer_offset = haystack_length - needle_length; + + if (caseless) { + result = strcasecmp(&haystack[trailer_offset], + needle); + } + else { + result = strcmp(&haystack[trailer_offset], + needle); + } + + if (result == 0) { + return FLB_TRUE; + } + + return FLB_FALSE; +} + +static void clear_property_variable(char property_type, void *property_value) +{ + if (property_type == 'y') { + *((uint8_t *) property_value) = 0; + } + else if (property_type == 'b') { + *((int *) property_value) = 0; + } + else if (property_type == 'n') { + *((int16_t *) property_value) = 0; + } + else if (property_type == 'q') { + *((uint16_t *) property_value) = 0; + } + else if (property_type == 'i') { + *((int32_t *) property_value) = 0; + } + else if (property_type == 'u') { + *((uint32_t *) property_value) = 0; + } + else if (property_type == 'x') { + *((int64_t *) property_value) = 0; + } + else if (property_type == 't') { + *((uint64_t *) property_value) = 0; + } + else if (property_type == 'd') { + *((double *) property_value) = 0; + } + else if (property_type == 's') { + *((char **) property_value) = NULL; + } + else if (property_type == 'o') { + *((char **) property_value) = NULL; + } + else if (property_type == 'g') { + *((char **) property_value) = NULL; + } + else if (property_type == 'h') { + *((int32_t *) property_value) = -1; + } +} + +static int get_system_property(struct flb_ne *ctx, + char *interface, + char *property_name, + char property_type, + void *property_value) +{ + int result; + + clear_property_variable(property_type, property_value); + + if (interface == NULL) { + interface = "org.freedesktop.systemd1.Manager"; + } + + if (property_type == 's' || + property_type == 'o' || + property_type == 'g') { + result = sd_bus_get_property_string((sd_bus *) ctx->systemd_dbus_handle, + "org.freedesktop.systemd1", + "/org/freedesktop/systemd1", + interface, + property_name, + NULL, + property_value); + } + else { + result = sd_bus_get_property_trivial((sd_bus *) ctx->systemd_dbus_handle, + "org.freedesktop.systemd1", + "/org/freedesktop/systemd1", + interface, + property_name, + NULL, + property_type, + property_value); + } + + if (result < 0) { + return -1; + } + + return 0; +} + +static int get_unit_property(struct flb_ne *ctx, + struct ne_systemd_unit *unit, + char *interface, + char *property_name, + char property_type, + void *property_value) +{ + int result; + + clear_property_variable(property_type, property_value); + + if (interface == NULL) { + if (unit->unit_type == SYSTEMD_UNIT_TYPE_SERVICE) { + interface = "org.freedesktop.systemd1.Service"; + } + else if (unit->unit_type == SYSTEMD_UNIT_TYPE_MOUNT) { + interface = "org.freedesktop.systemd1.Mount"; + } + else if (unit->unit_type == SYSTEMD_UNIT_TYPE_SOCKET) { + interface = "org.freedesktop.systemd1.Socket"; + } + else if (unit->unit_type == SYSTEMD_UNIT_TYPE_TIMER) { + interface = "org.freedesktop.systemd1.Timer"; + } + else { + interface = unit->name; + } + } + + if (property_type == 's' || + property_type == 'o' || + property_type == 'g') { + result = sd_bus_get_property_string((sd_bus *) ctx->systemd_dbus_handle, + "org.freedesktop.systemd1", + unit->path, + interface, + property_name, + NULL, + property_value); + } + else { + result = sd_bus_get_property_trivial((sd_bus *) ctx->systemd_dbus_handle, + "org.freedesktop.systemd1", + unit->path, + interface, + property_name, + NULL, + property_type, + property_value); + } + + if (result < 0) { + return -1; + } + + return 0; +} + +static int ne_systemd_update_unit_state(struct flb_ne *ctx) +{ + char *unit_states[] = { "activating", "active", + "deactivating", "inactive", + "failed" }; + double timer_trigger_timestamp; + uint64_t deactivating_units; + uint64_t activating_units; + double unit_start_time; + uint64_t inactive_units; + uint64_t active_units; + uint64_t failed_units; + int include_flag; + uint64_t timestamp; + int result; + size_t index; + sd_bus_message *reply; + struct ne_systemd_unit unit; + sd_bus *bus; + + bus = (sd_bus *) ctx->systemd_dbus_handle; + + result = sd_bus_call_method(bus, + "org.freedesktop.systemd1", + "/org/freedesktop/systemd1", + "org.freedesktop.systemd1.Manager", + "ListUnits", + NULL, + &reply, + ""); + + if (result < 0) { + return -1; + } + + result = sd_bus_message_enter_container(reply, 'a', "(ssssssouso)"); + + if (result < 0) { + sd_bus_message_unref(reply); + + return -2; + } + + timestamp = cfl_time_now(); + + deactivating_units = 0; + activating_units = 0; + inactive_units = 0; + active_units = 0; + failed_units = 0; + + do { + result = sd_bus_message_read(reply, + "(ssssssouso)", + &unit.name, + &unit.description, + &unit.load_state, + &unit.active_state, + &unit.sub_state, + &unit.followed, + &unit.path, + &unit.job_id, + &unit.job_type, + &unit.object_path); + + + if (result < 0) { + sd_bus_message_unref(reply); + + return -3; + } + else if(result > 0) { + unit.type = NULL; + + if (strcasecmp(unit.active_state, "activating") == 0) { + activating_units++; + } + else if (strcasecmp(unit.active_state, "deactivating") == 0) { + deactivating_units++; + } + else if (strcasecmp(unit.active_state, "inactive") == 0) { + inactive_units++; + } + else if (strcasecmp(unit.active_state, "active") == 0) { + active_units++; + } + else if (strcasecmp(unit.active_state, "failed") == 0) { + failed_units++; + } + + if (ctx->systemd_regex_include_list != NULL) { + include_flag = flb_regex_match(ctx->systemd_regex_include_list, + (unsigned char *) unit.name, + strlen(unit.name)); + } + else { + include_flag = FLB_TRUE; + } + + if (!include_flag) { + continue; + } + + if (ctx->systemd_regex_exclude_list != NULL) { + include_flag = !flb_regex_match(ctx->systemd_regex_exclude_list, + (unsigned char *) unit.name, + strlen(unit.name)); + } + else { + include_flag = FLB_TRUE; + } + + if (!include_flag) { + continue; + } + + if (strcasecmp(unit.load_state, "loaded") != 0) { + continue; + } + + if (str_ends_with(unit.name, ".service", FLB_TRUE)) { + unit.unit_type = SYSTEMD_UNIT_TYPE_SERVICE; + + result = get_service_type(ctx, + &unit, + &unit.type); + + if (ctx->systemd_include_service_restarts) { + result = get_service_restart_count(ctx, + &unit, + &unit.restart_count); + + cmt_counter_set(ctx->systemd_service_restarts, + timestamp, + unit.restart_count, + 1, + (char *[]){ unit.name }); + + } + + if (ctx->systemd_include_service_task_metrics) { + result = get_service_active_tasks(ctx, + &unit, + &unit.active_tasks); + + if (unit.active_tasks != UINT64_MAX) { + cmt_gauge_set(ctx->systemd_unit_tasks, + timestamp, + unit.active_tasks, + 1, + (char *[]){ unit.name }); + } + + result = get_service_max_tasks(ctx, + &unit, + &unit.max_tasks); + + if (unit.max_tasks != UINT64_MAX) { + cmt_gauge_set(ctx->systemd_unit_tasks_max, + timestamp, + unit.max_tasks, + 1, + (char *[]){ unit.name }); + } + } + + result = 1; + } + else if (str_ends_with(unit.name, ".mount", FLB_TRUE)) { + unit.unit_type = SYSTEMD_UNIT_TYPE_MOUNT; + } + else if (str_ends_with(unit.name, ".socket", FLB_TRUE)) { + unit.unit_type = SYSTEMD_UNIT_TYPE_SOCKET; + + result = get_socket_accepted_connection_count( + ctx, + &unit, + &unit.accepted_connections); + + result = get_socket_active_connection_count( + ctx, + &unit, + &unit.active_connections); + + result = get_socket_refused_connection_count( + ctx, + &unit, + &unit.refused_connections); + + cmt_gauge_set(ctx->systemd_socket_accepted_connections, + timestamp, + unit.accepted_connections, + 1, + (char *[]){ unit.name }); + + cmt_gauge_set(ctx->systemd_socket_active_connections, + timestamp, + unit.active_connections, + 1, + (char *[]){ unit.name }); + + cmt_gauge_set(ctx->systemd_socket_refused_connections, + timestamp, + unit.refused_connections, + 1, + (char *[]){ unit.name }); + + result = 1; + } + else if (str_ends_with(unit.name, ".timer", FLB_TRUE)) { + unit.unit_type = SYSTEMD_UNIT_TYPE_TIMER; + + result = get_timer_last_trigger_timestamp( + ctx, + &unit, + &unit.last_trigger_timestamp); + + timer_trigger_timestamp = (double) unit.last_trigger_timestamp; + timer_trigger_timestamp /= 1000000.0; + + cmt_gauge_set(ctx->systemd_timer_last_trigger_seconds, + timestamp, + timer_trigger_timestamp, + 1, + (char *[]){ unit.name }); + + result = 1; + } + else { + unit.unit_type = SYSTEMD_UNIT_TYPE_UNDEFINED; + } + + if (ctx->systemd_include_unit_start_times) { + if (strcasecmp(unit.active_state, "active") == 0) { + result = get_unit_start_time(ctx, &unit, &unit.start_time); + + unit_start_time = (double) unit.start_time; + unit_start_time /= 1000000.0; + } + else { + unit_start_time = 0; + } + + cmt_gauge_set(ctx->systemd_unit_start_times, + timestamp, + unit_start_time, + 1, + (char *[]){ unit.name }); + + result = 1; + } + + for(index = 0 ; index < 5 ; index++) { + cmt_gauge_add(ctx->systemd_unit_state, + timestamp, + 0, + 3, + (char *[]){ unit.name, + unit_states[index], + unit.type + }); + } + + cmt_gauge_inc(ctx->systemd_unit_state, + timestamp, + 3, + (char *[]){ unit.name, + unit.active_state, + unit.type + }); + + + if (unit.type != NULL) { + free(unit.type); + } + } + } + while (result > 0); + + sd_bus_message_exit_container(reply); + + sd_bus_message_unref(reply); + + cmt_gauge_set(ctx->systemd_units, + timestamp, + activating_units, + 1, + (char *[]){ "activating" }); + + cmt_gauge_set(ctx->systemd_units, + timestamp, + deactivating_units, + 1, + (char *[]){ "deactivating" }); + + cmt_gauge_set(ctx->systemd_units, + timestamp, + inactive_units, + 1, + (char *[]){ "inactive" }); + + cmt_gauge_set(ctx->systemd_units, + timestamp, + active_units, + 1, + (char *[]){ "active" }); + + cmt_gauge_set(ctx->systemd_units, + timestamp, + failed_units, + 1, + (char *[]){ "failed" }); + + return 0; +} + +static int ne_systemd_update_system_state(struct flb_ne *ctx) +{ + int system_running; + uint64_t timestamp; + char *version; + int result; + char *state; + + timestamp = cfl_time_now(); + + if (!ctx->systemd_initialization_flag) { + result = get_system_version(ctx, &version); + + if (result != 0) { + return -1; + } + + ctx->libsystemd_version_text = version; + ctx->libsystemd_version = strtod(version, NULL); + + cmt_gauge_set(ctx->systemd_version, + timestamp, + ctx->libsystemd_version, + 1, + (char *[]){ ctx->libsystemd_version_text }); + } + else { + cmt_gauge_add(ctx->systemd_version, + timestamp, + 0, + 1, + (char *[]){ ctx->libsystemd_version_text }); + } + + result = get_system_state(ctx, &state); + + if (result != 0) { + return -2; + } + + system_running = 0; + + if (strcasecmp(state, "running") == 0) { + system_running = 1; + } + + cmt_gauge_set(ctx->systemd_system_running, + timestamp, + system_running, + 0, + NULL); + free(state); + + return 0; +} + +static int ne_systemd_init(struct flb_ne *ctx) +{ + int result; + + ctx->systemd_dbus_handle = NULL; + + result = sd_bus_open_system((sd_bus **) &ctx->systemd_dbus_handle); + + if (result < 0) { + return -1; + } + + ctx->systemd_socket_accepted_connections = cmt_gauge_create(ctx->cmt, + "node", + "systemd", + "socket_accepted_connections_total", + "Total number of accepted " \ + "socket connections.", + 1, + (char *[]) {"name"}); + + if (ctx->systemd_socket_accepted_connections == NULL) { + return -1; + } + + ctx->systemd_socket_active_connections = cmt_gauge_create(ctx->cmt, + "node", + "systemd", + "socket_current_connections", + "Current number of socket " \ + "connections.", + 1, + (char *[]) {"name"}); + + if (ctx->systemd_socket_active_connections == NULL) { + return -1; + } + + ctx->systemd_socket_refused_connections = cmt_gauge_create(ctx->cmt, + "node", + "systemd", + "socket_refused_connections_total", + "Total number of refused " \ + "socket connections.", + 1, + (char *[]) {"name"}); + + if (ctx->systemd_socket_refused_connections == NULL) { + return -1; + } + + ctx->systemd_system_running = cmt_gauge_create(ctx->cmt, + "node", + "systemd", + "system_running", + "Whether the system is " \ + "operational (see 'systemctl" \ + " is-system-running')", + 0, NULL); + + if (ctx->systemd_system_running == NULL) { + return -1; + } + + ctx->systemd_timer_last_trigger_seconds = cmt_gauge_create(ctx->cmt, + "node", + "systemd", + "timer_last_trigger_seconds", + "Seconds since epoch of " \ + "last trigger.", + 1, + (char *[]) {"name"}); + + if (ctx->systemd_timer_last_trigger_seconds == NULL) { + return -1; + } + + ctx->systemd_service_restarts = cmt_counter_create(ctx->cmt, + "node", + "systemd", + "service_restart_total", + "Service unit count of " \ + "Restart triggers", + 1, (char *[]) {"name"}); + + if (ctx->systemd_service_restarts == NULL) { + return -1; + } + + cmt_counter_allow_reset(ctx->systemd_service_restarts); + + ctx->systemd_unit_tasks = cmt_gauge_create(ctx->cmt, + "node", + "systemd", + "unit_tasks_current", + "Current number of tasks " \ + "per Systemd unit.", + 1, (char *[]) {"name"}); + + if (ctx->systemd_unit_tasks == NULL) { + return -1; + } + + ctx->systemd_unit_tasks_max = cmt_gauge_create(ctx->cmt, + "node", + "systemd", + "unit_tasks_max", + "Maximum number of tasks " \ + "per Systemd unit.", + 1, (char *[]) {"name"}); + + if (ctx->systemd_unit_tasks == NULL) { + return -1; + } + + ctx->systemd_unit_start_times = cmt_gauge_create(ctx->cmt, + "node", + "systemd", + "unit_start_time_seconds", + "Start time of the unit since " \ + "unix epoch in seconds.", + 1, (char *[]) {"name"}); + + if (ctx->systemd_unit_start_times == NULL) { + return -1; + } + + ctx->systemd_unit_state = cmt_gauge_create(ctx->cmt, + "node", + "systemd", + "unit_state", + "Systemd unit", + 3, (char *[]) {"name", + "state", + "type"}); + + if (ctx->systemd_unit_state == NULL) { + return -1; + } + + ctx->systemd_units = cmt_gauge_create(ctx->cmt, + "node", + "systemd", + "units", + "Summary of systemd unit states", + 1, (char *[]) {"state"}); + + if (ctx->systemd_units == NULL) { + return -1; + } + + ctx->systemd_version = cmt_gauge_create(ctx->cmt, + "node", + "systemd", + "version", + "Detected systemd version", + 1, (char *[]) {"version"}); + + if (ctx->systemd_version == NULL) { + return -1; + } + + if (ctx->systemd_regex_include_list_text != NULL) { + ctx->systemd_regex_include_list = \ + flb_regex_create(ctx->systemd_regex_include_list_text); + + if (ctx->systemd_regex_include_list == NULL) { + return -1; + } + } + + if (ctx->systemd_regex_exclude_list_text != NULL) { + ctx->systemd_regex_exclude_list = \ + flb_regex_create(ctx->systemd_regex_exclude_list_text); + + if (ctx->systemd_regex_exclude_list == NULL) { + return -1; + } + } + + return 0; +} + +static int ne_systemd_update(struct flb_input_instance *ins, struct flb_config *config, void *in_context) +{ + int result; + struct flb_ne *ctx = (struct flb_ne *)in_context; + + result = ne_systemd_update_system_state(ctx); + + if (result != 0) { + return result; + } + + result = ne_systemd_update_unit_state(ctx); + + if (result != 0) { + return result; + } + + if (!ctx->systemd_initialization_flag) { + ctx->systemd_initialization_flag = FLB_TRUE; + } + + return 0; +} + +static int ne_systemd_exit(struct flb_ne *ctx) +{ + if (ctx->systemd_dbus_handle != NULL) { + sd_bus_unref((sd_bus *) ctx->systemd_dbus_handle); + + ctx->systemd_dbus_handle = NULL; + } + + if (ctx->systemd_regex_include_list != NULL) { + flb_regex_destroy(ctx->systemd_regex_include_list); + } + + if (ctx->systemd_regex_exclude_list != NULL) { + flb_regex_destroy(ctx->systemd_regex_exclude_list); + } + + if (ctx->libsystemd_version_text != NULL) { + flb_free(ctx->libsystemd_version_text); + } + return 0; +} + +struct flb_ne_collector systemd_collector = { + .name = "systemd", + .cb_init = ne_systemd_init, + .cb_update = ne_systemd_update, + .cb_exit = ne_systemd_exit +}; diff --git a/plugins/in_node_exporter_metrics/ne_systemd_linux.h b/plugins/in_node_exporter_metrics/ne_systemd_linux.h new file mode 100644 index 00000000000..efdbc19228d --- /dev/null +++ b/plugins/in_node_exporter_metrics/ne_systemd_linux.h @@ -0,0 +1,108 @@ +/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ + +/* Fluent Bit + * ========== + * Copyright (C) 2015-2022 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_SYSTEMD_LINUX_H +#define FLB_IN_NE_SYSTEMD_LINUX_H + +#include "ne.h" + +#define SYSTEMD_UNIT_TYPE_UNDEFINED 0 +#define SYSTEMD_UNIT_TYPE_SERVICE 1 +#define SYSTEMD_UNIT_TYPE_SOCKET 2 +#define SYSTEMD_UNIT_TYPE_MOUNT 3 +#define SYSTEMD_UNIT_TYPE_TIMER 4 + +struct ne_systemd_unit { + char *name; + char *description; + char *load_state; + char *active_state; + char *sub_state; + char *followed; + char *path; + uint32_t job_id; + char *job_type; + char *object_path; + + /* not part of the unit list result */ + uint64_t start_time; + int unit_type; + char *type; + + /* services */ + uint32_t restart_count; + uint64_t active_tasks; + uint64_t max_tasks; + + /* sockets */ + uint32_t accepted_connections; + uint32_t active_connections; + uint32_t refused_connections; + + /* timers */ + uint64_t last_trigger_timestamp; +}; + +#define get_system_state(context, output_variable) \ + get_system_property(context, NULL, "SystemState", \ + 's', (void *) (output_variable)) + +#define get_system_version(context, output_variable) \ + get_system_property(context, NULL, "Version", \ + 's', (void *) (output_variable)) + +#define get_service_type(context, unit, output_variable) \ + get_unit_property(context, unit, NULL, "Type", \ + 's', (void *) (output_variable)) + +#define get_service_active_tasks(context, unit, output_variable) \ + get_unit_property(context, unit, NULL, "TasksCurrent", \ + 't', (void *) (output_variable)) + +#define get_service_max_tasks(context, unit, output_variable) \ + get_unit_property(context, unit, NULL, "TasksMax", \ + 't', (void *) (output_variable)) + +#define get_service_restart_count(context, unit, output_variable) \ + get_unit_property(context, unit, NULL, "NRestarts", \ + 'u', (void *) (output_variable)) + +#define get_socket_accepted_connection_count(context, unit, output_variable) \ + get_unit_property(context, unit, NULL, "NAccepted", \ + 'u', (void *) (output_variable)) + +#define get_socket_active_connection_count(context, unit, output_variable) \ + get_unit_property(context, unit, NULL, "NConnections", \ + 'u', (void *) (output_variable)) + +#define get_socket_refused_connection_count(context, unit, output_variable) \ + get_unit_property(context, unit, NULL, "NRefused", \ + 'u', (void *) (output_variable)) + +#define get_timer_last_trigger_timestamp(context, unit, output_variable) \ + get_unit_property(context, unit, NULL, "LastTriggerUSec", \ + 't', (void *) (output_variable)) + +#define get_unit_start_time(context, unit, output_variable) \ + get_unit_property(context, \ + unit, \ + "org.freedesktop.systemd1.Unit", \ + "ActiveEnterTimestamp", \ + 't', (void *) (output_variable)) +#endif diff --git a/plugins/in_node_exporter_metrics/ne_textfile.c b/plugins/in_node_exporter_metrics/ne_textfile.c index f9c584453ee..9e8600a563d 100644 --- a/plugins/in_node_exporter_metrics/ne_textfile.c +++ b/plugins/in_node_exporter_metrics/ne_textfile.c @@ -19,4 +19,15 @@ #ifdef __linux__ #include "ne_textfile_linux.c" +#else + +#include "ne.h" + +struct flb_ne_collector textfile_collector = { + .name = "textfile", + .cb_init = NULL, + .cb_update = NULL, + .cb_exit = NULL +}; + #endif diff --git a/plugins/in_node_exporter_metrics/ne_textfile.h b/plugins/in_node_exporter_metrics/ne_textfile.h index 81ed25b2e41..c28184b9294 100644 --- a/plugins/in_node_exporter_metrics/ne_textfile.h +++ b/plugins/in_node_exporter_metrics/ne_textfile.h @@ -22,19 +22,6 @@ #include "ne.h" -#ifdef __linux__ -int ne_textfile_init(struct flb_ne *ctx); -int ne_textfile_update(struct flb_ne *ctx); -#else -static int ne_textfile_init(struct flb_ne *ctx) -{ - return 0; -} - -static int ne_textfile_update(struct flb_ne *ctx) -{ - return 0; -} -#endif +extern struct flb_ne_collector textfile_collector; #endif diff --git a/plugins/in_node_exporter_metrics/ne_textfile_linux.c b/plugins/in_node_exporter_metrics/ne_textfile_linux.c index d65e01441da..7cb1a1194f8 100644 --- a/plugins/in_node_exporter_metrics/ne_textfile_linux.c +++ b/plugins/in_node_exporter_metrics/ne_textfile_linux.c @@ -175,7 +175,7 @@ static int textfile_update(struct flb_ne *ctx) return 0; } -int ne_textfile_init(struct flb_ne *ctx) +static int ne_textfile_init(struct flb_ne *ctx) { ctx->load_errors = cmt_counter_create(ctx->cmt, "node", @@ -191,14 +191,23 @@ int ne_textfile_init(struct flb_ne *ctx) return 0; } -int ne_textfile_update(struct flb_ne *ctx) +static int ne_textfile_update(struct flb_input_instance *ins, struct flb_config *config, void *in_context) { + struct flb_ne *ctx = (struct flb_ne *)in_context; + textfile_update(ctx); return 0; } -int ne_textfile_exit(struct flb_ne *ctx) +static int ne_textfile_exit(struct flb_ne *ctx) { return 0; } + +struct flb_ne_collector textfile_collector = { + .name = "textfile", + .cb_init = ne_textfile_init, + .cb_update = ne_textfile_update, + .cb_exit = ne_textfile_exit +}; diff --git a/plugins/in_node_exporter_metrics/ne_time.c b/plugins/in_node_exporter_metrics/ne_time.c index 81bf57ed9f9..9d593b8d328 100644 --- a/plugins/in_node_exporter_metrics/ne_time.c +++ b/plugins/in_node_exporter_metrics/ne_time.c @@ -46,14 +46,23 @@ static int time_update(struct flb_ne *ctx) return 0; } -int ne_time_init(struct flb_ne *ctx) +static int ne_time_init(struct flb_ne *ctx) { time_configure(ctx); return 0; } -int ne_time_update(struct flb_ne *ctx) +static int ne_time_update(struct flb_input_instance *ins, struct flb_config *config, void *in_context) { + struct flb_ne *ctx = (struct flb_ne *)in_context; + time_update(ctx); return 0; } + +struct flb_ne_collector time_collector = { + .name = "time", + .cb_init = ne_time_init, + .cb_update = ne_time_update, + .cb_exit = NULL +}; diff --git a/plugins/in_node_exporter_metrics/ne_time.h b/plugins/in_node_exporter_metrics/ne_time.h index e594c935c97..e6d1b66286d 100644 --- a/plugins/in_node_exporter_metrics/ne_time.h +++ b/plugins/in_node_exporter_metrics/ne_time.h @@ -22,7 +22,6 @@ #include "ne.h" -int ne_time_init(struct flb_ne *ctx); -int ne_time_update(struct flb_ne *ctx); +extern struct flb_ne_collector time_collector; #endif diff --git a/plugins/in_node_exporter_metrics/ne_uname.c b/plugins/in_node_exporter_metrics/ne_uname.c index e32830842d8..dcc671d3eb7 100644 --- a/plugins/in_node_exporter_metrics/ne_uname.c +++ b/plugins/in_node_exporter_metrics/ne_uname.c @@ -21,4 +21,15 @@ #include "ne_uname_linux.c" #elif __APPLE__ #include "ne_uname_darwin.c" +#else + +#include "ne.h" + +struct flb_ne_collector uname_collector = { + .name = "uname", + .cb_init = NULL, + .cb_update = NULL, + .cb_exit = NULL +}; + #endif diff --git a/plugins/in_node_exporter_metrics/ne_uname.h b/plugins/in_node_exporter_metrics/ne_uname.h index cdfb8ad4318..272f5c05174 100644 --- a/plugins/in_node_exporter_metrics/ne_uname.h +++ b/plugins/in_node_exporter_metrics/ne_uname.h @@ -22,19 +22,6 @@ #include "ne.h" -#if defined(__linux__) || defined(__APPLE__) -int ne_uname_init(struct flb_ne *ctx); -int ne_uname_update(struct flb_ne *ctx); -#else -static int ne_uname_init(struct flb_ne *ctx) -{ - return 0; -} - -static int ne_uname_update(struct flb_ne *ctx) -{ - return 0; -} -#endif +extern struct flb_ne_collector uname_collector; #endif diff --git a/plugins/in_node_exporter_metrics/ne_uname_darwin.c b/plugins/in_node_exporter_metrics/ne_uname_darwin.c index 6d06bd420d3..784d13decc0 100644 --- a/plugins/in_node_exporter_metrics/ne_uname_darwin.c +++ b/plugins/in_node_exporter_metrics/ne_uname_darwin.c @@ -87,14 +87,23 @@ static int uname_update(struct flb_ne *ctx) return ret; } -int ne_uname_init(struct flb_ne *ctx) +static int ne_uname_init(struct flb_ne *ctx) { uname_configure(ctx); return 0; } -int ne_uname_update(struct flb_ne *ctx) +static int ne_uname_update(struct flb_input_instance *ins, struct flb_config *config, void *in_context) { + struct flb_ne *ctx = (struct flb_ne *)in_context; + uname_update(ctx); return 0; } + +struct flb_ne_collector uname_collector = { + .name = "uname", + .cb_init = ne_uname_init, + .cb_update = ne_uname_update, + .cb_exit = NULL +}; diff --git a/plugins/in_node_exporter_metrics/ne_uname_linux.c b/plugins/in_node_exporter_metrics/ne_uname_linux.c index e42cab6a3b2..1a6bf300e0a 100644 --- a/plugins/in_node_exporter_metrics/ne_uname_linux.c +++ b/plugins/in_node_exporter_metrics/ne_uname_linux.c @@ -71,14 +71,23 @@ static int uname_update(struct flb_ne *ctx) return ret; } -int ne_uname_init(struct flb_ne *ctx) +static int ne_uname_init(struct flb_ne *ctx) { uname_configure(ctx); return 0; } -int ne_uname_update(struct flb_ne *ctx) +static int ne_uname_update(struct flb_input_instance *ins, struct flb_config *config, void *in_context) { + struct flb_ne *ctx = (struct flb_ne *)in_context; + uname_update(ctx); return 0; } + +struct flb_ne_collector uname_collector = { + .name = "uname", + .cb_init = ne_uname_init, + .cb_update = ne_uname_update, + .cb_exit = NULL +}; diff --git a/plugins/in_node_exporter_metrics/ne_vmstat.c b/plugins/in_node_exporter_metrics/ne_vmstat.c new file mode 100644 index 00000000000..ef1dd83dc73 --- /dev/null +++ b/plugins/in_node_exporter_metrics/ne_vmstat.c @@ -0,0 +1,33 @@ +/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ + +/* Fluent Bit + * ========== + * Copyright (C) 2015-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_vmstat_linux.c" +#else + +#include "ne.h" + +struct flb_ne_collector vmstat_collector = { + .name = "vmstat", + .cb_init = NULL, + .cb_update = NULL, + .cb_exit = NULL +}; + +#endif diff --git a/plugins/in_node_exporter_metrics/ne_filefd_linux.h b/plugins/in_node_exporter_metrics/ne_vmstat.h similarity index 71% rename from plugins/in_node_exporter_metrics/ne_filefd_linux.h rename to plugins/in_node_exporter_metrics/ne_vmstat.h index 659cfd6298c..cb13500abea 100644 --- a/plugins/in_node_exporter_metrics/ne_filefd_linux.h +++ b/plugins/in_node_exporter_metrics/ne_vmstat.h @@ -17,24 +17,11 @@ * limitations under the License. */ -#ifndef FLB_IN_NE_FILEFD_H -#define FLB_IN_NE_FILEFD_H +#ifndef FLB_IN_NE_VMSTAT_H +#define FLB_IN_NE_VMSTAT_H #include "ne.h" -#ifdef __linux__ -int ne_filefd_init(struct flb_ne *ctx); -int ne_filefd_update(struct flb_ne *ctx); -#else -static int ne_filefd_init(struct flb_ne *ctx) -{ - return 0; -} - -static int ne_filefd_update(struct flb_ne *ctx) -{ - return 0; -} -#endif +extern struct flb_ne_collector vmstat_collector; #endif diff --git a/plugins/in_node_exporter_metrics/ne_vmstat_linux.c b/plugins/in_node_exporter_metrics/ne_vmstat_linux.c index a0240d4ee81..dde90cf9eb4 100644 --- a/plugins/in_node_exporter_metrics/ne_vmstat_linux.c +++ b/plugins/in_node_exporter_metrics/ne_vmstat_linux.c @@ -191,19 +191,21 @@ static int vmstat_update(struct flb_ne *ctx) return 0; } -int ne_vmstat_init(struct flb_ne *ctx) +static int ne_vmstat_init(struct flb_ne *ctx) { vmstat_configure(ctx); return 0; } -int ne_vmstat_update(struct flb_ne *ctx) +static int ne_vmstat_update(struct flb_input_instance *ins, struct flb_config *config, void *in_context) { + struct flb_ne *ctx = (struct flb_ne *)in_context; + vmstat_update(ctx); return 0; } -int ne_vmstat_exit(struct flb_ne *ctx) +static int ne_vmstat_exit(struct flb_ne *ctx) { if (ctx->vml_regex_fields) { flb_regex_destroy(ctx->vml_regex_fields); @@ -214,3 +216,10 @@ int ne_vmstat_exit(struct flb_ne *ctx) } return 0; } + +struct flb_ne_collector vmstat_collector = { + .name = "vmstat", + .cb_init = ne_vmstat_init, + .cb_update = ne_vmstat_update, + .cb_exit = ne_vmstat_exit +};