Skip to content

Commit

Permalink
mac disk info
Browse files Browse the repository at this point in the history
  • Loading branch information
Levminer committed Dec 20, 2024
1 parent aeb73ad commit 79f08d0
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 54 deletions.
52 changes: 29 additions & 23 deletions platforms/unix/hardwareinfo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,35 @@ pub struct CoresDisk {
pub last_timestamp: SystemTime,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct SmartDevice {
r#type: String,
}

#[derive(Debug, Serialize, Deserialize, Clone, Copy)]
pub struct SmartInfo {
temperature: u64,
percentage_used: u64,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct SmartAttributeArray {
name: String,
value: u64,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct SmartAttribute {
table: Vec<SmartAttributeArray>,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct SmartctlDiskInfo {
device: SmartDevice,
nvme_smart_health_information_log: Option<SmartInfo>,
ata_smart_attributes: Option<SmartAttribute>,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct CoresDiskInfo {
Expand Down Expand Up @@ -556,29 +585,6 @@ pub fn refresh_hardware_info(data: &mut Data) {
}
}

//Display processes ID, name na disk usage:
// for (_pid, process) in data.sys.processes() {}

// Disks
// if data.first_run {
// let disks = Disks::new_with_refreshed_list();
// for disk in disks.list() {
// let free_space = disk.available_space() as f64 / gb;
// let total_space = disk.total_space() as f64 / gb;
// let name = disk.name().to_str().unwrap().to_string();

// data.hw_info.system.storage.disks.push(CoresDisk {
// name: name.clone(),
// total_space: total_space as u64,
// free_space: free_space as u64,
// throughput_read: 0.0,
// throughput_write: 0.0,
// temperature: CoresSensor::default(),
// health: "N/A".to_string(),
// });
// }
// }

// Network info
match get_default_interface() {
Ok(int) => {
Expand Down
31 changes: 1 addition & 30 deletions platforms/unix/hardwareinfo/src/linux/drive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use std::{
sync::LazyLock,
};

use crate::{CoresDiskInfo, CoresSensor};
use crate::{CoresDiskInfo, CoresSensor, SmartctlDiskInfo};

const SYS_STATS: &str = r" *(?P<read_ios>[0-9]*) *(?P<read_merges>[0-9]*) *(?P<read_sectors>[0-9]*) *(?P<read_ticks>[0-9]*) *(?P<write_ios>[0-9]*) *(?P<write_merges>[0-9]*) *(?P<write_sectors>[0-9]*) *(?P<write_ticks>[0-9]*) *(?P<in_flight>[0-9]*) *(?P<io_ticks>[0-9]*) *(?P<time_in_queue>[0-9]*) *(?P<discard_ios>[0-9]*) *(?P<discard_merges>[0-9]*) *(?P<discard_sectors>[0-9]*) *(?P<discard_ticks>[0-9]*) *(?P<flush_ios>[0-9]*) *(?P<flush_ticks>[0-9]*)";

Expand All @@ -27,35 +27,6 @@ pub struct DriveData {
pub capacity: Result<u64>,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
struct SmartDevice {
r#type: String,
}

#[derive(Debug, Serialize, Deserialize, Clone, Copy)]
struct SmartInfo {
temperature: u64,
percentage_used: u64,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
struct SmartAttributeArray {
name: String,
value: u64,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
struct SmartAttribute {
table: Vec<SmartAttributeArray>,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
struct SmartctlDiskInfo {
device: SmartDevice,
nvme_smart_health_information_log: Option<SmartInfo>,
ata_smart_attributes: Option<SmartAttribute>,
}

impl DriveData {
pub fn new(path: &Path) -> Self {
let inner = Drive::from_sysfs(path);
Expand Down
66 changes: 65 additions & 1 deletion platforms/unix/hardwareinfo/src/mac/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{compare_sensor, CoresSensor, Data, Round};
use crate::{compare_sensor, CoresDisk, CoresSensor, Data, Round};

#[cfg(target_os = "macos")]
pub mod metrics;
Expand All @@ -7,6 +7,12 @@ pub mod sources;

#[cfg(target_os = "macos")]
pub fn macos_hardware_info(data: &mut Data) {
use core::str;
use std::{process::Command, time::SystemTime};
use sysinfo::Disks;

use crate::SmartctlDiskInfo;

let mut sampler = metrics::Sampler::new().unwrap();
let soc = sources::SocInfo::new().unwrap();

Expand Down Expand Up @@ -61,6 +67,64 @@ pub fn macos_hardware_info(data: &mut Data) {
});

data.hw_info.gpu.max_load = (data.hw_info.gpu.load[0].value as f64).fmt_num();

// Disks
let gb = 1024_f64.powi(3);
let disks = Disks::new_with_refreshed_list();
for disk in disks.list() {
let free_space = disk.available_space() as f64 / gb;
let total_space = disk.total_space() as f64 / gb;
let name = disk.name().to_str().unwrap().to_string();

if !disk.is_removable() && disk.mount_point().to_str() == Some("/") {
let mut primary_disk = CoresDisk {
name: name.clone(),
total_space: total_space as u64,
free_space: free_space as u64,
throughput_read: 0.0,
throughput_write: 0.0,
temperature: CoresSensor::default(),
health: "N/A".to_string(),
data_read: 0.0,
data_written: 0.0,
read_sectors: 0,
write_sectors: 0,
last_timestamp: SystemTime::now(),
};

let command = format!("smartctl -a disk0 -j");
let output = Command::new("sh").arg("-c").arg(&command).output();

if let Ok(output) = output {
if let Ok(result) = str::from_utf8(&output.stdout) {
let json = serde_json::from_str::<SmartctlDiskInfo>(result);

if let Ok(json) = json {
if json.device.r#type == "nvme" {
primary_disk.health = (100
- json
.nvme_smart_health_information_log
.unwrap()
.percentage_used
as u64)
.to_string();
primary_disk.temperature.value =
json.nvme_smart_health_information_log.unwrap().temperature
as f64;
primary_disk.temperature.max =
json.nvme_smart_health_information_log.unwrap().temperature
as f64;
primary_disk.temperature.min =
json.nvme_smart_health_information_log.unwrap().temperature
as f64;
}
}
}
}

data.hw_info.system.storage.disks.push(primary_disk);
}
}
} else {
let prev_gpu_temp = data.hw_info.gpu.temperature[0].clone();
let prev_gpu_power = data.hw_info.gpu.power[0].clone();
Expand Down

0 comments on commit 79f08d0

Please sign in to comment.