Notifications on program startup. Minor changes in scripts.
Notifications on program startup. Minor changes in scripts.
This commit is contained in:
parent
577ad19b9f
commit
ccb471dcc2
7
Cargo.lock
generated
7
Cargo.lock
generated
@ -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"
|
||||
|
||||
@ -15,3 +15,4 @@ chrono = "0.4.19"
|
||||
reqwest = "0.11"
|
||||
tokio = { version = "1", features = ["full"] }
|
||||
ini = "1.3.0"
|
||||
exitcode = "1.1.2"
|
||||
20
scripts/unix/cai-watchdog.conf.template
Normal file
20
scripts/unix/cai-watchdog.conf.template
Normal 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>
|
||||
@ -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/
|
||||
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));
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user