From bf96109742ba2f4087ed04b4ee3afcbff7c413fa Mon Sep 17 00:00:00 2001 From: "Alexander I. Chebykin" Date: Fri, 4 Nov 2022 01:07:07 +0300 Subject: [PATCH] Web-checks now runs in separate thread. Some fixes in scripts. Web-checks now runs in separate thread. Some fixes in scripts. --- Cargo.lock | 6 +- Cargo.toml | 4 +- .../etc/cai-watchdog/{icons-inc => inc-cons} | 0 scripts/unix/etc/cai-watchdog/send-mail-ru | 2 + scripts/unix/etc/cai-watchdog/send-telegram | 4 +- .../unix/etc/cai-watchdog/send-telegram-ru | 2 +- .../lib/systemd/system/cai-watchdog.service | 2 +- src/main.rs | 205 ++++++++++++------ 8 files changed, 145 insertions(+), 80 deletions(-) rename scripts/unix/etc/cai-watchdog/{icons-inc => inc-cons} (100%) diff --git a/Cargo.lock b/Cargo.lock index 1430caa..ab31ac8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -43,7 +43,7 @@ checksum = "ec8a7b6a70fde80372154c65702f00a0f56f3e1c36abbc6c440484be248856db" [[package]] name = "cai-watchdog" -version = "0.5.0" +version = "0.6.0" dependencies = [ "chrono", "exitcode", @@ -951,9 +951,9 @@ dependencies = [ [[package]] name = "sysinfo" -version = "0.26.6" +version = "0.26.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c6d0dedf2e65d25b365c588382be9dc3a3ee4b0ed792366cf722d174c359d948" +checksum = "c375d5fd899e32847b8566e10598d6e9f1d9b55ec6de3cdf9e7da4bdc51371bc" dependencies = [ "cfg-if", "core-foundation-sys", diff --git a/Cargo.toml b/Cargo.toml index a3ee213..b6f637d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "cai-watchdog" -version = "0.5.0" +version = "0.6.0" authors = ["Alexander I. Chebykin "] edition = "2018" @@ -17,4 +17,4 @@ tokio = { version = "1", features = ["full"] } ini = "1.3.0" exitcode = "1.1.2" hashmap = "0.0.1" -sysinfo = "0.26.6" +sysinfo = "0.26.7" diff --git a/scripts/unix/etc/cai-watchdog/icons-inc b/scripts/unix/etc/cai-watchdog/inc-cons similarity index 100% rename from scripts/unix/etc/cai-watchdog/icons-inc rename to scripts/unix/etc/cai-watchdog/inc-cons diff --git a/scripts/unix/etc/cai-watchdog/send-mail-ru b/scripts/unix/etc/cai-watchdog/send-mail-ru index 4e13489..2583273 100644 --- a/scripts/unix/etc/cai-watchdog/send-mail-ru +++ b/scripts/unix/etc/cai-watchdog/send-mail-ru @@ -7,6 +7,8 @@ if [[ -z "$APP_PATH" ]] ; then APP_PATH="/etc/cai-watchdog" fi +source ${APP_PATH}/inc-icons + RCPT=$1 SERVICE=$2 PROCESS_OR_URI=$3 diff --git a/scripts/unix/etc/cai-watchdog/send-telegram b/scripts/unix/etc/cai-watchdog/send-telegram index 8d97c28..b249ca1 100644 --- a/scripts/unix/etc/cai-watchdog/send-telegram +++ b/scripts/unix/etc/cai-watchdog/send-telegram @@ -7,6 +7,8 @@ if [[ -z "$APP_PATH" ]] ; then APP_PATH="/etc/cai-watchdog" fi +source ${APP_PATH}/inc-icons + SCRIPT_NAME=$0 MESSAGE_TEXT=$1 @@ -15,7 +17,7 @@ if [ "$#" -ne 1 ]; then exit 0 fi -ARGS=$(xargs echo $(perl -anle 's/^[^:]+//g && s/:\s+//g && print' ${APP_PATH}/email.conf) < /dev/null) +ARGS=$(xargs echo $(perl -anle 's/^[^:]+//g && s/:\s+//g && print' ${APP_PATH}/telegram.conf) < /dev/null) set -- $ARGS "$@"; GROUP_ID=$1 diff --git a/scripts/unix/etc/cai-watchdog/send-telegram-ru b/scripts/unix/etc/cai-watchdog/send-telegram-ru index 6ebc37b..d512295 100644 --- a/scripts/unix/etc/cai-watchdog/send-telegram-ru +++ b/scripts/unix/etc/cai-watchdog/send-telegram-ru @@ -44,7 +44,7 @@ if [ "$#" -ne 3 ]; then exit 0 fi -ARGS=$(xargs echo $(perl -anle 's/^[^:]+//g && s/:\s+//g && print' ${APP_PATH}/email.conf) < /dev/null) +ARGS=$(xargs echo $(perl -anle 's/^[^:]+//g && s/:\s+//g && print' ${APP_PATH}/telegram.conf) < /dev/null) set -- $ARGS "$@"; GROUP_ID=$1 diff --git a/scripts/unix/usr/lib/systemd/system/cai-watchdog.service b/scripts/unix/usr/lib/systemd/system/cai-watchdog.service index 2ae1ba8..1698f7e 100644 --- a/scripts/unix/usr/lib/systemd/system/cai-watchdog.service +++ b/scripts/unix/usr/lib/systemd/system/cai-watchdog.service @@ -6,7 +6,7 @@ User=wwwrun Group=www Type=simple ExecStart=/usr/local/sbin/cai-watchdog -#ExecStopPost=/etc/cai-watchdog/on-stop +ExecStopPost=/etc/cai-watchdog/on-stop KillMode=control-group NotifyAccess=all diff --git a/src/main.rs b/src/main.rs index 2a3cb19..374d496 100644 --- a/src/main.rs +++ b/src/main.rs @@ -166,6 +166,9 @@ fn execute(command: String) { 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()) { + const VERSION: &str = env!("CARGO_PKG_VERSION"); + + println!("CAI Watchdog ver {}", VERSION); println!(""); println!("Usage: {} [/? | /help | /v | /ver | config_file]", &args[0]); println!(" /? | /help : This help message"); @@ -175,15 +178,18 @@ fn print_help(args: Vec) { std::process::exit(exitcode::OK); } 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 { if args.len() > 1 && (args[1].to_string() == "--help".to_string() || args[1].to_string() == "-h".to_string()) { + const VERSION: &str = env!("CARGO_PKG_VERSION"); + + println!("CAI Watchdog ver {}", VERSION); println!(""); println!("Usage: {} [-h | --help | -v | --ver | config_file]", &args[0]); println!(" -h | --help : This help message"); @@ -195,7 +201,6 @@ fn print_help(args: Vec) { && (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); @@ -208,8 +213,6 @@ fn main() { print_help(args.clone()); - let mut just_started = true; - let cfg_file = if args.len() > 1 { &args[1] } else if cfg!(windows) { @@ -239,7 +242,8 @@ fn main() { 0 }; - let mut tasks = vec![]; + let mut tasks_prc = vec![]; + let mut tasks_web = vec![]; for i in 1..rules_count + 1 { let service = if cfg[&format!("rule{}", i)].contains_key("service") { @@ -279,16 +283,29 @@ fn main() { debug_log(format!("email {}", email)); debug_log(format!("command {}", command)); - tasks.push( - Rule{ - service: service, - uri: uri, - process: process, - email: email, - command: command, - last_state: false, - } - ); + if uri != "".to_string() { + tasks_web.push( + Rule{ + service: service, + uri: uri, + process: process, + email: email, + command: command, + last_state: false, + } + ); + } else { + tasks_prc.push( + Rule{ + service: service, + uri: uri, + process: process, + email: email, + command: command, + last_state: false, + } + ); + } } let on_start_command = if cfg["main"].contains_key("on_start_command") { @@ -303,36 +320,97 @@ fn main() { execute(on_start_command); } + let mut just_started_prc = true; + let mut just_started_web = true; + + std::thread::spawn(move || { + loop { + for i in 0..tasks_web.len() { + let rt = tokio::runtime::Runtime::new().unwrap(); + + rt.block_on(async { + //if check(tasks[i].uri.clone()).await { + if check(&tasks_web[i]).await { + if tasks_web[i].last_state != true || just_started_web { + if tasks_web[i].command.to_string() == "".to_string() { + println!("\u{2705} {} state changed to true", tasks_web[i].uri); + } else { + debug_log(format!("\u{2705} {} state changed to true", tasks_web[i].uri)); + + let shell_cmd = + tasks_web[i].command.to_string() + .replace("", &tasks_web[i].email) + .replace("", &format!("\"\u{2705} Service {} ({}) is online\"", tasks_web[i].service, tasks_web[i].uri)) + .replace("", &format!("\"\u{2705} Service {} ({}) is online now\"", tasks_web[i].service, tasks_web[i].uri)) + .replace("", &tasks_web[i].service) + .replace("", &tasks_web[i].uri) + .replace("", "online"); + + debug_log(format!("execute {}", shell_cmd)); + + execute(shell_cmd); + } + } + + debug_log(format!("\u{2705} {} check is ok", tasks_web[i].uri)); + + tasks_web[i].last_state = true; + } else { + if tasks_web[i].last_state != false || just_started_web { + if tasks_web[i].command.to_string() == "".to_string() { + println!("\u{274c} {} state changed to false", tasks_web[i].uri); + } else { + debug_log(format!("\u{274c} {} state changed to false", tasks_web[i].uri)); + + let shell_cmd = + tasks_web[i].command.to_string() + .replace("", &tasks_web[i].email) + .replace("", &format!("\"\u{274c} Service {} ({}) is offline\"", tasks_web[i].service, tasks_web[i].uri)) + .replace("", &format!("\"\u{274c} Service {} ({}) is offline now\"", tasks_web[i].service, tasks_web[i].uri)) + .replace("", &tasks_web[i].service) + .replace("", &tasks_web[i].uri) + .replace("", "offline"); + + debug_log(format!("execute {}", shell_cmd)); + + execute(shell_cmd); + } + } + + debug_log(format!("\u{274c} {} check failed", tasks_web[i].uri)); + + tasks_web[i].last_state = false; + } + }); + } + + just_started_web = false; + + thread::sleep(Duration::from_millis(check_interval)); + } + }); + loop { - for i in 0..tasks.len() { + for i in 0..tasks_prc.len() { let rt = tokio::runtime::Runtime::new().unwrap(); rt.block_on(async { //if check(tasks[i].uri.clone()).await { - if check(&tasks[i]).await { - if tasks[i].last_state != true || just_started { - if tasks[i].command.to_string() == "".to_string() { - println!("\u{2705} {} state changed to true", tasks[i].uri); + if check(&tasks_prc[i]).await { + if tasks_prc[i].last_state != true || just_started_prc { + if tasks_prc[i].command.to_string() == "".to_string() { + println!("\u{2705} {} state changed to true", tasks_prc[i].uri); } else { - debug_log(format!("\u{2705} {} state changed to true", tasks[i].uri)); + debug_log(format!("\u{2705} {} state changed to true", tasks_prc[i].uri)); - let shell_cmd = if tasks[i].uri.to_string() != "".to_string() { - tasks[i].command.to_string() - .replace("", &tasks[i].email) - .replace("", &format!("\"\u{2705} Service {} ({}) is online\"", tasks[i].service, tasks[i].uri)) - .replace("", &format!("\"\u{2705} Service {} ({}) is online now\"", tasks[i].service, tasks[i].uri)) - .replace("", &tasks[i].service) - .replace("", &tasks[i].uri) - .replace("", "online") - } else { - tasks[i].command.to_string() - .replace("", &tasks[i].email) - .replace("", &format!("\"\u{2705} Process {} ({}) is running\"", tasks[i].service, tasks[i].process)) - .replace("", &format!("\"\u{2705} Process {} ({}) is running now\"", tasks[i].service, tasks[i].process)) - .replace("", &tasks[i].service) - .replace("", &tasks[i].process) - .replace("", "running") - }; + let shell_cmd = + tasks_prc[i].command.to_string() + .replace("", &tasks_prc[i].email) + .replace("", &format!("\"\u{2705} Process {} ({}) is running\"", tasks_prc[i].service, tasks_prc[i].process)) + .replace("", &format!("\"\u{2705} Process {} ({}) is running now\"", tasks_prc[i].service, tasks_prc[i].process)) + .replace("", &tasks_prc[i].service) + .replace("", &tasks_prc[i].process) + .replace("", "running"); debug_log(format!("execute {}", shell_cmd)); @@ -340,56 +418,39 @@ fn main() { } } - if tasks[i].uri.to_string() != "".to_string() { - debug_log(format!("\u{2705} {} check is ok", tasks[i].uri)); - } else { - debug_log(format!("\u{2705} {} is running", tasks[i].process)); - } + debug_log(format!("\u{2705} {} is running", tasks_prc[i].process)); - tasks[i].last_state = true; + tasks_prc[i].last_state = true; } else { - if tasks[i].last_state != false || just_started { - if tasks[i].command.to_string() == "".to_string() { - println!("\u{274c} {} state changed to false", tasks[i].uri); + if tasks_prc[i].last_state != false || just_started_prc { + if tasks_prc[i].command.to_string() == "".to_string() { + println!("\u{274c} {} state changed to false", tasks_prc[i].uri); } else { - debug_log(format!("\u{274c} {} state changed to false", tasks[i].uri)); + debug_log(format!("\u{274c} {} state changed to false", tasks_prc[i].uri)); - let shell_cmd = if tasks[i].uri.to_string() != "".to_string() { - tasks[i].command.to_string() - .replace("", &tasks[i].email) - .replace("", &format!("\"\u{274c} Service {} ({}) is offline\"", tasks[i].service, tasks[i].uri)) - .replace("", &format!("\"\u{274c} Service {} ({}) is offline now\"", tasks[i].service, tasks[i].uri)) - .replace("", &tasks[i].service) - .replace("", &tasks[i].uri) - .replace("", "offline") - } else { - tasks[i].command.to_string() - .replace("", &tasks[i].email) - .replace("", &format!("\"\u{274c} Process {} ({}) is not running\"", tasks[i].service, tasks[i].process)) - .replace("", &format!("\"\u{274c} Process {} ({}) is not running now\"", tasks[i].service, tasks[i].process)) - .replace("", &tasks[i].service) - .replace("", &tasks[i].process) - .replace("", "stopped") - }; + let shell_cmd = + tasks_prc[i].command.to_string() + .replace("", &tasks_prc[i].email) + .replace("", &format!("\"\u{274c} Process {} ({}) is not running\"", tasks_prc[i].service, tasks_prc[i].process)) + .replace("", &format!("\"\u{274c} Process {} ({}) is not running now\"", tasks_prc[i].service, tasks_prc[i].process)) + .replace("", &tasks_prc[i].service) + .replace("", &tasks_prc[i].process) + .replace("", "stopped"); debug_log(format!("execute {}", shell_cmd)); execute(shell_cmd); } } - if tasks[i].uri.to_string() != "".to_string() { - debug_log(format!("\u{274c} {} check failed", tasks[i].uri)); - } else { - debug_log(format!("\u{274c} {} is not running", tasks[i].process)); - } + debug_log(format!("\u{274c} {} is not running", tasks_prc[i].process)); - tasks[i].last_state = false; + tasks_prc[i].last_state = false; } }); } - just_started = false; + just_started_prc = false; thread::sleep(Duration::from_millis(check_interval)); }