This repository was archived by the owner on Jun 29, 2026. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmod.rs
More file actions
83 lines (74 loc) · 2.72 KB
/
Copy pathmod.rs
File metadata and controls
83 lines (74 loc) · 2.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
use serde::Serialize;
use crate::error::ComponentResult;
pub mod disk;
pub mod network;
pub mod os;
pub mod resources;
pub mod user;
#[derive(Debug, Serialize, Default)]
pub struct SystemInformation {
// All fields are optional, to make it easy to disable modules one by one
pub resources: Option<resources::Resources>,
pub os: Option<os::OperatingSystem>,
pub current_user: Option<ComponentResult<user::User>>,
pub disks: Option<Vec<disk::Disk>>,
pub network: Option<ComponentResult<network::SystemNetworkInfo>>,
// TODO:
// Current time
// SElinux/AppArmor
// Maybe env variables (may contain secrets)
// dmesg/syslog?
// capabilities?
// downward API
// Somehow get the custom resources logged?
// Things left out for now because it doesn't seem too useful:
// - Running processes
// - Uptime/boot time
// - Load average
// - Network utilization
// - Users/Groups
}
/// Common data that is cached between [`SystemInformation::collect`] calls.
pub struct CollectContext {
system: sysinfo::System,
}
impl SystemInformation {
/// Collects static information that doesn't need to be refreshed.
#[tracing::instrument(name = "SystemInformation::init")]
pub fn init() -> CollectContext {
tracing::debug!("initializing");
let mut ctx = CollectContext {
// Each module is responsible for updating the information that it cares about.
system: sysinfo::System::new(),
};
if let Err(err) = user::User::init(&mut ctx.system) {
tracing::error!(
error = &err as &dyn std::error::Error,
"failed to initialize user module, ignoring but this will likely cause collection errors..."
);
}
tracing::debug!("init finished");
ctx
}
/// Collects and reports
#[tracing::instrument(name = "SystemInformation::collect", skip(ctx))]
pub async fn collect(ctx: &mut CollectContext) -> Self {
tracing::debug!("Starting data collection");
let info = Self {
resources: Some(resources::Resources::collect(&mut ctx.system)),
os: Some(os::OperatingSystem::collect()),
current_user: Some(ComponentResult::report_from_result(
"User::collect_current",
user::User::collect_current(&ctx.system),
)),
disks: Some(disk::Disk::collect_all()),
network: Some(ComponentResult::report_from_result(
"SystemNetworkInfo::collect",
network::SystemNetworkInfo::collect().await,
)),
// ..Default::default()
};
tracing::debug!("Data collection finished");
info
}
}