@@ -33,6 +33,7 @@ use toml::Table;
3333use tower:: ServiceBuilder ;
3434use std:: { collections:: HashMap , sync:: Arc } ;
3535use tokio:: sync:: { RwLock , Semaphore } ;
36+ use sysinfo:: Disks ;
3637
3738#[ derive( Parser , Debug ) ]
3839#[ command( version, about, long_about = None ) ]
@@ -193,6 +194,34 @@ async fn initial_setup() -> Option<RustlsConfig> {
193194 }
194195}
195196
197+ async fn ax_metrics ( ) -> ( StatusCode , String ) {
198+ /*
199+ Prometheus metrics:
200+ storage_files_cached NNN
201+ storage_free_space NNN
202+ */
203+ let mut metrics = String :: new ( ) ;
204+ // prometehus header
205+ metrics. push_str ( "# HELP storage_free_space Free space on the disk\n " ) ;
206+ metrics. push_str ( "# TYPE storage_free_space gauge\n " ) ;
207+ metrics. push_str ( "# HELP storage_total_space Total space on the disk\n " ) ;
208+ metrics. push_str ( "# TYPE storage_total_space gauge\n " ) ;
209+ let hostname = sysinfo:: System :: host_name ( ) . unwrap_or_else ( || "<unknown>" . to_owned ( ) ) ;
210+
211+ let disks = Disks :: new_with_refreshed_list ( ) ;
212+ for disk in disks. list ( ) {
213+ // name, mount_point, total_space, available_space
214+ let tag_diskname = disk. name ( ) . to_string_lossy ( ) ;
215+ let tag_mount_point = disk. mount_point ( ) . to_string_lossy ( ) ;
216+ let tag_total_space = disk. total_space ( ) ;
217+ let tag_available_space = disk. available_space ( ) ;
218+
219+ metrics. push_str ( & format ! ( "storage_free_space {{hostname=\" {}\" , diskname=\" {}\" , mount_point=\" {}\" }} {}\n " , hostname, tag_diskname, tag_mount_point, tag_available_space) ) ;
220+ metrics. push_str ( & format ! ( "storage_total_space {{hostname=\" {}\" , diskname=\" {}\" , mount_point=\" {}\" }} {}\n " , hostname, tag_diskname, tag_mount_point, tag_total_space) ) ;
221+ }
222+ ( StatusCode :: OK , metrics)
223+ }
224+
196225#[ tokio:: main]
197226async fn main ( ) {
198227 tracing_subscriber:: fmt:: init ( ) ;
@@ -216,6 +245,7 @@ async fn main() {
216245 . route ( "/upload" , post ( ax_post_file) )
217246 . route ( "/*filepath" , get ( ax_get_file) )
218247 . route ( "/v1/list" , get ( ax_list_files) )
248+ . route ( "/metrics" , get ( ax_metrics) )
219249 . layer ( ServiceBuilder :: new ( ) . layer ( DefaultBodyLimit :: max ( 1024 * 1024 * 1024 * 4 ) ) )
220250 . with_state ( state) ;
221251
0 commit comments