removing all related code to nohup/daemonize

This commit is contained in:
Jorge Bolois 2024-02-03 15:13:28 +01:00
parent 55e0afe415
commit 9f5d6551ed
4 changed files with 50 additions and 101 deletions

1
.gitignore vendored
View File

@ -4,4 +4,3 @@ Cargo.lock
**/*.rs.bk **/*.rs.bk
*.pdb *.pdb
/target /target
output.log

View File

@ -7,5 +7,4 @@ edition = "2021"
tokio = { version = "1.35.1", features = ["full"]} tokio = { version = "1.35.1", features = ["full"]}
kagiyama = "0.3.0" kagiyama = "0.3.0"
anyhow = "1.0.79" anyhow = "1.0.79"
daemonize = "0.5.0"
mtemp = "0.1.21" mtemp = "0.1.21"

View File

@ -1,38 +0,0 @@
#!/bin/bash
start_monitor() {
nohup cargo run --release > output.log 2>&1 &
sleep 1
echo "Monitor started"
}
stop_monitor() {
if [ -f /tmp/mtemp-monitor.pid ]; then
pid=$(cat /tmp/mtemp-monitor.pid)
echo "Stopping monitor with PID $pid"
kill $pid
rm /tmp/mtemp-monitor.pid
echo "Monitor stopped"
else
echo "Monitor is not running (PID file not found)"
fi
}
case "$1" in
start)
start_monitor
;;
stop)
stop_monitor
;;
restart)
stop_monitor
sleep 1
start_monitor
;;
*)
echo "Usage: $0 {start|stop|restart}"
exit 1
;;
esac

View File

@ -1,34 +1,16 @@
use anyhow::{anyhow, Context, Result}; use anyhow::Result;
use daemonize::Daemonize;
use kagiyama::prometheus::metrics::gauge::Gauge; use kagiyama::prometheus::metrics::gauge::Gauge;
use kagiyama::{AlwaysReady, Watcher}; use kagiyama::{AlwaysReady, Watcher};
use mtemp::Mtemp; use mtemp::Mtemp;
use std::env; use std::env;
use std::fs::File;
use std::sync::atomic::AtomicU64; use std::sync::atomic::AtomicU64;
use tokio::signal::ctrl_c;
use tokio::spawn;
use tokio::time::sleep; use tokio::time::sleep;
use tokio::time::Duration; use tokio::time::Duration;
fn main() -> Result<()> {
let name = "mtemp-monitor";
let stdout = File::create(format!("/tmp/{}.log", name)).context("Error creating log file")?;
let daemonize = Daemonize::new()
.pid_file(format!("/tmp/{}.pid", name))
.stdout(stdout)
.chown_pid_file(true)
.working_directory("/");
match daemonize.start() {
Ok(_) => {
println!("daemon started");
tokio_main()
}
Err(err) => Err(anyhow!("{:?}", err)),
}
}
#[tokio::main] #[tokio::main]
async fn tokio_main() -> Result<()> { async fn main() -> Result<()> {
let interval_secs: u64 = env::var("MTEMP_INTERVAL") let interval_secs: u64 = env::var("MTEMP_INTERVAL")
.unwrap_or_else(|_| "1".to_string()) .unwrap_or_else(|_| "1".to_string())
.parse()?; .parse()?;
@ -38,51 +20,58 @@ async fn tokio_main() -> Result<()> {
let mut watcher = Watcher::<AlwaysReady>::default(); let mut watcher = Watcher::<AlwaysReady>::default();
watcher.start_server(addr.parse()?).await; watcher.start_server(addr.parse()?).await;
let interval = Duration::from_secs(interval_secs);
let cpu_gauge = Gauge::<f64, AtomicU64>::default();
{ {
let mut registry = watcher.metrics_registry(); spawn(async move {
registry.register( let interval = Duration::from_secs(interval_secs);
"cpu_temp",
"Shows the temperature of the CPU in degrees C",
cpu_gauge.clone(),
);
}
let gpu_gauge = Gauge::<f64, AtomicU64>::default(); let cpu_gauge = Gauge::<f64, AtomicU64>::default();
{ {
let mut registry = watcher.metrics_registry(); let mut registry = watcher.metrics_registry();
registry.register( registry.register(
"gpu_temp", "cpu_temp",
"Shows the temperature of the GPU in degrees C", "Shows the temperature of the CPU in degrees C",
gpu_gauge.clone(), cpu_gauge.clone(),
); );
} }
let disk_gauge = Gauge::<f64, AtomicU64>::default(); let gpu_gauge = Gauge::<f64, AtomicU64>::default();
{ {
let mut registry = watcher.metrics_registry(); let mut registry = watcher.metrics_registry();
registry.register( registry.register(
"disk_temp", "gpu_temp",
"Shows the temperature of the disk in degrees C", "Shows the temperature of the GPU in degrees C",
disk_gauge.clone(), 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!("interval set every {} seconds", interval_secs);
println!("listening on {}", addr); println!("listening on {}", addr);
ctrl_c().await?;
loop { Ok(())
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);
}
}
} }