prepare supporting different formats
This commit is contained in:
parent
bf80240238
commit
8e1e6f5e32
52
src/main.rs
52
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<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>,
|
||||
}
|
||||
|
||||
#[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 {
|
||||
async fn handle_payload(&self, jabber: jabber::Handle) -> 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,7 +34,6 @@ async fn alerts(
|
|||
error_message = Some(format!("{}", e));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
match error_message {
|
||||
None =>
|
||||
|
@ -66,6 +44,16 @@ async fn alerts(
|
|||
).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 {
|
||||
|
|
31
src/prometheus_alert.rs
Normal file
31
src/prometheus_alert.rs
Normal 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>,
|
||||
}
|
|
@ -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 }}
|
15
templates/prometheus_alert.txt
Normal file
15
templates/prometheus_alert.txt
Normal 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 -%}
|
Loading…
Reference in New Issue
Block a user