Verbosity & silly bug

This commit is contained in:
2024-12-24 16:01:26 +00:00
parent f7e46528b8
commit c932e46ade

View File

@@ -6,8 +6,7 @@ use std::fs::File;
use std::io::{self, BufRead}; use std::io::{self, BufRead};
use std::process::Command; use std::process::Command;
use std::thread::sleep; use std::thread::sleep;
use std::time::Duration; use std::time::{Duration, SystemTime, UNIX_EPOCH};
use std::time::{SystemTime, UNIX_EPOCH};
#[derive(Parser, Debug, Clone)] #[derive(Parser, Debug, Clone)]
#[command(version, about, long_about = None)] #[command(version, about, long_about = None)]
@@ -39,9 +38,13 @@ struct Args {
/// Interfaces to monitor /// Interfaces to monitor
#[arg(short, long)] #[arg(short, long)]
interfaces: Vec<String>, interfaces: Vec<String>,
/// Verbosity
#[arg(short, long)]
verbosity: bool,
} }
#[derive(PartialEq)] #[derive(PartialEq, Debug)]
struct LeaseParams { struct LeaseParams {
iface_name: String, iface_name: String,
ip_addr: String, ip_addr: String,
@@ -157,6 +160,10 @@ impl Monitor {
let lease_ip_addr = lease_params.ip_addr.to_owned(); let lease_ip_addr = lease_params.ip_addr.to_owned();
let trigger_script_path = self.get_trigger_script_path(&iface_name); let trigger_script_path = self.get_trigger_script_path(&iface_name);
if self.verbosity() {
println!("Triggered: {:?}", lease_params);
}
let output = Command::new(trigger_script_path) let output = Command::new(trigger_script_path)
.env("DHCP_IFACE", iface_name) .env("DHCP_IFACE", iface_name)
.env("DHCP_IP_ADDR", lease_ip_addr) .env("DHCP_IP_ADDR", lease_ip_addr)
@@ -190,6 +197,9 @@ impl Monitor {
fn run(&mut self) { fn run(&mut self) {
loop { loop {
for iface_name in self.args.interfaces.clone() { for iface_name in self.args.interfaces.clone() {
if self.verbosity() {
println!("Checking: {}", iface_name);
}
let lease_file_path = self.get_lease_file_path(&iface_name); let lease_file_path = self.get_lease_file_path(&iface_name);
if self.check_file_modified(&lease_file_path) { if self.check_file_modified(&lease_file_path) {
let lease_params = self.get_actual_lease_params(&iface_name); let lease_params = self.get_actual_lease_params(&iface_name);
@@ -199,22 +209,36 @@ impl Monitor {
if *current_lease_params != lease_params { if *current_lease_params != lease_params {
true true
} else { } else {
if self.verbosity() {
println!("Lease params unchanged: {:?}", lease_params);
}
false false
} }
} }
None => false, None => true,
}; };
if trigger { if trigger {
if self.verbosity() {
println!("Triggered: {:?}", lease_params);
}
self.run_trigger_script(&lease_params); self.run_trigger_script(&lease_params);
self.lease_params self.lease_params
.insert(iface_name.to_owned(), lease_params); .insert(iface_name.to_owned(), lease_params);
} }
} else {
if self.verbosity() {
println!("File not modified for {}", iface_name);
}
} }
} }
sleep(Duration::new(self.args.interval.into(), 0)); sleep(Duration::new(self.args.interval.into(), 0));
} }
} }
fn verbosity(&self) -> bool {
self.args.verbosity
}
} }
fn main() { fn main() {
@@ -225,6 +249,7 @@ fn main() {
panic!("No interfaces to monitor"); panic!("No interfaces to monitor");
} }
if !args.foreground {
let daemonize = Daemonize::new().pid_file(&args.pid_file); let daemonize = Daemonize::new().pid_file(&args.pid_file);
match daemonize.start() { match daemonize.start() {
@@ -234,6 +259,7 @@ fn main() {
return; return;
} }
} }
}
monitor.run(); monitor.run();
} }