# Шаблоны сообщений
## Структура шаблонов
Шаблоны сообщений находятся в папке `templates/` и используют синтаксис Jinja2. Шаблоны разделены на две категории:
1. **Шаблоны для мессенджеров** - форматирование сообщений для отправки в Telegram, MAX/VK
2. **Шаблоны для Jira** - форматирование описания тикетов в Jira
## Шаблоны для мессенджеров
### Grafana (`templates/grafana.tmpl`)
Шаблон для форматирования алертов из Grafana:
```jinja2
{{ alert_icon }} {{ title }}
{% if status_name is defined %}{{ status_icon }} Критичность: {{ status_name }}{% endif %}
Подробнее:
{{ message }}
{% if labels %}
👉 Переменные:
{{ labels }}{% endif %}
{% if tags and state !='ok' %}💻 Ответственные:
{{ tags }}{% endif %}
```
**Доступные переменные:**
- `alert_icon` - иконка алерта (🔴, 🟢, 🟡, 🟠, 🔵)
- `status_icon` - иконка статуса (💀, ✅, 🐢, 🤷, 🥴)
- `status_name` - название статуса
- `title` - заголовок алерта
- `message` - сообщение алерта
- `state` - состояние алерта (alerting, ok, paused, pending, no_data)
- `labels` - метрики (evalMatches)
- `tags` - теги алерта
### Zabbix (`templates/zabbix.tmpl`)
Шаблон для форматирования алертов из Zabbix:
```jinja2
{{ alert_icon }} {{ title }}
{% if status_name is defined %}{{ status_icon }} Критичность: {{ status_name }} ({{ state }}){% endif %}
Кратко:
{{ subject }}
Подробнее:
{{ message }}
👉 Значение:
{{ message_data }}
🌐 Сеть:
Хост: {{ label_host }}
IP: {{ label_ip }}
Порт: {{ label_port }}
```
**Доступные переменные:**
- `alert_icon` - иконка алерта
- `status_icon` - иконка статуса
- `status_name` - название статуса
- `title` - название события
- `subject` - тема алерта
- `message` - сообщение алерта
- `message_data` - дополнительные данные события
- `label_host` - имя хоста
- `label_ip` - IP адрес хоста
- `label_port` - порт хоста
### AlertManager (`templates/alertmanager.tmpl`)
Шаблон для форматирования алертов из AlertManager:
```jinja2
{{ alert_icon }} {{ alertname }}
{% if summary is defined %}{{ summary }}{% endif %}
{% if status_name is defined %}{{ status_icon }} Критичность: {{ status_name }}{% endif %}{% if description is defined %}
Подробнее:
{{ description }}{% endif %}{% if another_annotations != "" %}
{{ another_annotations }}{% endif %}
{% if clustername is defined %}👉 Kubernetes:
Кластер: {{ clustername }}{% if node is defined %}
Нода: {{ node }}{% endif %}{% if ns is defined %}
Неймспейс: {{ ns }}{% endif %}{% if deployment is defined %}
Деплоймент: {{ deployment }}{% endif %}{% if daemonset is defined %}
Демонсет: {{ daemonset }}{% endif %}{% if replicaset is defined %}
Репликасет: {{ replicaset }}{% endif %}{% if statefulset is defined %}
Стейтфулсет: {{ statefulset }}{% endif %}{% if container is defined %}
Контейнер: {{ container }}{% endif %}{% if pod is defined %}
Под: {{ pod }}{% endif %}{% if persistentvolumeclaim is defined %}
PVC: {{ persistentvolumeclaim }}{% endif %}{% if job_name is defined %}
Имя джобы: {{ job_name }}{% endif %}{% if job is defined %}
Джоба: {{ job }}{% endif %}{% if reason is defined %}
Причина: {{ reason }}{% endif %}{% if endpoint is defined %}
Эндпоинт: {{ endpoint }}{% endif %}{% if instance is defined %}
Инстанс: {{ instance }}{% endif %}{% if condition is defined %}
Состояние: {{ condition }}{% endif %}{% if reason is defined %}
Причина: {{ reason }}{% endif %}{% endif %}
{% if prometheus is defined %}🔍 Прометей:
Сервер: {{ prometheus }}{% if service is defined %}
Сервис: {{ service }}{% endif %}{% if metrics_path is defined %}
Метрики: {{ metrics_path }}{% endif %}{% if uid is defined %}
UID: {{ uid }}{% endif %}{% if integration is defined %}
Integration: {{ integration }}{% endif %}{% if To is defined %}
To: {{ integration }}{% endif %}{% endif %}
{% if another_labels != "" %}
🤷 Разное:
{{ another_labels }}{% endif %}
```
**Доступные переменные:**
- `alert_icon` - иконка алерта
- `status_icon` - иконка статуса
- `status_name` - название статуса
- `alertname` - название алерта
- `summary` - краткое описание
- `description` - подробное описание
- `another_annotations` - дополнительные аннотации
- `clustername` - имя кластера Kubernetes
- `node` - имя ноды
- `ns` - namespace
- `deployment` - имя deployment
- `pod` - имя pod
- `container` - имя контейнера
- И другие метки из `commonLabels`
## Шаблоны для Jira
### Общий шаблон (`templates/jira_common.tmpl`)
Шаблон для форматирования описания тикетов в Jira:
```jinja2
**Источник:** {{ source }}
{% if k8s_cluster %}**Kubernetes кластер:** {{ k8s_cluster }}{% endif %}
---
## Детали алерта
{% if source == "alertmanager" %}
**Статус:** {{ status }}
**Название:** {{ alertname }}
**Критичность:** {{ severity }}
{% if summary %}**Краткое описание:**
{{ summary }}
{% endif %}
{% if description %}**Подробное описание:**
{{ description }}
{% endif %}
**Метки:**
{% for key, value in common_labels.items() %}
- *{{ key }}*: {{ value }}
{% endfor %}
**Аннотации:**
{% for key, value in common_annotations.items() %}
- *{{ key }}*: {{ value }}
{% endfor %}
{% endif %}
{% if source == "grafana" %}
**Состояние:** {{ state }}
**Правило:** {{ ruleName }}
**Заголовок:** {{ title }}
{% if message %}**Сообщение:**
{{ message }}
{% endif %}
{% if tags %}**Теги:**
{% for key, value in tags.items() %}
- *{{ key }}*: {{ value }}
{% endfor %}
{% endif %}
{% if evalMatches %}**Метрики:**
{% for match in evalMatches %}
- *{{ match.metric }}*: {{ match.value }}
{% endfor %}
{% endif %}
{% endif %}
{% if source == "zabbix" %}
**Статус:** {{ status }}
**Серьезность:** {{ event_severity }}
**Событие:** {{ event_name }}
**Хост:** {{ host_name }} ({{ host_ip }}:{{ host_port }})
**Тема:**
{{ alert_subject }}
**Сообщение:**
{{ alert_message }}
{% endif %}
---
*Тикет создан автоматически системой мониторинга*
```
**Примечания:**
- Шаблон поддерживает условное форматирование в зависимости от источника алерта
- Для каждого источника доступны свои переменные
- Можно создать отдельные шаблоны для каждого источника: `jira_alertmanager.tmpl`, `jira_grafana.tmpl`, `jira_zabbix.tmpl`