-
Notifications
You must be signed in to change notification settings - Fork 3
/
alert.go
186 lines (151 loc) · 4.98 KB
/
alert.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
184
185
186
package main
import (
"encoding/json"
"fmt"
"strings"
"time"
log "github.com/Sirupsen/logrus"
"github.com/hashicorp/consul/api"
)
type AlertState struct {
Status string `json:"status"`
Node string `json:"node"`
Service string `json:"service"`
Tag string `json:"tag"`
UpdateIndex int64 `json:"update_index"`
LastAlerted string `json:"last_alerted"`
Message string `json:"message"`
Details string `json:"details"`
}
// Parses a CheckState from a given Consul K/V path
func getAlertState(kvPath string, client *api.Client) (*AlertState, error) {
kvPair, _, err := client.KV().Get(kvPath, nil)
check := &AlertState{}
if err != nil {
log.Error("Error loading alert state: ", err)
return nil, err
}
if kvPair == nil {
return nil, nil
}
if string(kvPair.Value) == "" {
return nil, nil
}
err = json.Unmarshal(kvPair.Value, check)
if err != nil {
log.Error("Error parsing alert state: ", err)
return nil, err
}
return check, nil
}
// Sets an alert state in at a given K/V path, returns true if succeeded
func setAlertState(kvPath string, alert *AlertState, client *api.Client) error {
serialized, err := json.Marshal(alert)
if err != nil {
return fmt.Errorf("Error forming state for alert in Consul: %s", err)
}
_, err = client.KV().Put(&api.KVPair{
Key: kvPath,
Value: serialized,
}, nil)
if err != nil {
return fmt.Errorf("Error storing state for alert in Consul: %s", err)
}
return nil
}
// Waits for changeThreshold duration, then alerts if LastUpdated has not
// changed in the meantime (which would indicate another alert resetting the timer)
func tryAlert(kvPath string, update AlertState, watchOpts *WatchOptions) {
// Lock the mutex while reading or writing the alert state to avoid race conditions
watchOpts.alertLock.Lock()
alert, err := getAlertState(kvPath, watchOpts.client)
if err != nil {
log.Error("Error fetching alert state: ", err)
watchOpts.alertLock.Unlock()
return
}
// Create a new alert state if there's no pre-existing one
if alert == nil {
alert = &AlertState{
Node: watchOpts.node,
Service: watchOpts.service,
Tag: watchOpts.tag,
LastAlerted: api.HealthPassing,
}
}
alert.Status = update.Status
alert.Message = update.Message
alert.Details = update.Details
// Increment the update index and store it, so we can check later to see if it changed
alert.UpdateIndex++
updateIndex := alert.UpdateIndex
// Set LastUpdated on the alert to reset the timer
err = setAlertState(kvPath, alert, watchOpts.client)
if err != nil {
log.Error("Error setting alert state: ", err)
watchOpts.alertLock.Unlock()
return
}
watchOpts.alertLock.Unlock()
changeThreshold := watchOpts.config.serviceChangeThreshold(watchOpts.service)
log.Debugf("Starting timer for alert: '%s'", update.Message)
time.Sleep(time.Duration(changeThreshold) * time.Second)
watchOpts.alertLock.Lock()
defer watchOpts.alertLock.Unlock()
alert, err = getAlertState(kvPath, watchOpts.client)
if err != nil {
log.Error("Error fetching alert state: ", err)
return
}
if alert == nil {
log.Errorf("Alert state not found at path %s", kvPath)
return
}
// If no new alerts were triggered during the sleep, send the alert to each handler to be processed
if alert.UpdateIndex == updateIndex && update.Status != alert.LastAlerted {
for _, handler := range watchOpts.config.serviceHandlers(watchOpts.service) {
handler.Alert(watchOpts.config.ConsulDatacenter, alert)
}
alert.LastAlerted = update.Status
err = setAlertState(kvPath, alert, watchOpts.client)
if err != nil {
log.Error("Error setting alert state: ", err)
}
}
}
// Returns each failing check and its output, used for formatting alert details
func nodeDetails(checks []*api.HealthCheck) string {
details := ""
for _, check := range checks {
if check.ServiceID == "" && (check.Status == api.HealthCritical || check.Status == api.HealthWarning) {
details = details + fmt.Sprintf("=> (check) %s:\n%s", check.Name, check.Output)
}
}
// Only set details if we have failing checks
if details != "" {
details = "Failing checks:\n" + details
}
return strings.TrimSpace(details)
}
// Returns each failing check and its output, grouped by node, used for formatting alert details
func serviceDetails(checks []*api.HealthCheck) string {
details := ""
// Make a map for combining the failing health check outputs on each node
nodeStatuses := make(map[string]string)
for _, check := range checks {
if check.Status == api.HealthCritical || check.Status == api.HealthWarning {
if _, ok := nodeStatuses[check.Node]; !ok {
nodeStatuses[check.Node] = ""
}
nodeStatuses[check.Node] = nodeStatuses[check.Node] + fmt.Sprintf("==> (check) %s:\n%s", check.Name, check.Output)
}
}
// Only set details if we have failing checks
if len(nodeStatuses) > 0 {
details = "Failing checks:\n"
for node, status := range nodeStatuses {
details = details + fmt.Sprintf("=> (node) %s\n%s", node, status)
}
}
return strings.TrimSpace(details)
}