adding disk and debug

This commit is contained in:
Jorge Bolois 2023-08-20 13:55:49 +02:00
parent b8386dfbbd
commit b0620fcc47
4 changed files with 64 additions and 27 deletions

View File

@ -6,8 +6,12 @@ use structopt::StructOpt;
about = "Simple personal CLI tool for monitoring temperatures." about = "Simple personal CLI tool for monitoring temperatures."
)] )]
pub enum Arguments { pub enum Arguments {
#[structopt(about = "Temperature in Celsius (Only AMD)")] #[structopt(about = "CPU temp in Celsius (Only AMD)")]
CPU, Cpu,
#[structopt(about = "Temperature in Celsius (Only NVIDIA)")] #[structopt(about = "GPU temp in Celsius (Only NVIDIA)")]
GPU, Gpu,
#[structopt(about = "Disk temp in Celsius")]
Disk,
#[structopt(about = "Debug information, mainly for development purposes")]
Debug,
} }

View File

@ -5,23 +5,36 @@ use serde::Deserialize;
use std::collections::BTreeMap; use std::collections::BTreeMap;
use std::process::Command; 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 { pub struct SensorData {
chips: BTreeMap<String, Sensor>, chips: BTreeMap<String, Sensor>,
} }
impl SensorData { impl SensorData {
pub fn cpu_chip(&self) -> Option<&Sensor> { 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) { if let Some(chip) = self.chips.get(cpu_name) {
return Some(chip); return Some(chip);
} }
} }
None 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 { pub struct Sensor {
#[serde(rename = "Adapter")] #[serde(rename = "Adapter")]
_adapter: String, _adapter: String,
@ -43,8 +56,7 @@ impl Sensor {
match sensor { match sensor {
None => return None, None => return None,
Some(temperatures) => { 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) { if let Some(temp) = temperatures.get(temp_name) {
return Some(*temp); return Some(*temp);
} }
@ -80,10 +92,10 @@ pub fn sensors_data() -> Result<SensorData> {
if output.status.success() { if output.status.success() {
let json_output = String::from_utf8_lossy(&output.stdout).to_string(); let json_output = String::from_utf8_lossy(&output.stdout).to_string();
let sensor_data: Result<BTreeMap<String, Sensor>, serde_json::Error> = let sensors_data: Result<BTreeMap<String, Sensor>, serde_json::Error> =
serde_json::from_str(&json_output); serde_json::from_str(&json_output);
match sensor_data { match sensors_data {
Ok(data) => Ok(SensorData { chips: data }), Ok(chips) => Ok(SensorData { chips }),
Err(err) => Err(anyhow!(err)), Err(err) => Err(anyhow!(err)),
} }
} else { } else {

View File

@ -1,38 +1,55 @@
mod cli; mod cli;
mod lm_sensors; mod lm_sensors;
mod nvidia_smi; mod nvidia_smi;
mod sensor_names;
use cli::Arguments; use cli::Arguments;
use lm_sensors::sensors_data; use lm_sensors::{sensors_data, Sensor};
use nvidia_smi::temperature; use nvidia_smi::temperature;
use structopt::StructOpt; use structopt::StructOpt;
fn main() { fn main() {
let opts = Arguments::from_args(); let opts = Arguments::from_args();
match opts { match opts {
Arguments::CPU => print_cpu_temp(), Arguments::Cpu => print_cpu_temp(),
Arguments::GPU => print_gpu_temp(), Arguments::Gpu => print_gpu_temp(),
Arguments::Disk => print_disk_temp(),
Arguments::Debug => debug(),
} }
} }
fn print_cpu_temp() { fn print_cpu_temp() {
let sensors_data = sensors_data(); let sensors_data = sensors_data();
match sensors_data { if let Ok(sensors) = sensors_data {
Ok(sensors) => { print_chip_temp(sensors.cpu_chip());
let cpu_chip = sensors.cpu_chip(); }
if let Some(chip) = 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() { if let Some(temp) = chip.temp() {
println!("{}", temp); println!("{}", temp);
} }
} }
} }
Err(_) => {}
}
}
fn print_gpu_temp() { fn print_gpu_temp() {
let gpu_temp_data = temperature(); let gpu_temp_data = temperature();
match gpu_temp_data { if let Ok(temp) = gpu_temp_data {
Ok(temp) => println!("{}", temp), println!("{}", temp)
Err(_) => {}
} }
} }
fn debug() {
let sensors_data = sensors_data();
println!("lm-sensors: {:?}", sensors_data);
let nvidia_smi_temp = temperature();
println!("nvidia-smi: {:?}", nvidia_smi_temp);
}

4
src/sensor_names.rs Normal file
View File

@ -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"];