forked from mperham/inspeqtor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rules.go
176 lines (147 loc) · 3.74 KB
/
rules.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
package inspeqtor
import (
"reflect"
"github.com/mperham/inspeqtor/util"
)
type Operator uint8
const (
LT Operator = iota
GT
)
func (o Operator) String() string {
switch o {
case GT:
return "greater than"
case LT:
return "less than"
default:
return "???"
}
}
type RuleState string
const (
Ok RuleState = "Ok"
Triggered RuleState = "Triggered"
Recovered RuleState = "Recovered"
)
func (o RuleState) String() string {
return string(o)
}
type Rule struct {
Entity Checkable
MetricFamily string
MetricName string
Op Operator
DisplayThreshold string
Threshold float64
CurrentValue float64
PerSec bool
CycleCount int
TrippedCount int
State RuleState
Actions []Action
}
func (r *Rule) Consequence() string {
for _, a := range r.Actions {
// So clean!
if reflect.ValueOf(a).Elem().Type().Name() == "Restarter" {
return ", restarting"
}
}
return ""
}
func (r *Rule) Metric() string {
s := r.MetricFamily
if r.MetricName != "" {
s += ":" + r.MetricName
}
return s
}
func (r *Rule) EntityName() string {
return r.Entity.Name()
}
func (r *Rule) DisplayState() string {
if r.State == Triggered {
return "!"
}
return ""
}
func (r *Rule) FetchLatestMetricValue() float64 {
return r.Entity.Metrics().Get(r.MetricFamily, r.MetricName)
}
func (r *Rule) FetchDisplayCurrentValue() string {
return r.Entity.Metrics().Display(r.MetricFamily, r.MetricName)
}
func (r *Rule) Reset() {
r.TrippedCount = 0
}
/*
Run through all Rules and check if we need to trigger actions.
"tripped" means the Rule threshold was breached **this cycle**.
"triggered" means the Rule threshold was breached enough cycles in
a row to fire the alerts associated with the Rule.
There are three possible states for Rules:
Ok - rule was fine last time and passed this time too.
Triggered - threshold breached enough times, action should be taken
Recovered - rule is currently Triggered but threshold was not breached this time
*/
func (r *Rule) Check(cycleTime uint) *Event {
r.CurrentValue = r.FetchLatestMetricValue()
if r.CurrentValue == -1 {
return nil
}
curVal := r.CurrentValue
if r.PerSec {
curVal = curVal / float64(cycleTime)
}
tripped := false
switch r.Op {
case LT:
tripped = curVal < r.Threshold
case GT:
tripped = curVal > r.Threshold
}
if tripped {
r.TrippedCount = r.TrippedCount + 1
} else {
r.TrippedCount = 0
}
return stateMachine[r.State](r, tripped)
}
type stateHandler func(*Rule, bool) *Event
var (
stateMachine = map[RuleState]stateHandler{
Ok: okHandler,
Recovered: recoveredHandler,
Triggered: triggeredHandler,
}
)
func okHandler(rule *Rule, tripped bool) *Event {
if tripped && rule.TrippedCount == rule.CycleCount {
util.Warn("%s[%s] triggered. Current value = %.1f", rule.EntityName(), rule.Metric(), rule.CurrentValue)
rule.State = Triggered
return &Event{RuleFailed, rule.Entity, rule}
}
if tripped {
util.Debug("%s[%s] tripped. Current: %.1f, Threshold: %.1f", rule.EntityName(), rule.Metric(), rule.CurrentValue, rule.Threshold)
}
return nil
}
func recoveredHandler(rule *Rule, tripped bool) *Event {
if tripped && rule.TrippedCount == rule.CycleCount {
util.Warn("%s[%s] flapped. Current value = %.1f", rule.EntityName(), rule.Metric(), rule.CurrentValue)
rule.State = Triggered
return nil
}
rule.State = Ok
return &Event{RuleRecovered, rule.Entity, rule}
}
func triggeredHandler(rule *Rule, tripped bool) *Event {
if !tripped {
util.Info("%s[%s] recovered.", rule.EntityName(), rule.Metric())
rule.State = Recovered
return nil
}
util.Debug("%s[%s] still triggered. Current: %.1f, Threshold: %.1f", rule.EntityName(), rule.Metric(), rule.CurrentValue, rule.Threshold)
return nil
}