first commit

This commit is contained in:
Jorge Bolois 2023-08-19 17:15:18 +02:00
parent 61af53a241
commit 2e46eec537
7 changed files with 465 additions and 16 deletions

17
.gitignore vendored
View File

@ -1,16 +1 @@
# ---> Rust /target
# Generated by Cargo
# will have compiled files and executables
debug/
target/
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
Cargo.lock
# These are backup files generated by rustfmt
**/*.rs.bk
# MSVC Windows builds of rustc generate these, which store debugging information
*.pdb

288
Cargo.lock generated Normal file
View File

@ -0,0 +1,288 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "ansi_term"
version = "0.12.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d52a9bb7ec0cf484c551830a7ce27bd20d67eac647e1befb56b0be4ee39a55d2"
dependencies = [
"winapi",
]
[[package]]
name = "anyhow"
version = "1.0.75"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a4668cab20f66d8d020e1fbc0ebe47217433c1b6c8f2040faf858554e394ace6"
[[package]]
name = "atty"
version = "0.2.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8"
dependencies = [
"hermit-abi",
"libc",
"winapi",
]
[[package]]
name = "bitflags"
version = "1.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
[[package]]
name = "clap"
version = "2.34.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a0610544180c38b88101fecf2dd634b174a62eef6946f84dfc6a7127512b381c"
dependencies = [
"ansi_term",
"atty",
"bitflags",
"strsim",
"textwrap",
"unicode-width",
"vec_map",
]
[[package]]
name = "heck"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6d621efb26863f0e9924c6ac577e8275e5e6b77455db64ffa6c65c904e9e132c"
dependencies = [
"unicode-segmentation",
]
[[package]]
name = "hermit-abi"
version = "0.1.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33"
dependencies = [
"libc",
]
[[package]]
name = "itoa"
version = "1.0.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "af150ab688ff2122fcef229be89cb50dd66af9e01a4ff320cc137eecc9bacc38"
[[package]]
name = "lazy_static"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
[[package]]
name = "libc"
version = "0.2.147"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b4668fb0ea861c1df094127ac5f1da3409a82116a4ba74fca2e58ef927159bb3"
[[package]]
name = "proc-macro-error"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c"
dependencies = [
"proc-macro-error-attr",
"proc-macro2",
"quote",
"syn 1.0.109",
"version_check",
]
[[package]]
name = "proc-macro-error-attr"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869"
dependencies = [
"proc-macro2",
"quote",
"version_check",
]
[[package]]
name = "proc-macro2"
version = "1.0.66"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "18fb31db3f9bddb2ea821cde30a9f70117e3f119938b5ee630b7403aa6e2ead9"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.33"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5267fca4496028628a95160fc423a33e8b2e6af8a5302579e322e4b520293cae"
dependencies = [
"proc-macro2",
]
[[package]]
name = "ryu"
version = "1.0.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1ad4cc8da4ef723ed60bced201181d83791ad433213d8c24efffda1eec85d741"
[[package]]
name = "serde"
version = "1.0.183"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "32ac8da02677876d532745a130fc9d8e6edfa81a269b107c5b00829b91d8eb3c"
dependencies = [
"serde_derive",
]
[[package]]
name = "serde_derive"
version = "1.0.183"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "aafe972d60b0b9bee71a91b92fee2d4fb3c9d7e8f6b179aa99f27203d99a4816"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.29",
]
[[package]]
name = "serde_json"
version = "1.0.105"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "693151e1ac27563d6dbcec9dee9fbd5da8539b20fa14ad3752b2e6d363ace360"
dependencies = [
"itoa",
"ryu",
"serde",
]
[[package]]
name = "strsim"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a"
[[package]]
name = "structopt"
version = "0.3.26"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0c6b5c64445ba8094a6ab0c3cd2ad323e07171012d9c98b0b15651daf1787a10"
dependencies = [
"clap",
"lazy_static",
"structopt-derive",
]
[[package]]
name = "structopt-derive"
version = "0.4.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dcb5ae327f9cc13b68763b5749770cb9e048a99bd9dfdfa58d0cf05d5f64afe0"
dependencies = [
"heck",
"proc-macro-error",
"proc-macro2",
"quote",
"syn 1.0.109",
]
[[package]]
name = "syn"
version = "1.0.109"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "syn"
version = "2.0.29"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c324c494eba9d92503e6f1ef2e6df781e78f6a7705a0202d9801b198807d518a"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "temp"
version = "0.1.0"
dependencies = [
"anyhow",
"serde",
"serde_json",
"structopt",
]
[[package]]
name = "textwrap"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060"
dependencies = [
"unicode-width",
]
[[package]]
name = "unicode-ident"
version = "1.0.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "301abaae475aa91687eb82514b328ab47a211a533026cb25fc3e519b86adfc3c"
[[package]]
name = "unicode-segmentation"
version = "1.10.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1dd624098567895118886609431a7c3b8f516e41d30e0643f03d94592a147e36"
[[package]]
name = "unicode-width"
version = "0.1.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c0edd1e5b14653f783770bce4a4dabb4a5108a5370a5f5d8cfe8710c361f6c8b"
[[package]]
name = "vec_map"
version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191"
[[package]]
name = "version_check"
version = "0.9.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"
[[package]]
name = "winapi"
version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
dependencies = [
"winapi-i686-pc-windows-gnu",
"winapi-x86_64-pc-windows-gnu",
]
[[package]]
name = "winapi-i686-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
[[package]]
name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"

12
Cargo.toml Normal file
View File

@ -0,0 +1,12 @@
[package]
name = "temp"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
anyhow = "1.0.75"
serde = {version = "1.0.183", features = ["derive"]}
serde_json = "1.0.103"
structopt = "0.3.26"

13
src/cli.rs Normal file
View File

@ -0,0 +1,13 @@
use structopt::StructOpt;
#[derive(Debug, StructOpt)]
#[structopt(
name = "temp",
about = "Simple personal CLI to gather machine temperatures."
)]
pub enum Arguments {
#[structopt(about = "Temperature in Celsius, ONLY AMD")]
CPU,
#[structopt(about = "Temperature in Celsius, ONLY NVIDIA")]
GPU,
}

92
src/lm_sensors.rs Normal file
View File

@ -0,0 +1,92 @@
use anyhow::anyhow;
use anyhow::Result;
use core::f64;
use serde::Deserialize;
use std::collections::BTreeMap;
use std::process::Command;
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 {
if let Some(chip) = self.chips.get(cpu_name) {
return Some(chip);
}
}
None
}
}
#[derive(Deserialize)]
pub struct Sensor {
#[serde(rename = "Adapter")]
_adapter: String,
#[serde(rename = "Tctl")]
tctl: Option<BTreeMap<String, f64>>,
#[serde(rename = "Tccd1")]
tccd1: Option<BTreeMap<String, f64>>,
temp1: Option<BTreeMap<String, f64>>,
#[serde(rename = "Composite")]
composite: Option<BTreeMap<String, f64>>,
}
impl Sensor {
pub fn temp(&self) -> Option<f64> {
let sensor = self.actual_sector();
match sensor {
None => return None,
Some(temperatures) => {
let temp_names = vec!["temp1_input"];
for temp_name in temp_names {
if let Some(temp) = temperatures.get(temp_name) {
return Some(*temp);
}
}
}
}
None
}
fn actual_sector(&self) -> Option<BTreeMap<String, f64>> {
if self.tctl.is_some() {
return self.tctl.clone();
}
if self.tccd1.is_some() {
return self.tccd1.clone();
}
if self.temp1.is_some() {
return self.temp1.clone();
}
if self.composite.is_some() {
return self.composite.clone();
}
None
}
}
pub fn sensors_data() -> Result<SensorData> {
let output = Command::new("sensors").arg("-j").output()?;
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> =
serde_json::from_str(&json_output);
match sensor_data {
Ok(data) => Ok(SensorData { chips: data }),
Err(err) => Err(anyhow!(err)),
}
} else {
Err(anyhow!(String::from_utf8_lossy(&output.stderr).to_string()))
}
}

38
src/main.rs Normal file
View File

@ -0,0 +1,38 @@
mod cli;
mod lm_sensors;
mod nvidia_smi;
use cli::Arguments;
use lm_sensors::sensors_data;
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(),
}
}
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);
}
}
}
Err(_) => {}
}
}
fn print_gpu_temp() {
let gpu_temp_data = temperature();
match gpu_temp_data {
Ok(temp) => println!("{}", temp),
Err(_) => {}
}
}

21
src/nvidia_smi.rs Normal file
View File

@ -0,0 +1,21 @@
use anyhow::anyhow;
use anyhow::Result;
use std::process::Command;
use std::result::Result::Ok;
pub fn temperature() -> Result<f64> {
let output = Command::new("nvidia-smi")
.arg("--query-gpu=temperature.gpu")
.arg("--format=csv,noheader")
.output()?;
if output.status.success() {
let string_temp = String::from_utf8_lossy(&output.stdout).to_string();
let clean_temp = string_temp.trim();
match &clean_temp.parse::<f64>() {
Ok(temp) => Ok(*temp),
Err(err) => Err(anyhow!(err.to_string())),
}
} else {
Err(anyhow!(String::from_utf8_lossy(&output.stderr).to_string()))
}
}