Compare commits

..

No commits in common. "c8d080d7b027da15950cd613f59acc022897ee3f" and "dbb860a00bd441d88831edf186fda360ef4de9f5" have entirely different histories.

8 changed files with 90 additions and 155 deletions

6
Cargo.lock generated
View File

@ -43,7 +43,7 @@ checksum = "ec8a7b6a70fde80372154c65702f00a0f56f3e1c36abbc6c440484be248856db"
[[package]] [[package]]
name = "cai-watchdog" name = "cai-watchdog"
version = "0.6.0" version = "0.5.0"
dependencies = [ dependencies = [
"chrono", "chrono",
"exitcode", "exitcode",
@ -951,9 +951,9 @@ dependencies = [
[[package]] [[package]]
name = "sysinfo" name = "sysinfo"
version = "0.26.7" version = "0.26.6"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c375d5fd899e32847b8566e10598d6e9f1d9b55ec6de3cdf9e7da4bdc51371bc" checksum = "c6d0dedf2e65d25b365c588382be9dc3a3ee4b0ed792366cf722d174c359d948"
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.6.0" version = "0.5.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.7" sysinfo = "0.26.6"

View File

@ -7,8 +7,6 @@ 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,8 +7,6 @@ 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
@ -17,7 +15,7 @@ if [ "$#" -ne 1 ]; then
exit 0 exit 0
fi fi
ARGS=$(xargs echo $(perl -anle 's/^[^:]+//g && s/:\s+//g && print' ${APP_PATH}/telegram.conf) < /dev/null) ARGS=$(xargs echo $(perl -anle 's/^[^:]+//g && s/:\s+//g && print' ${APP_PATH}/email.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}/telegram.conf) < /dev/null) ARGS=$(xargs echo $(perl -anle 's/^[^:]+//g && s/:\s+//g && print' ${APP_PATH}/email.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,9 +166,6 @@ 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");
@ -178,18 +175,15 @@ 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");
@ -201,6 +195,7 @@ 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);
@ -213,6 +208,8 @@ 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) {
@ -242,36 +239,35 @@ fn main() {
0 0
}; };
let mut tasks_prc = vec![]; let mut tasks = 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()
}; };
@ -283,29 +279,16 @@ fn main() {
debug_log(format!("email {}", email)); debug_log(format!("email {}", email));
debug_log(format!("command {}", command)); debug_log(format!("command {}", command));
if uri != "".to_string() { tasks.push(
tasks_web.push( Rule{
Rule{ service: service,
service: service, uri: uri,
uri: uri, process: process,
process: process, email: email,
email: email, command: command,
command: command, 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") {
@ -320,97 +303,36 @@ 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 {
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 { loop {
for i in 0..tasks_prc.len() { for i in 0..tasks.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_prc[i]).await { if check(&tasks[i]).await {
if tasks_prc[i].last_state != true || just_started_prc { if tasks[i].last_state != true || just_started {
if tasks_prc[i].command.to_string() == "".to_string() { if tasks[i].command.to_string() == "".to_string() {
println!("\u{2705} {} state changed to true", tasks_prc[i].uri); println!("\u{2705} {} state changed to true", tasks[i].uri);
} else { } else {
debug_log(format!("\u{2705} {} state changed to true", tasks_prc[i].uri)); debug_log(format!("\u{2705} {} state changed to true", tasks[i].uri));
let shell_cmd = let shell_cmd = if tasks[i].uri.to_string() != "".to_string() {
tasks_prc[i].command.to_string() tasks[i].command.to_string()
.replace("<email>", &tasks_prc[i].email) .replace("<email>", &tasks[i].email)
.replace("<subject>", &format!("\"\u{2705} Process {} ({}) is running\"", tasks_prc[i].service, tasks_prc[i].process)) .replace("<subject>", &format!("\"\u{2705} Service {} ({}) is online\"", tasks[i].service, tasks[i].uri))
.replace("<message>", &format!("\"\u{2705} Process {} ({}) is running now\"", tasks_prc[i].service, tasks_prc[i].process)) .replace("<message>", &format!("\"\u{2705} Service {} ({}) is online now\"", tasks[i].service, tasks[i].uri))
.replace("<service>", &tasks_prc[i].service) .replace("<service>", &tasks[i].service)
.replace("<process>", &tasks_prc[i].process) .replace("<uri>", &tasks[i].uri)
.replace("<state>", "running"); .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));
@ -418,39 +340,56 @@ fn main() {
} }
} }
debug_log(format!("\u{2705} {} is running", tasks_prc[i].process)); 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));
}
tasks_prc[i].last_state = true; tasks[i].last_state = true;
} else { } else {
if tasks_prc[i].last_state != false || just_started_prc { if tasks[i].last_state != false || just_started {
if tasks_prc[i].command.to_string() == "".to_string() { if tasks[i].command.to_string() == "".to_string() {
println!("\u{274c} {} state changed to false", tasks_prc[i].uri); println!("\u{274c} {} state changed to false", tasks[i].uri);
} else { } else {
debug_log(format!("\u{274c} {} state changed to false", tasks_prc[i].uri)); debug_log(format!("\u{274c} {} state changed to false", tasks[i].uri));
let shell_cmd = let shell_cmd = if tasks[i].uri.to_string() != "".to_string() {
tasks_prc[i].command.to_string() tasks[i].command.to_string()
.replace("<email>", &tasks_prc[i].email) .replace("<email>", &tasks[i].email)
.replace("<subject>", &format!("\"\u{274c} Process {} ({}) is not running\"", tasks_prc[i].service, tasks_prc[i].process)) .replace("<subject>", &format!("\"\u{274c} Service {} ({}) is offline\"", tasks[i].service, tasks[i].uri))
.replace("<message>", &format!("\"\u{274c} Process {} ({}) is not running now\"", tasks_prc[i].service, tasks_prc[i].process)) .replace("<message>", &format!("\"\u{274c} Service {} ({}) is offline now\"", tasks[i].service, tasks[i].uri))
.replace("<service>", &tasks_prc[i].service) .replace("<service>", &tasks[i].service)
.replace("<process>", &tasks_prc[i].process) .replace("<uri>", &tasks[i].uri)
.replace("<state>", "stopped"); .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);
} }
} }
debug_log(format!("\u{274c} {} is not running", tasks_prc[i].process)); 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));
}
tasks_prc[i].last_state = false; tasks[i].last_state = false;
} }
}); });
} }
just_started_prc = false; just_started = false;
thread::sleep(Duration::from_millis(check_interval)); thread::sleep(Duration::from_millis(check_interval));
} }