prepare supporting different formats

This commit is contained in:
Astro 2022-12-28 01:18:13 +01:00
parent bf80240238
commit 8e1e6f5e32
4 changed files with 75 additions and 54 deletions

View File

@ -10,44 +10,23 @@ use axum::{
use serde::Deserialize;
mod jabber;
mod prometheus_alert;
#[derive(Debug, Clone, Deserialize)]
struct Payload {
alerts: Vec<Alert>,
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<String, askama::Error> {
match self {
Payload::Prometheus(payload) => payload.render(),
}
}
#[derive(Debug, Clone, Deserialize)]
struct AlertLabels {
alertname: Option<String>,
host: Option<String>,
instance: Option<String>,
exported_instance: Option<String>,
}
async fn handle_payload(&self, jabber: jabber::Handle) -> Response {
let mut error_message = None;
#[derive(Debug, Clone, Deserialize)]
struct AlertAnnotations {
message: Option<String>,
description: Option<String>,
}
async fn alerts(
State(jabber): State<jabber::Handle>,
Json(payload): Json<Payload>,
) -> 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<jabber::Handle>,
Json(payload): Json<prometheus_alert::Payload>,
) -> Response {
Payload::Prometheus(payload)
.handle_payload(jabber)
.await
}
#[derive(Deserialize)]
struct Config {
listen_port: u16,

31
src/prometheus_alert.rs Normal file
View File

@ -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<Alert>,
}
#[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<String>,
pub host: Option<String>,
pub instance: Option<String>,
pub exported_instance: Option<String>,
}
#[derive(Debug, Clone, Deserialize)]
pub struct AlertAnnotations {
pub message: Option<String>,
pub description: Option<String>,
}

View File

@ -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 }}

View File

@ -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 -%}