-
Notifications
You must be signed in to change notification settings - Fork 0
/
alerts.go
67 lines (62 loc) · 1.75 KB
/
alerts.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
package main
import (
"bytes"
"encoding/json"
"net/http"
"time"
log "github.com/sirupsen/logrus"
)
// PromAlert is the alert payload alertmanager sent to the hook
type PromAlert struct {
Receiver string `json:"receiver"`
Status string `json:"status"`
Alerts []struct {
Status string `json:"status"`
Labels struct {
Alertname string `json:"alertname"`
Selfhealing string `json:"selfhealing`
Severity string `json:"severity"`
Component string `json:"component"`
Instance string `json:"instance"`
} `json:"labels"`
Annotations struct {
Summary string `json:"summary"`
} `json:"annotations"`
StartsAt time.Time `json:"startsAt"`
EndsAt time.Time `json:"endsAt"`
GeneratorURL string `json:"generatorURL"`
Fingerprint string `json:"fingerprint"`
} `json:"alerts"`
}
// AlertFire is the payload from https://prometheus.io/docs/alerting/latest/clients/
type AlertFire struct {
Status string `json:"status"`
Labels struct {
Alertname string `json:"alertname"`
Severity string `json:"severity"`
Component string `json:"component"`
Instance string `json:"instance"`
} `json:"labels"`
Annotations struct {
Summary string `json:"summary"`
} `json:"annotations"`
GeneratorURL string `json:"generatorURL"`
}
func (alert *AlertFire) sendAlert(url string) {
alerts := make([]AlertFire, 1)
alerts[0] = *alert
body := alerts
buf := new(bytes.Buffer)
json.NewEncoder(buf).Encode(body)
req, err := http.NewRequest("POST", url, buf)
if err != nil {
log.Errorf("Error sending http post alert %s", err)
}
client := &http.Client{}
res, err := client.Do(req)
if err != nil {
log.Error(err)
}
log.Infof("Alert from handler to alertmanager sent %s", alert.Labels.Alertname)
defer res.Body.Close()
}