summaryrefslogtreecommitdiffstats
path: root/src/service.rs
blob: c5eb0d732c4ce09e86285abab307ff98ddaed1c3 (plain)
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
use std::{fmt::Display, process::Command};

use reqwest::blocking::Client;
use serde::Deserialize;

use crate::Error;

#[derive(Debug, Clone, Deserialize)]
pub struct Service {
    pub name: String,
    #[serde(flatten)]
    pub kind: Kind,
    #[serde(skip)]
    pub state: State,
}

impl Service {
    pub fn check(&mut self, client: Client) -> Result<bool, Error> {
        self.state = self.kind.get_state(client)?;
        Ok(self.state.is_operational())
    }
}

#[derive(Debug, Clone, Deserialize)]
#[serde(tag = "type", rename_all = "lowercase")]
pub enum Kind {
    Tcp {
        address: String,
    },
    Http {
        url: String,
        #[serde(default = "Kind::default_method")]
        method: String,
        #[serde(default = "Kind::default_code")]
        status_code: u16,
    },
    Systemd {
        service: String,
    },
}

impl Kind {
    fn default_method() -> String {
        "GET".to_string()
    }

    fn default_code() -> u16 {
        200
    }

    pub fn get_state(&self, client: Client) -> Result<State, Error> {
        let state = match self {
            Kind::Tcp { address } => {
                if std::net::TcpStream::connect(address).is_ok() {
                    State::Operational
                } else {
                    State::Down("Unreachable".to_string())
                }
            }
            Kind::Http {
                method,
                url,
                status_code,
            } => {
                match client
                    .request(method.parse().map_err(|_| Error::Method)?, url)
                    .send()?
                    .status()
                {
                    s if s.as_u16() == *status_code => State::Operational,
                    s => State::Down(s.to_string()),
                }
            }
            Kind::Systemd { service } => {
                let output = Command::new("systemctl")
                    .arg("is-active")
                    .arg(service)
                    .output()?;

                if output.status.success() {
                    State::Operational
                } else {
                    State::Down(String::from_utf8_lossy(&output.stdout).to_string())
                }
            }
        };

        Ok(state)
    }
}

impl Display for Kind {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Kind::Tcp { address } => write!(f, "tcp://{address}"),
            Kind::Http { method, url, .. } => write!(f, "{method} {url}"),
            Kind::Systemd { service } => write!(f, "{service}"),
        }
    }
}

#[derive(Debug, Clone, Default)]
pub struct Status {
    pub info: String,
    pub state: State,
}

#[derive(Debug, Clone, Default)]
pub enum State {
    #[default]
    Unknown,
    Operational,
    Down(String),
}

impl State {
    /// Returns `true` if this is a `Unknown` variant.
    pub fn is_unknown(&self) -> bool {
        matches!(self, Self::Unknown)
    }

    /// Returns `true` if this is a `Operational` variant.
    pub fn is_operational(&self) -> bool {
        matches!(self, Self::Operational)
    }

    /// Returns `true` if this is a `Down` variant.
    pub fn is_down(&self) -> bool {
        matches!(self, Self::Down(_))
    }

    /// Converts the `State` into an `Option` containing `String` description if the `State` was
    /// `Down` and `None` otherwise.
    pub fn down_value(self) -> Option<String> {
        match self {
            State::Unknown => None,
            State::Operational => None,
            State::Down(s) => Some(s),
        }
    }

    pub fn as_level(&self) -> String {
        match self {
            State::Unknown => "warning",
            State::Operational => "ok",
            State::Down(_) => "error",
        }
        .to_string()
    }
}

impl Display for State {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            State::Unknown => write!(f, "Unknown"),
            State::Operational => write!(f, "Operational"),
            State::Down(s) => write!(f, "{s}"),
        }
    }
}