From 2f1f2386be509af6dc17fa30fd3375dae7cd4840 Mon Sep 17 00:00:00 2001 From: Hiroshi Hatake Date: Thu, 31 Aug 2023 19:10:17 +0900 Subject: [PATCH] in_node_exporter_metrics: Check procfs exsitence before retrieving info Signed-off-by: Hiroshi Hatake --- .../ne_processes_linux.c | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/plugins/in_node_exporter_metrics/ne_processes_linux.c b/plugins/in_node_exporter_metrics/ne_processes_linux.c index 7ff5e798af8..431c8f268ff 100644 --- a/plugins/in_node_exporter_metrics/ne_processes_linux.c +++ b/plugins/in_node_exporter_metrics/ne_processes_linux.c @@ -120,6 +120,35 @@ static int update_processes_proc_state(struct flb_ne *ctx, struct proc_state *st return 0; } +static int check_path_for_proc(struct flb_ne *ctx, const char *prefix, const char *path) +{ + int len; + flb_sds_t p; + + /* Compose the proc path */ + p = flb_sds_create(prefix); + if (!p) { + return -1; + } + + if (path) { + flb_sds_cat_safe(&p, "/", 1); + len = strlen(path); + flb_sds_cat_safe(&p, path, len); + } + + if (access(p, F_OK) == -1 && + (errno == ENOENT || errno == ESRCH)) { + flb_plg_debug(ctx->ins, "error reading stat for path %s. errno = %d", p, errno); + flb_sds_destroy(p); + + return -1; + } + + flb_sds_destroy(p); + return 0; +} + static int processes_thread_update(struct flb_ne *ctx, flb_sds_t pid_str, flb_sds_t pstate_str, struct proc_state *tstate) { @@ -161,6 +190,10 @@ static int processes_thread_update(struct flb_ne *ctx, flb_sds_t pid_str, flb_sd continue; } + if (check_path_for_proc(ctx, thread->str, "stat") != 0) { + continue; + } + mk_list_init(&stat_list); ret = ne_utils_file_read_lines(thread->str, "/stat", &stat_list); if (ret == -1) { @@ -275,6 +308,10 @@ static int processes_update(struct flb_ne *ctx) process = mk_list_entry(head, struct flb_slist_entry, _head); pid_str = process->str + strlen(ctx->path_procfs) + 1; + if (check_path_for_proc(ctx, process->str, "stat") != 0) { + continue; + } + mk_list_init(&stat_list); ret = ne_utils_file_read_lines(process->str, "/stat", &stat_list); if (ret == -1) {