forked from cloudflare/alertmanager2es
-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
217 lines (189 loc) · 6.32 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
package main
import (
"bytes"
"encoding/base64"
"encoding/json"
"errors"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"runtime"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
const supportedWebhookVersion = "4"
var (
addr = "localhost:9097"
application = "alertmanager2es"
// See AlertManager docs for info on alert groupings:
// https://prometheus.io/docs/alerting/configuration/#route-<route>
// Index by month as we don't produce enough data to warrant a daily index
esIndexDateFormat = "2006.01"
esIndexName = "alertmanager"
esURL string
esUsername string
esPassword string
revision = "unknown"
versionString = fmt.Sprintf("%s %s (%s)", application, revision, 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 basicAuth(username, password string) string {
auth := esUsername + ":" + esPassword
return base64.StdEncoding.EncodeToString([]byte(auth))
}
func main() {
var showVersion bool
flag.StringVar(&addr, "addr", addr, "host:port to listen to")
flag.StringVar(&esIndexDateFormat, "esIndexDateFormat", esIndexDateFormat, "Elasticsearch index date format")
flag.StringVar(&esIndexName, "esIndexName", esIndexName, "Elasticsearch index name")
flag.StringVar(&esURL, "esURL", esURL, "Elasticsearch HTTP URL")
flag.StringVar(&esUsername, "esUsername", esUsername, "Elasticsearch username")
flag.StringVar(&esPassword, "esPassword", esPassword, "Elasticsearch password")
flag.BoolVar(&showVersion, "version", false, "Print version number and exit")
flag.Parse()
if showVersion {
fmt.Println(versionString)
os.Exit(0)
}
if esURL == "" {
fmt.Fprintln(os.Stderr, "Must specify HTTP URL for Elasticsearch")
flag.Usage()
os.Exit(2)
}
http.DefaultClient.Timeout = 10 * time.Second
s := &http.Server{
Addr: addr,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
}
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, versionString)
})
http.Handle("/metrics", promhttp.Handler())
http.HandleFunc("/webhook", prometheus.InstrumentHandlerFunc("webhook", http.HandlerFunc(handler)))
log.Print(versionString)
log.Printf("Listening on %s", addr)
log.Fatal(s.ListenAndServe())
}
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)
index := fmt.Sprintf("%s-%s", esIndexName, now.Format(esIndexDateFormat))
url := fmt.Sprintf("%s/%s/_doc", esURL, index)
b, err = json.Marshal(&msg)
if err != nil {
notificationsErrored.Inc()
http.Error(w, err.Error(), http.StatusInternalServerError)
log.Print(err)
return
}
req, err := http.NewRequest("POST", url, bytes.NewBuffer(b))
if (esUsername != "") && (esPassword != "") {
req.Header.Add("Authorization", "Basic "+basicAuth(esUsername, esPassword))
}
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 Elasticsearch on %q returned HTTP %d: %s", url, resp.StatusCode, body)
http.Error(w, err.Error(), http.StatusInternalServerError)
log.Print(err)
return
}
}
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"`
}