Sending Telegram Alerts in Grafana
Let's understand how Grafana sends alerts in Telegram by understanding this flow.
Prerequisites
Before setting up Telegram alerts, ensure the following:
- You have administrator access to your Grafana instance.
- You have a Telegram account and can create bots using the BotFather.
Step 1: Create a Telegram Bot
- Open Telegram and search for
BotFather. - Start a conversation with
BotFatherand send the command/newbot. - Follow the prompts to:
- Name your bot.
- Set a unique username for your bot (e.g.,
MyGrafanaBot).
- After completing the setup, you’ll receive an API Token (e.g.,
123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11).
Step 2: Find Your Chat ID
- Search for your bot in Telegram and start a conversation with it.
- Send any message to the bot (e.g., "Hello").
- Use a tool like
curlor a browser to get the chat ID:- Replace
<API_TOKEN>with your bot's API token:curl https://api.telegram.org/bot<API_TOKEN>/getUpdates - Look for
"chat":{"id":<chat_id>}in the response. - Note down the
<chat_id>.
- Replace
Step 3: Configure Telegram Notification Channel in Grafana
- Log in to your Grafana instance.
- Navigate to Alerting > Notification channels.
- Click New Notification Channel and choose Telegram.
- Fill in the details:
- Name: Provide a name (e.g.,
Telegram Alerts). - Bot Token: Paste the API token from Step 1.
- Chat ID: Enter the chat ID from Step 2.
- Message: Customize the message template, if needed.
- Name: Provide a name (e.g.,
- Click Send Test to verify the configuration.
- Save the notification channel.
Step 4: Create an Alert Rule
- Go to a Grafana dashboard and click Edit for a panel.
- Switch to the Alert tab and click Create Alert.
- Define the alert conditions:
- Set the metric thresholds, evaluation frequency, and duration.
- Add a notification:
- Select the Telegram notification channel configured earlier.
- Save the changes.
Testing the Telegram Alert
- Trigger the alert by ensuring the condition is met (e.g., CPU usage exceeding 80%).
- Verify that the alert message is sent to your Telegram chat.