-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathalertmanager_webhook.go
97 lines (86 loc) · 2.69 KB
/
alertmanager_webhook.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
package main
import (
"net/http"
"time"
"github.com/gin-gonic/gin"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
log "github.com/sirupsen/logrus"
)
type KV map[string]string
type Alerts []Alert
type Alert struct {
Status string `json:"status"`
Labels KV `json:"labels"`
Annotations KV `json:"annotations"`
StartAt time.Time `json:"startsAt"`
EndsAt time.Time `json:"endsAt"`
GeneratorUrl string `json:"generatorURL"`
Fingerprint string `json:"fingerprint"`
}
type Webhook struct {
Version string `json:"version"`
GroupKey string `json:"groupKey"`
TruncatedAlerts uint64 `json:"truncatedAlerts"`
Status string `json:"status"`
Receiver string `json:"receiver"`
GroupLabels KV `json:"groupLabels"`
CommonLabels KV `json:"commonLabels"`
CommonAnnotations KV `json:"commonAnnotations"`
ExternalUrl string `json:"externalURL"`
Alerts Alerts `json:"alerts"`
}
const (
STATUS_RESOLVED string = "resolved"
STATUS_FIRING string = "firing"
LABEL_ACTIVE string = "active"
LABEL_INACTIVE string = "inactive"
)
var (
actions_counter = promauto.NewCounterVec(prometheus.CounterOpts{
Name: "sgready_action_total",
Namespace: "isg",
Help: "Amount of successful invocations per action-level",
}, []string{"action"})
error_counter = promauto.NewCounter(prometheus.CounterOpts{
Name: "sgready_error_total",
Namespace: "isg",
Help: "Amount of failed invocations",
})
)
func callAlertmanagerWebhook(c *gin.Context) {
var webhook Webhook
log.Info("Webhook called")
if err := c.ShouldBindJSON(&webhook); err != nil {
error_counter.Inc()
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
log.Info(webhook)
for _, alert := range webhook.Alerts {
switch alert.Status {
case STATUS_RESOLVED:
log.Info("Triggering sgReady 2")
SetSGReadyLevel(Normal)
actions_counter.WithLabelValues(Normal).Inc()
case STATUS_FIRING:
switch alert.Labels["target"] {
case LABEL_ACTIVE:
log.Info("Triggering sgReady 3")
SetSGReadyLevel(Active)
actions_counter.WithLabelValues(Active).Inc()
case LABEL_INACTIVE:
log.Info("Triggering sgReady 1")
SetSGReadyLevel(Lock)
actions_counter.WithLabelValues(Lock).Inc()
default:
log.Warnf("Received unexpected target label %s", alert.Labels["target"])
actions_counter.WithLabelValues("unknown").Inc()
}
default:
log.Warnf("Received unexpected status %s", webhook.Status)
actions_counter.WithLabelValues("unknown").Inc()
}
}
c.JSON(http.StatusOK, gin.H{"status": "webhook received"})
}