1use std::fs;
2
3#[derive(Clone, Debug, Default)]
4pub struct HostInfo {
5 pub hostname: String,
6 pub os_image: String,
7 pub kernel: String,
8 pub arch: &'static str,
9 pub cpus: u64,
10 pub mem_total_kib: u64,
11 pub machine_id: String,
12 pub boot_id: String,
13}
14
15pub fn host_info() -> HostInfo {
16 let hostname = fs::read_to_string("/etc/hostname")
17 .map(|s| s.trim().to_string())
18 .ok()
19 .filter(|s| !s.is_empty())
20 .or_else(|| std::env::var("HOSTNAME").ok())
21 .unwrap_or_else(|| "krab-node".into());
22 let os_image = fs::read_to_string("/etc/os-release")
23 .ok()
24 .and_then(|s| {
25 s.lines()
26 .find_map(|l| l.strip_prefix("PRETTY_NAME="))
27 .map(|v| v.trim_matches('"').to_string())
28 })
29 .unwrap_or_else(|| std::env::consts::OS.to_string());
30 let kernel = fs::read_to_string("/proc/sys/kernel/osrelease")
31 .map(|s| s.trim().to_string())
32 .unwrap_or_default();
33 let arch = match std::env::consts::ARCH {
34 "aarch64" => "arm64",
35 "x86_64" => "amd64",
36 o => o,
37 };
38 let cpus = std::thread::available_parallelism()
39 .map(|n| n.get() as u64)
40 .unwrap_or(1);
41 HostInfo {
42 hostname: sanitize(&hostname),
43 os_image,
44 kernel,
45 arch,
46 cpus,
47 mem_total_kib: meminfo().0,
48 machine_id: fs::read_to_string("/etc/machine-id")
49 .map(|s| s.trim().to_string())
50 .unwrap_or_default(),
51 boot_id: fs::read_to_string("/proc/sys/kernel/random/boot_id")
52 .map(|s| s.trim().to_string())
53 .unwrap_or_default(),
54 }
55}
56
57pub fn sanitize(s: &str) -> String {
58 let out: String = s
59 .to_lowercase()
60 .chars()
61 .map(|c| {
62 if c.is_ascii_alphanumeric() || c == '-' || c == '.' {
63 c
64 } else {
65 '-'
66 }
67 })
68 .collect();
69 let t = out.trim_matches(|c| c == '-' || c == '.');
70 if t.is_empty() {
71 "unnamed".into()
72 } else {
73 t.chars().take(63).collect()
74 }
75}
76
77pub fn meminfo() -> (u64, u64) {
78 let s = fs::read_to_string("/proc/meminfo").unwrap_or_default();
79 let get = |k: &str| {
80 s.lines()
81 .find_map(|l| l.strip_prefix(k))
82 .and_then(|v| {
83 v.trim_start_matches(':')
84 .split_whitespace()
85 .next()
86 .and_then(|x| x.parse::<u64>().ok())
87 })
88 .unwrap_or(0)
89 };
90 (get("MemTotal"), get("MemAvailable"))
91}
92
93pub fn disk_free_percent(path: &str) -> Option<u64> {
94 let c = std::ffi::CString::new(path).ok()?;
95 let mut st: libc::statvfs = unsafe { std::mem::zeroed() };
96 if unsafe { libc::statvfs(c.as_ptr(), &mut st) } != 0 || st.f_blocks == 0 {
97 return None;
98 }
99 Some(st.f_bavail as u64 * 100 / st.f_blocks as u64)
100}
101
102pub fn threads_used_percent() -> Option<u64> {
103 let total: u64 = fs::read_to_string("/proc/loadavg")
104 .ok()?
105 .split_whitespace()
106 .nth(3)?
107 .split('/')
108 .nth(1)?
109 .parse()
110 .ok()?;
111 let max: u64 = fs::read_to_string("/proc/sys/kernel/pid_max")
112 .ok()?
113 .trim()
114 .parse()
115 .ok()?;
116 (max > 0).then(|| total * 100 / max)
117}
118
119pub fn cpu_busy_ticks() -> Option<u64> {
120 crate::cgroup::parse_stat_cpu(&fs::read_to_string("/proc/stat").ok()?)
121}
122
123pub fn rss_kib() -> u64 {
124 fs::read_to_string("/proc/self/status")
125 .ok()
126 .and_then(|s| {
127 s.lines()
128 .find_map(|l| l.strip_prefix("VmRSS:"))
129 .and_then(|v| v.split_whitespace().next().and_then(|x| x.parse().ok()))
130 })
131 .unwrap_or(0)
132}