-
Notifications
You must be signed in to change notification settings - Fork 10
/
aggregate.go
95 lines (81 loc) · 1.93 KB
/
aggregate.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
package healthchecks
import (
"sync"
)
// Execute all statusEndpoint StatusCheck() functions asynchronously and return the
// overall status by returning the highest severity item in the following order:
// CRIT, WARN, OK
func Aggregate(statusEndpoints []StatusEndpoint, typeFilter string, apiVersion APIVersion) string {
if len(typeFilter) > 0 {
if typeFilter != "internal" && typeFilter != "external" {
sl := StatusList{
StatusList: []Status{
{
Description: "Invalid type",
Result: CRITICAL,
Details: "Unknown check type given for aggregate check",
},
},
}
return SerializeStatusList(sl, apiVersion)
}
}
s := statusEndpoints
if typeFilter != "" {
s = []StatusEndpoint{}
for _, statusEndpoint := range statusEndpoints {
if typeFilter == "internal" {
if statusEndpoint.Type == "internal" {
s = append(s, statusEndpoint)
}
} else if typeFilter == "external" {
if statusEndpoint.Type != "internal" {
s = append(s, statusEndpoint)
}
}
}
}
responses := make(chan StatusList)
var wg sync.WaitGroup
wg.Add(len(s))
for _, statusEndpoint := range s {
go func(statusEndpoint StatusEndpoint) {
responses <- statusEndpoint.StatusCheck.CheckStatus(statusEndpoint.Name)
}(statusEndpoint)
}
var crits []StatusList
var warns []StatusList
var oks []StatusList
go func() {
for r := range responses {
switch r.StatusList[0].Result {
case CRITICAL:
crits = append(crits, r)
case WARNING:
warns = append(warns, r)
case OK:
oks = append(oks, r)
default:
panic("Invalid AlertLevel")
}
wg.Done()
}
}()
wg.Wait()
close(responses)
sl := StatusList{
StatusList: []Status{
{
Description: "Aggregate Check",
Result: OK,
Details: "All checks are OK",
},
},
}
if len(crits) > 0 {
sl = crits[0]
} else if len(warns) > 0 {
sl = warns[0]
}
return SerializeStatusList(sl, apiVersion)
}