Processes watching added

Processes watching added
This commit is contained in:
2022-06-29 23:48:34 +03:00
parent 56d738bf8e
commit b638d510a3
8 changed files with 257 additions and 27 deletions

View File

@@ -7,6 +7,7 @@ use std::path::Path;
use std::process::Command;
use std::thread;
use std::time::Duration;
use sysinfo::{System, SystemExt};
/// Rule description structure
pub struct Rule {
@@ -14,6 +15,8 @@ pub struct Rule {
pub service: String,
/// Monitored URI
pub uri: String,
/// Monitored process name
pub process: String,
/// E-mail address for messages
pub email: String,
/// This command will be executed on state change
@@ -22,7 +25,58 @@ pub struct Rule {
pub last_state: bool,
}
/// Checks service availability
/// Check rule
///
/// # Arguments
///
/// * `rule` Rule to be checked
///
/// # Example
///
/// ```
/// if check(&tasks[i]).await {
/// println!("ok");
/// } else {
/// println!("uh-oh... something wrong!");
/// }
/// ```
async fn check(rule: &Rule) -> bool {
return if rule.uri.to_string() != "".to_string() {
check_uri(rule.uri.clone()).await
} else {
check_process(rule.process.clone())
};
}
/// Check is process running
///
/// # Arguments
///
/// * `process_name` Process name to be checked
///
/// # Example
///
/// ```
/// if check_process("httpd".to_string()).await {
/// println!("ok");
/// } else {
/// println!("uh-oh... something wrong!");
/// }
/// ```
fn check_process(process_name: String) -> bool {
let mut sys = System::new_all();
let mut result: bool = false;
sys.refresh_all();
for _process in sys.processes_by_exact_name(&process_name) {
result = true;
}
return result;
}
/// Checks web-service availability
///
/// # Arguments
///
@@ -31,13 +85,13 @@ pub struct Rule {
/// # Example
///
/// ```
/// if if check("https://somesite.local/api/v1/".to_string()).await {
/// if check_uri("https://somesite.local/api/v1/".to_string()).await {
/// println!("ok");
/// } else {
/// println!("uh-oh... something wrong!");
/// }
/// ```
async fn check(uri: String) -> bool {
async fn check_uri(uri: String) -> bool {
if let Err(_e) = reqwest::get(uri).await {
return false;
} else {
@@ -156,6 +210,18 @@ fn main() {
let mut just_started = true;
if check_process("svchost.exe".to_string()) {
println!("svchost is running");
} else {
println!("svchost is not running");
}
if check_process("httpd.exe".to_string()) {
println!("httpd is running");
} else {
println!("httpd is not running");
}
let cfg_file = if args.len() > 1 {
&args[1]
} else if cfg!(windows) {
@@ -200,6 +266,12 @@ fn main() {
"".to_string()
};
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()
} else {
@@ -215,6 +287,7 @@ fn main() {
debug_log(format!("rule {}", i));
debug_log(format!("service {}", service));
debug_log(format!("uri {}", uri));
debug_log(format!("process {}", process));
debug_log(format!("email {}", email));
debug_log(format!("command {}", command));
@@ -222,6 +295,7 @@ fn main() {
Rule{
service: service,
uri: uri,
process: process,
email: email,
command: command,
last_state: false,
@@ -265,17 +339,25 @@ fn main() {
let rt = tokio::runtime::Runtime::new().unwrap();
rt.block_on(async {
if check(tasks[i].uri.clone()).await {
//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);
} else {
debug_log(format!("\u{2705} {} state changed to true", tasks[i].uri));
let shell_cmd = 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 now online\"", tasks[i].service, tasks[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))
} 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))
};
debug_log(format!("execute {}", shell_cmd));
@@ -283,7 +365,11 @@ fn main() {
}
}
debug_log(format!("\u{2705} {} check is ok", tasks[i].uri));
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[i].last_state = true;
} else {
@@ -293,26 +379,37 @@ fn main() {
} else {
debug_log(format!("\u{274c} {} state changed to false", tasks[i].uri));
let shell_cmd = 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 now offline\"", tasks[i].service, tasks[i].uri))
.replace("<icon>", "WARNING");
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 now offline\"", tasks[i].service, tasks[i].uri))
} 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))
};
debug_log(format!("execute {}", shell_cmd));
execute(shell_cmd);
}
}
debug_log(format!("\u{274c} {} check failed", tasks[i].uri));
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[i].last_state = false;
}
just_started = false;
});
}
just_started = false;
thread::sleep(Duration::from_millis(check_interval));
}
}