|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "encoding/json" |
| 7 | + "errors" |
| 8 | + "fmt" |
| 9 | + elasticsearch "github.com/elastic/go-elasticsearch/v7" |
| 10 | + "github.com/elastic/go-elasticsearch/v7/esapi" |
| 11 | + "github.com/prometheus/client_golang/prometheus" |
| 12 | + "io/ioutil" |
| 13 | + "net/http" |
| 14 | + "strings" |
| 15 | + "time" |
| 16 | +) |
| 17 | + |
| 18 | +const supportedWebhookVersion = "4" |
| 19 | + |
| 20 | +type ( |
| 21 | + AlertmanagerElasticsearchExporter struct { |
| 22 | + elasticSearchClient *elasticsearch.Client |
| 23 | + elasticsearchIndexName string |
| 24 | + elasticsearchBatchCount int |
| 25 | + elasticsearchRetryCount int |
| 26 | + elasticsearchRetryDelay time.Duration |
| 27 | + |
| 28 | + prometheus struct { |
| 29 | + alertsReceived *prometheus.CounterVec |
| 30 | + alertsInvalid *prometheus.CounterVec |
| 31 | + alertsSuccessful *prometheus.CounterVec |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + AlertmanagerEntry struct { |
| 36 | + Alerts []struct { |
| 37 | + Annotations map[string]string `json:"annotations"` |
| 38 | + EndsAt time.Time `json:"endsAt"` |
| 39 | + GeneratorURL string `json:"generatorURL"` |
| 40 | + Labels map[string]string `json:"labels"` |
| 41 | + StartsAt time.Time `json:"startsAt"` |
| 42 | + Status string `json:"status"` |
| 43 | + } `json:"alerts"` |
| 44 | + CommonAnnotations map[string]string `json:"commonAnnotations"` |
| 45 | + CommonLabels map[string]string `json:"commonLabels"` |
| 46 | + ExternalURL string `json:"externalURL"` |
| 47 | + GroupLabels map[string]string `json:"groupLabels"` |
| 48 | + Receiver string `json:"receiver"` |
| 49 | + Status string `json:"status"` |
| 50 | + Version string `json:"version"` |
| 51 | + GroupKey string `json:"groupKey"` |
| 52 | + |
| 53 | + // Timestamp records when the alert notification was received |
| 54 | + Timestamp string `json:"@timestamp"` |
| 55 | + } |
| 56 | +) |
| 57 | + |
| 58 | +func (e *AlertmanagerElasticsearchExporter) Init() { |
| 59 | + e.prometheus.alertsReceived = prometheus.NewCounterVec( |
| 60 | + prometheus.CounterOpts{ |
| 61 | + Name: "alertmanager2es_alerts_received", |
| 62 | + Help: "alertmanager2es received alerts", |
| 63 | + }, |
| 64 | + []string{}, |
| 65 | + ) |
| 66 | + prometheus.MustRegister(e.prometheus.alertsReceived) |
| 67 | + |
| 68 | + e.prometheus.alertsInvalid = prometheus.NewCounterVec( |
| 69 | + prometheus.CounterOpts{ |
| 70 | + Name: "alertmanager2es_alerts_invalid", |
| 71 | + Help: "alertmanager2es invalid alerts", |
| 72 | + }, |
| 73 | + []string{}, |
| 74 | + ) |
| 75 | + prometheus.MustRegister(e.prometheus.alertsInvalid) |
| 76 | + |
| 77 | + e.prometheus.alertsSuccessful = prometheus.NewCounterVec( |
| 78 | + prometheus.CounterOpts{ |
| 79 | + Name: "alertmanager2es_alerts_successful", |
| 80 | + Help: "alertmanager2es successful stored alerts", |
| 81 | + }, |
| 82 | + []string{}, |
| 83 | + ) |
| 84 | + prometheus.MustRegister(e.prometheus.alertsSuccessful) |
| 85 | +} |
| 86 | + |
| 87 | +func (e *AlertmanagerElasticsearchExporter) ConnectElasticsearch(cfg elasticsearch.Config, indexName string) { |
| 88 | + var err error |
| 89 | + e.elasticSearchClient, err = elasticsearch.NewClient(cfg) |
| 90 | + if err != nil { |
| 91 | + panic(err) |
| 92 | + } |
| 93 | + |
| 94 | + tries := 0 |
| 95 | + for { |
| 96 | + _, err = e.elasticSearchClient.Info() |
| 97 | + if err != nil { |
| 98 | + tries++ |
| 99 | + if tries >= 5 { |
| 100 | + panic(err) |
| 101 | + } else { |
| 102 | + daemonLogger.Info("Failed to connect to ES, retry...") |
| 103 | + time.Sleep(5 * time.Second) |
| 104 | + continue |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + break |
| 109 | + } |
| 110 | + |
| 111 | + e.elasticsearchIndexName = indexName |
| 112 | +} |
| 113 | + |
| 114 | +func (e *AlertmanagerElasticsearchExporter) buildIndexName(createTime time.Time) string { |
| 115 | + ret := e.elasticsearchIndexName |
| 116 | + |
| 117 | + ret = strings.Replace(ret, "%y", createTime.Format("2006"), -1) |
| 118 | + ret = strings.Replace(ret, "%m", createTime.Format("01"), -1) |
| 119 | + ret = strings.Replace(ret, "%d", createTime.Format("02"), -1) |
| 120 | + |
| 121 | + return ret |
| 122 | +} |
| 123 | + |
| 124 | +func (e *AlertmanagerElasticsearchExporter) HttpHandler(w http.ResponseWriter, r *http.Request) { |
| 125 | + e.prometheus.alertsReceived.WithLabelValues().Inc() |
| 126 | + |
| 127 | + if r.Body == nil { |
| 128 | + e.prometheus.alertsInvalid.WithLabelValues().Inc() |
| 129 | + err := errors.New("got empty request body") |
| 130 | + http.Error(w, err.Error(), http.StatusBadRequest) |
| 131 | + daemonLogger.Error(err) |
| 132 | + return |
| 133 | + } |
| 134 | + |
| 135 | + b, err := ioutil.ReadAll(r.Body) |
| 136 | + if err != nil { |
| 137 | + e.prometheus.alertsInvalid.WithLabelValues().Inc() |
| 138 | + http.Error(w, err.Error(), http.StatusInternalServerError) |
| 139 | + daemonLogger.Error(err) |
| 140 | + return |
| 141 | + } |
| 142 | + defer r.Body.Close() |
| 143 | + |
| 144 | + var msg AlertmanagerEntry |
| 145 | + err = json.Unmarshal(b, &msg) |
| 146 | + if err != nil { |
| 147 | + e.prometheus.alertsInvalid.WithLabelValues().Inc() |
| 148 | + http.Error(w, err.Error(), http.StatusBadRequest) |
| 149 | + daemonLogger.Error(err) |
| 150 | + return |
| 151 | + } |
| 152 | + |
| 153 | + if msg.Version != supportedWebhookVersion { |
| 154 | + e.prometheus.alertsInvalid.WithLabelValues().Inc() |
| 155 | + err := fmt.Errorf("do not understand webhook version %q, only version %q is supported.", msg.Version, supportedWebhookVersion) |
| 156 | + http.Error(w, err.Error(), http.StatusBadRequest) |
| 157 | + daemonLogger.Error(err) |
| 158 | + return |
| 159 | + } |
| 160 | + |
| 161 | + now := time.Now() |
| 162 | + msg.Timestamp = now.Format(time.RFC3339) |
| 163 | + |
| 164 | + incidentJson, _ := json.Marshal(msg) |
| 165 | + |
| 166 | + req := esapi.IndexRequest{ |
| 167 | + Index: e.buildIndexName(now), |
| 168 | + Body: bytes.NewReader(incidentJson), |
| 169 | + } |
| 170 | + res, err := req.Do(context.Background(), e.elasticSearchClient) |
| 171 | + if err != nil { |
| 172 | + e.prometheus.alertsInvalid.WithLabelValues().Inc() |
| 173 | + err := fmt.Errorf("unable to insert document in elasticsearch") |
| 174 | + http.Error(w, err.Error(), http.StatusBadRequest) |
| 175 | + daemonLogger.Error(err) |
| 176 | + return |
| 177 | + } |
| 178 | + defer res.Body.Close() |
| 179 | + |
| 180 | + daemonLogger.Verbosef("received and stored alert: %v", msg.CommonLabels) |
| 181 | + e.prometheus.alertsSuccessful.WithLabelValues().Inc() |
| 182 | +} |
0 commit comments