prepare supporting different formats
This commit is contained in:
parent
bf80240238
commit
8e1e6f5e32
70
src/main.rs
70
src/main.rs
|
@ -10,44 +10,23 @@ use axum::{
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
|
|
||||||
mod jabber;
|
mod jabber;
|
||||||
|
mod prometheus_alert;
|
||||||
|
|
||||||
#[derive(Debug, Clone, Deserialize)]
|
enum Payload {
|
||||||
struct Payload {
|
Prometheus(prometheus_alert::Payload),
|
||||||
alerts: Vec<Alert>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Deserialize, Template)]
|
impl Payload {
|
||||||
#[template(path="alert.txt", escape="none")]
|
fn render(&self) -> Result<String, askama::Error> {
|
||||||
struct Alert {
|
match self {
|
||||||
status: String,
|
Payload::Prometheus(payload) => payload.render(),
|
||||||
labels: AlertLabels,
|
}
|
||||||
annotations: AlertAnnotations,
|
}
|
||||||
#[serde(rename = "generatorURL")]
|
|
||||||
generator_url: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, Deserialize)]
|
async fn handle_payload(&self, jabber: jabber::Handle) -> Response {
|
||||||
struct AlertLabels {
|
let mut error_message = None;
|
||||||
alertname: Option<String>,
|
|
||||||
host: Option<String>,
|
|
||||||
instance: Option<String>,
|
|
||||||
exported_instance: Option<String>,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, Deserialize)]
|
match self.render() {
|
||||||
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() {
|
|
||||||
Ok(message) => {
|
Ok(message) => {
|
||||||
jabber.send_message(message).await;
|
jabber.send_message(message).await;
|
||||||
}
|
}
|
||||||
|
@ -55,18 +34,27 @@ async fn alerts(
|
||||||
error_message = Some(format!("{}", e));
|
error_message = Some(format!("{}", e));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
match error_message {
|
match error_message {
|
||||||
None =>
|
None =>
|
||||||
StatusCode::OK.into_response(),
|
StatusCode::OK.into_response(),
|
||||||
Some(error_message) =>
|
Some(error_message) =>
|
||||||
(StatusCode::INTERNAL_SERVER_ERROR,
|
(StatusCode::INTERNAL_SERVER_ERROR,
|
||||||
error_message
|
error_message
|
||||||
).into_response()
|
).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)]
|
#[derive(Deserialize)]
|
||||||
struct Config {
|
struct Config {
|
||||||
listen_port: u16,
|
listen_port: u16,
|
||||||
|
|
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