Skip to content
This repository has been archived by the owner on Jul 25, 2024. It is now read-only.
/ grafana-tg-bot Public archive

A proof-of-concept Telegram bot for sending Grafana alerts

License

Notifications You must be signed in to change notification settings

crt0r/grafana-tg-bot

Repository files navigation

Send Webhook-based Grafana Alerts via Telegram

Warning
This is an experimental prototype, a proof of concept. Don’t use in production. A lot of things don’t work as expected, and extensive testing is still needed.
Note
The webhook endpoint doesn’t support auth and encryption. If you need HTTPS and auth, use a reverse proxy.
Prerequisites
Note
If you are planning to run it in Docker containers, ignore this list. See Dockerfile and compose.yml.
  • Node.js latest

  • Redis-compatible in-memory database (I prefer Valkey)

  • pnpm

Features

  • Access control based on Telegram UIDs

  • Multiple chats can be subscribed simultaneously

  • Both private chats and group chats can be subscribed to alerts

    Note
    The user interacting with the bot in a group chat must be added to the ACL, but not the group chat itself.
  • Unauthorized users are ignored

  • Arbitrary messages and unrecognized commands are ignored

  • Unauthorized access to valid bot commands is logged

  • Grafana interacts with the bot via a webhook endpoint

  • Style messages with Telegram-supported HTML tags (the bot doesn’t support Markdown)

Demo

Suppose, you use ClickHouse as a data warehouse for your logs, and it’s connected to your Grafana instance as a data source.

You have created an alert rule with the following SQL query:

Screenshot 2024 06 18 095726 101238

And annotations look like the following:

Screenshot 2024 06 18 100320 101234

When the alert rule is triggered, all subscribed Telegram chats will get messages similar to this:

Screenshot 20240617 161126
Another Example for Sudo Alerts
Screenshot 20240617 161101
Example Video

Build From Source

$ pnpm build

Configure

  1. Copy the example configuration file botconfig.toml to a new directory ./config.

  2. Edit config.

Run

Note
Don’t run using pnpm. It breaks signal handling.
$ node build/main.js

Environment

You can set environment variables to change the program’s behavior.

Variable Used for Default

BOTCONFIG

Change config file path.

`${getProjectRoot()}/config/botconfig.toml`

Reload Config

To reload configuration, send the SIGHUP signal to the process.

Example 1

$ pnpm reload

Example 2

$ kill -HUP <PID here> # You can use something like $(pidof node) if there's only one Node.js process.

Connect Grafana

  1. Create a webhook contact point pointing to your bot instance

  2. Make sure it uses the POST method

contact point

Sub/unsub to Alerts

Subscribe

Group Chat

Add the bot to a chat just like a regular user, and issue the /start@bot_name command.

Personal Chat

Open a chat with the bot, and press the start button.

Unsubscribe

Same as above, but the command is /stop@bot_name or /stop depending on the chat type.

License

To Do

  • Catch Grammy errors

  • Make queues reliable

  • Send out messages with time represented in a local timezone instead of UTC

  • Improve cache error handling, fix infinite frequent retries on wrong user/password

  • Improve logging, revert cache connect and disconnect logging

  • Improve config validation

About

A proof-of-concept Telegram bot for sending Grafana alerts

Resources

License

Stars

Watchers

Forks

Packages

No packages published