prometheus / alertmanager

Prometheus Alertmanager
https://prometheus.io
Apache License 2.0
6.68k stars 2.16k forks source link

Discord webhook : Cannot send an empty message #3923

Closed lazbord closed 3 months ago

lazbord commented 4 months ago

What did you do?

I tried to triggered my discord webhook when an alert is firing. I tested my discord webhook with curl and it work just fine :

curl -X POST -H "Content-Type: application/json" -d '{"content": "Another test!"}' https://discord.com/api/webhooks/discordwebhooktoken image

Prometheus and Alermanager do recognize the alert :

image

image

Everything seemed to work but i don't have any messages in my discord channel when an alert is firing.

Here is the log from alertmanager :

`Jul 19 12:54:37 CT-Monitoring alertmanager[3075]: ts=2024-07-19T12:54:37.265Z caller=dispatch.go:353 level=error component=dispatcher msg="Notify for alerts failed" num_alerts=1 err="discord_webhook/webhook[0]: notify retry canceled due to unrecoverable error after 1 attempts: unexpected status code 400: https://discordapp.com/api/webhooks/mywebhooktoken: {\"message\": \"Cannot send an empty message\", \"code\": 50006}"`

I have tried to resolve this issue by specifying template that specifies a message but it doesn't change a thing.

I'm a newbie so maybe I miss something out, but I believe I have done everything correctly...

route:
  group_by: ['alertname']
  group_wait: 30s
  group_interval: 5m
  repeat_interval: 1h
  receiver: 'discord_webhook'
receivers:
  - name: 'discord_webhook'
    webhook_configs:
      - url: 'https://discordapp.com/api/webhooks/1247133913369940038/discordwebhooktoken'
global:
  scrape_interval: 15s                                                                          
  evaluation_interval: 15s                                                                  

rule_files:
  - "alert_rules.yml"

alerting:
  alertmanagers:
    - static_configs:
        - targets:
            - 'localhost:9093'

*alert-rules.yml :

groups:
- name: alerts
  rules:
  - alert: NodeExporterDown
    expr: up == 0
    for: 5s
    labels:
      severity: critical
    annotations:
      summary: "NodeExporter on {{ $labels.instance }} is down"
      description: "NodeExporter on {{ $labels.instance }} has been down for more than 5 seconds."
Mohamed-Mostafa69 commented 3 months ago

How did you made it works

lazbord commented 3 months ago

In Alertmanager config file, you have to group by alertname AND job, like so :

route:
  group_by: ['alertname','job']
  group_wait: 30s
  group_interval: 5m
  repeat_interval: 1h
  receiver: 'discord_webhook'
receivers:
  - name: 'discord_webhook'
    webhook_configs:
      - url: 'https://discordapp.com/api/webhooks/1247133913369940038/discordwebhooktoken'
Mohamed-Mostafa69 commented 3 months ago

are you sure, bec. i did have the same config as you mentioned but didn't work for me however the solution for me was running 1 more container for the discord integration for more.

https://github.com/evanugarte/prometheus-monitoring-tutorial https://github.com/benjojo/alertmanager-discord

discord-alerts:
    image: benjojo/alertmanager-discord
    container_name: discord-alerts
    restart: unless-stopped
    environment:
      DISCORD_WEBHOOK: <discord_hook>
    ports:
      - "9094:9094"
lazbord commented 3 months ago

Honestly, since the problem was resolved, I haven't touched a single config file. Good thing if your solution works!