Skip to content

Commit

Permalink
feat: add /start command
Browse files Browse the repository at this point in the history
  • Loading branch information
freak12techno committed Jul 8, 2023
1 parent 4e4c366 commit e0f8c8a
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 1 deletion.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ grafana-interacter is a tool to interact with your Grafana instance via a Telegr
- `/datasources` - will return Grafana datasources.
- `/alerts` - will list both Grafana alerts and Prometheus alerts from all Prometheus datasources, if any
- `/firing` - will list firing and pending alerts from both Grafana and Prometheus datasources, along with their details
- `/silence <duration> <params>` - creates a silence for Grafana alert. You need to pass a duration (like `/silence 2h test alert`) and some params for matching alerts to silence. You may use `=` for matching the value exactly (example: `/silence 2h host=localhost`), `!=` for matching everything except this value (example: `/silence 2h host!=localhost`), `=~` for matching everything that matches the regexp (example: `/silence 2h host=~local`), , `!~` for matching everything that doesn't the regexp (example: `/silence 2h host!~local`), or just provide a string that will be treated as an alert name (example: `/silence 2h test alert`).
- `/silence <duration> <params>` - creates a silence for Grafana alert. You need to pass a duration (like `/silence 2h test alert`) and some params for matching alerts to silence. You may use `=` for matching the value exactly (example: `/silence 2h host=localhost`), `!=` for matching everything except this value (example: `/silence 2h host!=localhost`), `=~` for matching everything that matches the regexp (example: `/silence 2h host=~local`), , `!~` for matching everything that doesn't match the regexp (example: `/silence 2h host!~local`), or just provide a string that will be treated as an alert name (example: `/silence 2h test alert`).
- `/silences` - list silences (both active and expired).
- `/unsilence <silence ID>` - deletes a silence.
- `/alertmanager_silences` - same as `/silences`, but using external Alertmanager.
Expand All @@ -24,6 +24,8 @@ Prerequisite: You need Grafana itself with new alerting enabled, as well as the
Before starting, you need to create a Telegram bot. Go to @Botfather at Telegram and create a new bot there. For bot commands, put the following:

```
start - Display help message
help - Display help message
render - Render a panel
dashboards - List dashboards
dashboard - See dashboard and its panels
Expand Down
2 changes: 2 additions & 0 deletions pkg/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ func NewApp(config *config.Config) *App {
}

func (a *App) Start() {
a.Bot.Handle("/start", a.HandleHelp)
a.Bot.Handle("/help", a.HandleHelp)
a.Bot.Handle("/dashboards", a.HandleListDashboards)
a.Bot.Handle("/dashboard", a.HandleShowDashboard)
a.Bot.Handle("/render", a.HandleRenderPanel)
Expand Down
26 changes: 26 additions & 0 deletions pkg/app/help.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package app

import (
"fmt"

Check failure on line 4 in pkg/app/help.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofumpt`-ed (gofumpt)
tele "gopkg.in/telebot.v3"

Check failure on line 5 in pkg/app/help.go

View workflow job for this annotation

GitHub Actions / lint

File is not `goimports`-ed (goimports)
"main/pkg/types/render"

Check failure on line 6 in pkg/app/help.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofumpt`-ed (gofumpt)
)

func (a *App) HandleHelp(c tele.Context) error {
a.Logger.Info().
Str("sender", c.Sender().Username).
Str("text", c.Text()).
Msg("Got help query")

template, err := a.TemplateManager.Render("help", render.RenderStruct{
Grafana: a.Grafana,
Alertmanager: a.Alertmanager,
Data: nil,
})
if err != nil {
a.Logger.Error().Err(err).Msg("Error rendering help template")
return c.Reply(fmt.Sprintf("Error rendering template: %s", err))
}

return a.BotReply(c, template)
}
19 changes: 19 additions & 0 deletions templates/help.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<a href="https://github.com/freak12techno/grafana-interacter">grafana-interacter</a>
A Telegram bot that allows you to interact with your Grafana, Prometheus and Alertmanager instances.
Can understand the following commands:

- /help, or /start - displays this message
- /render [opts] panelname - renders the panel and sends it as image. If there are multiple panels with the same name (for example, you have a 'dashboard1' and 'dashboard2' both containing panel with name 'panel'), it will render the first panel it will find. For specifying it, you may add the dashboard name as a prefix to your query (like <code>/render dashboard1 panel</code>). You can also provide options in a 'key=value' format, which will be internally passed to a <code>/render</code> query to Grafana. Some examples are 'from', 'to', 'width', 'height' (the command would look something like <code>/render from=now-14d to=now-7d width=100 height=100 dashboard1 panel</code>). By default, the params are: <code>width=1000&height=500&from=now-30m&to=now&tz=Europe/Moscow</code>.
- /dashboards - will list Grafana dashboards and links to them.
- /dashboard [name] - will return a link to a dashboard and its panels.
- /datasources - will return Grafana datasources.
- /alerts - will list both Grafana alerts and Prometheus alerts from all Prometheus datasources, if any
- /firing - will list firing and pending alerts from both Grafana and Prometheus datasources, along with their details
- /silence [duration] [params] - creates a silence for Grafana alert. You need to pass a duration (like <code>/silence 2h test alert</code>) and some params for matching alerts to silence. You may use '=' for matching the value exactly (example: <code>/silence 2h host=localhost</code>), '!=' for matching everything except this value (example: <code>/silence 2h host!=localhost</code>), '=~' for matching everything that matches the regexp (example: <code>/silence 2h host=~local</code>), '!~' for matching everything that doesn't match the regexp (example: <code>/silence 2h host!~local</code>), or just provide a string that will be treated as an alert name (example: <code>/silence 2h test alert</code>).
- /silences - list silences (both active and expired).
- /unsilence [silence ID] - deletes a silence.
- /alertmanager_silences - same as /silences, but using external Alertmanager.
- /alertmanager_silence - same as /silence, but using external Alertmanager.
- /alertmanager_unsilence - same as /unsilence, but using external Alertmanager.

Created by <a href="https://github.com/freak12techno">freak12techno</a> with ❤️.

0 comments on commit e0f8c8a

Please sign in to comment.