-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
235 lines (203 loc) · 6.61 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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package main
import (
"bytes"
"encoding/json"
"errors"
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"path"
"runtime"
"text/template"
"time"
"github.com/Masterminds/sprig"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
const supportedWebhookVersion = "4"
var (
addr = "localhost:9097"
application = "gawa"
disableChunked = false
maxClients = 30
postTemplate = "rocketchat2.tmpl"
targetURL string
version = "undefined"
versionString = fmt.Sprintf("%s %s (%s)", application, version, runtime.Version())
notificationsErrored = prometheus.NewCounter(prometheus.CounterOpts{
Namespace: application,
Name: "notifications_errored_total",
Help: "Total number of alert notifications that errored during processing and should be retried",
})
notificationsInvalid = prometheus.NewCounter(prometheus.CounterOpts{
Namespace: application,
Name: "notifications_invalid_total",
Help: "Total number of invalid alert notifications received",
})
notificationsReceived = prometheus.NewCounter(prometheus.CounterOpts{
Namespace: application,
Name: "notifications_received_total",
Help: "Total number of alert notifications received",
})
)
func init() {
prometheus.MustRegister(notificationsErrored)
prometheus.MustRegister(notificationsInvalid)
prometheus.MustRegister(notificationsReceived)
}
func main() {
// defer profile.Start(profile.MemProfile).Stop()
var showVersion bool
flag.StringVar(&addr, "addr", addr, "host:port to listen to")
flag.StringVar(&targetURL, "targetURL", targetURL, "HTTP URL to post to")
flag.StringVar(&postTemplate, "postTemplate", postTemplate, "Template for the post content")
flag.BoolVar(&showVersion, "version", false, "Print version number and exit")
flag.BoolVar(&disableChunked, "disableChunked", disableChunked, "Disable chunked encondig")
flag.IntVar(&maxClients, "maxClients", maxClients, "maximum concurrent clients for /webhook")
flag.Parse()
if showVersion {
fmt.Println(versionString)
os.Exit(0)
}
if targetURL == "" {
fmt.Fprintln(os.Stderr, "Must specify HTTP URL")
flag.Usage()
os.Exit(2)
}
// test template and panic if it is broken
template.Must(template.New(path.Base(postTemplate)).Funcs(sprig.TxtFuncMap()).ParseFiles(postTemplate))
http.DefaultClient.Timeout = 10 * time.Second
s := &http.Server{
Addr: addr,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
}
http.HandleFunc("/webhook", prometheus.InstrumentHandlerFunc("webhook", maxClientsFunc(handler, maxClients)))
http.HandleFunc("/version", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, versionString)
})
http.Handle("/metrics", promhttp.Handler())
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
fmt.Fprint(w, "404: Not found")
log.Printf("404 when serving path: %s requested by %s", r.RequestURI, r.RemoteAddr)
return
})
log.Print(versionString)
log.Printf("Listening on %s", addr)
log.Fatal(s.ListenAndServe())
}
func maxClientsFunc(h http.HandlerFunc, n int) http.HandlerFunc {
sema := make(chan struct{}, n)
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
sema <- struct{}{}
defer func() { <-sema }()
h.ServeHTTP(w, r)
})
}
func handler(w http.ResponseWriter, r *http.Request) {
notificationsReceived.Inc()
if r.Body == nil {
notificationsInvalid.Inc()
err := errors.New("got empty request body")
http.Error(w, err.Error(), http.StatusBadRequest)
log.Print(err)
return
}
b, err := ioutil.ReadAll(r.Body)
if err != nil {
notificationsErrored.Inc()
http.Error(w, err.Error(), http.StatusInternalServerError)
log.Print(err)
return
}
defer r.Body.Close()
var msg notification
err = json.Unmarshal(b, &msg)
if err != nil {
notificationsInvalid.Inc()
http.Error(w, err.Error(), http.StatusBadRequest)
log.Print(err)
return
}
if msg.Version != supportedWebhookVersion {
notificationsInvalid.Inc()
err := fmt.Errorf("do not understand webhook version %q, only version %q is supported", msg.Version, supportedWebhookVersion)
http.Error(w, err.Error(), http.StatusBadRequest)
log.Print(err)
return
}
now := time.Now()
// ISO8601: https://github.com/golang/go/issues/2141#issuecomment-66058048
msg.Timestamp = now.Format(time.RFC3339)
tmpl, err := template.New(path.Base(postTemplate)).Funcs(sprig.TxtFuncMap()).ParseFiles(postTemplate)
pipeReader, pipeWriter := io.Pipe()
go func() {
err = tmpl.Execute(pipeWriter, &msg)
defer pipeWriter.Close()
}()
defer pipeReader.Close()
var req *http.Request
if disableChunked {
var tpl bytes.Buffer
err = tmpl.Execute(&tpl, &msg)
req, err = http.NewRequest("POST", targetURL, &tpl)
} else {
req, err = http.NewRequest("POST", targetURL, pipeReader)
}
if err != nil {
notificationsErrored.Inc()
http.Error(w, err.Error(), http.StatusInternalServerError)
log.Print(err)
return
}
req.Header.Set("User-Agent", versionString)
req.Header.Set("Content-Type", "application/json")
resp, err := http.DefaultClient.Do(req)
if err != nil {
notificationsErrored.Inc()
http.Error(w, err.Error(), http.StatusInternalServerError)
log.Print(err)
return
}
body, err := ioutil.ReadAll(resp.Body)
defer resp.Body.Close()
if err != nil {
notificationsErrored.Inc()
http.Error(w, err.Error(), http.StatusInternalServerError)
log.Print(err)
return
}
if resp.StatusCode/100 != 2 {
notificationsErrored.Inc()
err := fmt.Errorf("POST to rest target on %q returned HTTP %d: %s", targetURL, resp.StatusCode, body)
http.Error(w, err.Error(), resp.StatusCode)
log.Print(err)
return
}
}
// notification data format from alertmanager
type notification struct {
Alerts []struct {
Annotations map[string]string `json:"annotations"`
EndsAt time.Time `json:"endsAt"`
GeneratorURL string `json:"generatorURL"`
Labels map[string]string `json:"labels"`
StartsAt time.Time `json:"startsAt"`
Status string `json:"status"`
} `json:"alerts"`
CommonAnnotations map[string]string `json:"commonAnnotations"`
CommonLabels map[string]string `json:"commonLabels"`
ExternalURL string `json:"externalURL"`
GroupLabels map[string]string `json:"groupLabels"`
Receiver string `json:"receiver"`
Status string `json:"status"`
Version string `json:"version"`
GroupKey string `json:"groupKey"`
// Timestamp records when the alert notification was received
Timestamp string `json:"@timestamp"`
}