sysmonk/resources/
info.rs

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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
use crate::{legacy, resources, squire};
use chrono::Utc;
use std::collections::HashMap;
use std::collections::HashSet;
use sysinfo::Disks;
use sysinfo::System;

/// Function to get total disk usage.
///
/// # Returns
///
/// A `u64` value containing the total disk usage.
pub fn get_disk_usage(disks: &Disks) -> u64 {
    let mut disks_space: Vec<u64> = vec![];
    for disk in disks.list() {
        disks_space.push(disk.total_space());
    }
    disks_space.iter().sum()
}

/// Function to get individual disk specs.
///
/// # Returns
///
/// A `vec` of .
pub fn get_disks(disks: &Disks) -> Vec<HashMap<String, String>> {
    let mut disks_info = vec![];
    for disk in disks.list() {
        disks_info.push(
            HashMap::from([
                ("Name".to_string(), disk.name().to_string_lossy().to_string()),
                ("Size".to_string(), squire::util::size_converter(disk.total_space()).to_string()),
                ("Kind".to_string(), disk.file_system().to_string_lossy().to_string()),
                ("Mount Point".to_string(), disk.mount_point().to_string_lossy().to_string()),
            ])
        );
    }
    disks_info
}

/// Function to get CPU brand names as a comma separated string.
///
/// # Arguments
///
/// * `system` - A reference to the `System` struct.
///
/// # Returns
///
/// A `String` with CPU brand names.
fn get_cpu_brand(sys: &System) -> String {
    let mut cpu_brands: HashSet<String> = HashSet::new();
    let cpus = sys.cpus();
    for cpu in cpus {
        cpu_brands.insert(cpu.brand().to_string());
    }
    if cpu_brands.is_empty() {
        let legacy_cpu_brand_name = legacy::cpu_brand::get_name();
        return if let Some(cpu_brand) = legacy_cpu_brand_name {
            log::debug!("Using legacy methods for CPU brand!!");
            cpu_brand
        } else {
            log::error!("Unable to get brand information for all {} CPUs", cpus.len());
            "Unknown".to_string()
        };
    }
    let mut cpu_brand_list: Vec<String> = cpu_brands.into_iter().collect();
    cpu_brand_list.sort_by_key(|brand| brand.len());
    match cpu_brand_list.len() {
        0 => String::new(),
        1 => cpu_brand_list[0].clone(),
        2 => format!("{} and {}", cpu_brand_list[0], cpu_brand_list[1]),
        _ => {
            let last = cpu_brand_list.pop().unwrap(); // Remove and store the last element
            format!("{}, and {}", cpu_brand_list.join(", "), last)
        }
    }
}

/// Function to get system information
///
/// This function retrieves system information such as basic system information and memory/storage information.
///
/// # Returns
///
/// A tuple containing the `SystemInfoBasic` and `SystemInfoMemStorage` structs.
pub fn get_sys_info(disks: &Disks) -> HashMap<&'static str, HashMap<&'static str, String>> {
    let mut sys = System::new_all();
    sys.refresh_all();

    // Uptime
    let boot_time = System::boot_time();
    let uptime_duration = Utc::now().timestamp() - boot_time as i64;
    let uptime = squire::util::convert_seconds(uptime_duration);

    let total_memory = squire::util::size_converter(sys.total_memory());  // in bytes
    let total_storage = squire::util::size_converter(get_disk_usage(disks));  // in bytes

    // Basic and Memory/Storage Info
    let os_arch = resources::system::os_arch();
    let basic = HashMap::from_iter(vec![
        ("Hostname", System::host_name().unwrap_or("Unknown".to_string())),
        ("Operating System", squire::util::capwords(&os_arch.name, None)),
        ("Architecture", os_arch.architecture),
        ("Uptime", uptime),
        ("CPU cores", sys.cpus().len().to_string()),
        ("CPU brand", get_cpu_brand(&sys))
    ]);
    let mut hash_vec = vec![
        ("Memory", total_memory),
        ("Storage", total_storage)
    ];

    let total_swap = sys.total_swap();  // in bytes
    if total_swap != 0 {
        hash_vec.push(("Swap", squire::util::size_converter(total_swap)));
    }
    let mem_storage = HashMap::from_iter(hash_vec);
    HashMap::from_iter(vec![
        ("basic", basic),
        ("mem_storage", mem_storage)
    ])
}