diff --git a/bin/core/src/alert.rs b/bin/core/src/alert.rs new file mode 100644 index 000000000..1b08ed3fb --- /dev/null +++ b/bin/core/src/alert.rs @@ -0,0 +1,68 @@ +use anyhow::{anyhow, Context}; +use monitor_types::entities::{alert::Alert, alerter::*}; +use reqwest::StatusCode; + +pub async fn send_alert(alerter: &Alerter, alert: &Alert) -> anyhow::Result<()> { + match &alerter.config { + AlerterConfig::Slack(SlackAlerterConfig { url }) => send_slack_alert(url, alert).await, + AlerterConfig::Custom(CustomAlerterConfig { url }) => send_custom_alert(url, alert).await, + } +} + +pub async fn send_slack_alert(url: &str, alert: &Alert) -> anyhow::Result<()> { + #[allow(unused)] + let message = match alert { + Alert::ServerUnreachable { id, name, region } => String::new(), + Alert::ServerCpu { + id, + name, + region, + state, + percentage, + top_procs, + } => String::new(), + Alert::ServerMem { + id, + name, + region, + state, + used, + total, + top_procs, + } => String::new(), + Alert::ServerDisk { + id, + name, + region, + state, + path, + used, + total, + } => String::new(), + Alert::ContainerStateChange { + id, + name, + server, + from, + to, + } => String::new(), + }; + let res = reqwest::Client::new() + .post(url) + .send() + .await + .context("failed to make request to slack")?; + let status = res.status(); + if status != StatusCode::OK { + let text = res + .text() + .await + .context("failed to get slack alert response as test")?; + return Err(anyhow!("{status} | {text}")); + } + Ok(()) +} + +pub async fn send_custom_alert(url: &str, alert: &Alert) -> anyhow::Result<()> { + Ok(()) +} diff --git a/bin/core/src/main.rs b/bin/core/src/main.rs index 0f02f2a00..84f2f3805 100644 --- a/bin/core/src/main.rs +++ b/bin/core/src/main.rs @@ -4,6 +4,7 @@ extern crate log; use axum::{Extension, Router}; use termination_signal::tokio::immediate_term_handle; +mod alert; mod auth; mod cache; mod channel; @@ -12,7 +13,7 @@ mod config; mod db; mod helpers; mod listener; -mod monitoring; +mod monitor; mod requests; mod state; mod ws; diff --git a/bin/core/src/monitoring.rs b/bin/core/src/monitor.rs similarity index 100% rename from bin/core/src/monitoring.rs rename to bin/core/src/monitor.rs diff --git a/bin/core/src/state.rs b/bin/core/src/state.rs index 72f16c928..377cc9154 100644 --- a/bin/core/src/state.rs +++ b/bin/core/src/state.rs @@ -16,7 +16,7 @@ use crate::{ channel::BroadcastChannel, config::{CoreConfig, Env}, db::DbClient, - monitoring::{CachedDeploymentStatus, CachedServerStatus}, + monitor::{CachedDeploymentStatus, CachedServerStatus}, }; pub type StateExtension = Extension>; diff --git a/lib/types/src/entities/alert.rs b/lib/types/src/entities/alert.rs new file mode 100644 index 000000000..005bc9894 --- /dev/null +++ b/lib/types/src/entities/alert.rs @@ -0,0 +1,50 @@ +use derive_variants::EnumVariants; +use serde::{Deserialize, Serialize}; +use typeshare::typeshare; + +use super::{server::stats::{StatsState, SystemProcess}, deployment::DockerContainerState}; + +#[typeshare] +#[derive(Serialize, Deserialize, Debug, Clone, EnumVariants)] +#[variant_derive(Serialize, Deserialize, Debug, Clone, Copy)] +#[serde(tag = "type", content = "data")] +pub enum Alert { + ServerUnreachable { + id: String, + name: String, + region: Option, + }, + ServerCpu { + id: String, + name: String, + region: Option, + state: StatsState, + percentage: f64, + top_procs: Vec, + }, + ServerMem { + id: String, + name: String, + region: String, + state: StatsState, + used: f64, + total: f64, + top_procs: Vec, + }, + ServerDisk { + id: String, + name: String, + region: String, + state: StatsState, + path: String, + used: f64, + total: f64, + }, + ContainerStateChange { + id: String, + name: String, + server: String, // server name + from: DockerContainerState, + to: DockerContainerState, + } +} diff --git a/lib/types/src/entities/mod.rs b/lib/types/src/entities/mod.rs index 4f8d154f8..e22ba9d72 100644 --- a/lib/types/src/entities/mod.rs +++ b/lib/types/src/entities/mod.rs @@ -8,6 +8,7 @@ use typeshare::typeshare; use crate::optional_string; +pub mod alert; pub mod alerter; pub mod build; pub mod builder;