forked from freak12techno/grafana-interacter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
alert_show.go
42 lines (32 loc) · 955 Bytes
/
alert_show.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package main
import (
"fmt"
"strings"
tele "gopkg.in/telebot.v3"
)
func HandleSingleAlert(c tele.Context) error {
log.Info().
Str("sender", c.Sender().Username).
Str("text", c.Text()).
Msg("Got single alert query")
args := strings.SplitN(c.Text(), " ", 2)
_, args = args[0], args[1:] // removing first argument as it's always /alert
if len(args) != 1 {
return c.Reply("Usage: /alert <alert name>")
}
rules, err := Grafana.GetAllAlertingRules()
if err != nil {
return c.Reply(fmt.Sprintf("Error querying alerts: %s", err))
}
rule, found := FindAlertRuleByName(rules, args[0])
if !found {
return c.Reply("Could not find alert. See /alert for alerting rules.")
}
var sb strings.Builder
sb.WriteString(fmt.Sprintf("<strong>Alert rule: </strong>%s\n", rule.Name))
sb.WriteString("<strong>Alerts: </strong>\n")
for _, alert := range rule.Alerts {
sb.WriteString(alert.Serialize())
}
return BotReply(c, sb.String())
}