From 207cf6ddc3434402ffbe8bca96b76b7249ca425d Mon Sep 17 00:00:00 2001 From: Utkarsh Srivastava Date: Thu, 12 Jun 2025 18:26:22 +0530 Subject: [PATCH] Add disk usage info to nsfs metrics Signed-off-by: Utkarsh Srivastava missed committing config.js Signed-off-by: Utkarsh Srivastava fix stupid coderrabit AI suggestion Signed-off-by: Utkarsh Srivastava change ratio to percentage Signed-off-by: Utkarsh Srivastava --- config.js | 5 +++ .../analytic_services/prometheus_reporting.js | 32 +++++++++++++++++-- 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/config.js b/config.js index 6338dd7d9a..62eebe60c8 100644 --- a/config.js +++ b/config.js @@ -954,6 +954,11 @@ config.NSFS_GLACIER_FORCE_EXPIRE_ON_GET = false; // interval config.NSFS_GLACIER_MIGRATE_LOG_THRESHOLD = 50 * 1024; +// NSFS_GLACIER_METRICS_STAT_PATH if set NooBaa will start reporting the statfs info of that +// path as part of its metrics report +config.NSFS_GLACIER_METRICS_STATFS_PATH = ''; +config.NSFS_GLACIER_METRICS_STATFS_INTERVAL = 60 * 1000; // Refresh statfs value every minute + /** * NSFS_GLACIER_RESERVED_BUCKET_TAGS defines an object of bucket tags which will be reserved * by the system and PUT operations for them via S3 API would be limited - as in they would be diff --git a/src/server/analytic_services/prometheus_reporting.js b/src/server/analytic_services/prometheus_reporting.js index 7d2e5a7e24..36864cdb0f 100644 --- a/src/server/analytic_services/prometheus_reporting.js +++ b/src/server/analytic_services/prometheus_reporting.js @@ -13,6 +13,8 @@ const stats_aggregator = require('../system_services/stats_aggregator'); const AggregatorRegistry = require('prom-client').AggregatorRegistry; const aggregatorRegistry = new AggregatorRegistry(); const http_utils = require('../../util/http_utils'); +const nb_native = require('../../util/nb_native'); +const { get_process_fs_context } = require('../../util/native_fs_utils'); // Currenty supported reprots const reports = Object.seal({ @@ -48,6 +50,15 @@ async function export_all_metrics() { return all_metrics.join('\n\n'); } +/** @type {Record} */ +let nsfs_metrics_statfs_path_cache = {}; + +function get_disk_usage_perc(statfs) { + if (!statfs || !statfs.blocks) return undefined; + const ratio = (statfs.blocks - statfs.bfree) / statfs.blocks; + return Number((ratio * 100)?.toPrecision(5)); +} + /** * Start Noobaa metrics server for http and https server * @@ -68,6 +79,21 @@ async function start_server( if (!config.PROMETHEUS_ENABLED) { return; } + + if (config.NSFS_GLACIER_METRICS_STATFS_PATH) { + setInterval(async () => { + try { + nsfs_metrics_statfs_path_cache = await nb_native().fs.statfs( + get_process_fs_context(), + config.NSFS_GLACIER_METRICS_STATFS_PATH + ); + dbg.log4('updated \'nsfs_metrics_statfs_path_cache\' with', nsfs_metrics_statfs_path_cache); + } catch (error) { + dbg.warn('failed to updated \'nsfs_metrics_statfs_path_cache\':', error); + } + }, config.NSFS_GLACIER_METRICS_STATFS_INTERVAL).unref(); + } + const metrics_request_handler = async (req, res) => { // Serve all metrics on the root path for system that do have one or more fork running. if (fork_enabled) { @@ -77,7 +103,8 @@ async function start_server( const nsfs_report = { nsfs_counters: io_stats_complete, op_stats_counters: ops_stats_complete, - fs_worker_stats_counters: fs_worker_stats_complete + fs_worker_stats_counters: fs_worker_stats_complete, + disk_usage: get_disk_usage_perc(nsfs_metrics_statfs_path_cache), }; res.end(JSON.stringify(nsfs_report)); return; @@ -209,7 +236,8 @@ async function metrics_nsfs_stats_handler() { const nsfs_report = { nsfs_counters: nsfs_io_stats, op_stats_counters: op_stats_counters, - fs_worker_stats_counters: fs_worker_stats_counters + fs_worker_stats_counters: fs_worker_stats_counters, + disk_usage: get_disk_usage_perc(nsfs_metrics_statfs_path_cache), }; dbg.log1('_create_nsfs_report: nsfs_report', nsfs_report); return JSON.stringify(nsfs_report);