From 8e1e6f5e323b5a68f2a78ddbd077ac663643f756 Mon Sep 17 00:00:00 2001 From: Astro Date: Wed, 28 Dec 2022 01:18:13 +0100 Subject: [PATCH] prepare supporting different formats --- src/main.rs | 70 ++++++++++++++-------------------- src/prometheus_alert.rs | 31 +++++++++++++++ templates/alert.txt | 13 ------- templates/prometheus_alert.txt | 15 ++++++++ 4 files changed, 75 insertions(+), 54 deletions(-) create mode 100644 src/prometheus_alert.rs delete mode 100644 templates/alert.txt create mode 100644 templates/prometheus_alert.txt diff --git a/src/main.rs b/src/main.rs index 0b311d9..99570fc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,44 +10,23 @@ use axum::{ use serde::Deserialize; mod jabber; +mod prometheus_alert; -#[derive(Debug, Clone, Deserialize)] -struct Payload { - alerts: Vec, +enum Payload { + Prometheus(prometheus_alert::Payload), } -#[derive(Debug, Clone, Deserialize, Template)] -#[template(path="alert.txt", escape="none")] -struct Alert { - status: String, - labels: AlertLabels, - annotations: AlertAnnotations, - #[serde(rename = "generatorURL")] - generator_url: String, -} +impl Payload { + fn render(&self) -> Result { + match self { + Payload::Prometheus(payload) => payload.render(), + } + } -#[derive(Debug, Clone, Deserialize)] -struct AlertLabels { - alertname: Option, - host: Option, - instance: Option, - exported_instance: Option, -} + async fn handle_payload(&self, jabber: jabber::Handle) -> Response { + let mut error_message = None; -#[derive(Debug, Clone, Deserialize)] -struct AlertAnnotations { - message: Option, - description: Option, -} - -async fn alerts( - State(jabber): State, - Json(payload): Json, -) -> Response { - let mut error_message = None; - - for alert in payload.alerts { - match alert.render() { + match self.render() { Ok(message) => { jabber.send_message(message).await; } @@ -55,18 +34,27 @@ async fn alerts( error_message = Some(format!("{}", e)); } } - } - match error_message { - None => - StatusCode::OK.into_response(), - Some(error_message) => - (StatusCode::INTERNAL_SERVER_ERROR, - error_message - ).into_response() + match error_message { + None => + StatusCode::OK.into_response(), + Some(error_message) => + (StatusCode::INTERNAL_SERVER_ERROR, + error_message + ).into_response() + } } } +async fn alerts( + State(jabber): State, + Json(payload): Json, +) -> Response { + Payload::Prometheus(payload) + .handle_payload(jabber) + .await +} + #[derive(Deserialize)] struct Config { listen_port: u16, diff --git a/src/prometheus_alert.rs b/src/prometheus_alert.rs new file mode 100644 index 0000000..b015231 --- /dev/null +++ b/src/prometheus_alert.rs @@ -0,0 +1,31 @@ +use askama::Template; +use serde::Deserialize; + +#[derive(Debug, Clone, Deserialize, Template)] +#[template(path="prometheus_alert.txt", escape="none")] +pub struct Payload { + pub alerts: Vec, +} + +#[derive(Debug, Clone, Deserialize)] +pub struct Alert { + pub status: String, + pub labels: AlertLabels, + pub annotations: AlertAnnotations, + #[serde(rename = "generatorURL")] + pub generator_url: String, +} + +#[derive(Debug, Clone, Deserialize)] +pub struct AlertLabels { + pub alertname: Option, + pub host: Option, + pub instance: Option, + pub exported_instance: Option, +} + +#[derive(Debug, Clone, Deserialize)] +pub struct AlertAnnotations { + pub message: Option, + pub description: Option, +} diff --git a/templates/alert.txt b/templates/alert.txt deleted file mode 100644 index aca02f8..0000000 --- a/templates/alert.txt +++ /dev/null @@ -1,13 +0,0 @@ -{{ status|upper -}}: -{% if labels.alertname.as_ref().is_some() %}*{{ labels.alertname.as_ref().unwrap() }}*{% endif -%} -{% if labels.host.as_ref().is_some() -%} -at {{ labels.host.as_ref().unwrap() -}} -{% else if labels.exported_instance.as_ref().is_some() -%} -at {{ labels.exported_instance.as_ref().unwrap() }} -{% else if labels.instance.as_ref().is_some() -%} -at {{ labels.instance.as_ref().unwrap() }}{% endif -%} -{% if annotations.message.as_ref().is_some() %} -{{ annotations.message.as_ref().unwrap() }}{% endif %} -{% if annotations.description.as_ref().is_some() %} -{{ annotations.description.as_ref().unwrap() }}{% endif %} -Link: {{ generator_url }} diff --git a/templates/prometheus_alert.txt b/templates/prometheus_alert.txt new file mode 100644 index 0000000..3cdb815 --- /dev/null +++ b/templates/prometheus_alert.txt @@ -0,0 +1,15 @@ +{% for alert in alerts -%} +{{ alert.status|upper -}}: +{% if alert.labels.alertname.as_ref().is_some() %}*{{ alert.labels.alertname.as_ref().unwrap() }}*{% endif -%} +{% if alert.labels.host.as_ref().is_some() -%} +at {{ alert.labels.host.as_ref().unwrap() -}} +{% else if alert.labels.exported_instance.as_ref().is_some() -%} +at {{ alert.labels.exported_instance.as_ref().unwrap() }} +{% else if alert.labels.instance.as_ref().is_some() -%} +at {{ alert.labels.instance.as_ref().unwrap() }}{% endif -%} +{% if alert.annotations.message.as_ref().is_some() %} +{{ alert.annotations.message.as_ref().unwrap() }}{% endif %} +{% if alert.annotations.description.as_ref().is_some() %} +{{ alert.annotations.description.as_ref().unwrap() }}{% endif %} +Link: {{ alert.generator_url }} +{% endfor -%}