Answer the question
In order to leave comments, you need to log in
How to pass text with variables from alertmanager via web-hook?
From Prometheus, an alert of the form flies:
- name: alert.rules
rules:
- alert: InstanceDown
expr: up == 0
for: 1m
labels:
severity: critical
annotations:
description: 'Недоступен сервер {{ $labels.instance }} или сервис {{ $labels.job }} более чем 1 минуту'
summary: Instance {{ $labels.instance }} down
receivers:
- name: 'webhook'
webhook_configs:
- url: 'https://api.telegram.org/bot[ТОКЕН_БОТА]/[email protected][USERNAME_КАНАЛА]&text=тест'
- url: 'https://api.telegram.org/bot[ТОКЕН_БОТА]/[email protected][USERNAME_КАНАЛА]&text=Упал сервер {{ $labels.instance }}'
Answer the question
In order to leave comments, you need to log in
Long communication with colleagues at work and questions in the devops gelochannel helped resolve the issue.
The funny thing is that for almost everyone, the webhook is sent to the telegrambot intermediary program, which already sends the SAME webhook to the telegram API. To the question, why not immediately directly send to telegrams? They answer one thing - they didn’t manage to win initially, they decided to beat them up. Extra traffic, extra point of failure, extra risk somewhere along the way to lose something. Not our way. I show you how to do it right.
1. Create a rule in prometheus. The rule is simple, just for example
# cat /etc/prometheus/rules/alert.rules.yml
groups:
- name: alert.rules
rules:
- alert: InstanceDown
expr: up == 0
for: 0m
labels:
severity: critical
annotations:
description: "Server down {{ $labels.instance }} or service {{ $labels.job }}"
instancename: "{{ $labels.instance }}"
summary: "Instance down"
instancename: {{ $labels.instance }}
# cat /etc/alertmanager/alertmanager.yml
global:
# Отсюда читаем все шаблоны:
- '/etc/alertmanager/templates/*.tmpl'
route:
# Группировка алертов
group_by: ['alertname', 'cluster', 'service']
# время ожидания перед отправкой уведомления для группы
group_wait: 10s
# время отправки повторного сообщения для группы
group_interval: 10s
# время до отправки повторного сообщения
repeat_interval: 10s
receiver: 'telega'
receivers:
- name: 'telega'
telegram_configs:
- bot_token: '<Здесь указываем ваш bot_token>'
api_url: 'https://api.telegram.org'
chat_id: <Здесь указываем ваш chat_id <b>БЕЗ КАВЫЧЕК!!!</b>>
message: "Alertname: {{ .GroupLabels.alertname }}\n Severity: {{ .CommonLabels.severity }}\n {{ range .Alerts }}{{ .Annotations.description }}\n{{ end }}"
<b> parse_mode: ''</b>
monitoring_bot, [02.04.2022 14:38]
Alertname: InstanceDown
Severity: critical
Server down 192.168.0.210:8300 or service Consul SD 192.168.0.211
annotations:
description: "Server down {{ $labels.instance }} or service {{ $labels.job }}"
instancename: "{{ $labels.instance }}"
summary: "Instance down"
aaaaaaaaa: 'XXXXXXXX'
bbbbbbbb: 'XXXXXXXX'
cccccccccc: 'XXXXXXXX'
{{ range .Alerts }}{{ .Annotations.aaaaaaaaa }}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question