sysmonk/resources/
info.rs1use crate::{legacy, resources, squire};
2use chrono::Utc;
3use std::collections::HashMap;
4use std::collections::HashSet;
5use sysinfo::Disks;
6use sysinfo::System;
7
8pub fn get_disk_usage(disks: &Disks) -> u64 {
14 let mut disks_space: Vec<u64> = vec![];
15 for disk in disks.list() {
16 disks_space.push(disk.total_space());
17 }
18 disks_space.iter().sum()
19}
20
21pub fn get_disks(disks: &Disks) -> Vec<HashMap<String, String>> {
27 let mut disks_info = vec![];
28 for disk in disks.list() {
29 disks_info.push(
30 HashMap::from([
31 ("Name".to_string(), disk.name().to_string_lossy().to_string()),
32 ("Size".to_string(), squire::util::size_converter(disk.total_space()).to_string()),
33 ("Kind".to_string(), disk.file_system().to_string_lossy().to_string()),
34 ("Mount Point".to_string(), disk.mount_point().to_string_lossy().to_string()),
35 ])
36 );
37 }
38 disks_info
39}
40
41
42fn get_gpu_info() -> Vec<String> {
43 let gpu_info = legacy::gpu::get_gpu_info();
44 log::debug!("GPUs: {:?}", gpu_info);
45 let mut gpus: Vec<String> = vec![];
46 for gpu in gpu_info {
47 if let Some(gpu_model) = gpu.get("model") {
48 gpus.push(gpu_model.to_string());
49 }
50 }
51 gpus
52}
53
54fn get_cpu_brand(sys: &System) -> String {
64 let mut cpu_brands: HashSet<String> = HashSet::new();
65 let cpus = sys.cpus();
66 for cpu in cpus {
67 cpu_brands.insert(cpu.brand().to_string());
68 }
69 if cpu_brands.is_empty() {
70 let legacy_cpu_brand_name = legacy::cpu::get_name();
71 return if let Some(cpu_brand) = legacy_cpu_brand_name {
72 log::debug!("Using legacy methods for CPU brand!!");
73 cpu_brand
74 } else {
75 log::error!("Unable to get brand information for all {} CPUs", cpus.len());
76 "Unknown".to_string()
77 };
78 }
79 let mut cpu_brand_list: Vec<String> = cpu_brands.into_iter().collect();
80 cpu_brand_list.sort_by_key(|brand| brand.len());
81 match cpu_brand_list.len() {
82 0 => String::new(),
83 1 => cpu_brand_list[0].clone(),
84 2 => format!("{} and {}", cpu_brand_list[0], cpu_brand_list[1]),
85 _ => {
86 let last = cpu_brand_list.pop().unwrap(); format!("{}, and {}", cpu_brand_list.join(", "), last)
88 }
89 }
90}
91
92pub fn get_sys_info(disks: &Disks) -> HashMap<&'static str, HashMap<&'static str, String>> {
100 let mut sys = System::new_all();
101 sys.refresh_all();
102
103 let boot_time = System::boot_time();
105 let uptime_duration = Utc::now().timestamp() - boot_time as i64;
106 let uptime = squire::util::convert_seconds(uptime_duration);
107
108 let total_memory = squire::util::size_converter(sys.total_memory()); let total_storage = squire::util::size_converter(get_disk_usage(disks)); let os_arch = resources::system::os_arch();
113 let mut basic = HashMap::from_iter(vec![
114 ("Hostname", System::host_name().unwrap_or("Unknown".to_string())),
115 ("Operating System", squire::util::capwords(&os_arch.name, None)),
116 ("Architecture", os_arch.architecture),
117 ("Uptime", uptime),
118 ("CPU cores", sys.cpus().len().to_string()),
119 ("CPU brand", get_cpu_brand(&sys))
120 ]);
121 let gpu_info = get_gpu_info();
122 if !gpu_info.is_empty() {
123 let key = if gpu_info.len() == 1 { "GPU" } else { "GPUs" };
124 basic.insert(key, gpu_info.join(", "));
125 }
126 let mut hash_vec = vec![
127 ("Memory", total_memory),
128 ("Storage", total_storage)
129 ];
130
131 let total_swap = sys.total_swap(); if total_swap != 0 {
133 hash_vec.push(("Swap", squire::util::size_converter(total_swap)));
134 }
135 let mem_storage = HashMap::from_iter(hash_vec);
136 HashMap::from_iter(vec![
137 ("basic", basic),
138 ("mem_storage", mem_storage)
139 ])
140}