Compare commits

...

2 Commits

Author SHA1 Message Date
d406657bfb Merge pull request 'Notifications on program startup. Minor changes in scripts.' (#4) from v1 into master
Reviewed-on: https://www.cainet.info/git/cai/CAI-Watchdog/pulls/4
2022-06-27 18:15:55 +03:00
Alexander I. Chebykin
ccb471dcc2 Notifications on program startup. Minor changes in scripts.
Notifications on program startup. Minor changes in scripts.
2022-06-27 18:14:13 +03:00
7 changed files with 118 additions and 10 deletions

7
Cargo.lock generated
View File

@ -37,6 +37,7 @@ name = "cai-watchdog"
version = "0.1.0"
dependencies = [
"chrono",
"exitcode",
"file-utils",
"ini",
"reqwest",
@ -99,6 +100,12 @@ dependencies = [
"cfg-if",
]
[[package]]
name = "exitcode"
version = "1.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "de853764b47027c2e862a995c34978ffa63c1501f2e15f987ba11bd4f9bba193"
[[package]]
name = "fastrand"
version = "1.7.0"

View File

@ -15,3 +15,4 @@ chrono = "0.4.19"
reqwest = "0.11"
tokio = { version = "1", features = ["full"] }
ini = "1.3.0"
exitcode = "1.1.2"

View File

@ -0,0 +1,20 @@
[main]
check_interval = 10000
rules_count = 2
[notifications]
email = admin@server.local
command = send-telegram <message>
service_start = true
[rule1]
service = Test1
address = http://127.0.0.1:3000/api/v1/
email = admin@server.local
command = send-telegram <message>
[rule2]
service = Test2
address = http://127.0.0.1:3300/api/v1/
email = admin@server.local
command = send-telegram <message>

View File

@ -1,11 +1,18 @@
[main]
check_interval = 10000
rules_count = 2
[notifications]
email = admin@server.local
command = ./send-telegram.ps1 <message>
service_start = true
[rule1]
service = Test1
address = http://127.0.0.1:3000/api/v1/
email = admin@server.local
command = ./send-telegram.ps1 <message>
[rule2]
service = Test2
address = http://127.0.0.1:3300/api/v1/

View File

@ -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);
@ -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>", &notification_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();
@ -140,7 +213,7 @@ fn main() {
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("<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));
@ -157,7 +230,7 @@ fn main() {
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("<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));