Skip to content

Commit

Permalink
Fix incorrect CPU usage metrics
Browse files Browse the repository at this point in the history
Remove dedicated in house module for processor and disks
Upgrade `sysinfo` dependency
  • Loading branch information
dormant-user committed Sep 25, 2024
1 parent 69a30ea commit 6d20208
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 366 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ base64 = "0.22.1"
sha2 = "0.10.8"
rand = "0.8.5"
fernet = "0.2.2"
sysinfo = "0.26.7"
sysinfo = "0.31.4"
reqwest = { version = "0.12.7", features = ["blocking", "json"] }
minijinja = { version = "2.3.1", features = ["loader"] }
url = "2.5.2"
Expand Down
238 changes: 0 additions & 238 deletions src/resources/disks.rs

This file was deleted.

86 changes: 73 additions & 13 deletions src/resources/info.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,84 @@
use crate::{resources, squire};
use chrono::Utc;
use std::collections::HashMap;
use sysinfo::{DiskExt, System, SystemExt};
use std::collections::HashSet;
use sysinfo::System;
use sysinfo::Disks;

/// Function to get total disk usage.
///
/// # Arguments
///
/// * `system` - A reference to the `System` struct.
///
/// # Returns
///
/// A `u64` value containing the total disk usage.
pub fn get_disk_usage(system: &System) -> u64 {
pub fn get_disk_usage() -> u64 {
let mut disks_space = vec![];
for disk in system.disks() {
let disks = Disks::new_with_refreshed_list();
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() -> Vec<HashMap<String, String>> {
let mut disks_info = vec![];
let disks = Disks::new_with_refreshed_list();
for disk in disks.list() {
// todo: This is a redundant refresh, perhaps reuse `sys` from `get_sys_info`
// todo: Rename this as partitions instead (becuase that's what they are)
// todo: Check sysinfo source code for an easy way to get the `Model` information
// if disk.name().to_string_lossy().starts_with("/boot") {
// continue;
// }
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()),
("Model".to_string(), disk.name().to_string_lossy().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() {
log::error!("Unable to get brand information for all {} CPUs", cpus.len());
return "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.
Expand All @@ -32,22 +91,23 @@ pub fn get_sys_info() -> HashMap<&'static str, HashMap<&'static str, String>> {
sys.refresh_all();

// Uptime
let boot_time = sys.boot_time();
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(&sys)); // in bytes
let total_storage = squire::util::size_converter(get_disk_usage()); // in bytes

// Basic and Memory/Storage Info
let os_arch = resources::system::os_arch();
let basic = HashMap::from_iter(vec![
("node", sys.host_name().unwrap_or("Unknown".to_string())),
("system", squire::util::capwords(&os_arch.name, None)),
("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_raw", sys.cpus().len().to_string()
)]);
("CPU_cores_raw", sys.cpus().len().to_string()),
("CPU_brand_raw", get_cpu_brand(&sys))
]);
let mut hash_vec = vec![
("memory", total_memory),
("storage", total_storage)
Expand Down
4 changes: 0 additions & 4 deletions src/resources/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@
pub mod info;
/// This module contains all the network related functions.
pub mod network;
/// This module contains processor related functions.
pub mod processor;
/// This module contains disk related functions.
pub mod disks;
/// This module contains system related functions.
pub mod system;
/// This module contains functions that are responsible to stream information via websockets.
Expand Down
Loading

0 comments on commit 6d20208

Please sign in to comment.