@@ -13,6 +13,8 @@ const stats_aggregator = require('../system_services/stats_aggregator');
1313const AggregatorRegistry = require ( 'prom-client' ) . AggregatorRegistry ;
1414const aggregatorRegistry = new AggregatorRegistry ( ) ;
1515const http_utils = require ( '../../util/http_utils' ) ;
16+ const nb_native = require ( '../../util/nb_native' ) ;
17+ const { get_process_fs_context } = require ( '../../util/native_fs_utils' ) ;
1618
1719// Currenty supported reprots
1820const reports = Object . seal ( {
@@ -51,9 +53,44 @@ async function export_all_metrics() {
5153 return all_metrics . join ( '\n\n' ) ;
5254}
5355
56+ /** @type {Record<string, Record<string, number>> } */
57+ const nsfs_metrics_statfs_path_cache = { } ;
58+
59+ /**
60+ *
61+ * @param {Record<string, number> } statfs
62+ * @returns
63+ */
64+ function get_disk_usage_perc ( statfs ) {
65+ if ( ! statfs || ! statfs . blocks || typeof statfs . bfree !== 'number' ) return undefined ;
66+ const ratio = ( statfs . blocks - statfs . bfree ) / statfs . blocks ;
67+ return Number ( ( ratio * 100 ) . toFixed ( 3 ) ) ;
68+ }
69+
70+ /**
71+ * @param {Record<string, Record<string, number>> } statfs_map
72+ */
73+ function get_disk_usage_report ( statfs_map ) {
74+ return Object . keys ( statfs_map ) . reduce ( ( acc , curr ) => ( { ...acc , [ curr ] : get_disk_usage_perc ( statfs_map [ curr ] ) } ) , { } ) ;
75+ }
76+
77+ function setup_statfs_monitor ( ) {
78+ setInterval ( async ( ) => {
79+ const fs_context = get_process_fs_context ( ) ;
80+ await Promise . all ( config . NSFS_GLACIER_METRICS_STATFS_PATHS ?. map ( async path => {
81+ try {
82+ nsfs_metrics_statfs_path_cache [ path ] = await nb_native ( ) . fs . statfs ( fs_context , path ) ;
83+ dbg . log4 ( 'updated \'nsfs_metrics_statfs_path_cache\' with' , nsfs_metrics_statfs_path_cache ) ;
84+ } catch ( error ) {
85+ dbg . warn ( 'failed to updated \'nsfs_metrics_statfs_path_cache\':' , error ) ;
86+ }
87+ } ) || [ ] ) ;
88+ } , config . NSFS_GLACIER_METRICS_STATFS_INTERVAL ) . unref ( ) ;
89+ }
90+
5491/**
5592 * Start Noobaa metrics server for http and https server
56- *
93+ *
5794 * @param {number? } port prometheus metris port.
5895 * @param {number? } ssl_port prometheus metris https port.
5996 * @param {boolean? } fork_enabled request from frok or not.
@@ -71,10 +108,15 @@ async function start_server(
71108 if ( ! config . PROMETHEUS_ENABLED ) {
72109 return ;
73110 }
111+
112+ if ( config . NSFS_GLACIER_METRICS_STATFS_PATHS ?. length ) {
113+ setup_statfs_monitor ( ) ;
114+ }
115+
74116 const metrics_request_handler = async ( req , res ) => {
75117 if ( config . NOOBAA_METRICS_AUTH_ENABLED && ! http_utils . authorize_bearer ( req , res , [ ROLE_METRICS , ROLE_ADMIN ] ) ) {
76118 // Authorize bearer token metrics endpoint
77- // Role 'metrics' is used in operator RPC call,
119+ // Role 'metrics' is used in operator RPC call,
78120 // Update operator RPC call first before changing role
79121 return ;
80122 }
@@ -86,7 +128,8 @@ async function start_server(
86128 const nsfs_report = {
87129 nsfs_counters : io_stats_complete ,
88130 op_stats_counters : ops_stats_complete ,
89- fs_worker_stats_counters : fs_worker_stats_complete
131+ fs_worker_stats_counters : fs_worker_stats_complete ,
132+ disk_usage : get_disk_usage_report ( nsfs_metrics_statfs_path_cache ) ,
90133 } ;
91134 res . end ( JSON . stringify ( nsfs_report ) ) ;
92135 return ;
@@ -218,7 +261,8 @@ async function metrics_nsfs_stats_handler() {
218261 const nsfs_report = {
219262 nsfs_counters : nsfs_io_stats ,
220263 op_stats_counters : op_stats_counters ,
221- fs_worker_stats_counters : fs_worker_stats_counters
264+ fs_worker_stats_counters : fs_worker_stats_counters ,
265+ disk_usage : get_disk_usage_report ( nsfs_metrics_statfs_path_cache ) ,
222266 } ;
223267 dbg . log1 ( '_create_nsfs_report: nsfs_report' , nsfs_report ) ;
224268 return JSON . stringify ( nsfs_report ) ;
0 commit comments