Merge pull request 'Configuration file options modified.' (#5) from v1 into master

Reviewed-on: https://www.cainet.info/git/cai/CAI-Watchdog/pulls/5
This commit is contained in:
Alexander I. Chebykin 2022-06-27 21:38:44 +03:00
commit c981bfd033
7 changed files with 112 additions and 28 deletions

2
Cargo.lock generated
View File

@ -34,7 +34,7 @@ checksum = "c4872d67bab6358e59559027aa3b9157c53d9358c51423c17554809a8858e0f8"
[[package]]
name = "cai-watchdog"
version = "0.1.0"
version = "0.2.0"
dependencies = [
"chrono",
"exitcode",

View File

@ -1,6 +1,6 @@
[package]
name = "cai-watchdog"
version = "0.1.0"
version = "0.2.0"
authors = ["Alexander I. Chebykin <alex.chebykin@gmail.com>"]
edition = "2018"

View File

@ -1,3 +1,84 @@
# CAI-Watchdog
Watchdog for monitoring web-services
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 ```<email>```, ```<subject>``` and ```<message>```
- ```<email>``` - E-mail address for notifications
- ```<subject>``` - E-mail subject
- ```<message>``` - Message text
### Scripts configurations
#### *nix
##### send-telegram
Next lines needs to be configured:
- ```GROUP_ID=<group_id>``` - Set Telegram group ID here
- ```BOT_TOKEN=<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

View File

@ -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 <message>
[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 <message>

View File

@ -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 <message>
[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 <message>

View File

@ -1,5 +1,3 @@
Param([Parameter(Mandatory=$true)][String]$Message)
$Telegramtoken = "Your_Telegram_Token"
$Telegramchatid = "Your_Telegram_Chat_ID"

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
}