Notifications on program startup. Minor changes in scripts.
Notifications on program startup. Minor changes in scripts.
This commit is contained in:
91
src/main.rs
91
src/main.rs
@@ -1,6 +1,8 @@
|
||||
#[macro_use]
|
||||
extern crate ini;
|
||||
extern crate exitcode;
|
||||
|
||||
use std::env;
|
||||
use std::thread;
|
||||
use std::time::Duration;
|
||||
use std::process::Command;
|
||||
@@ -93,11 +95,65 @@ fn execute(command: String) {
|
||||
debug_log(format!("{:?}", result));
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let cfg_file = if cfg!(windows) {
|
||||
"cai-watchdog.cfg"
|
||||
/// Print help and program version information
|
||||
///
|
||||
/// # Arguments
|
||||
///
|
||||
/// * `args` - Command-line arguments
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// let args: Vec<String> = env::args().collect();
|
||||
///
|
||||
/// print_help(args.clone());
|
||||
/// ```
|
||||
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()) {
|
||||
println!("");
|
||||
println!("Usage: {} [/? | /help | /v | /ver | config_file]", &args[0]);
|
||||
println!(" /? | /help : This help message");
|
||||
println!(" /v | /ver : Version info");
|
||||
println!(" config_file : Configuration file");
|
||||
} 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 {
|
||||
"/etc/cai-watchdog.cfg"
|
||||
if args.len() > 1 && (args[1].to_string() == "--help".to_string() || args[1].to_string() == "-h".to_string()) {
|
||||
println!("");
|
||||
println!("Usage: {} [-h | --help | -v | --ver | config_file]", &args[0]);
|
||||
println!(" -h | --help : This help message");
|
||||
println!(" -v | --ver : Version info");
|
||||
println!(" config_file : Configuration file");
|
||||
} 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);
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let args: Vec<String> = env::args().collect();
|
||||
|
||||
print_help(args.clone());
|
||||
|
||||
let cfg_file = if args.len() > 1 {
|
||||
&args[1]
|
||||
} else if cfg!(windows) {
|
||||
"cai-watchdog.ini"
|
||||
} else {
|
||||
"/etc/cai-watchdog.conf"
|
||||
};
|
||||
|
||||
let cfg = ini!(cfg_file);
|
||||
@@ -116,7 +172,7 @@ fn main() {
|
||||
debug_log(format!("rule {}", i));
|
||||
debug_log(format!("service {}", service));
|
||||
debug_log(format!("address {}", address));
|
||||
debug_log(format!("email {}", email));
|
||||
debug_log(format!("email {}", email));
|
||||
debug_log(format!("command {}", command));
|
||||
|
||||
tasks.push(
|
||||
@@ -130,6 +186,23 @@ fn main() {
|
||||
);
|
||||
}
|
||||
|
||||
let start_notification = cfg["notifications"]["service_start"].clone().unwrap().to_string() == "true".to_string();
|
||||
let notification_email = cfg["notifications"]["email"].clone().unwrap();
|
||||
let notification_command = cfg["notifications"]["command"].clone().unwrap();
|
||||
|
||||
if start_notification {
|
||||
debug_log(format!("Service started"));
|
||||
|
||||
let shell_cmd = notification_command.to_string()
|
||||
.replace("<email>", ¬ification_email.to_string())
|
||||
.replace("<header>", &format!("\"Watchdog service started\""))
|
||||
.replace("<message>", &format!("\"Watchdog service started\""));
|
||||
|
||||
debug_log(format!("execute {}", shell_cmd));
|
||||
|
||||
execute(shell_cmd);
|
||||
}
|
||||
|
||||
loop {
|
||||
for i in 0..tasks.len() {
|
||||
let rt = tokio::runtime::Runtime::new().unwrap();
|
||||
@@ -139,8 +212,8 @@ fn main() {
|
||||
debug_log(format!("{} state changed to true", tasks[i].address));
|
||||
|
||||
let shell_cmd = tasks[i].command.to_string()
|
||||
.replace("<email>", &tasks[i].email)
|
||||
.replace("<header>", &format!("\"Service {} ({}) online\"", tasks[i].service, tasks[i].address))
|
||||
.replace("<email>", &tasks[i].email)
|
||||
.replace("<header>", &format!("\"Service {} ({}) is online\"", tasks[i].service, tasks[i].address))
|
||||
.replace("<message>", &format!("\"Service {} ({}) is now online\"", tasks[i].service, tasks[i].address));
|
||||
|
||||
debug_log(format!("execute {}", shell_cmd));
|
||||
@@ -156,8 +229,8 @@ fn main() {
|
||||
debug_log(format!("{} state changed to false", tasks[i].address));
|
||||
|
||||
let shell_cmd = tasks[i].command.to_string()
|
||||
.replace("<email>", &tasks[i].email)
|
||||
.replace("<header>", &format!("\"Service {} ({}) offline\"", tasks[i].service, tasks[i].address))
|
||||
.replace("<email>", &tasks[i].email)
|
||||
.replace("<header>", &format!("\"Service {} ({}) is offline\"", tasks[i].service, tasks[i].address))
|
||||
.replace("<message>", &format!("\"Service {} ({}) is now offline\"", tasks[i].service, tasks[i].address));
|
||||
|
||||
debug_log(format!("execute {}", shell_cmd));
|
||||
|
||||
Reference in New Issue
Block a user