From 71a1f64f2df34718fe79c61a1139ece75e55c285 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javier=20Jaramago=20Fern=C3=A1ndez?= Date: Tue, 26 Oct 2021 16:29:36 +0200 Subject: [PATCH] Added new metric 'proxysql_fds_in_use' reporting the current number of file descriptors in use #3592 --- include/proxysql_admin.h | 1 + lib/ProxySQL_Admin.cpp | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/include/proxysql_admin.h b/include/proxysql_admin.h index 54927da24..af744d588 100644 --- a/include/proxysql_admin.h +++ b/include/proxysql_admin.h @@ -84,6 +84,7 @@ struct p_admin_gauge { stmt_server_active_unique, stmt_max_stmt_id, stmt_cached, + fds_in_use, __size }; }; diff --git a/lib/ProxySQL_Admin.cpp b/lib/ProxySQL_Admin.cpp index d4ef4bf96..f4eeccef6 100644 --- a/lib/ProxySQL_Admin.cpp +++ b/lib/ProxySQL_Admin.cpp @@ -27,6 +27,7 @@ #include "Web_Interface.hpp" +#include #include #include #include @@ -807,6 +808,12 @@ admin_metrics_map = std::make_tuple( "proxysql_stmt_cached", "This is the number of global prepared statements for which proxysql has metadata.", metric_tags {} + ), + std::make_tuple ( + p_admin_gauge::fds_in_use, + "proxysql_fds_in_use", + "The number of file descriptors currently in use by ProxySQL.", + metric_tags {} ) } ); @@ -7836,6 +7843,30 @@ void ProxySQL_Admin::p_update_metrics() { this->p_update_stmt_metrics(); } +/** + * @brief Gets the number of currently opened file descriptors. In case of error '-1' is + * returned and error is logged. + * @return On success, the number of currently opened file descriptors, '-1' otherwise. + */ +int32_t get_open_fds() { + DIR* dir = opendir("/proc/self/fd"); + if (dir == NULL) { + proxy_error("'opendir()' failed with error: '%d'\n", errno); + return -1; + } + + struct dirent* dp = nullptr; + int32_t count = -3; + + while ((dp = readdir(dir)) != NULL) { + count++; + } + + closedir(dir); + + return count; +} + void ProxySQL_Admin::p_stats___memory_metrics() { if (!GloMTH) return; @@ -7929,6 +7960,12 @@ void ProxySQL_Admin::p_stats___memory_metrics() { const auto& stack_memory_cluster_threads = __sync_fetch_and_add(&GloVars.statuses.stack_memory_cluster_threads, 0); this->metrics.p_gauge_array[p_admin_gauge::stack_memory_cluster_threads]->Set(stack_memory_cluster_threads); + + // Update opened file descriptors + int32_t cur_fds = get_open_fds(); + if (cur_fds != -1) { + this->metrics.p_gauge_array[p_admin_gauge::fds_in_use]->Set(cur_fds); + } } void ProxySQL_Admin::stats___memory_metrics() {