11use crate :: helpers:: {
22 CheckGroup , CheckGroupResult , CheckResult ,
33 CheckResultValue :: { Errored , Failed , Passed } ,
4+ host_executor:: HostNamespaceExecutor ,
45} ;
56
7+ use futures:: { FutureExt , future:: join_all} ;
68use async_trait:: async_trait;
79use log:: debug;
810use sysinfo:: { Disks , System } ;
@@ -12,11 +14,16 @@ const NAME: &str = "System Checks";
1214const MINIMUM_MEMORY : u64 = 4 * 1024 * 1024 * 1024 ; // 4GB
1315const MINIMUM_DISK : u64 = 20 * 1024 * 1024 * 1024 ; // 20GB
1416
15- pub struct SystemChecks ;
17+ pub struct SystemChecks {
18+ host_executor : HostNamespaceExecutor ,
19+ }
1620
1721impl SystemChecks {
18- pub fn run_all ( & self ) -> CheckGroupResult {
19- let results = vec ! [ self . enough_memory( ) , self . enough_disk( ) ] ;
22+ pub fn new ( host_executor : HostNamespaceExecutor ) -> Self {
23+ SystemChecks { host_executor }
24+ }
25+ pub async fn run_all ( & self ) -> CheckGroupResult {
26+ let results = join_all ( [ self . enough_memory ( ) . boxed ( ) , self . enough_disk ( ) . boxed ( ) ] ) . await ;
2027
2128 let mut group_result = Passed ;
2229 for res in results. iter ( ) {
@@ -37,12 +44,25 @@ impl SystemChecks {
3744 }
3845 }
3946
40- fn enough_memory ( & self ) -> CheckResult {
47+ async fn enough_memory ( & self ) -> CheckResult {
4148 let name = String :: from ( "Enough Memory" ) ;
42- let mut sys = System :: new_all ( ) ;
43- sys. refresh_all ( ) ;
4449
45- let total_mem = sys. total_memory ( ) ;
50+ let total_mem = match self
51+ . host_executor
52+ . spawn_in_host_ns ( async {
53+ let mut sys = System :: new_all ( ) ;
54+ sys. refresh_all ( ) ;
55+
56+ sys. total_memory ( )
57+ } )
58+ . await
59+ {
60+ Ok ( mem) => mem,
61+ Err ( e) => {
62+ return CheckResult :: new ( & name, Errored ( e. to_string ( ) ) ) ;
63+ }
64+ } ;
65+
4666 debug ! ( "total memory = {total_mem}" ) ;
4767
4868 let mut result = Passed ;
@@ -53,26 +73,40 @@ impl SystemChecks {
5373 CheckResult :: new ( & name, result)
5474 }
5575
56- fn enough_disk ( & self ) -> CheckResult {
76+ async fn enough_disk ( & self ) -> CheckResult {
5777 let name = String :: from ( "Enough Disk" ) ;
58- let mut result = Failed ( String :: from ( "Not enough disk space on any disk" ) ) ;
59- let disks = Disks :: new_with_refreshed_list ( ) ;
60- for disk in & disks {
61- if disk. available_space ( ) < MINIMUM_DISK {
62- debug ! (
63- "Not enough space on disk mounted at {} - {}" ,
64- disk. mount_point( ) . display( ) ,
65- disk. available_space( )
66- ) ;
67- } else {
68- debug ! (
69- "Enough space on disk mounted at {} - {}" ,
70- disk. mount_point( ) . display( ) ,
71- disk. available_space( )
72- ) ;
73- result = Passed ;
78+
79+ let result = match self
80+ . host_executor
81+ . spawn_in_host_ns ( async {
82+ let mut result = Failed ( String :: from ( "Not enough disk space on any disk" ) ) ;
83+ let disks = Disks :: new_with_refreshed_list ( ) ;
84+ for disk in & disks {
85+ if disk. available_space ( ) < MINIMUM_DISK {
86+ debug ! (
87+ "Not enough space on disk mounted at {} - {}" ,
88+ disk. mount_point( ) . display( ) ,
89+ disk. available_space( )
90+ ) ;
91+ } else {
92+ debug ! (
93+ "Enough space on disk mounted at {} - {}" ,
94+ disk. mount_point( ) . display( ) ,
95+ disk. available_space( )
96+ ) ;
97+ result = Passed ;
98+ }
99+ }
100+ result
101+ } )
102+ . await
103+ {
104+ Ok ( result) => result,
105+ Err ( e) => {
106+ return CheckResult :: new ( & name, Errored ( e. to_string ( ) ) ) ;
74107 }
75- }
108+ } ;
109+
76110 CheckResult :: new ( & name, result)
77111 }
78112}
@@ -92,6 +126,6 @@ impl CheckGroup for SystemChecks {
92126 }
93127
94128 async fn run ( & self ) -> CheckGroupResult {
95- self . run_all ( )
129+ self . run_all ( ) . await
96130 }
97131}
0 commit comments