@@ -8,13 +8,14 @@ pub mod os;
88pub mod resources;
99pub mod user;
1010
11- #[ derive( Debug , Serialize ) ]
11+ #[ derive( Debug , Serialize , Default ) ]
1212pub struct SystemInformation {
13- pub resources : resources:: Resources ,
14- pub os : os:: OperatingSystem ,
15- pub current_user : ComponentResult < user:: User > ,
16- pub disks : Vec < disk:: Disk > ,
17- pub network : network:: SystemNetworkInfo ,
13+ // All fields are optional, to make it easy to disable modules one by one
14+ pub resources : Option < resources:: Resources > ,
15+ pub os : Option < os:: OperatingSystem > ,
16+ pub current_user : Option < ComponentResult < user:: User > > ,
17+ pub disks : Option < Vec < disk:: Disk > > ,
18+ pub network : Option < network:: SystemNetworkInfo > ,
1819 // TODO:
1920 // Current time
2021 // SElinux/AppArmor
@@ -32,26 +33,47 @@ pub struct SystemInformation {
3233 // - Users/Groups
3334}
3435
36+ /// Common data that is cached between [`SystemInformation::collect`] calls.
37+ pub struct CollectContext {
38+ system : sysinfo:: System ,
39+ }
40+
3541impl SystemInformation {
36- #[ tracing:: instrument( name = "SystemInformation::collect" ) ]
37- pub fn collect ( ) -> Self {
38- tracing:: info!( "Starting data collection" ) ;
42+ /// Collects static information that doesn't need to be refreshed.
43+ #[ tracing:: instrument( name = "SystemInformation::init" ) ]
44+ pub fn init ( ) -> CollectContext {
45+ tracing:: info!( "initializing" ) ;
46+ let mut ctx = CollectContext {
47+ // Each module is responsible for updating the information that it cares about.
48+ system : sysinfo:: System :: new ( ) ,
49+ } ;
50+ if let Err ( err) = user:: User :: init ( & mut ctx. system ) {
51+ tracing:: error!(
52+ error = & err as & dyn std:: error:: Error ,
53+ "failed to initialize user module, ignoring but this will likely cause collection errors..."
54+ ) ;
55+ }
56+ tracing:: info!( "init finished" ) ;
57+ ctx
58+ }
3959
40- // Please note that we use "new_all" to ensure that all list of
41- // components, network interfaces, disks and users are already
42- // filled!
43- let sys = sysinfo :: System :: new_all ( ) ;
60+ /// Collects and reports
61+ # [ tracing :: instrument ( name = "SystemInformation::collect" , skip ( ctx ) ) ]
62+ pub fn collect ( ctx : & mut CollectContext ) -> Self {
63+ tracing :: info! ( "Starting data collection" ) ;
4464
4565 let info = Self {
46- resources : resources:: Resources :: collect ( & sys ) ,
47- os : os:: OperatingSystem :: collect ( ) ,
48- current_user : ComponentResult :: report_from_result (
66+ resources : Some ( resources:: Resources :: collect ( & mut ctx . system ) ) ,
67+ os : Some ( os:: OperatingSystem :: collect ( ) ) ,
68+ current_user : Some ( ComponentResult :: report_from_result (
4969 "User::collect_current" ,
50- user:: User :: collect_current ( & sys) ,
51- ) ,
52- disks : disk:: Disk :: collect_all ( ) ,
53- network : network:: SystemNetworkInfo :: collect ( ) ,
70+ user:: User :: collect_current ( & ctx. system ) ,
71+ ) ) ,
72+ disks : Some ( disk:: Disk :: collect_all ( ) ) ,
73+ network : Some ( network:: SystemNetworkInfo :: collect ( ) ) ,
74+ // ..Default::default()
5475 } ;
76+
5577 tracing:: info!( "Data collection finished" ) ;
5678 info
5779 }
0 commit comments