Notifications on program startup. Minor changes in scripts. #4

Merged
cai merged 1 commits from v1 into master 2022-06-27 18:15:55 +03:00
7 changed files with 118 additions and 10 deletions
Showing only changes of commit ccb471dcc2 - Show all commits

7
Cargo.lock generated
View File

@ -37,6 +37,7 @@ name = "cai-watchdog"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"chrono", "chrono",
"exitcode",
"file-utils", "file-utils",
"ini", "ini",
"reqwest", "reqwest",
@ -99,6 +100,12 @@ dependencies = [
"cfg-if", "cfg-if",
] ]
[[package]]
name = "exitcode"
version = "1.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "de853764b47027c2e862a995c34978ffa63c1501f2e15f987ba11bd4f9bba193"
[[package]] [[package]]
name = "fastrand" name = "fastrand"
version = "1.7.0" version = "1.7.0"

View File

@ -15,3 +15,4 @@ chrono = "0.4.19"
reqwest = "0.11" reqwest = "0.11"
tokio = { version = "1", features = ["full"] } tokio = { version = "1", features = ["full"] }
ini = "1.3.0" ini = "1.3.0"
exitcode = "1.1.2"

View File

@ -0,0 +1,20 @@
[main]
check_interval = 10000
rules_count = 2
[notifications]
email = admin@server.local
command = send-telegram <message>
service_start = true
[rule1]
service = Test1
address = http://127.0.0.1:3000/api/v1/
email = admin@server.local
command = send-telegram <message>
[rule2]
service = Test2
address = http://127.0.0.1:3300/api/v1/
email = admin@server.local
command = send-telegram <message>

View File

@ -1,11 +1,18 @@
[main] [main]
check_interval = 10000 check_interval = 10000
rules_count = 2 rules_count = 2
[notifications]
email = admin@server.local
command = ./send-telegram.ps1 <message>
service_start = true
[rule1] [rule1]
service = Test1 service = Test1
address = http://127.0.0.1:3000/api/v1/ address = http://127.0.0.1:3000/api/v1/
email = admin@server.local email = admin@server.local
command = ./send-telegram.ps1 <message> command = ./send-telegram.ps1 <message>
[rule2] [rule2]
service = Test2 service = Test2
address = http://127.0.0.1:3300/api/v1/ address = http://127.0.0.1:3300/api/v1/

View File

@ -1,6 +1,8 @@
#[macro_use] #[macro_use]
extern crate ini; extern crate ini;
extern crate exitcode;
use std::env;
use std::thread; use std::thread;
use std::time::Duration; use std::time::Duration;
use std::process::Command; use std::process::Command;
@ -93,11 +95,65 @@ fn execute(command: String) {
debug_log(format!("{:?}", result)); debug_log(format!("{:?}", result));
} }
fn main() { /// Print help and program version information
let cfg_file = if cfg!(windows) { ///
"cai-watchdog.cfg" /// # Arguments
///
/// * `args` - Command-line arguments
///
/// # Example
///
/// ```
/// let args: Vec<String> = env::args().collect();
///
/// print_help(args.clone());
/// ```
fn print_help(args: Vec<String>) {
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 { } 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<String> = 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); let cfg = ini!(cfg_file);
@ -116,7 +172,7 @@ fn main() {
debug_log(format!("rule {}", i)); debug_log(format!("rule {}", i));
debug_log(format!("service {}", service)); debug_log(format!("service {}", service));
debug_log(format!("address {}", address)); debug_log(format!("address {}", address));
debug_log(format!("email {}", email)); debug_log(format!("email {}", email));
debug_log(format!("command {}", command)); debug_log(format!("command {}", command));
tasks.push( 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("<email>", &notification_email.to_string())
.replace("<header>", &format!("\"Watchdog service started\""))
.replace("<message>", &format!("\"Watchdog service started\""));
debug_log(format!("execute {}", shell_cmd));
execute(shell_cmd);
}
loop { loop {
for i in 0..tasks.len() { for i in 0..tasks.len() {
let rt = tokio::runtime::Runtime::new().unwrap(); let rt = tokio::runtime::Runtime::new().unwrap();
@ -139,8 +212,8 @@ fn main() {
debug_log(format!("{} state changed to true", tasks[i].address)); debug_log(format!("{} state changed to true", tasks[i].address));
let shell_cmd = tasks[i].command.to_string() let shell_cmd = tasks[i].command.to_string()
.replace("<email>", &tasks[i].email) .replace("<email>", &tasks[i].email)
.replace("<header>", &format!("\"Service {} ({}) online\"", tasks[i].service, tasks[i].address)) .replace("<header>", &format!("\"Service {} ({}) is online\"", tasks[i].service, tasks[i].address))
.replace("<message>", &format!("\"Service {} ({}) is now online\"", tasks[i].service, tasks[i].address)); .replace("<message>", &format!("\"Service {} ({}) is now online\"", tasks[i].service, tasks[i].address));
debug_log(format!("execute {}", shell_cmd)); debug_log(format!("execute {}", shell_cmd));
@ -156,8 +229,8 @@ fn main() {
debug_log(format!("{} state changed to false", tasks[i].address)); debug_log(format!("{} state changed to false", tasks[i].address));
let shell_cmd = tasks[i].command.to_string() let shell_cmd = tasks[i].command.to_string()
.replace("<email>", &tasks[i].email) .replace("<email>", &tasks[i].email)
.replace("<header>", &format!("\"Service {} ({}) offline\"", tasks[i].service, tasks[i].address)) .replace("<header>", &format!("\"Service {} ({}) is offline\"", tasks[i].service, tasks[i].address))
.replace("<message>", &format!("\"Service {} ({}) is now offline\"", tasks[i].service, tasks[i].address)); .replace("<message>", &format!("\"Service {} ({}) is now offline\"", tasks[i].service, tasks[i].address));
debug_log(format!("execute {}", shell_cmd)); debug_log(format!("execute {}", shell_cmd));