From 49cdbbd7c4e07cda6d8d0fcf07082a77e511d24d Mon Sep 17 00:00:00 2001 From: Vignesh Rao Date: Wed, 25 Sep 2024 23:53:18 -0500 Subject: [PATCH] Reuse util functions Add more debug logging Update README.md --- .github/workflows/rust.yml | 8 ++---- README.md | 12 ++++---- src/legacy/cpu_brand.rs | 6 ++-- src/legacy/disks.rs | 16 +++++------ src/legacy/helper.rs | 58 -------------------------------------- src/legacy/mod.rs | 2 -- src/resources/info.rs | 3 +- src/resources/stream.rs | 1 - src/routes/monitor.rs | 12 ++++++-- src/squire/util.rs | 1 - src/templates/monitor.rs | 2 +- 11 files changed, 32 insertions(+), 89 deletions(-) delete mode 100644 src/legacy/helper.rs diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 8d84556..9cc9526 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -109,17 +109,17 @@ jobs: matrix: platform: - release_for: Linux-x86_64 - os: linux-amd64 + os: linux-x64 bin: ${{ needs.release.outputs.bin-name }} name: ${{ needs.release.outputs.pkg-name }}-Linux-x86_64.tar.gz - release_for: Windows-x86_64 - os: windows-amd64 + os: windows-x64 bin: ${{ needs.release.outputs.bin-name }}.exe name: ${{ needs.release.outputs.pkg-name }}-Windows-x86_64.zip - release_for: macOS-x86_64 - os: darwin-amd64 + os: darwin-x64 bin: ${{ needs.release.outputs.bin-name }} name: ${{ needs.release.outputs.pkg-name }}-Darwin-x86_64.tar.gz @@ -155,7 +155,6 @@ jobs: printf '*%.0s' {1..60} && printf "\n" echo "Existing rust version: $(rustc --version)" printf '*%.0s' {1..60} && printf "\n\n" - rustup update && printf "\n" rustup default 1.81.0 && printf "\n" printf '*%.0s' {1..60} && printf "\n" echo "Updated rust version: $(rustc --version)" @@ -250,7 +249,6 @@ jobs: printf '*%.0s' {1..60} && printf "\n" echo "Existing rust version: $(rustc --version)" printf '*%.0s' {1..60} && printf "\n\n" - rustup update && printf "\n" rustup default 1.81.0 && printf "\n" printf '*%.0s' {1..60} && printf "\n" echo "Updated rust version: $(rustc --version)" diff --git a/README.md b/README.md index ac75e46..2f67c1e 100644 --- a/README.md +++ b/README.md @@ -36,22 +36,22 @@ async fn main() {
Download OS specific Executable -###### macOS +###### macOS (x86_64) ```shell curl -o SysMonk-Darwin-x86_64.tar.gz -LH "Accept: application/octet-stream" "https://github.com/thevickypedia/SysMonk/releases/latest/download/SysMonk-Darwin-x86_64.tar.gz" ``` -###### Linux +###### macOS (arm64) ```shell -curl -o SysMonk-Linux-x86_64.tar.gz -LH "Accept: application/octet-stream" "https://github.com/thevickypedia/SysMonk/releases/latest/download/SysMonk-Linux-x86_64.tar.gz" +curl -o SysMonk-Darwin-arm64.tar.gz -LH "Accept: application/octet-stream" "https://github.com/thevickypedia/SysMonk/releases/latest/download/SysMonk-Darwin-arm64.tar.gz" ``` -###### RaspberryPi +###### Linux (x86_64) ```shell -curl -o SysMonk-RaspberryPi.tar.gz -LH "Accept: application/octet-stream" "https://github.com/thevickypedia/SysMonk/releases/latest/download/SysMonk-RaspberryPi.tar.gz" +curl -o SysMonk-Linux-x86_64.tar.gz -LH "Accept: application/octet-stream" "https://github.com/thevickypedia/SysMonk/releases/latest/download/SysMonk-Linux-x86_64.tar.gz" ``` -###### Windows +###### Windows (x86_64) ```shell curl -o SysMonk-Windows-x86_64.zip -LH "Accept: application/octet-stream" "https://github.com/thevickypedia/SysMonk/releases/latest/download/SysMonk-Windows-x86_64.zip" ``` diff --git a/src/legacy/cpu_brand.rs b/src/legacy/cpu_brand.rs index 9814381..2d1c3ec 100644 --- a/src/legacy/cpu_brand.rs +++ b/src/legacy/cpu_brand.rs @@ -1,7 +1,7 @@ use std::fs::File; use std::io::{self, BufRead}; -use crate::legacy::helper::run_command; +use crate::squire; /// Function to get processor information. /// @@ -13,7 +13,7 @@ use crate::legacy::helper::run_command; /// /// A `Option` containing the processor information if successful, otherwise `None`. fn get_processor_info_darwin(lib_path: &str) -> Result { - let result = run_command(lib_path, &["-n", "machdep.cpu.brand_string"]); + let result = squire::util::run_command(lib_path, &["-n", "machdep.cpu.brand_string"]); if result.is_err() { return Err("Failed to get processor info"); } @@ -60,7 +60,7 @@ fn get_processor_info_linux(lib_path: &str) -> Result { /// /// A `Option` containing the processor information if successful, otherwise `None`. fn get_processor_info_windows(lib_path: &str) -> Result { - let result = run_command(lib_path, &["cpu", "get", "name"]); + let result = squire::util::run_command(lib_path, &["cpu", "get", "name"]); let output = match result { Ok(output) => output, Err(_) => return Err("Failed to get processor info"), diff --git a/src/legacy/disks.rs b/src/legacy/disks.rs index 5a07b8b..236123c 100644 --- a/src/legacy/disks.rs +++ b/src/legacy/disks.rs @@ -3,7 +3,7 @@ use serde_json::Value; use std::collections::HashMap; use std::str; -use crate::legacy::helper::{run_command, size_converter}; +use crate::squire; /// Function to parse size string for Linux. /// @@ -27,7 +27,7 @@ fn parse_size(size_str: &str) -> String { ("P", 2_f64.powi(50)), ]); let multiplier = unit_multipliers.get(unit).unwrap_or(&1.0); - return size_converter((value * multiplier) as u64); + return squire::util::size_converter((value * multiplier) as u64); } size_str.replace("K", " KB") .replace("M", " MB") @@ -47,7 +47,7 @@ fn parse_size(size_str: &str) -> String { /// /// A `bool` indicating if the disk is physical. fn is_physical_disk(lib_path: &str, device_id: &str) -> bool { - let result = run_command(lib_path, &["info", device_id]); + let result = squire::util::run_command(lib_path, &["info", device_id]); let output = match result { Ok(output) => output, Err(_) => { @@ -73,7 +73,7 @@ fn is_physical_disk(lib_path: &str, device_id: &str) -> bool { /// /// A `Vec` of `HashMap` containing the disk information. fn linux_disks(lib_path: &str) -> Vec> { - let result = run_command(lib_path, &["-o", "NAME,SIZE,TYPE,MODEL", "-d"]); + let result = squire::util::run_command(lib_path, &["-o", "NAME,SIZE,TYPE,MODEL", "-d"]); let output = match result { Ok(output) => output, Err(_) => { @@ -112,7 +112,7 @@ fn linux_disks(lib_path: &str) -> Vec> { /// /// A `Vec` of `HashMap` containing the disk information. fn darwin_disks(lib_path: &str) -> Vec> { - let result = run_command(lib_path, &["list"]); + let result = squire::util::run_command(lib_path, &["list"]); let output = match result { Ok(output) => output, Err(_) => { @@ -128,7 +128,7 @@ fn darwin_disks(lib_path: &str) -> Vec> { if !is_physical_disk(lib_path, device_id) { continue; } - let result = run_command(lib_path, &["info", device_id]); + let result = squire::util::run_command(lib_path, &["info", device_id]); let disk_info_output = match result { Ok(output) => output, Err(_) => { @@ -166,7 +166,7 @@ fn reformat_windows(data: &mut HashMap) -> HashMap) -> HashMap Vec> { let ps_command = "Get-CimInstance Win32_DiskDrive | Select-Object Caption, DeviceID, Model, Partitions, Size | ConvertTo-Json"; - let result = run_command(lib_path, &["-Command", ps_command]); + let result = squire::util::run_command(lib_path, &["-Command", ps_command]); let output = match result { Ok(output) => output, Err(_) => { diff --git a/src/legacy/helper.rs b/src/legacy/helper.rs deleted file mode 100644 index 758613e..0000000 --- a/src/legacy/helper.rs +++ /dev/null @@ -1,58 +0,0 @@ -use std::process::Command; - -/// Function to convert byte size to human-readable format -/// -/// # Arguments -/// -/// * `byte_size` - The size in bytes to convert -/// -/// # Returns -/// -/// A `String` containing the human-readable format of the byte size -pub fn size_converter(byte_size: u64) -> String { - let size_name = ["B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"]; - let mut index = 0; - let mut size = byte_size as f64; - - while size >= 1024.0 && index < size_name.len() - 1 { - size /= 1024.0; - index += 1; - } - - format!("{:.2} {}", size, size_name[index]) -} - - -/// Function to parse size string. -/// -/// # Arguments -/// -/// * `size_str` - The size string to parse -/// * `unit` - The unit to convert the size to -/// -/// # Returns -/// -/// A `String` containing the parsed size string. -pub fn run_command(command: &str, args: &[&str]) -> Result { - match Command::new(command) - .args(args) - .output() - { - Ok(output) => { - if output.status.success() { - log::debug!("Command [{}] executed successfully", &command); - Ok(String::from_utf8_lossy(&output.stdout).trim().to_string()) - } else { - let stderr = String::from_utf8_lossy(&output.stderr).to_string(); - let exit_code = output.status.code().unwrap_or(-1); - log::error!("Command [{}] failed with exit code: {}", command, exit_code); - log::error!("Stderr: {}", stderr); - Err(stderr) - } - } - Err(err) => { - log::error!("Failed to execute command: {}", err); - Err(err.to_string()) - } - } -} diff --git a/src/legacy/mod.rs b/src/legacy/mod.rs index bc73065..927d4c1 100644 --- a/src/legacy/mod.rs +++ b/src/legacy/mod.rs @@ -2,5 +2,3 @@ pub mod cpu_brand; /// This module contains disk related functions. pub mod disks; -/// Helper for legacy functions. -pub mod helper; diff --git a/src/resources/info.rs b/src/resources/info.rs index db9c484..bc78d3c 100644 --- a/src/resources/info.rs +++ b/src/resources/info.rs @@ -56,11 +56,12 @@ fn get_cpu_brand(sys: &System) -> String { cpu_brands.insert(cpu.brand().to_string()); } if cpu_brands.is_empty() { - log::warn!("Unable to get brand information for all {} CPUs", cpus.len()); 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() }; } diff --git a/src/resources/stream.rs b/src/resources/stream.rs index 61dd26f..03d2a91 100644 --- a/src/resources/stream.rs +++ b/src/resources/stream.rs @@ -47,7 +47,6 @@ fn get_docker_stats() -> Result, Box { - log::debug!("No running containers"); return Ok(vec![]); } Err(err) => { diff --git a/src/routes/monitor.rs b/src/routes/monitor.rs index 7db62a7..c2b2a31 100644 --- a/src/routes/monitor.rs +++ b/src/routes/monitor.rs @@ -41,10 +41,16 @@ pub async fn monitor(request: HttpRequest, let sys_info_map = resources::info::get_sys_info(&disks); let legacy_disk_info = legacy::disks::get_all_disks(); - let sys_info_disks = if legacy_disk_info.is_empty() { - resources::info::get_disks(&disks) - } else { + // legacy functions have a mechanism to check for physical devices, so it takes precedence + let has_name_and_size = !legacy_disk_info.is_empty() && + legacy_disk_info.iter().all(|disk| { + disk.contains_key("Name") && disk.contains_key("Size") + }); + let sys_info_disks = if has_name_and_size { + log::debug!("Using legacy methods for disks!"); legacy_disk_info + } else { + resources::info::get_disks(&disks) }; let sys_info_network = resources::network::get_network_info().await; diff --git a/src/squire/util.rs b/src/squire/util.rs index e6b0eea..a14760a 100644 --- a/src/squire/util.rs +++ b/src/squire/util.rs @@ -104,7 +104,6 @@ pub fn run_command(command: &str, args: &[&str]) -> Result { { Ok(output) => { if output.status.success() { - log::debug!("Command [{}] executed successfully", &command); Ok(String::from_utf8_lossy(&output.stdout).trim().to_string()) } else { let stderr = String::from_utf8_lossy(&output.stderr).to_string(); diff --git a/src/templates/monitor.rs b/src/templates/monitor.rs index c9630fd..7b7f931 100644 --- a/src/templates/monitor.rs +++ b/src/templates/monitor.rs @@ -197,7 +197,7 @@ pub fn get_content() -> String {

SysMonk - System Monitor

- System Information + System Information
{% for key, value in sys_info_basic|items() %} {{ key }}: {{ value }}