forked from webdevops/alertmanager2es
-
Notifications
You must be signed in to change notification settings - Fork 4
/
alertmanager2kafka.go
183 lines (159 loc) · 5.21 KB
/
alertmanager2kafka.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
package main
import (
"context"
"crypto/tls"
"crypto/x509"
"encoding/json"
"errors"
"fmt"
"github.com/prometheus/client_golang/prometheus"
kafka "github.com/segmentio/kafka-go"
log "github.com/sirupsen/logrus"
"io/ioutil"
"net/http"
"strings"
"time"
)
const supportedWebhookVersion = "4"
type (
AlertmanagerKafkaExporter struct {
kafkaWriter *kafka.Writer
prometheus struct {
alertsReceived *prometheus.CounterVec
alertsInvalid *prometheus.CounterVec
alertsSuccessful *prometheus.CounterVec
}
}
KafkaSSLConfig struct {
EnableSSL bool
CertFile string
KeyFile string
CACertFile string
}
AlertmanagerEntry 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"`
}
)
func (e *AlertmanagerKafkaExporter) Init() {
e.prometheus.alertsReceived = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "alertmanager2kafka_alerts_received",
Help: "alertmanager2kafka received alerts",
},
[]string{},
)
prometheus.MustRegister(e.prometheus.alertsReceived)
e.prometheus.alertsInvalid = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "alertmanager2kafka_alerts_invalid",
Help: "alertmanager2kafka invalid alerts",
},
[]string{},
)
prometheus.MustRegister(e.prometheus.alertsInvalid)
e.prometheus.alertsSuccessful = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "alertmanager2kafka_alerts_successful",
Help: "alertmanager2kafka successful stored alerts",
},
[]string{},
)
prometheus.MustRegister(e.prometheus.alertsSuccessful)
}
func (e *AlertmanagerKafkaExporter) ConnectKafka(host string, topic string, sslConfig *KafkaSSLConfig) {
dialer := kafka.DefaultDialer
if sslConfig.EnableSSL {
cert, err := tls.LoadX509KeyPair(sslConfig.CertFile, sslConfig.KeyFile)
if err != nil {
log.Fatalf("cannot load SSL key/certificate pair (key=%s, cert=%s): %s", sslConfig.KeyFile, sslConfig.CertFile, err)
}
if sslConfig.CACertFile == "" {
sslConfig.CACertFile = "/etc/ssl/certs/ca-certificates.crt"
}
caCertPEM, err := ioutil.ReadFile(sslConfig.CACertFile)
if err != nil {
log.Fatalf("cannot read SSL CA certificate file %s: %s", sslConfig.CACertFile, err)
}
caCertPool := x509.NewCertPool()
if ok := caCertPool.AppendCertsFromPEM([]byte(caCertPEM)); !ok {
log.Fatalf("cannot load SSL CA certificates from file %s: %s", sslConfig.CACertFile, err)
}
log.Infof("configured client-side SSL: key=%s, cert=%s, cacert=%s", sslConfig.KeyFile, sslConfig.CertFile, sslConfig.CACertFile)
dialer.TLS = &tls.Config{
Certificates: []tls.Certificate{cert},
RootCAs: caCertPool,
}
}
e.kafkaWriter = kafka.NewWriter(kafka.WriterConfig{
Brokers: strings.Split(host, ","),
Topic: topic,
Dialer: dialer,
})
}
func (e *AlertmanagerKafkaExporter) HttpHandler(w http.ResponseWriter, r *http.Request) {
e.prometheus.alertsReceived.WithLabelValues().Inc()
if r.Body == nil {
e.prometheus.alertsInvalid.WithLabelValues().Inc()
err := errors.New("got empty request body")
http.Error(w, err.Error(), http.StatusBadRequest)
log.Error(err)
return
}
b, err := ioutil.ReadAll(r.Body)
if err != nil {
e.prometheus.alertsInvalid.WithLabelValues().Inc()
http.Error(w, err.Error(), http.StatusInternalServerError)
log.Error(err)
return
}
defer r.Body.Close()
var msg AlertmanagerEntry
err = json.Unmarshal(b, &msg)
if err != nil {
e.prometheus.alertsInvalid.WithLabelValues().Inc()
http.Error(w, err.Error(), http.StatusBadRequest)
log.Error(err)
return
}
if msg.Version != supportedWebhookVersion {
e.prometheus.alertsInvalid.WithLabelValues().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.Error(err)
return
}
now := time.Now()
msg.Timestamp = now.Format(time.RFC3339)
incidentJson, _ := json.Marshal(msg)
err = e.kafkaWriter.WriteMessages(context.Background(), kafka.Message{Value: incidentJson})
if err != nil {
switch kafkaErr := err.(type) {
case kafka.WriteErrors:
err = kafkaErr[0]
}
errMsg := fmt.Errorf("unable to write into kafka: %s", err)
e.prometheus.alertsInvalid.WithLabelValues().Inc()
http.Error(w, errMsg.Error(), http.StatusBadRequest)
log.Error(errMsg)
return
}
log.Debugf("received and stored alert: %v", msg.CommonLabels)
e.prometheus.alertsSuccessful.WithLabelValues().Inc()
}