-
Notifications
You must be signed in to change notification settings - Fork 0
/
flapping.go
61 lines (51 loc) · 1.56 KB
/
flapping.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
package v2
// isFlapping determines if the check is flapping, based on the TotalStateChange
// and configured thresholds
func isFlapping(check *Check) bool {
if check == nil {
return false
}
if check.LowFlapThreshold == 0 || check.HighFlapThreshold == 0 {
return false
}
// Is the check already flapping?
if check.State == EventFlappingState {
return check.TotalStateChange > check.LowFlapThreshold
}
// The check was not flapping, now determine if it does now
return check.TotalStateChange >= check.HighFlapThreshold
}
// updateCheckState determines the check state based on whether the check is
// flapping, and its status
func updateCheckState(check *Check) {
check.TotalStateChange = totalStateChange(check)
if check == nil {
return
}
if flapping := isFlapping(check); flapping {
check.State = EventFlappingState
} else if check.Status == 0 {
check.State = EventPassingState
check.LastOK = check.Executed
} else {
check.State = EventFailingState
}
}
// totalStateChange calculates the total state change percentage for the
// history, which is later used for check state flap detection.
func totalStateChange(check *Check) uint32 {
if check == nil || len(check.History) < 21 {
return 0
}
stateChanges := 0.00
changeWeight := 0.80
previousStatus := check.History[len(check.History)-1].Status
for i := len(check.History) - 2; i >= 0; i-- {
if check.History[i].Status != previousStatus {
stateChanges += changeWeight
}
changeWeight += 0.02
previousStatus = check.History[i].Status
}
return uint32(float32(stateChanges) / 20 * 100)
}