Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Customizing Alertmanager Notifications for Telegram #4076

Open
basharmadani opened this issue Oct 20, 2024 · 2 comments
Open

Customizing Alertmanager Notifications for Telegram #4076

basharmadani opened this issue Oct 20, 2024 · 2 comments

Comments

@basharmadani
Copy link

Hi everyone! 👋

I'm setting up custom notifications in Alertmanager for a Telegram receiver, and I want to send different messages when an alert is FIRING and when it is RESOLVED.

The issue I’m facing is that Alertmanager keeps repeating the FIRING message even after the issue is resolved. I want to ensure that only the RESOLVED message is sent when the problem is fixed.

Here’s my current configuration:

global:
  resolve_timeout: 5m

route:
  receiver: telegram_receiver
  group_by: ["alertname", "Host"]
  group_wait: 1s
  group_interval: 1s
  repeat_interval: 24h

  routes:
  - receiver: 'telegram_receiver'
    matchers:
    - severity="Critical"

receivers:
- name: 'telegram_receiver'
  telegram_configs:
  - api_url: 'https://api.telegram.org'
    send_resolved: true
    bot_token: xxxxx
    chat_id: ttttttttttttt
    message: '{{ range .Alerts }}Alert: {{ printf "%s\n" .Labels.alertname }}{{ printf "%s\n" .Annotations.summary }}{{ printf "%s\n" .Annotations.description }}{{ end }}'
    parse_mode: 'HTML'

Does anyone have examples or best practices to share?

@grobinson-grafana
Copy link
Contributor

For examples and best practices, it's better to ask in the Prometheus Users mailing list. GitHub issues are for bug reports and feature requests.

@SckyzO
Copy link

SckyzO commented Oct 23, 2024

Hello, currently, I use an old code to send alerts to Telegram, but it can be improve :

{{ define "telegram.message" }}

{{ if eq .Status "firing" }}🔥 {{ end }}{{ if eq .Status "resolved" }}✅ {{ end }}<b>[{{ .Status | toUpper }}] {{ .CommonLabels.alertname }} on {{ .CommonLabels.instance }}</b>
{{ range .Alerts }}
{{$severity := .Labels.severity -}}
{{if eq $severity "warning" -}}
<b>Severity: </b> {{$severity}} ⚠️
{{else if eq $severity "critical" -}}
<b>Severity: </b> {{$severity}} 🚨
{{else -}}
<b>Severity: </b> {{$severity}} 😱
{{ end -}}
{{$status := .Status -}}
{{ if eq $status "firing" -}}
<b>Date/Time: </b> {{ .StartsAt.Format "Mon 02 Jan 15:04:05 MST 2006" }}
{{else if eq $status "resolved" -}}
<b>Date/Time: </b> {{ .EndsAt.Format "Mon 02 Jan 15:04:05 MST 2006" }}
{{ end -}}
<b>Alert: </b> {{ .Annotations.title }}
<b>Description: </b> {{ .Annotations.description }}
<b>Details: </b>
  {{ range .Labels.SortedPairs }} - {{ .Name | title }}: {{ .Value }}
  {{ end }}
{{ end }}
{{ end }}

Generate alerts like :

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants