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."
)]
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,
}

View File

@ -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<String, Sensor>,
}
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<SensorData> {
if output.status.success() {
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);
match sensor_data {
Ok(data) => Ok(SensorData { chips: data }),
match sensors_data {
Ok(chips) => Ok(SensorData { chips }),
Err(err) => Err(anyhow!(err)),
}
} else {

View File

@ -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);
}

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