pub trait Monitor: Send + Sync {
// Required method
fn check(&mut self) -> Action;
}
Expand description
The Monitor
trait represents the ability to monitor a system for a specific condition.
§Example
use log::{info, error};
pub use reqwest::blocking::Client;
use gargoyle::{Action, Monitor};
pub struct WebAvailability {
pub url: String,
web_client: Client,
}
impl WebAvailability {
pub fn new(url: &str) -> Self {
let web_client = Client::builder()
.user_agent("Gargoyle/0.1")
.build()
.unwrap();
Self {
url: url.to_string(),
web_client,
}
}
}
impl Monitor for WebAvailability {
fn check(&mut self) -> Action {
match self.web_client.get(&self.url).send() {
Ok(response) => {
if response.status().is_success() {
info!("{} is up", self.url);
Action::Nothing
} else {
info!("{} is down", self.url);
error!("Failed to get {} - {}", self.url, response.status());
Action::Notify(Some(format!("Failed to get {} - {}", self.url, response.status())))
}
}
Err(_) => {
info!("{} is down", self.url);
error!("Failed to connect to {}", self.url);
Action::Notify(Some(format!("Failed to connect to {}", self.url)))
}
}
}
}