From b0620fcc4720301c7cfa14a183e919e73dd97486 Mon Sep 17 00:00:00 2001 From: Jorge Bolois Date: Sun, 20 Aug 2023 13:55:49 +0200 Subject: [PATCH] adding disk and debug --- src/cli.rs | 12 ++++++++---- src/lm_sensors.rs | 28 +++++++++++++++++++-------- src/main.rs | 47 ++++++++++++++++++++++++++++++--------------- src/sensor_names.rs | 4 ++++ 4 files changed, 64 insertions(+), 27 deletions(-) create mode 100644 src/sensor_names.rs diff --git a/src/cli.rs b/src/cli.rs index c85925d..357d13a 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -6,8 +6,12 @@ use structopt::StructOpt; about = "Simple personal CLI tool for monitoring temperatures." )] pub enum Arguments { - #[structopt(about = "Temperature in Celsius (Only AMD)")] - CPU, - #[structopt(about = "Temperature in Celsius (Only NVIDIA)")] - GPU, + #[structopt(about = "CPU temp in Celsius (Only AMD)")] + Cpu, + #[structopt(about = "GPU temp in Celsius (Only NVIDIA)")] + Gpu, + #[structopt(about = "Disk temp in Celsius")] + Disk, + #[structopt(about = "Debug information, mainly for development purposes")] + Debug, } diff --git a/src/lm_sensors.rs b/src/lm_sensors.rs index 7650667..405db97 100644 --- a/src/lm_sensors.rs +++ b/src/lm_sensors.rs @@ -5,23 +5,36 @@ use serde::Deserialize; use std::collections::BTreeMap; use std::process::Command; +use crate::sensor_names::CPU_NAMES; +use crate::sensor_names::DISK_NAMES; +use crate::sensor_names::TEMP_NAMES; + +#[derive(Debug)] pub struct SensorData { chips: BTreeMap, } impl SensorData { pub fn cpu_chip(&self) -> Option<&Sensor> { - let cpu_names = vec!["k10temp-pci-00c3"]; - for cpu_name in cpu_names { + for cpu_name in CPU_NAMES { if let Some(chip) = self.chips.get(cpu_name) { return Some(chip); } } None } + + pub fn disk_chip(&self) -> Option<&Sensor> { + for disk_name in DISK_NAMES { + if let Some(chip) = self.chips.get(disk_name) { + return Some(chip); + } + } + None + } } -#[derive(Deserialize)] +#[derive(Debug, Deserialize)] pub struct Sensor { #[serde(rename = "Adapter")] _adapter: String, @@ -43,8 +56,7 @@ impl Sensor { match sensor { None => return None, Some(temperatures) => { - let temp_names = vec!["temp1_input"]; - for temp_name in temp_names { + for temp_name in TEMP_NAMES { if let Some(temp) = temperatures.get(temp_name) { return Some(*temp); } @@ -80,10 +92,10 @@ pub fn sensors_data() -> Result { if output.status.success() { let json_output = String::from_utf8_lossy(&output.stdout).to_string(); - let sensor_data: Result, serde_json::Error> = + let sensors_data: Result, serde_json::Error> = serde_json::from_str(&json_output); - match sensor_data { - Ok(data) => Ok(SensorData { chips: data }), + match sensors_data { + Ok(chips) => Ok(SensorData { chips }), Err(err) => Err(anyhow!(err)), } } else { diff --git a/src/main.rs b/src/main.rs index cbfefde..aea7065 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,38 +1,55 @@ mod cli; mod lm_sensors; mod nvidia_smi; +mod sensor_names; use cli::Arguments; -use lm_sensors::sensors_data; +use lm_sensors::{sensors_data, Sensor}; use nvidia_smi::temperature; use structopt::StructOpt; fn main() { let opts = Arguments::from_args(); match opts { - Arguments::CPU => print_cpu_temp(), - Arguments::GPU => print_gpu_temp(), + Arguments::Cpu => print_cpu_temp(), + Arguments::Gpu => print_gpu_temp(), + Arguments::Disk => print_disk_temp(), + Arguments::Debug => debug(), } } fn print_cpu_temp() { let sensors_data = sensors_data(); - match sensors_data { - Ok(sensors) => { - let cpu_chip = sensors.cpu_chip(); - if let Some(chip) = cpu_chip { - if let Some(temp) = chip.temp() { - println!("{}", temp); - } - } + if let Ok(sensors) = sensors_data { + print_chip_temp(sensors.cpu_chip()); + } +} + +fn print_disk_temp() { + let sensors_data = sensors_data(); + if let Ok(sensors) = sensors_data { + print_chip_temp(sensors.disk_chip()); + } +} + +fn print_chip_temp(chip: Option<&Sensor>) { + if let Some(chip) = chip { + if let Some(temp) = chip.temp() { + println!("{}", temp); } - Err(_) => {} } } fn print_gpu_temp() { let gpu_temp_data = temperature(); - match gpu_temp_data { - Ok(temp) => println!("{}", temp), - Err(_) => {} + if let Ok(temp) = gpu_temp_data { + println!("{}", temp) } } + +fn debug() { + let sensors_data = sensors_data(); + println!("lm-sensors: {:?}", sensors_data); + + let nvidia_smi_temp = temperature(); + println!("nvidia-smi: {:?}", nvidia_smi_temp); +} diff --git a/src/sensor_names.rs b/src/sensor_names.rs new file mode 100644 index 0000000..d3812d0 --- /dev/null +++ b/src/sensor_names.rs @@ -0,0 +1,4 @@ +pub const TEMP_NAMES: [&str; 1] = ["temp1_input"]; + +pub const CPU_NAMES: [&str; 1] = ["k10temp-pci-00c3"]; +pub const DISK_NAMES: [&str; 1] = ["nvme-pci-0100"];