Merge pull request 'v1' (#20) from v1 into master

Reviewed-on: https://www.cainet.info/git/cai/CAI-Watchdog/pulls/20
This commit is contained in:
Alexander I. Chebykin 2022-11-04 01:09:47 +03:00
commit c8d080d7b0
8 changed files with 155 additions and 90 deletions

6
Cargo.lock generated
View File

@ -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",

View File

@ -1,6 +1,6 @@
[package]
name = "cai-watchdog"
version = "0.5.0"
version = "0.6.0"
authors = ["Alexander I. Chebykin <alex.chebykin@gmail.com>"]
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"

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -166,6 +166,9 @@ fn execute(command: String) {
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()) {
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<String>) {
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<String>) {
&& (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,35 +242,36 @@ 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") {
cfg[&format!("{}{}", "rule", i)]["service"].clone().unwrap()
let service = if cfg[&format!("rule{}", i)].contains_key("service") {
cfg[&format!("rule{}", i)]["service"].clone().unwrap()
} else {
format!("Service {}", i)
};
let uri = if cfg[&format!("{}{}", "rule", i)].contains_key("uri") {
cfg[&format!("{}{}", "rule", i)]["uri"].clone().unwrap()
let uri = if cfg[&format!("rule{}", i)].contains_key("uri") {
cfg[&format!("rule{}", i)]["uri"].clone().unwrap()
} else {
"".to_string()
};
let process = if cfg[&format!("{}{}", "rule", i)].contains_key("process") {
cfg[&format!("{}{}", "rule", i)]["process"].clone().unwrap()
let process = if cfg[&format!("rule{}", i)].contains_key("process") {
cfg[&format!("rule{}", i)]["process"].clone().unwrap()
} else {
"".to_string()
};
let email = if cfg[&format!("{}{}", "rule", i)].contains_key("email") {
cfg[&format!("{}{}", "rule", i)]["email"].clone().unwrap()
let email = if cfg[&format!("rule{}", i)].contains_key("email") {
cfg[&format!("rule{}", i)]["email"].clone().unwrap()
} else {
"".to_string()
};
let command = if cfg[&format!("{}{}", "rule", i)].contains_key("command") {
cfg[&format!("{}{}", "rule", i)]["command"].clone().unwrap()
let command = if cfg[&format!("rule{}", i)].contains_key("command") {
cfg[&format!("rule{}", i)]["command"].clone().unwrap()
} else {
"".to_string()
};
@ -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("<email>", &tasks_web[i].email)
.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_web[i].service, tasks_web[i].uri))
.replace("<service>", &tasks_web[i].service)
.replace("<uri>", &tasks_web[i].uri)
.replace("<state>", "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("<email>", &tasks_web[i].email)
.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_web[i].service, tasks_web[i].uri))
.replace("<service>", &tasks_web[i].service)
.replace("<uri>", &tasks_web[i].uri)
.replace("<state>", "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("<email>", &tasks[i].email)
.replace("<subject>", &format!("\"\u{2705} Service {} ({}) is online\"", tasks[i].service, tasks[i].uri))
.replace("<message>", &format!("\"\u{2705} Service {} ({}) is online now\"", tasks[i].service, tasks[i].uri))
.replace("<service>", &tasks[i].service)
.replace("<uri>", &tasks[i].uri)
.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")
};
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));
@ -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("<email>", &tasks[i].email)
.replace("<subject>", &format!("\"\u{274c} Service {} ({}) is offline\"", tasks[i].service, tasks[i].uri))
.replace("<message>", &format!("\"\u{274c} Service {} ({}) is offline now\"", tasks[i].service, tasks[i].uri))
.replace("<service>", &tasks[i].service)
.replace("<uri>", &tasks[i].uri)
.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")
};
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);
}
}
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));
}