forked from nopp/alertmanager-webhook-telegram-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
125 lines (104 loc) · 2.72 KB
/
main.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package main
import (
"encoding/json"
"flag"
"log"
"net/http"
"os"
"strconv"
"time"
botapi "github.com/go-telegram-bot-api/telegram-bot-api"
"github.com/gorilla/mux"
)
var (
BotToken string
ChatID int64
addressListen string
)
const (
timeDateFormat = "2006-01-02 15:04:05"
)
func main() {
s := os.Getenv("CHAT_ID")
if s == "" {
log.Fatal("Empty env CHAT_ID")
}
n, err := strconv.ParseInt(s, 10, 64)
if err != nil {
log.Fatal(err)
}
ChatID = n
BotToken = os.Getenv("BOT_TOKEN")
if BotToken == "" {
log.Fatal("Empty env BOT_TOKEN")
}
flag.StringVar(&addressListen, "l", ":8080", "Listen adderss")
flag.Parse()
router := mux.NewRouter()
router.HandleFunc("/alert", ToTelegram).Methods("POST")
log.Println("Listen", addressListen)
log.Fatal(http.ListenAndServe(addressListen, router))
}
type alertmanagerAlert struct {
Receiver string `json:"receiver"`
Status string `json:"status"`
Alerts []struct {
Status string `json:"status"`
Labels struct {
Name string `json:"name"`
Instance string `json:"instance"`
Alertname string `json:"alertname"`
Service string `json:"service"`
Severity string `json:"severity"`
} `json:"labels"`
Annotations struct {
Info string `json:"info"`
Description string `json:"description"`
Summary string `json:"summary"`
} `json:"annotations"`
StartsAt time.Time `json:"startsAt"`
EndsAt time.Time `json:"endsAt"`
GeneratorURL string `json:"generatorURL"`
Fingerprint string `json:"fingerprint"`
} `json:"alerts"`
GroupLabels struct {
Alertname string `json:"alertname"`
} `json:"groupLabels"`
CommonLabels struct {
Alertname string `json:"alertname"`
Service string `json:"service"`
Severity string `json:"severity"`
} `json:"commonLabels"`
CommonAnnotations struct {
Summary string `json:"summary"`
} `json:"commonAnnotations"`
ExternalURL string `json:"externalURL"`
Version string `json:"version"`
GroupKey string `json:"groupKey"`
}
// ToTelegram function responsible to send msg to telegram
func ToTelegram(w http.ResponseWriter, r *http.Request) {
var alerts alertmanagerAlert
bot, err := botapi.NewBotAPI(BotToken)
if err != nil {
log.Panic(err)
}
_ = json.NewDecoder(r.Body).Decode(&alerts)
for _, alert := range alerts.Alerts {
var status string
switch alert.Status {
case "firing":
status = "🔥 " + alert.Labels.Alertname
case "resolved":
status = "✅ " + alert.Labels.Alertname
}
telegramMsg := status + "\n"
if alert.Annotations.Description != "" {
telegramMsg += alert.Annotations.Description + "\n"
}
msg := botapi.NewMessage(-ChatID, telegramMsg)
bot.Send(msg)
}
log.Println(alerts)
json.NewEncoder(w).Encode(alerts)
}