-
Notifications
You must be signed in to change notification settings - Fork 80
/
main.go
204 lines (170 loc) · 5.29 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package main
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"regexp"
"strings"
)
// Discord color values
const (
ColorRed = 0x992D22
ColorGreen = 0x2ECC71
ColorGrey = 0x95A5A6
)
type alertManAlert struct {
Annotations struct {
Description string `json:"description"`
Summary string `json:"summary"`
} `json:"annotations"`
EndsAt string `json:"endsAt"`
GeneratorURL string `json:"generatorURL"`
Labels map[string]string `json:"labels"`
StartsAt string `json:"startsAt"`
Status string `json:"status"`
}
type alertManOut struct {
Alerts []alertManAlert `json:"alerts"`
CommonAnnotations struct {
Summary string `json:"summary"`
} `json:"commonAnnotations"`
CommonLabels struct {
Alertname string `json:"alertname"`
} `json:"commonLabels"`
ExternalURL string `json:"externalURL"`
GroupKey string `json:"groupKey"`
GroupLabels struct {
Alertname string `json:"alertname"`
} `json:"groupLabels"`
Receiver string `json:"receiver"`
Status string `json:"status"`
Version string `json:"version"`
}
type discordOut struct {
Content string `json:"content"`
Embeds []discordEmbed `json:"embeds"`
}
type discordEmbed struct {
Title string `json:"title"`
Description string `json:"description"`
Color int `json:"color"`
Fields []discordEmbedField `json:"fields"`
}
type discordEmbedField struct {
Name string `json:"name"`
Value string `json:"value"`
}
const defaultListenAddress = "127.0.0.1:9094"
var (
whURL = flag.String("webhook.url", os.Getenv("DISCORD_WEBHOOK"), "Discord WebHook URL.")
listenAddress = flag.String("listen.address", os.Getenv("LISTEN_ADDRESS"), "Address:Port to listen on.")
)
func checkWhURL(whURL string) {
if whURL == "" {
log.Fatalf("Environment variable 'DISCORD_WEBHOOK' or CLI parameter 'webhook.url' not found.")
}
_, err := url.Parse(whURL)
if err != nil {
log.Fatalf("The Discord WebHook URL doesn't seem to be a valid URL.")
}
re := regexp.MustCompile(`https://discord(?:app)?.com/api/webhooks/[0-9]{18,19}/[a-zA-Z0-9_-]+`)
if ok := re.Match([]byte(whURL)); !ok {
log.Printf("The Discord WebHook URL doesn't seem to be valid.")
}
}
func sendWebhook(amo *alertManOut) {
groupedAlerts := make(map[string][]alertManAlert)
for _, alert := range amo.Alerts {
groupedAlerts[alert.Status] = append(groupedAlerts[alert.Status], alert)
}
for status, alerts := range groupedAlerts {
DO := discordOut{}
RichEmbed := discordEmbed{
Title: fmt.Sprintf("[%s:%d] %s", strings.ToUpper(status), len(alerts), amo.CommonLabels.Alertname),
Description: amo.CommonAnnotations.Summary,
Color: ColorGrey,
Fields: []discordEmbedField{},
}
if status == "firing" {
RichEmbed.Color = ColorRed
} else if status == "resolved" {
RichEmbed.Color = ColorGreen
}
if amo.CommonAnnotations.Summary != "" {
DO.Content = fmt.Sprintf(" === %s === \n", amo.CommonAnnotations.Summary)
}
for _, alert := range alerts {
realname := alert.Labels["instance"]
if strings.Contains(realname, "localhost") && alert.Labels["exported_instance"] != "" {
realname = alert.Labels["exported_instance"]
}
RichEmbed.Fields = append(RichEmbed.Fields, discordEmbedField{
Name: fmt.Sprintf("[%s]: %s on %s", strings.ToUpper(status), alert.Labels["alertname"], realname),
Value: alert.Annotations.Description,
})
}
DO.Embeds = []discordEmbed{RichEmbed}
DOD, _ := json.Marshal(DO)
http.Post(*whURL, "application/json", bytes.NewReader(DOD))
}
}
func sendRawPromAlertWarn() {
badString := `This program is suppose to be fed by alertmanager.` + "\n" +
`It is not a replacement for alertmanager, it is a ` + "\n" +
`webhook target for it. Please read the README.md ` + "\n" +
`for guidance on how to configure it for alertmanager` + "\n" +
`or https://prometheus.io/docs/alerting/latest/configuration/#webhook_config`
log.Print(`/!\ -- You have misconfigured this software -- /!\`)
log.Print(`--- -- -- ---`)
log.Print(badString)
DO := discordOut{
Content: "",
Embeds: []discordEmbed{
{
Title: "You have misconfigured this software",
Description: badString,
Color: ColorGrey,
Fields: []discordEmbedField{},
},
},
}
DOD, _ := json.Marshal(DO)
http.Post(*whURL, "application/json", bytes.NewReader(DOD))
}
func main() {
flag.Parse()
checkWhURL(*whURL)
if *listenAddress == "" {
*listenAddress = defaultListenAddress
}
log.Printf("Listening on: %s", *listenAddress)
log.Fatalf("Failed to listen on HTTP: %v",
http.ListenAndServe(*listenAddress, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log.Printf("%s - [%s] %s", r.Host, r.Method, r.URL.RawPath)
b, err := ioutil.ReadAll(r.Body)
if err != nil {
panic(err)
}
amo := alertManOut{}
err = json.Unmarshal(b, &amo)
if err != nil {
if isRawPromAlert(b) {
sendRawPromAlertWarn()
return
}
if len(b) > 1024 {
log.Printf("Failed to unpack inbound alert request - %s...", string(b[:1023]))
} else {
log.Printf("Failed to unpack inbound alert request - %s", string(b))
}
return
}
sendWebhook(&amo)
})))
}