1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/// The `Action` enum represents the possible actions that can occur
/// as a result of a monitor check.
/// 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)))
/// }
/// }
/// }
/// }
/// ```