diff options
Diffstat (limited to 'src/service')
-rw-r--r-- | src/service/http.rs | 49 | ||||
-rw-r--r-- | src/service/systemd.rs | 33 | ||||
-rw-r--r-- | src/service/tcp.rs | 28 |
3 files changed, 110 insertions, 0 deletions
diff --git a/src/service/http.rs b/src/service/http.rs new file mode 100644 index 0000000..15696a1 --- /dev/null +++ b/src/service/http.rs @@ -0,0 +1,49 @@ +use std::fmt::Display; + +use serde::Deserialize; + +use crate::{Check, Error, Status}; + +#[derive(Debug, Clone, Deserialize)] +pub struct Http { + pub url: String, + #[serde(default = "Http::default_method")] + pub method: String, + #[serde(default = "Http::default_code")] + pub status_code: u16, +} + +impl Http { + fn default_method() -> String { + "GET".to_string() + } + + fn default_code() -> u16 { + 200 + } +} + +impl Display for Http { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{} {}", self.method, self.url) + } +} + +impl Http { + pub async fn check(&self, client: reqwest::Client) -> Result<Check, Error> { + let status_code = client + .request(self.method.parse().map_err(|_| Error::Method)?, &self.url) + .send() + .await? + .status() + .as_u16(); + + match status_code == self.status_code { + true => Ok(Check::default()), + false => Ok(Check { + status: Status::Fail, + output: Some(format!("Status code: {status_code}")), + }), + } + } +} diff --git a/src/service/systemd.rs b/src/service/systemd.rs new file mode 100644 index 0000000..2e3b74c --- /dev/null +++ b/src/service/systemd.rs @@ -0,0 +1,33 @@ +use std::{fmt::Display, process::Command}; + +use serde::Deserialize; + +use crate::{Check, Error, Status}; + +#[derive(Debug, Clone, Deserialize)] +pub struct Systemd { + pub service: String, +} + +impl Display for Systemd { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{}.service", self.service.trim_end_matches(".service")) + } +} + +impl Systemd { + pub async fn check(&self) -> Result<Check, Error> { + let output = Command::new("systemctl") + .arg("is-active") + .arg(&self.service) + .output()?; + + let stdout = String::from_utf8_lossy(&output.stdout).to_string(); + Ok((!output.status.success()) + .then(|| Check { + status: Status::Fail, + output: Some(format!("Service state: {}", stdout.trim())), + }) + .unwrap_or_default()) + } +} diff --git a/src/service/tcp.rs b/src/service/tcp.rs new file mode 100644 index 0000000..5f55091 --- /dev/null +++ b/src/service/tcp.rs @@ -0,0 +1,28 @@ +use std::fmt::Display; + +use serde::Deserialize; + +use crate::{Check, Error, Status}; + +#[derive(Debug, Clone, Deserialize)] +pub struct Tcp { + pub address: String, +} + +impl Display for Tcp { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "tcp://{}", self.address) + } +} + +impl Tcp { + pub async fn check(&self) -> Result<Check, Error> { + Ok(std::net::TcpStream::connect(&self.address) + .err() + .map(|err| Check { + status: Status::Fail, + output: Some(format!("error: {err}")), + }) + .unwrap_or_default()) + } +} |