Solved: Real-time Server Health Monitoring: Grafana to Telegram Integration
Source: Dev.to
Why This Matters
As a DevOps Engineer you know the drill: constant vigilance, endless email alerts, complex dashboards, and the occasional āuserāreportedā incident. This reactive approach:
- Increases downtime and damages reputation.
- Generates alert fatigue.
- Wastes budget on SaaS features you may never use.
Imagine receiving critical serverāhealth alerts instantly on your phone via a familiar messaging app. This tutorial walks you through building that exact workflow.
Prerequisites
- Grafana ā operational instance (v7.0āÆor newer) with dashboards & data sources configured (e.g., Prometheus, Node Exporter).
- Telegram ā account with the desktop or mobile app installed.
- Telegram Bot ā ability to create/manage bots via BotFather.
- CLI access ā a server (or local machine) where you can run
curl. - Basic serverāadmin knowledge and familiarity with Grafana alert rule creation.
StepāÆ1 ā Create a Dedicated Telegram Bot
| Action | Details |
|---|---|
| Start BotFather | Open Telegram, search for @BotFather, and start a chat. |
| Create a new bot | Send /newbot and follow the prompts. Choose a display name (e.g., TechResolve Alerts) and a username ending with bot (e.g., techresolve_alerts_bot). |
| Save the Bot Token | BotFather returns an HTTP API token like 123456789:AAHāu0vj2oāK0j5g_3x9t_1aB9āP9L0k7m. Store it securely ā Grafana will need it. |
| Initiate a chat with the bot | Search for your botās username, open a conversation, and send any message (e.g., āHelloā). This step is required because bots can only message chats they have interacted with. |
| Obtain the Chat ID | Use curl to fetch updates and extract the chat ID. Replace YOUR_BOT_TOKEN with the token you just saved: |
curl -s "https://api.telegram.org/botYOUR_BOT_TOKEN/getUpdates" \
| grep -oP '"chat":{"id":\K[^,]*'
The output will be one or more numeric IDs:
- Private chat ā your personal chat ID (positive number).
- Group chat ā group chat ID (usually negative, e.g.,
-123456789).
Copy the appropriate ID for the next step.
StepāÆ2 ā Configure Grafanaās Telegram Notification Channel
-
Open Notification Channels
- In Grafana, click the Alerting (bell) icon ā Notification channels.
-
Add a New Channel
- Click Add channel.
-
Fill in the Settings
| Field | Value / Notes |
|---|---|
| Name | Descriptive name, e.g., Telegram Alerts. |
| Type | Select Telegram from the dropdown. |
| Bot Token | Paste the token from StepāÆ1. |
| Chat ID | Paste the chat ID from StepāÆ1. |
| Optional | ⢠Send notification on resolved ā get a message when an issue clears. ⢠Include image ā attach a graph snapshot with the alert. |
| Message Template | (Optional) Use Go templating for custom messages ā see the āAdvanced Templatingā section later. |
-
Test the Channel
- Click Test at the bottom. You should receive a āTest notificationā in the Telegram chat.
- If you donāt, verify:
- Bot Token & Chat ID are correct.
- Grafana server can reach
api.telegram.org(outbound internet access).
-
Save the channel.
StepāÆ3 ā Create Alert Rules that Use the Telegram Channel
- Navigate to a Dashboard containing the metric you want to monitor (e.g., CPU usage, memory, disk I/O).
- Open the Panelās Alert Tab (or create a new panel).
- Define the Alert Condition (e.g., āwhen
node_cpu_seconds_total> 80āÆ% for 5āÆminutesā). - Add Notification ā select the Telegram Alerts channel you just created.
- Save the Dashboard ā the alert rule is now active and will push notifications to Telegram when the condition fires.
Advanced: Custom Telegram Message Templates (Go Templating)
Grafana lets you craft rich messages using Goās templating syntax. Example template:
{{ define "telegram.default.message" }}
šØ *{{ .Title }}* šØ
{{ if .State }}*State:* {{ .State }}{{ end }}
{{ if .Message }}*Message:* {{ .Message }}{{ end }}
*Labels:* {{ range $key, $value := .Labels }}{{ $key }}={{ $value }} {{ end }}
*Annotations:* {{ range $key, $value := .Annotations }}{{ $key }}={{ $value }} {{ end }}
{{ if .EvalMatches }}
*Metrics:* {{ range .EvalMatches }}
- {{ .Metric }} = {{ .Value }}{{ end }}
{{ end }}
[View Dashboard]({{ .RuleUrl }})
{{ end }}
- Place this in the Message Template field of the Telegram channel.
- Use
*bold*,_italic_, or`code`for Markdown formatting (Telegram supports a subset).
Troubleshooting Checklist
| Issue | Fix |
|---|---|
| Bot Token typo? | Reācopy from BotFather. |
| Wrong Chat ID? | Run the curl command again after sending a fresh message to the bot. |
| Grafana cannot reach Telegram? | Verify firewall/NAT rules allow outbound HTTPS to api.telegram.org. |
| No alerts firing? | Ensure the alert ruleās evaluation interval and condition thresholds are appropriate. |
| Message formatting broken? | Test a simple template first; then gradually add Go templating logic. |
Recap
By following these steps you now have:
- A Telegram bot that can receive messages.
- A Grafana notification channel wired to that bot.
- Alert rules that push realātime serverāhealth notifications straight to your mobile device.
Enjoy faster incident response, reduced alert fatigue, and a leaner monitoring stackāno costly SaaS subscriptions required. š
Setting Up Grafana Alerts for Server Health (Telegram Integration)
1. Edit a Panel
- Hover over the panel you want to alert on (e.g., a CPU usage graph).
- Click the Edit icon (pencil).
2. Access the Alert Tab
In the panel editor, click the Alert tab (bell icon).
3. Create an Alert Rule
- Click Create Alert.
- Fill in the required fields:
| Field | Description |
|---|---|
| Name | Give your alert a clear name (e.g., High CPU Usage ā Server X). |
| Evaluate every | How frequently Grafana checks the condition (e.g., 1m). |
| For | How long the condition must be true before the alert fires (e.g., 5m). |
4. Define Query and Conditions
Query
Select your data source and write the query that fetches the metric.
Example (Prometheus):
avg(node_cpu_seconds_total{mode="idle",instance="server-x:9100"}) by (instance)
You would then transform this to trigger when 100 - (AVG_IDLE_CPU_PERCENTAGE) > 80.
Conditions
Set the alert threshold.
WHEN last() OF A IS ABOVE 80
5. Configure Notification
- In the Send to section, select your Telegram Alerts channel from the dropdown.
- (Optional) Add a custom message for this alert ā it will override the channelās default template.
6. Save the Alert
Click Save at the topāright of the panel editor.
7. Repeat for Other Metrics
Create similar alerts for:
- Memory usage
- Disk space
- Network errors
Tailor thresholds and messages to your operational needs.
Using Go Templating for Rich Telegram Messages
Grafanaās alerting engine supports Go templating, allowing you to craft dynamic, contextārich notifications.
Example Message Template
Add the following to the Message Template field of your Telegram notification channel (or individual alert rules):
{{ range .Alerts }}
š„ ALERTING š„
{{ .Labels.alertname }} on {{ .Labels.instance }} is FIRING!
Severity: {{ .Labels.severity | default "critical" }}
Summary: {{ .Annotations.summary }}
Description: {{ .Annotations.description }}
Value: {{ .ValueString }}
Dashboard: {{ .DashboardURL }}
Panel: {{ .PanelURL }}
{{ end }}
Template Breakdown
{{ .Alerts }}ā slice of all alerts firing for the notification.{{ .Labels }}ā access labels from Prometheus metrics or Grafana alerts (e.g.,alertname,instance,severity).{{ .Annotations }}ā custom keyāvalue pairs defined in the alert rule (e.g.,summary,description).{{ .ValueString }}ā the actual value that triggered the alert.{{ .DashboardURL }}/{{ .PanelURL }}ā direct links back to Grafana for quick investigation.
Tip: Define annotations in the alert rule, e.g.:
annotations:
summary: "CPU utilization is too high"
description: "Investigate processes consuming excessive CPU. Consider scaling up or optimizing application."
Common Issues & Troubleshooting
| Issue | Description | Fix |
|---|---|---|
| Incorrect Bot Token or Chat ID | Bot token typo or wrong chat ID. | Verify the token exactly and ensure the chat ID belongs to a chat where the bot has already received a message. Add the bot to groups before using the groupās chat ID. |
| Grafana Network Connectivity | Grafana canāt reach api.telegram.org. | Check firewall, proxy, and DNS settings. Review Grafana logs for network errors. |
| Telegram API Rate Limits | Too many alerts per minute may trigger a temporary block. | Design alerts to be meaningful; avoid excessive firing frequency. |
| Alert Rule Logic Flaws | Wrong query or threshold causing missed or noisy alerts. | Doubleācheck the Prometheus query, evaluation period, and condition logic. |
Next Steps
- Alert Grouping & Silencing: Reduce noise by grouping related alerts or silencing during maintenance windows.
- OnāCall Integration: Connect Telegram alerts to Grafana OnCall, PagerDuty, or other escalation tools for 24/7 coverage.
- Advanced Templates: Enrich messages with additional context (e.g., runbooks, links to ticketing systems).
Youāve now integrated Grafana with Telegram, giving your team instant, actionable serverāhealth notifications without relying on email or costly SaaS solutions.