From fd992d7e3c03f37fbcafe9d3f26c72a2ead3b2a7 Mon Sep 17 00:00:00 2001 From: Toby Vincent Date: Thu, 26 Sep 2024 17:31:16 -0500 Subject: feat!: impl full api --- src/service/http.rs | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ src/service/systemd.rs | 33 +++++++++++++++++++++++++++++++++ src/service/tcp.rs | 28 ++++++++++++++++++++++++++++ 3 files changed, 110 insertions(+) create mode 100644 src/service/http.rs create mode 100644 src/service/systemd.rs create mode 100644 src/service/tcp.rs (limited to 'src/service') 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 { + 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 { + 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 { + Ok(std::net::TcpStream::connect(&self.address) + .err() + .map(|err| Check { + status: Status::Fail, + output: Some(format!("error: {err}")), + }) + .unwrap_or_default()) + } +} -- cgit v1.2.3-70-g09d2