-
Notifications
You must be signed in to change notification settings - Fork 20
/
alertmanager2es.go
181 lines (155 loc) · 4.85 KB
/
alertmanager2es.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
package main
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"strings"
"time"
elasticsearch "github.com/elastic/go-elasticsearch/v7"
"github.com/elastic/go-elasticsearch/v7/esapi"
"github.com/prometheus/client_golang/prometheus"
log "github.com/sirupsen/logrus"
)
const supportedWebhookVersion = "4"
type (
AlertmanagerElasticsearchExporter struct {
elasticSearchClient *elasticsearch.Client
elasticsearchIndexName string
prometheus struct {
alertsReceived *prometheus.CounterVec
alertsInvalid *prometheus.CounterVec
alertsSuccessful *prometheus.CounterVec
}
}
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 *AlertmanagerElasticsearchExporter) Init() {
e.prometheus.alertsReceived = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "alertmanager2es_alerts_received",
Help: "alertmanager2es received alerts",
},
[]string{},
)
prometheus.MustRegister(e.prometheus.alertsReceived)
e.prometheus.alertsInvalid = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "alertmanager2es_alerts_invalid",
Help: "alertmanager2es invalid alerts",
},
[]string{},
)
prometheus.MustRegister(e.prometheus.alertsInvalid)
e.prometheus.alertsSuccessful = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "alertmanager2es_alerts_successful",
Help: "alertmanager2es successful stored alerts",
},
[]string{},
)
prometheus.MustRegister(e.prometheus.alertsSuccessful)
}
func (e *AlertmanagerElasticsearchExporter) ConnectElasticsearch(cfg elasticsearch.Config, indexName string) {
var err error
e.elasticSearchClient, err = elasticsearch.NewClient(cfg)
if err != nil {
panic(err)
}
tries := 0
for {
_, err = e.elasticSearchClient.Info()
if err != nil {
tries++
if tries >= 5 {
panic(err)
} else {
log.Info("Failed to connect to ES, retry...")
time.Sleep(5 * time.Second)
continue
}
}
break
}
e.elasticsearchIndexName = indexName
}
func (e *AlertmanagerElasticsearchExporter) buildIndexName(createTime time.Time) string {
ret := e.elasticsearchIndexName
ret = strings.Replace(ret, "%y", createTime.Format("2006"), -1)
ret = strings.Replace(ret, "%m", createTime.Format("01"), -1)
ret = strings.Replace(ret, "%d", createTime.Format("02"), -1)
return ret
}
func (e *AlertmanagerElasticsearchExporter) 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)
req := esapi.IndexRequest{
Index: e.buildIndexName(now),
Body: bytes.NewReader(incidentJson),
}
res, err := req.Do(context.Background(), e.elasticSearchClient)
if err != nil {
e.prometheus.alertsInvalid.WithLabelValues().Inc()
err := fmt.Errorf("unable to insert document in elasticsearch")
http.Error(w, err.Error(), http.StatusBadRequest)
log.Error(err)
return
}
defer res.Body.Close()
log.Debugf("received and stored alert: %v", msg.CommonLabels)
e.prometheus.alertsSuccessful.WithLabelValues().Inc()
}