refactor(notif): Use discord_client crate

- Moved code into a new Rust crate `node-notifier`
- Create new crate discord client
- node_notifier use now discord client
This commit is contained in:
2025-07-20 13:28:20 +02:00
parent f625268fca
commit 944d9d663d
8 changed files with 1939 additions and 116 deletions

1775
node_notifier/Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

12
node_notifier/Cargo.toml Normal file
View File

@@ -0,0 +1,12 @@
[package]
name = "node-notifier"
version = "0.1.0"
edition = "2024"
[dependencies]
dotenvy = "0.15.7"
reqwest = { version = "0.12.22", features = ["blocking", "json"] }
serde = "1.0.219"
serde_json = "1.0.140"
sysinfo = "0.35.2"
discord_client = { path = "../discord_client" }

106
node_notifier/src/main.rs Normal file
View File

@@ -0,0 +1,106 @@
use discord_client::DiscordNotifier;
use dotenvy::dotenv;
use sysinfo::{Disks, System};
fn get_cpu_usage(sys: &System) -> f32 {
sys.global_cpu_usage()
}
fn get_memory_usage(sys: &System) -> f32 {
let total_memory = sys.total_memory() as f32;
let used_memory = sys.used_memory() as f32;
used_memory / total_memory * 100.0
}
fn get_swap_usage(sys: &System) -> f32 {
let total_swap = sys.total_swap() as f32;
let used_swap = sys.used_swap() as f32;
used_swap / total_swap * 100.0
}
fn get_disk_usage(disks: &Disks) -> f32 {
let mut total_used_space = 0.0;
let mut total_space = 0.0;
for disk in disks {
if let Some(mount_point) = disk.mount_point().to_str() {
if mount_point == "/" || mount_point == "/home" {
total_used_space += disk.total_space() as f32 - disk.available_space() as f32;
total_space += disk.total_space() as f32;
}
}
}
if total_space == 0.0 {
return 0.0; // Avoid division by zero
}
(total_used_space / total_space) * 100.0
}
fn main() {
dotenv().ok();
let notifier = DiscordNotifier::new(
std::env::var("DISCORD_WEBHOOK").expect("DISCORD_WEBHOOK environment variable not set"),
"System Monitor".to_string(),
"https://cdn.shopify.com/s/files/1/0262/1423/6212/files/Lord_of_the_Rings_eye_of_Sauron_-_Ghtic.com_-_Blog.png?v=1579680018".to_string(),
);
let mut sys = System::new_all();
let mut disks: Disks = Disks::new_with_refreshed_list();
sys.refresh_all();
disks.refresh(true);
let cpu_usage = get_cpu_usage(&sys);
if cpu_usage > 80.0 {
notifier
.send_notification(&format!("High CPU usage detected: {cpu_usage:.2}%"))
.expect("Failed to send notification for high CPU usage");
}
let memory_usage = get_memory_usage(&sys);
if memory_usage > 80.0 {
notifier
.send_notification(&format!("High memory usage detected: {memory_usage:.2}%"))
.expect("Failed to send notification for high memory usage");
}
let swap_usage = get_swap_usage(&sys);
if swap_usage > 80.0 {
notifier
.send_notification(&format!("High swap usage detected: {swap_usage:.2}%"))
.expect("Failed to send notification for high swap usage");
}
let disk_usage = get_disk_usage(&disks);
if disk_usage > 80.0 {
notifier
.send_notification(&format!("High disk usage detected: {disk_usage:.2}%"))
.expect("Failed to send notification for high disk usage");
}
println!("System Information:");
println!("CPU usage: {cpu_usage:.2}%");
println!("Memory usage: {memory_usage:.2}%");
println!("Swap usage: {swap_usage:.2}%");
println!("Disk usage: {disk_usage:.2}%");
// Send a final notification with all system information
let final_message = format!(
concat!(
"System Information:\n",
"CPU usage: {:.2}%\n",
"Memory usage: {:.2}%\n",
"Swap usage: {:.2}%\n",
"Disk usage: {:.2}%"
),
cpu_usage, memory_usage, swap_usage, disk_usage
);
notifier
.send_notification(&final_message)
.expect("Failed to send final notification");
}