diff --git a/conf/fluent-bit-metrics.conf b/conf/fluent-bit-metrics.conf index 988e0dcbb89..352ec8f526d 100644 --- a/conf/fluent-bit-metrics.conf +++ b/conf/fluent-bit-metrics.conf @@ -15,6 +15,8 @@ name node_exporter_metrics tag node_metrics scrape_interval 2 + # use host root path when running in containers + # path.rootfs /host [OUTPUT] name prometheus_exporter diff --git a/docker_compose/node-exporter-dashboard/docker-compose.yml b/docker_compose/node-exporter-dashboard/docker-compose.yml index 8170ee7ebf4..7cc084f7a15 100644 --- a/docker_compose/node-exporter-dashboard/docker-compose.yml +++ b/docker_compose/node-exporter-dashboard/docker-compose.yml @@ -4,14 +4,13 @@ services: fluentbit: image: fluent/fluent-bit:latest container_name: fluentbit - command: /fluent-bit/bin/fluent-bit -i node_exporter_metrics -p path.procfs=/host/proc -p path.sysfs=/host/sys -o prometheus_exporter -p "add_label=host fluentbit" -f 1 + command: /fluent-bit/bin/fluent-bit -i node_exporter_metrics -p path.rootfs=/host -o prometheus_exporter -p "add_label=host fluentbit" -f 1 ports: - 2021:2021 networks: - exporter-network volumes: - - /proc:/host/proc - - /sys:/host/sys + - /:/host:ro grafana: image: grafana/grafana:latest diff --git a/plugins/in_node_exporter_metrics/ne.c b/plugins/in_node_exporter_metrics/ne.c index e365559d637..3c13cb7ec12 100644 --- a/plugins/in_node_exporter_metrics/ne.c +++ b/plugins/in_node_exporter_metrics/ne.c @@ -441,6 +441,12 @@ static struct flb_config_map config_map[] = { "Specify file path or directory to collect textfile metrics from the node." }, + { + FLB_CONFIG_MAP_STR, "path.rootfs", "/", + 0, FLB_TRUE, offsetof(struct flb_ne, path_rootfs), + "rootfs mount point" + }, + { FLB_CONFIG_MAP_STR, "path.procfs", "/proc", 0, FLB_TRUE, offsetof(struct flb_ne, path_procfs), diff --git a/plugins/in_node_exporter_metrics/ne.h b/plugins/in_node_exporter_metrics/ne.h index 36021e0a0f5..a2d32236873 100644 --- a/plugins/in_node_exporter_metrics/ne.h +++ b/plugins/in_node_exporter_metrics/ne.h @@ -48,6 +48,7 @@ struct flb_ne { /* configuration */ + flb_sds_t path_rootfs; flb_sds_t path_procfs; flb_sds_t path_sysfs; flb_sds_t path_textfile; diff --git a/plugins/in_node_exporter_metrics/ne_config.c b/plugins/in_node_exporter_metrics/ne_config.c index c9d7a55ffc8..dd613c8c6f1 100644 --- a/plugins/in_node_exporter_metrics/ne_config.c +++ b/plugins/in_node_exporter_metrics/ne_config.c @@ -24,6 +24,8 @@ struct flb_ne *flb_ne_config_create(struct flb_input_instance *ins, struct flb_config *config) { int ret; + int root_len; + flb_sds_t tmp; struct flb_ne *ctx; ctx = flb_calloc(1, sizeof(struct flb_ne)); @@ -41,6 +43,59 @@ struct flb_ne *flb_ne_config_create(struct flb_input_instance *ins, } /* mount points */ + flb_plg_info(ins, "path.rootfs = %s", ctx->path_rootfs); + + if (ctx->path_rootfs && strcmp(ctx->path_rootfs, "/") != 0) { + root_len = strlen(ctx->path_rootfs); + if (root_len > 1 && ctx->path_rootfs[root_len - 1] == '/') { + root_len--; + } + + /* Compose procfs path */ + tmp = flb_sds_create_size(1024); + if (tmp) { + if (ctx->path_procfs[0] == '/') { + tmp = flb_sds_printf(&tmp, "%.*s%s", root_len, ctx->path_rootfs, ctx->path_procfs); + } + else { + tmp = flb_sds_printf(&tmp, "%.*s/%s", root_len, ctx->path_rootfs, ctx->path_procfs); + } + if (tmp) { + ctx->path_procfs = tmp; + } + } + + /* Compose sysfs path */ + tmp = flb_sds_create_size(1024); + if (tmp) { + if (ctx->path_sysfs[0] == '/') { + tmp = flb_sds_printf(&tmp, "%.*s%s", root_len, ctx->path_rootfs, ctx->path_sysfs); + } + else { + tmp = flb_sds_printf(&tmp, "%.*s/%s", root_len, ctx->path_rootfs, ctx->path_sysfs); + } + if (tmp) { + ctx->path_sysfs = tmp; + } + } + + /* Compose textfile path if any */ + if (ctx->path_textfile) { + tmp = flb_sds_create_size(1024); + if (tmp) { + if (ctx->path_textfile[0] == '/') { + tmp = flb_sds_printf(&tmp, "%.*s%s", root_len, ctx->path_rootfs, ctx->path_textfile); + } + else { + tmp = flb_sds_printf(&tmp, "%.*s/%s", root_len, ctx->path_rootfs, ctx->path_textfile); + } + if (tmp) { + ctx->path_textfile = tmp; + } + } + } + } + flb_plg_info(ins, "path.procfs = %s", ctx->path_procfs); flb_plg_info(ins, "path.sysfs = %s", ctx->path_sysfs); diff --git a/plugins/in_node_exporter_metrics/ne_stat_linux.c b/plugins/in_node_exporter_metrics/ne_stat_linux.c index eb37630931d..d514fa5c158 100644 --- a/plugins/in_node_exporter_metrics/ne_stat_linux.c +++ b/plugins/in_node_exporter_metrics/ne_stat_linux.c @@ -85,6 +85,7 @@ static int stat_update(struct flb_ne *ctx) mk_list_init(&list); ret = ne_utils_file_read_lines(ctx->path_procfs, "/stat", &list); if (ret == -1) { + flb_plg_error(ctx->ins, "failed to read %s/stat", ctx->path_procfs); return -1; } diff --git a/plugins/in_node_exporter_metrics/ne_utils.c b/plugins/in_node_exporter_metrics/ne_utils.c index 7206f60bbd7..0d2a6574512 100644 --- a/plugins/in_node_exporter_metrics/ne_utils.c +++ b/plugins/in_node_exporter_metrics/ne_utils.c @@ -196,9 +196,9 @@ int ne_utils_file_read_lines(const char *mount, const char *path, struct mk_list /* * Read a file and store the first line as a string. */ -int ne_utils_file_read_sds(const char *mount, - const char *path, - const char *join_a, +int ne_utils_file_read_sds(const char *mount, + const char *path, + const char *join_a, const char *join_b, flb_sds_t *str) { diff --git a/plugins/in_node_exporter_metrics/ne_utils.h b/plugins/in_node_exporter_metrics/ne_utils.h index 17e8742b17d..f875dbe6b76 100644 --- a/plugins/in_node_exporter_metrics/ne_utils.h +++ b/plugins/in_node_exporter_metrics/ne_utils.h @@ -35,8 +35,8 @@ int ne_utils_file_read_uint64(const char *mount, int ne_utils_file_read_sds(const char *mount, const char *path, - const char *join_a, - const char *join_b, + const char *join_a, + const char *join_b, flb_sds_t *str); int ne_utils_file_read_lines(const char *mount, const char *path, struct mk_list *list);