using daemonize
This commit is contained in:
parent
b382c5121d
commit
094ba3631d
1
.gitignore
vendored
1
.gitignore
vendored
@ -4,4 +4,3 @@ Cargo.lock
|
||||
**/*.rs.bk
|
||||
*.pdb
|
||||
/target
|
||||
nohup.out
|
||||
|
@ -6,5 +6,6 @@ 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"
|
||||
daemonize = "0.5.0"
|
||||
mtemp = "0.1.21"
|
||||
|
111
src/main.rs
111
src/main.rs
@ -1,15 +1,34 @@
|
||||
use anyhow::Result;
|
||||
use anyhow::{anyhow, Context, Result};
|
||||
use daemonize::Daemonize;
|
||||
use kagiyama::prometheus::metrics::gauge::Gauge;
|
||||
use kagiyama::{AlwaysReady, Watcher};
|
||||
use mtemp::Mtemp;
|
||||
use std::env;
|
||||
use std::fs::File;
|
||||
use std::sync::atomic::AtomicU64;
|
||||
use tokio::spawn;
|
||||
use tokio::time::sleep;
|
||||
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]
|
||||
async fn main() -> Result<()> {
|
||||
async fn tokio_main() -> Result<()> {
|
||||
let interval_secs: u64 = env::var("MTEMP_INTERVAL")
|
||||
.unwrap_or_else(|_| "1".to_string())
|
||||
.parse()?;
|
||||
@ -19,59 +38,51 @@ async fn main() -> Result<()> {
|
||||
let mut watcher = Watcher::<AlwaysReady>::default();
|
||||
watcher.start_server(addr.parse()?).await;
|
||||
|
||||
// CPU
|
||||
let interval = Duration::from_secs(interval_secs);
|
||||
|
||||
let cpu_gauge = Gauge::<f64, AtomicU64>::default();
|
||||
{
|
||||
spawn(async move {
|
||||
let interval = Duration::from_secs(interval_secs);
|
||||
let mut registry = watcher.metrics_registry();
|
||||
registry.register(
|
||||
"cpu_temp",
|
||||
"Shows the temperature of the CPU in degrees C",
|
||||
cpu_gauge.clone(),
|
||||
);
|
||||
}
|
||||
|
||||
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 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);
|
||||
}
|
||||
}
|
||||
});
|
||||
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(),
|
||||
);
|
||||
}
|
||||
|
||||
println!("interval set every {} seconds", interval_secs);
|
||||
println!("listening on {}", addr);
|
||||
tokio::signal::ctrl_c().await.unwrap();
|
||||
|
||||
Ok(())
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user