Compare commits

..

3 Commits

Author SHA1 Message Date
c8d080d7b0 Merge pull request 'v1' (#20) from v1 into master
Reviewed-on: https://www.cainet.info/git/cai/CAI-Watchdog/pulls/20
2022-11-04 01:09:47 +03:00
bf96109742 Web-checks now runs in separate thread. Some fixes in scripts.
Web-checks now runs in separate thread. Some fixes in scripts.
2022-11-04 01:07:07 +03:00
87e326e869 Minor changes
Minor changes
2022-10-30 23:38:19 +03:00
8 changed files with 155 additions and 90 deletions

6
Cargo.lock generated
View File

@ -43,7 +43,7 @@ checksum = "ec8a7b6a70fde80372154c65702f00a0f56f3e1c36abbc6c440484be248856db"
[[package]] [[package]]
name = "cai-watchdog" name = "cai-watchdog"
version = "0.5.0" version = "0.6.0"
dependencies = [ dependencies = [
"chrono", "chrono",
"exitcode", "exitcode",
@ -951,9 +951,9 @@ dependencies = [
[[package]] [[package]]
name = "sysinfo" name = "sysinfo"
version = "0.26.6" version = "0.26.7"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c6d0dedf2e65d25b365c588382be9dc3a3ee4b0ed792366cf722d174c359d948" checksum = "c375d5fd899e32847b8566e10598d6e9f1d9b55ec6de3cdf9e7da4bdc51371bc"
dependencies = [ dependencies = [
"cfg-if", "cfg-if",
"core-foundation-sys", "core-foundation-sys",

View File

@ -1,6 +1,6 @@
[package] [package]
name = "cai-watchdog" name = "cai-watchdog"
version = "0.5.0" version = "0.6.0"
authors = ["Alexander I. Chebykin <alex.chebykin@gmail.com>"] authors = ["Alexander I. Chebykin <alex.chebykin@gmail.com>"]
edition = "2018" edition = "2018"
@ -17,4 +17,4 @@ tokio = { version = "1", features = ["full"] }
ini = "1.3.0" ini = "1.3.0"
exitcode = "1.1.2" exitcode = "1.1.2"
hashmap = "0.0.1" hashmap = "0.0.1"
sysinfo = "0.26.6" sysinfo = "0.26.7"

View File

@ -7,6 +7,8 @@ if [[ -z "$APP_PATH" ]] ; then
APP_PATH="/etc/cai-watchdog" APP_PATH="/etc/cai-watchdog"
fi fi
source ${APP_PATH}/inc-icons
RCPT=$1 RCPT=$1
SERVICE=$2 SERVICE=$2
PROCESS_OR_URI=$3 PROCESS_OR_URI=$3

View File

@ -7,6 +7,8 @@ if [[ -z "$APP_PATH" ]] ; then
APP_PATH="/etc/cai-watchdog" APP_PATH="/etc/cai-watchdog"
fi fi
source ${APP_PATH}/inc-icons
SCRIPT_NAME=$0 SCRIPT_NAME=$0
MESSAGE_TEXT=$1 MESSAGE_TEXT=$1
@ -15,7 +17,7 @@ if [ "$#" -ne 1 ]; then
exit 0 exit 0
fi 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 "$@"; set -- $ARGS "$@";
GROUP_ID=$1 GROUP_ID=$1

View File

@ -44,7 +44,7 @@ if [ "$#" -ne 3 ]; then
exit 0 exit 0
fi 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 "$@"; set -- $ARGS "$@";
GROUP_ID=$1 GROUP_ID=$1

View File

@ -6,7 +6,7 @@ User=wwwrun
Group=www Group=www
Type=simple Type=simple
ExecStart=/usr/local/sbin/cai-watchdog ExecStart=/usr/local/sbin/cai-watchdog
#ExecStopPost=/etc/cai-watchdog/on-stop ExecStopPost=/etc/cai-watchdog/on-stop
KillMode=control-group KillMode=control-group
NotifyAccess=all NotifyAccess=all

View File

@ -166,6 +166,9 @@ fn execute(command: String) {
fn print_help(args: Vec<String>) { fn print_help(args: Vec<String>) {
if cfg!(windows) { if cfg!(windows) {
if args.len() > 1 && (args[1].to_string() == "/help".to_string() || args[1].to_string() == "/?".to_string()) { 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!("");
println!("Usage: {} [/? | /help | /v | /ver | config_file]", &args[0]); println!("Usage: {} [/? | /help | /v | /ver | config_file]", &args[0]);
println!(" /? | /help : This help message"); println!(" /? | /help : This help message");
@ -175,15 +178,18 @@ fn print_help(args: Vec<String>) {
std::process::exit(exitcode::OK); std::process::exit(exitcode::OK);
} else if args.len() > 1 } else if args.len() > 1
&& (args[1].to_string() == "/ver".to_string() || args[1].to_string() == "/v".to_string()) { && (args[1].to_string() == "/ver".to_string() || args[1].to_string() == "/v".to_string()) {
const VERSION: &str = env!("CARGO_PKG_VERSION"); const VERSION: &str = env!("CARGO_PKG_VERSION");
println!("");
println!("CAI Watchdog ver {}", VERSION); println!("CAI Watchdog ver {}", VERSION);
std::process::exit(exitcode::OK); std::process::exit(exitcode::OK);
} }
} else { } else {
if args.len() > 1 && (args[1].to_string() == "--help".to_string() || args[1].to_string() == "-h".to_string()) { 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!("");
println!("Usage: {} [-h | --help | -v | --ver | config_file]", &args[0]); println!("Usage: {} [-h | --help | -v | --ver | config_file]", &args[0]);
println!(" -h | --help : This help message"); println!(" -h | --help : This help message");
@ -195,7 +201,6 @@ fn print_help(args: Vec<String>) {
&& (args[1].to_string() == "--ver".to_string() || args[1].to_string() == "-v".to_string()) { && (args[1].to_string() == "--ver".to_string() || args[1].to_string() == "-v".to_string()) {
const VERSION: &str = env!("CARGO_PKG_VERSION"); const VERSION: &str = env!("CARGO_PKG_VERSION");
println!("");
println!("CAI Watchdog ver {}", VERSION); println!("CAI Watchdog ver {}", VERSION);
std::process::exit(exitcode::OK); std::process::exit(exitcode::OK);
@ -208,8 +213,6 @@ fn main() {
print_help(args.clone()); print_help(args.clone());
let mut just_started = true;
let cfg_file = if args.len() > 1 { let cfg_file = if args.len() > 1 {
&args[1] &args[1]
} else if cfg!(windows) { } else if cfg!(windows) {
@ -239,35 +242,36 @@ fn main() {
0 0
}; };
let mut tasks = vec![]; let mut tasks_prc = vec![];
let mut tasks_web = vec![];
for i in 1..rules_count + 1 { for i in 1..rules_count + 1 {
let service = if cfg[&format!("{}{}", "rule", i)].contains_key("service") { let service = if cfg[&format!("rule{}", i)].contains_key("service") {
cfg[&format!("{}{}", "rule", i)]["service"].clone().unwrap() cfg[&format!("rule{}", i)]["service"].clone().unwrap()
} else { } else {
format!("Service {}", i) format!("Service {}", i)
}; };
let uri = if cfg[&format!("{}{}", "rule", i)].contains_key("uri") { let uri = if cfg[&format!("rule{}", i)].contains_key("uri") {
cfg[&format!("{}{}", "rule", i)]["uri"].clone().unwrap() cfg[&format!("rule{}", i)]["uri"].clone().unwrap()
} else { } else {
"".to_string() "".to_string()
}; };
let process = if cfg[&format!("{}{}", "rule", i)].contains_key("process") { let process = if cfg[&format!("rule{}", i)].contains_key("process") {
cfg[&format!("{}{}", "rule", i)]["process"].clone().unwrap() cfg[&format!("rule{}", i)]["process"].clone().unwrap()
} else { } else {
"".to_string() "".to_string()
}; };
let email = if cfg[&format!("{}{}", "rule", i)].contains_key("email") { let email = if cfg[&format!("rule{}", i)].contains_key("email") {
cfg[&format!("{}{}", "rule", i)]["email"].clone().unwrap() cfg[&format!("rule{}", i)]["email"].clone().unwrap()
} else { } else {
"".to_string() "".to_string()
}; };
let command = if cfg[&format!("{}{}", "rule", i)].contains_key("command") { let command = if cfg[&format!("rule{}", i)].contains_key("command") {
cfg[&format!("{}{}", "rule", i)]["command"].clone().unwrap() cfg[&format!("rule{}", i)]["command"].clone().unwrap()
} else { } else {
"".to_string() "".to_string()
}; };
@ -279,7 +283,8 @@ fn main() {
debug_log(format!("email {}", email)); debug_log(format!("email {}", email));
debug_log(format!("command {}", command)); debug_log(format!("command {}", command));
tasks.push( if uri != "".to_string() {
tasks_web.push(
Rule{ Rule{
service: service, service: service,
uri: uri, uri: uri,
@ -289,6 +294,18 @@ fn main() {
last_state: false, 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") { let on_start_command = if cfg["main"].contains_key("on_start_command") {
@ -303,36 +320,31 @@ fn main() {
execute(on_start_command); execute(on_start_command);
} }
let mut just_started_prc = true;
let mut just_started_web = true;
std::thread::spawn(move || {
loop { loop {
for i in 0..tasks.len() { for i in 0..tasks_web.len() {
let rt = tokio::runtime::Runtime::new().unwrap(); let rt = tokio::runtime::Runtime::new().unwrap();
rt.block_on(async { rt.block_on(async {
//if check(tasks[i].uri.clone()).await { //if check(tasks[i].uri.clone()).await {
if check(&tasks[i]).await { if check(&tasks_web[i]).await {
if tasks[i].last_state != true || just_started { if tasks_web[i].last_state != true || just_started_web {
if tasks[i].command.to_string() == "".to_string() { if tasks_web[i].command.to_string() == "".to_string() {
println!("\u{2705} {} state changed to true", tasks[i].uri); println!("\u{2705} {} state changed to true", tasks_web[i].uri);
} else { } else {
debug_log(format!("\u{2705} {} state changed to true", tasks[i].uri)); debug_log(format!("\u{2705} {} state changed to true", tasks_web[i].uri));
let shell_cmd = if tasks[i].uri.to_string() != "".to_string() { let shell_cmd =
tasks[i].command.to_string() tasks_web[i].command.to_string()
.replace("<email>", &tasks[i].email) .replace("<email>", &tasks_web[i].email)
.replace("<subject>", &format!("\"\u{2705} Service {} ({}) is online\"", tasks[i].service, tasks[i].uri)) .replace("<subject>", &format!("\"\u{2705} Service {} ({}) is online\"", tasks_web[i].service, tasks_web[i].uri))
.replace("<message>", &format!("\"\u{2705} Service {} ({}) is online now\"", tasks[i].service, tasks[i].uri)) .replace("<message>", &format!("\"\u{2705} Service {} ({}) is online now\"", tasks_web[i].service, tasks_web[i].uri))
.replace("<service>", &tasks[i].service) .replace("<service>", &tasks_web[i].service)
.replace("<uri>", &tasks[i].uri) .replace("<uri>", &tasks_web[i].uri)
.replace("<state>", "online") .replace("<state>", "online");
} else {
tasks[i].command.to_string()
.replace("<email>", &tasks[i].email)
.replace("<subject>", &format!("\"\u{2705} Process {} ({}) is running\"", tasks[i].service, tasks[i].process))
.replace("<message>", &format!("\"\u{2705} Process {} ({}) is running now\"", tasks[i].service, tasks[i].process))
.replace("<service>", &tasks[i].service)
.replace("<process>", &tasks[i].process)
.replace("<state>", "running")
};
debug_log(format!("execute {}", shell_cmd)); debug_log(format!("execute {}", shell_cmd));
@ -340,56 +352,105 @@ fn main() {
} }
} }
if tasks[i].uri.to_string() != "".to_string() { debug_log(format!("\u{2705} {} check is ok", tasks_web[i].uri));
debug_log(format!("\u{2705} {} check is ok", tasks[i].uri));
} else {
debug_log(format!("\u{2705} {} is running", tasks[i].process));
}
tasks[i].last_state = true; tasks_web[i].last_state = true;
} else { } else {
if tasks[i].last_state != false || just_started { if tasks_web[i].last_state != false || just_started_web {
if tasks[i].command.to_string() == "".to_string() { if tasks_web[i].command.to_string() == "".to_string() {
println!("\u{274c} {} state changed to false", tasks[i].uri); println!("\u{274c} {} state changed to false", tasks_web[i].uri);
} else { } else {
debug_log(format!("\u{274c} {} state changed to false", tasks[i].uri)); debug_log(format!("\u{274c} {} state changed to false", tasks_web[i].uri));
let shell_cmd = if tasks[i].uri.to_string() != "".to_string() { let shell_cmd =
tasks[i].command.to_string() tasks_web[i].command.to_string()
.replace("<email>", &tasks[i].email) .replace("<email>", &tasks_web[i].email)
.replace("<subject>", &format!("\"\u{274c} Service {} ({}) is offline\"", tasks[i].service, tasks[i].uri)) .replace("<subject>", &format!("\"\u{274c} Service {} ({}) is offline\"", tasks_web[i].service, tasks_web[i].uri))
.replace("<message>", &format!("\"\u{274c} Service {} ({}) is offline now\"", tasks[i].service, tasks[i].uri)) .replace("<message>", &format!("\"\u{274c} Service {} ({}) is offline now\"", tasks_web[i].service, tasks_web[i].uri))
.replace("<service>", &tasks[i].service) .replace("<service>", &tasks_web[i].service)
.replace("<uri>", &tasks[i].uri) .replace("<uri>", &tasks_web[i].uri)
.replace("<state>", "offline") .replace("<state>", "offline");
} else {
tasks[i].command.to_string()
.replace("<email>", &tasks[i].email)
.replace("<subject>", &format!("\"\u{274c} Process {} ({}) is not running\"", tasks[i].service, tasks[i].process))
.replace("<message>", &format!("\"\u{274c} Process {} ({}) is not running now\"", tasks[i].service, tasks[i].process))
.replace("<service>", &tasks[i].service)
.replace("<process>", &tasks[i].process)
.replace("<state>", "stopped")
};
debug_log(format!("execute {}", shell_cmd)); debug_log(format!("execute {}", shell_cmd));
execute(shell_cmd); execute(shell_cmd);
} }
} }
if tasks[i].uri.to_string() != "".to_string() { debug_log(format!("\u{274c} {} check failed", tasks_web[i].uri));
debug_log(format!("\u{274c} {} check failed", tasks[i].uri));
} else {
debug_log(format!("\u{274c} {} is not running", tasks[i].process));
}
tasks_web[i].last_state = false;
tasks[i].last_state = false;
} }
}); });
} }
just_started = false; just_started_web = false;
thread::sleep(Duration::from_millis(check_interval));
}
});
loop {
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_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_prc[i].uri));
let shell_cmd =
tasks_prc[i].command.to_string()
.replace("<email>", &tasks_prc[i].email)
.replace("<subject>", &format!("\"\u{2705} Process {} ({}) is running\"", tasks_prc[i].service, tasks_prc[i].process))
.replace("<message>", &format!("\"\u{2705} Process {} ({}) is running now\"", tasks_prc[i].service, tasks_prc[i].process))
.replace("<service>", &tasks_prc[i].service)
.replace("<process>", &tasks_prc[i].process)
.replace("<state>", "running");
debug_log(format!("execute {}", shell_cmd));
execute(shell_cmd);
}
}
debug_log(format!("\u{2705} {} is running", tasks_prc[i].process));
tasks_prc[i].last_state = true;
} else {
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_prc[i].uri));
let shell_cmd =
tasks_prc[i].command.to_string()
.replace("<email>", &tasks_prc[i].email)
.replace("<subject>", &format!("\"\u{274c} Process {} ({}) is not running\"", tasks_prc[i].service, tasks_prc[i].process))
.replace("<message>", &format!("\"\u{274c} Process {} ({}) is not running now\"", tasks_prc[i].service, tasks_prc[i].process))
.replace("<service>", &tasks_prc[i].service)
.replace("<process>", &tasks_prc[i].process)
.replace("<state>", "stopped");
debug_log(format!("execute {}", shell_cmd));
execute(shell_cmd);
}
}
debug_log(format!("\u{274c} {} is not running", tasks_prc[i].process));
tasks_prc[i].last_state = false;
}
});
}
just_started_prc = false;
thread::sleep(Duration::from_millis(check_interval)); thread::sleep(Duration::from_millis(check_interval));
} }