forked from AcalephStorage/consul-alerts
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcheck-handler.go
130 lines (110 loc) · 2.78 KB
/
check-handler.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
package main
import (
"time"
"net/http"
"github.com/AcalephStorage/consul-alerts/consul"
"github.com/AcalephStorage/consul-alerts/notifier"
log "github.com/AcalephStorage/consul-alerts/Godeps/_workspace/src/github.com/Sirupsen/logrus"
)
type CheckProcessor struct {
inChan chan []consul.Check
closeChan chan struct{}
firstRun bool
notifEngine *NotifEngine
leaderElection *LeaderElection
}
func (c *CheckProcessor) start() {
cleanup := false
for !cleanup {
select {
case checks := <-c.inChan:
c.handleChecks(checks)
case <-c.closeChan:
cleanup = true
}
}
}
func (c *CheckProcessor) stop() {
close(c.closeChan)
}
func (c *CheckProcessor) handleChecks(checks []consul.Check) {
consulClient.LoadConfig()
retryCount := 0
for !hasLeader() {
if retryCount >= 6 {
return
}
log.Println("There is current no consul-alerts leader... waiting for one.")
time.Sleep(5 * time.Second)
retryCount++
}
if !c.leaderElection.leader {
log.Println("Currently not the leader. Ignoring checks.")
return
}
log.Println("Running health check.")
changeThreshold := consulClient.CheckChangeThreshold()
for elapsed := 0; elapsed < changeThreshold; elapsed += 10 {
consulClient.UpdateCheckData()
time.Sleep(10 * time.Second)
}
consulClient.UpdateCheckData()
log.Println("Processing health checks for notification.")
alerts := consulClient.NewAlerts()
if len(alerts) > 0 {
c.notify(alerts)
}
}
func (c *CheckProcessor) notify(alerts []consul.Check) {
messages := make([]notifier.Message, len(alerts))
for i, alert := range alerts {
messages[i] = notifier.Message{
Node: alert.Node,
ServiceId: alert.ServiceID,
Service: alert.ServiceName,
CheckId: alert.CheckID,
Check: alert.Name,
Status: alert.Status,
Output: alert.Output,
Notes: alert.Notes,
Timestamp: time.Now(),
}
}
if len(messages) == 0 {
log.Println("Nothing to notify.")
return
}
c.notifEngine.queueMessages(messages)
}
func startCheckProcessor(leaderCandidate *LeaderElection, notifEngine *NotifEngine) *CheckProcessor {
cp := &CheckProcessor{
inChan: make(chan []consul.Check, 1),
closeChan: make(chan struct{}),
firstRun: true,
notifEngine: notifEngine,
leaderElection: leaderCandidate,
}
go cp.start()
return cp
}
func (c *CheckProcessor) checkHandler(w http.ResponseWriter, r *http.Request) {
consulClient.LoadConfig()
if c.firstRun {
log.Println("Now watching for health changes.")
c.firstRun = false
w.WriteHeader(200)
return
}
if !consulClient.ChecksEnabled() {
log.Println("Checks handling disabled. Checks ignored.")
w.WriteHeader(200)
return
}
if len(c.inChan) == 1 {
<-c.inChan
}
var checks []consul.Check
toWatchObject(r.Body, &checks)
c.inChan <- checks
w.WriteHeader(200)
}