Configuration file options modified.

address renamed to uri, <header> renamed to <subject>
This commit is contained in:
2022-06-27 21:37:11 +03:00
parent ccb471dcc2
commit b55f72f2e4
7 changed files with 112 additions and 28 deletions

View File

@@ -12,7 +12,7 @@ pub struct Rule {
/// Monitored service name
pub service: String,
/// Monitored URI
pub address: String,
pub uri: String,
/// E-mail address for messages
pub email: String,
/// This command will be executed on state change
@@ -116,15 +116,17 @@ fn print_help(args: Vec<String>) {
println!(" /? | /help : This help message");
println!(" /v | /ver : Version info");
println!(" config_file : Configuration file");
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);
std::process::exit(exitcode::OK);
}
} else {
if args.len() > 1 && (args[1].to_string() == "--help".to_string() || args[1].to_string() == "-h".to_string()) {
println!("");
@@ -132,14 +134,17 @@ fn print_help(args: Vec<String>) {
println!(" -h | --help : This help message");
println!(" -v | --ver : Version info");
println!(" config_file : Configuration file");
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);
}
std::process::exit(exitcode::OK);
}
}
@@ -158,27 +163,27 @@ fn main() {
let cfg = ini!(cfg_file);
let check_interval = cfg["main"]["check_interval"].clone().unwrap().parse::<u64>().unwrap();
let check_interval = cfg["main"]["check_interval"].clone().unwrap().parse::<u64>().unwrap() * 1000;
let rules_count = cfg["main"]["rules_count"].clone().unwrap().parse::<u8>().unwrap();
let mut tasks = vec![];
for i in 1..rules_count + 1 {
let service = cfg[&format!("{}{}", "rule", i)]["service"].clone().unwrap();
let address = cfg[&format!("{}{}", "rule", i)]["address"].clone().unwrap();
let uri = cfg[&format!("{}{}", "rule", i)]["uri"].clone().unwrap();
let email = cfg[&format!("{}{}", "rule", i)]["email"].clone().unwrap();
let command = cfg[&format!("{}{}", "rule", i)]["command"].clone().unwrap();
debug_log(format!("rule {}", i));
debug_log(format!("rule {}", i));
debug_log(format!("service {}", service));
debug_log(format!("address {}", address));
debug_log(format!("uri {}", uri));
debug_log(format!("email {}", email));
debug_log(format!("command {}", command));
tasks.push(
Rule{
service: cfg[&format!("{}{}", "rule", i)]["service"].clone().unwrap().to_string(),
address: cfg[&format!("{}{}", "rule", i)]["address"].clone().unwrap().to_string(),
uri: cfg[&format!("{}{}", "rule", i)]["uri"].clone().unwrap().to_string(),
email: cfg[&format!("{}{}", "rule", i)]["email"].clone().unwrap().to_string(),
command: cfg[&format!("{}{}", "rule", i)]["command"].clone().unwrap().to_string(),
last_state: false,
@@ -195,7 +200,7 @@ fn main() {
let shell_cmd = notification_command.to_string()
.replace("<email>", &notification_email.to_string())
.replace("<header>", &format!("\"Watchdog service started\""))
.replace("<subject>", &format!("\"Watchdog service started\""))
.replace("<message>", &format!("\"Watchdog service started\""));
debug_log(format!("execute {}", shell_cmd));
@@ -207,37 +212,37 @@ fn main() {
for i in 0..tasks.len() {
let rt = tokio::runtime::Runtime::new().unwrap();
rt.block_on(async {
if check(tasks[i].address.clone()).await {
if check(tasks[i].uri.clone()).await {
if tasks[i].last_state != true {
debug_log(format!("{} state changed to true", tasks[i].address));
debug_log(format!("{} state changed to true", tasks[i].uri));
let shell_cmd = tasks[i].command.to_string()
.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));
.replace("<subject>", &format!("\"Service {} ({}) is online\"", tasks[i].service, tasks[i].uri))
.replace("<message>", &format!("\"Service {} ({}) is now online\"", tasks[i].service, tasks[i].uri));
debug_log(format!("execute {}", shell_cmd));
execute(shell_cmd);
}
debug_log(format!("{} check is ok", tasks[i].address));
debug_log(format!("{} check is ok", tasks[i].uri));
tasks[i].last_state = true
} else {
if tasks[i].last_state != false {
debug_log(format!("{} state changed to false", tasks[i].address));
debug_log(format!("{} state changed to false", tasks[i].uri));
let shell_cmd = tasks[i].command.to_string()
.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));
.replace("<subject>", &format!("\"Service {} ({}) is offline\"", tasks[i].service, tasks[i].uri))
.replace("<message>", &format!("\"Service {} ({}) is now offline\"", tasks[i].service, tasks[i].uri));
debug_log(format!("execute {}", shell_cmd));
execute(shell_cmd);
}
debug_log(format!("{} check failed", tasks[i].address));
debug_log(format!("{} check failed", tasks[i].uri));
tasks[i].last_state = false
}