diff --git a/platforms/unix/hardwareinfo/src/linux/drive.rs b/platforms/unix/hardwareinfo/src/linux/drive.rs index 8a54fd6..5b80fab 100644 --- a/platforms/unix/hardwareinfo/src/linux/drive.rs +++ b/platforms/unix/hardwareinfo/src/linux/drive.rs @@ -1,9 +1,11 @@ use anyhow::{Context, Result}; +use core::str; use regex::Regex; use std::{ collections::HashMap, fmt::Display, path::{Path, PathBuf}, + process::Command, sync::LazyLock, }; @@ -289,3 +291,29 @@ impl Drive { } } } + +pub fn get_free_space(path: &PathBuf) -> u64 { + // Define the device path as a variable + let path_str = path.to_str().unwrap_or("").replace("/sys/block/", "/dev/"); + + // Construct the command string + let command = format!( + "lsblk -bno FSAVAIL,MOUNTPOINT {} | awk '{{sum += $1}} END {{print sum / (1024^3) \"\"}}'", + path_str + ); + + // Execute the command + let output = Command::new("sh").arg("-c").arg(&command).output(); + + // Check if the command was successful + if let Ok(output) = output { + // Print the command's standard output + if let Ok(result) = str::from_utf8(&output.stdout) { + return (result.trim().parse::().unwrap_or(0.0) * 1.074) as u64; + } else { + return 0; + } + } else { + return 0; + } +} diff --git a/platforms/unix/hardwareinfo/src/linux/mod.rs b/platforms/unix/hardwareinfo/src/linux/mod.rs index 4040859..2edf987 100644 --- a/platforms/unix/hardwareinfo/src/linux/mod.rs +++ b/platforms/unix/hardwareinfo/src/linux/mod.rs @@ -64,10 +64,12 @@ pub fn linux_hardware_info(data: &mut Data) { if !d.is_virtual { let inner = &d.inner; + let drive_space = drive::get_free_space(path); + data.hw_info.system.storage.disks.push(CoresDisk { name: inner.clone().model.unwrap_or("N/A".to_string()), total_space: inner.clone().capacity().unwrap_or(1) / 1000 / 1000 / 1000, - free_space: 0, + free_space: drive_space, throughput_read: 0.0, throughput_write: 0.0, data_read: 0.0,