X-Git-Url: http://git.euphorik.ch/?a=blobdiff_plain;f=src%2Fmain.rs;h=08039fb26eb442810a7783296bae277554d5cf70;hb=8ff916896124ea900ea2c6b66c87e6cc72ffe1eb;hp=49668d0a7151a60384aff5c1fc4956da8a7ed412;hpb=c39054926459bd77c7d2f16d1454d544bd118b98;p=stakingWatchdog.git diff --git a/src/main.rs b/src/main.rs index 49668d0..08039fb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,6 +3,7 @@ */ use std::{ + fmt, net::UdpSocket, sync::{Arc, Mutex}, thread, @@ -59,8 +60,9 @@ fn main() -> Result<()> { // Send e-mail. println!("Sending email..."); match send_email( - "Staking ERROR", - &format!("Error: {:?}", error), + "Watchdog: Staking error", + &format!("Error: {}", error), + &config.smtp_relay_address, &config.smtp_login, &config.smtp_password, ) { @@ -105,6 +107,58 @@ enum CheckError { CheckAlive, } +impl fmt::Display for CheckError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match self { + CheckError::HttpError(text) => { + write!( + f, + "Beacon node health check can't be reached (HTTP error): {}", + text + ) + } + CheckError::NotSync => { + write!( + f, + "Beacon node health check is syncing (currently not sync)" + ) + } + CheckError::InvalidSyncStatus => { + write!(f, "Beacon node health check returns an invalid status code") + } + CheckError::NodeHavingIssues => { + write!( + f, + "Beacon node health check is not initilized or having issue" + ) + } + CheckError::UnknownCodeFromHealthCheck(code) => { + write!( + f, + "Beacon node health check returns an unknown code: {}", + code + ) + } + CheckError::ReqwestError(error) => { + write!(f, "Error from reqwest: {}", error) + } + CheckError::ValidatorError { pub_key, message } => { + write!(f, "Validator '{}' returns an error: {}", pub_key, message) + } + CheckError::ValidatorStatusError { pub_key, message } => { + write!( + f, + "Validator '{}' returns a status error: {}", + pub_key, message + ) + } + CheckError::CheckAlive => { + write!(f, "Check alive ping hasn't been received") + } + } + } +} + impl From for CheckError { fn from(value: reqwest::Error) -> Self { CheckError::ReqwestError(value) @@ -192,7 +246,13 @@ fn check_validators(pub_keys: &[String]) -> std::result::Result<(), CheckError> Ok(()) } -fn send_email(title: &str, body: &str, login: &str, pass: &str) -> Result<()> { +fn send_email( + title: &str, + body: &str, + smtp_relay_address: &str, + login: &str, + pass: &str, +) -> Result<()> { let email = Message::builder() .message_id(None) .from("Staking Watchdog ".parse()?) @@ -203,12 +263,10 @@ fn send_email(title: &str, body: &str, login: &str, pass: &str) -> Result<()> { let creds = Credentials::new(login.to_string(), pass.to_string()); - // Open a remote connection to gmail - let mailer = SmtpTransport::relay("mail.gandi.net")? + let mailer = SmtpTransport::relay(smtp_relay_address)? .credentials(creds) .build(); - // Send the email let response = mailer.send(&email)?; println!("{:?}", response);