From ccb471dcc2fc80f62db7dfee8080672c8ace3588 Mon Sep 17 00:00:00 2001 From: "Alexander I. Chebykin" Date: Mon, 27 Jun 2022 18:14:13 +0300 Subject: [PATCH] Notifications on program startup. Minor changes in scripts. Notifications on program startup. Minor changes in scripts. --- Cargo.lock | 7 ++ Cargo.toml | 3 +- scripts/unix/cai-watchdog.conf.template | 20 ++++ .../cai-watchdog.ini.template} | 7 ++ .../send-mail.ps1.template | 0 .../send-telegram.ps1.template | 0 src/main.rs | 91 +++++++++++++++++-- 7 files changed, 118 insertions(+), 10 deletions(-) create mode 100644 scripts/unix/cai-watchdog.conf.template rename scripts/{cai-watchdog.cfg.template => windows/cai-watchdog.ini.template} (74%) rename scripts/{windowss => windows}/send-mail.ps1.template (100%) rename scripts/{windowss => windows}/send-telegram.ps1.template (100%) diff --git a/Cargo.lock b/Cargo.lock index bc85891..554ec4f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -37,6 +37,7 @@ name = "cai-watchdog" version = "0.1.0" dependencies = [ "chrono", + "exitcode", "file-utils", "ini", "reqwest", @@ -99,6 +100,12 @@ dependencies = [ "cfg-if", ] +[[package]] +name = "exitcode" +version = "1.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "de853764b47027c2e862a995c34978ffa63c1501f2e15f987ba11bd4f9bba193" + [[package]] name = "fastrand" version = "1.7.0" diff --git a/Cargo.toml b/Cargo.toml index 94a01d5..6a98ca5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,4 +14,5 @@ chrono = "0.4.19" # Async reqwest = "0.11" tokio = { version = "1", features = ["full"] } -ini = "1.3.0" \ No newline at end of file +ini = "1.3.0" +exitcode = "1.1.2" \ No newline at end of file diff --git a/scripts/unix/cai-watchdog.conf.template b/scripts/unix/cai-watchdog.conf.template new file mode 100644 index 0000000..358bbfe --- /dev/null +++ b/scripts/unix/cai-watchdog.conf.template @@ -0,0 +1,20 @@ +[main] +check_interval = 10000 +rules_count = 2 + +[notifications] +email = admin@server.local +command = send-telegram +service_start = true + +[rule1] +service = Test1 +address = http://127.0.0.1:3000/api/v1/ +email = admin@server.local +command = send-telegram + +[rule2] +service = Test2 +address = http://127.0.0.1:3300/api/v1/ +email = admin@server.local +command = send-telegram diff --git a/scripts/cai-watchdog.cfg.template b/scripts/windows/cai-watchdog.ini.template similarity index 74% rename from scripts/cai-watchdog.cfg.template rename to scripts/windows/cai-watchdog.ini.template index 87f20dc..f4bc925 100644 --- a/scripts/cai-watchdog.cfg.template +++ b/scripts/windows/cai-watchdog.ini.template @@ -1,11 +1,18 @@ [main] check_interval = 10000 rules_count = 2 + +[notifications] +email = admin@server.local +command = ./send-telegram.ps1 +service_start = true + [rule1] service = Test1 address = http://127.0.0.1:3000/api/v1/ email = admin@server.local command = ./send-telegram.ps1 + [rule2] service = Test2 address = http://127.0.0.1:3300/api/v1/ diff --git a/scripts/windowss/send-mail.ps1.template b/scripts/windows/send-mail.ps1.template similarity index 100% rename from scripts/windowss/send-mail.ps1.template rename to scripts/windows/send-mail.ps1.template diff --git a/scripts/windowss/send-telegram.ps1.template b/scripts/windows/send-telegram.ps1.template similarity index 100% rename from scripts/windowss/send-telegram.ps1.template rename to scripts/windows/send-telegram.ps1.template diff --git a/src/main.rs b/src/main.rs index 845b163..ad4501d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,8 @@ #[macro_use] extern crate ini; +extern crate exitcode; +use std::env; use std::thread; use std::time::Duration; use std::process::Command; @@ -93,11 +95,65 @@ fn execute(command: String) { debug_log(format!("{:?}", result)); } -fn main() { - let cfg_file = if cfg!(windows) { - "cai-watchdog.cfg" +/// Print help and program version information +/// +/// # Arguments +/// +/// * `args` - Command-line arguments +/// +/// # Example +/// +/// ``` +/// let args: Vec = env::args().collect(); +/// +/// print_help(args.clone()); +/// ``` +fn print_help(args: Vec) { + if cfg!(windows) { + if args.len() > 1 && (args[1].to_string() == "/help".to_string() || args[1].to_string() == "/?".to_string()) { + println!(""); + println!("Usage: {} [/? | /help | /v | /ver | config_file]", &args[0]); + println!(" /? | /help : This help message"); + println!(" /v | /ver : Version info"); + println!(" config_file : Configuration file"); + } else if args.len() > 1 + && (args[1].to_string() == "/ver".to_string() || args[1].to_string() == "/v".to_string()) { + const VERSION: &str = env!("CARGO_PKG_VERSION"); + + println!(""); + println!("CAI Watchdog ver {}", VERSION); + } + + std::process::exit(exitcode::OK); } else { - "/etc/cai-watchdog.cfg" + if args.len() > 1 && (args[1].to_string() == "--help".to_string() || args[1].to_string() == "-h".to_string()) { + println!(""); + println!("Usage: {} [-h | --help | -v | --ver | config_file]", &args[0]); + println!(" -h | --help : This help message"); + println!(" -v | --ver : Version info"); + println!(" config_file : Configuration file"); + } else if args.len() > 1 + && (args[1].to_string() == "--ver".to_string() || args[1].to_string() == "-v".to_string()) { + const VERSION: &str = env!("CARGO_PKG_VERSION"); + + println!(""); + println!("CAI Watchdog ver {}", VERSION); + } + std::process::exit(exitcode::OK); + } +} + +fn main() { + let args: Vec = env::args().collect(); + + print_help(args.clone()); + + let cfg_file = if args.len() > 1 { + &args[1] + } else if cfg!(windows) { + "cai-watchdog.ini" + } else { + "/etc/cai-watchdog.conf" }; let cfg = ini!(cfg_file); @@ -116,7 +172,7 @@ fn main() { debug_log(format!("rule {}", i)); debug_log(format!("service {}", service)); debug_log(format!("address {}", address)); - debug_log(format!("email {}", email)); + debug_log(format!("email {}", email)); debug_log(format!("command {}", command)); tasks.push( @@ -130,6 +186,23 @@ fn main() { ); } + let start_notification = cfg["notifications"]["service_start"].clone().unwrap().to_string() == "true".to_string(); + let notification_email = cfg["notifications"]["email"].clone().unwrap(); + let notification_command = cfg["notifications"]["command"].clone().unwrap(); + + if start_notification { + debug_log(format!("Service started")); + + let shell_cmd = notification_command.to_string() + .replace("", ¬ification_email.to_string()) + .replace("
", &format!("\"Watchdog service started\"")) + .replace("", &format!("\"Watchdog service started\"")); + + debug_log(format!("execute {}", shell_cmd)); + + execute(shell_cmd); + } + loop { for i in 0..tasks.len() { let rt = tokio::runtime::Runtime::new().unwrap(); @@ -139,8 +212,8 @@ fn main() { debug_log(format!("{} state changed to true", tasks[i].address)); let shell_cmd = tasks[i].command.to_string() - .replace("", &tasks[i].email) - .replace("
", &format!("\"Service {} ({}) online\"", tasks[i].service, tasks[i].address)) + .replace("", &tasks[i].email) + .replace("
", &format!("\"Service {} ({}) is online\"", tasks[i].service, tasks[i].address)) .replace("", &format!("\"Service {} ({}) is now online\"", tasks[i].service, tasks[i].address)); debug_log(format!("execute {}", shell_cmd)); @@ -156,8 +229,8 @@ fn main() { debug_log(format!("{} state changed to false", tasks[i].address)); let shell_cmd = tasks[i].command.to_string() - .replace("", &tasks[i].email) - .replace("
", &format!("\"Service {} ({}) offline\"", tasks[i].service, tasks[i].address)) + .replace("", &tasks[i].email) + .replace("
", &format!("\"Service {} ({}) is offline\"", tasks[i].service, tasks[i].address)) .replace("", &format!("\"Service {} ({}) is now offline\"", tasks[i].service, tasks[i].address)); debug_log(format!("execute {}", shell_cmd)); -- 2.51.0