first commit

This commit is contained in:
Jorge Bolois 2024-02-03 13:28:08 +01:00
parent 79182db9c2
commit be21122056
5 changed files with 102 additions and 0 deletions

5
.gitignore vendored
View File

@ -14,3 +14,8 @@ Cargo.lock
# MSVC Windows builds of rustc generate these, which store debugging information # MSVC Windows builds of rustc generate these, which store debugging information
*.pdb *.pdb
# Added by cargo
/target

10
Cargo.toml Normal file
View File

@ -0,0 +1,10 @@
[package]
name = "mtemp-monitor"
version = "0.1.0"
edition = "2021"
[dependencies]
tokio = { version = "1.35.1", features = ["full"]}
kagiyama = "0.3.0"
mtemp = { git = "https://git.midefos.com/midefos/mtemp.git", branch = "main" }
anyhow = "1.0.79"

77
src/main.rs Normal file
View File

@ -0,0 +1,77 @@
use anyhow::Result;
use kagiyama::prometheus::metrics::gauge::Gauge;
use kagiyama::{AlwaysReady, Watcher};
use mtemp::Mtemp;
use std::env;
use std::sync::atomic::AtomicU64;
use tokio::spawn;
use tokio::time::sleep;
use tokio::time::Duration;
#[tokio::main]
async fn main() -> Result<()> {
let interval_secs: u64 = env::var("MTEMP_INTERVAL")
.unwrap_or_else(|_| "1".to_string())
.parse()?;
let port = env::var("MTEMP_PORT").unwrap_or_else(|_| "9090".to_string());
let addr = format!("127.0.0.1:{}", port);
let mut watcher = Watcher::<AlwaysReady>::default();
watcher.start_server(addr.parse()?).await;
// CPU
{
spawn(async move {
let interval = Duration::from_secs(interval_secs);
let cpu_gauge = Gauge::<f64, AtomicU64>::default();
{
let mut registry = watcher.metrics_registry();
registry.register(
"cpu_temp",
"Shows the temperature of the CPU in degrees C",
cpu_gauge.clone(),
);
}
let gpu_gauge = Gauge::<f64, AtomicU64>::default();
{
let mut registry = watcher.metrics_registry();
registry.register(
"gpu_temp",
"Shows the temperature of the GPU in degrees C",
gpu_gauge.clone(),
);
}
let disk_gauge = Gauge::<f64, AtomicU64>::default();
{
let mut registry = watcher.metrics_registry();
registry.register(
"disk_temp",
"Shows the temperature of the disk in degrees C",
disk_gauge.clone(),
);
}
loop {
sleep(interval).await;
if let Some(cpu) = Mtemp::cpu() {
cpu_gauge.set(cpu);
}
if let Some(gpu) = Mtemp::gpu() {
gpu_gauge.set(gpu);
}
if let Some(disk) = Mtemp::disk() {
disk_gauge.set(disk);
}
}
});
}
println!("interval set every {} seconds", interval_secs);
println!("listening on {}", addr);
tokio::signal::ctrl_c().await.unwrap();
Ok(())
}

5
start.sh Normal file
View File

@ -0,0 +1,5 @@
#!/bin/bash
cargo r -r &
sleep 1
echo "Monitor started"

5
stop.sh Normal file
View File

@ -0,0 +1,5 @@
#!/bin/bash
pkill mtemp-monitor
sleep 1
echo "Monitor stopped"