Web-checks now runs in separate thread. Some fixes in scripts.

Web-checks now runs in separate thread. Some fixes in scripts.
This commit is contained in:
2022-11-04 01:07:07 +03:00
parent 87e326e869
commit bf96109742
8 changed files with 145 additions and 80 deletions

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,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("<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));
}