From b55f72f2e42baebbf84827086de66faa10ab83ce Mon Sep 17 00:00:00 2001 From: "Alexander I. Chebykin" Date: Mon, 27 Jun 2022 21:37:11 +0300 Subject: [PATCH] Configuration file options modified. address renamed to uri,
renamed to --- Cargo.lock | 2 +- Cargo.toml | 2 +- README.md | 83 +++++++++++++++++++++- scripts/unix/cai-watchdog.conf.template | 4 +- scripts/windows/cai-watchdog.ini.template | 4 +- scripts/windows/send-telegram.ps1.template | 2 - src/main.rs | 43 ++++++----- 7 files changed, 112 insertions(+), 28 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 554ec4f..ecd4412 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -34,7 +34,7 @@ checksum = "c4872d67bab6358e59559027aa3b9157c53d9358c51423c17554809a8858e0f8" [[package]] name = "cai-watchdog" -version = "0.1.0" +version = "0.2.0" dependencies = [ "chrono", "exitcode", diff --git a/Cargo.toml b/Cargo.toml index 6a98ca5..4f6d70f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "cai-watchdog" -version = "0.1.0" +version = "0.2.0" authors = ["Alexander I. Chebykin "] edition = "2018" diff --git a/README.md b/README.md index 39abf49..f3e760c 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,84 @@ # CAI-Watchdog -Watchdog for monitoring web-services \ No newline at end of file +Watchdog for monitoring web-services + +## Requirements + +**On \*nix:** OpenSSL 1.0.1, 1.0.2, 1.1.0, or 1.1.1 with headers (see https://github.com/sfackler/rust-openssl) + +**On Windows:** Nothing + +## Configuration file + +Default configuration file location: + +**On \*nix:** /etc/cai-watchdog.conf + +**On Windows:** current directory + +You can specify config file as parameter: ```cai-watchdog /path/to/config/config_file.conf``` (*nix) or ```cai-watchdog.exe drive:\path\to\config\config_file.ini``` (Windows) + +### Configuration file parameters + +``` +[main] +check_interval - Interval between checks in seconds +rules_count - Rules count to be loaded from config. Rules sections must be enumerated continuously [rule1], [rule2] ... etc + +[notifications] +email - E-mail address for system notifications. Can be empty +command - Command to send notification +service_start - Send program start notification [true | false] + +[rule1] +service = Service name +uri = URI to be checked +email = E-mail address for notifications +command = Command to send notification +``` + +In commands You can use fields ``````, `````` and `````` + +- `````` - E-mail address for notifications +- `````` - E-mail subject +- `````` - Message text + +### Scripts configurations + +#### *nix + +##### send-telegram + +Next lines needs to be configured: + +- ```GROUP_ID=``` - Set Telegram group ID here +- ```BOT_TOKEN=``` - Set Telegram token here +#### Windows + +##### send-mail.ps1 + +Next lines needs to be configured: + +- ```$EmailFrom = "yourmailadress@somedomain.com"``` - Set sender e-mail address here +- ```$SMTPServer = "smtp.somedomain.com"``` - Set SMTP-server address here +- ```$SMTPClient.EnableSsl = $true``` - Set SSL flag here +- ```$SMTPClient.Credentials = New-Object System.Net.NetworkCredential("usr", "pass");``` - Set user ("usr") and password ("pass") here + +##### send-telegram.ps1 + +- ```$Telegramtoken = "Your_Telegram_Token"``` - Set Telegram token here +- ```$Telegramchatid = "Your_Telegram_Chat_ID"``` - Set Telegram chat ID here + +### How to get Telegram token and chat ID + +1. Open ```@BotFather``` bot +1. Run ```/newbot``` command and give name to your new bot +1. Enter a username for the bot +1. Take note of the API token. We will need this later. **Note:** it is case sensitive +1. Click the link to open a chat with the newly created bot + +Next you need to find your Telegram Chat ID. + +1. From the Telegram home screen, search for ```chatid_echo_bot```. Click Chat ID Echo to open a chat +1. Enter ```/start``` to get the bot to send you your Telegram Chat ID +1. Take note of the Telegram Chat ID returned diff --git a/scripts/unix/cai-watchdog.conf.template b/scripts/unix/cai-watchdog.conf.template index 358bbfe..bea3839 100644 --- a/scripts/unix/cai-watchdog.conf.template +++ b/scripts/unix/cai-watchdog.conf.template @@ -9,12 +9,12 @@ service_start = true [rule1] service = Test1 -address = http://127.0.0.1:3000/api/v1/ +uri = http://127.0.0.1:3000/api/v1/ email = admin@server.local command = send-telegram [rule2] service = Test2 -address = http://127.0.0.1:3300/api/v1/ +uri = http://127.0.0.1:3300/api/v1/ email = admin@server.local command = send-telegram diff --git a/scripts/windows/cai-watchdog.ini.template b/scripts/windows/cai-watchdog.ini.template index f4bc925..366f78a 100644 --- a/scripts/windows/cai-watchdog.ini.template +++ b/scripts/windows/cai-watchdog.ini.template @@ -9,12 +9,12 @@ service_start = true [rule1] service = Test1 -address = http://127.0.0.1:3000/api/v1/ +uri = http://127.0.0.1:3000/api/v1/ email = admin@server.local command = ./send-telegram.ps1 [rule2] service = Test2 -address = http://127.0.0.1:3300/api/v1/ +uri = http://127.0.0.1:3300/api/v1/ email = admin@server.local command = ./send-telegram.ps1 diff --git a/scripts/windows/send-telegram.ps1.template b/scripts/windows/send-telegram.ps1.template index ee72e73..207461c 100644 --- a/scripts/windows/send-telegram.ps1.template +++ b/scripts/windows/send-telegram.ps1.template @@ -1,5 +1,3 @@ -Param([Parameter(Mandatory=$true)][String]$Message) - $Telegramtoken = "Your_Telegram_Token" $Telegramchatid = "Your_Telegram_Chat_ID" diff --git a/src/main.rs b/src/main.rs index ad4501d..fadf284 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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) { 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) { 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::().unwrap(); + let check_interval = cfg["main"]["check_interval"].clone().unwrap().parse::().unwrap() * 1000; let rules_count = cfg["main"]["rules_count"].clone().unwrap().parse::().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("", ¬ification_email.to_string()) - .replace("
", &format!("\"Watchdog service started\"")) + .replace("", &format!("\"Watchdog service started\"")) .replace("", &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("", &tasks[i].email) - .replace("
", &format!("\"Service {} ({}) is online\"", tasks[i].service, tasks[i].address)) - .replace("", &format!("\"Service {} ({}) is now online\"", tasks[i].service, tasks[i].address)); + .replace("", &format!("\"Service {} ({}) is online\"", tasks[i].service, tasks[i].uri)) + .replace("", &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("", &tasks[i].email) - .replace("
", &format!("\"Service {} ({}) is offline\"", tasks[i].service, tasks[i].address)) - .replace("", &format!("\"Service {} ({}) is now offline\"", tasks[i].service, tasks[i].address)); + .replace("", &format!("\"Service {} ({}) is offline\"", tasks[i].service, tasks[i].uri)) + .replace("", &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 } -- 2.51.0