add jinja templating

This commit is contained in:
histausse 2021-10-08 15:46:49 +02:00
parent d2f202c72c
commit 2352626598
Signed by: histausse
GPG key ID: 67486F107F62E9E9
3 changed files with 52 additions and 10 deletions

View file

@ -21,7 +21,8 @@ async def __main():
) )
format_corout = format_alerts( format_corout = format_alerts(
alert_queue, alert_queue,
message_queue message_queue,
config
) )
webhook_corout = run_webhook( webhook_corout = run_webhook(
alert_queue, alert_queue,

View file

@ -21,6 +21,8 @@ class Config:
tls_crt: Optional[str] = None tls_crt: Optional[str] = None
tls_key: Optional[str] = None tls_key: Optional[str] = None
ca_crt: Optional[str] = None ca_crt: Optional[str] = None
default_template: Optional[str] = None
templates: dict[str, str] = dataclasses.field(default_factory=dict)
def check_integrity(self)->bool: def check_integrity(self)->bool:
""" Check the integrity of the config. """ Check the integrity of the config.

View file

@ -5,14 +5,16 @@ Format the alert message.
import asyncio import asyncio
import dataclasses import dataclasses
import json import json
from collections import defaultdict
from typing import ( from typing import (
Any, Any,
NoReturn, NoReturn,
Optional Optional
) )
from jinja2 import Environment, BaseLoader from jinja2 import BaseLoader, Environment, Template
from .config import Config
template_raw = ( default_template_raw = (
"{% if alert['labels']['severity'] == 'critical' and alert['status'] == 'firing' %}" "{% if alert['labels']['severity'] == 'critical' and alert['status'] == 'firing' %}"
"@room<b><font color='red'>" "@room<b><font color='red'>"
"{% elif alert['labels']['severity'] == 'warning' and alert['status'] == 'firing' %}" "{% elif alert['labels']['severity'] == 'warning' and alert['status'] == 'firing' %}"
@ -27,24 +29,58 @@ template_raw = (
"{{ alert['annotations']['title'] }}<br/>" "{{ alert['annotations']['title'] }}<br/>"
"{{ alert['annotations']['description'] }}<br/>" "{{ alert['annotations']['description'] }}<br/>"
) )
template = Environment(loader=BaseLoader).from_string(template_raw)
@dataclasses.dataclass @dataclasses.dataclass
class Message: class Message:
body: str body: str
formated_body: Optional[str] formated_body: Optional[str]
def load_templates(
config: Config
)->defaultdict[str, Template]:
"""
Create a dict mapping alert names to the template to use from
the config, with a default template either from the config file
or the default default_template_raw.
"""
if config.default_template is None:
default_template = Environment(
loader=BaseLoader
).from_string(
default_template_raw
)
else:
default_template = Environment(
loader=BaseLoader
).from_string(
config.default_template
)
templates = defaultdict(lambda: default_template)
for alert_name in config.templates:
templates[alert_name] = Environment(
loader=BaseLoader
).from_string(
config.templates[alert_name]
)
return templates
def format_alert( def format_alert(
alert_json: dict[str, Any] alert_json: dict[str, Any],
templates: defaultdict[str, Template]
)->list[Message]: )->list[Message]:
""" """
Format an alert in json format to a nice string. Format an alert in json format to a nice string.
""" """
messages = [] messages = []
for alert in alert_json['alerts']: for alert in alert_json['alerts']:
formated_body = template.render(alert=alert, status=alert_json['status']) template = templates[alert['labels']['alertname']]
body = f"{ alert['annotations']['title'] }:\n{ alert['annotations']['description'] }" formated_body = template.render(alert=alert)
body = "alert {status}:\n{alertname} on {instance}".format(
status=alert['status'],
alertname=alert['labels']['alertname'],
instance=alert['labels']['instance']
)
if '@room' in formated_body: if '@room' in formated_body:
body = '@room ' + body body = '@room ' + body
formated_body = formated_body.replace('@room', '') formated_body = formated_body.replace('@room', '')
@ -53,14 +89,17 @@ def format_alert(
async def format_alerts( async def format_alerts(
alert_queue: asyncio.Queue[dict[str,Any]], alert_queue: asyncio.Queue[dict[str,Any]],
message_queue: asyncio.Queue[Message] message_queue: asyncio.Queue[Message],
config: Config
)->NoReturn: )->NoReturn:
""" """
Read alerts from alert_queue, format them, and put them in message_queue. Read alerts from alert_queue, format them, and put them in message_queue.
""" """
templates = load_templates(config)
while True: while True:
alert = await alert_queue.get() alert = await alert_queue.get()
messages = format_alert(alert) messages = format_alert(alert, templates)
for message in messages: for message in messages:
await message_queue.put(message) await message_queue.put(message)
alert_queue.task_done() alert_queue.task_done()