forked from moira-alert/moira
-
Notifications
You must be signed in to change notification settings - Fork 0
/
datatypes.go
360 lines (324 loc) · 11.7 KB
/
datatypes.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
package moira
import (
"bytes"
"fmt"
"math"
"strings"
"time"
)
var (
eventStates = [...]string{"OK", "WARN", "ERROR", "NODATA", "EXCEPTION", "TEST"}
)
var scores = map[string]int64{
"OK": 0,
"DEL": 0,
"WARN": 1,
"ERROR": 100,
"NODATA": 1000,
"EXCEPTION": 100000,
}
var eventStateWeight = map[string]int{
"OK": 0,
"WARN": 1,
"ERROR": 100,
"NODATA": 10000,
}
// NotificationEvent represents trigger state changes event
type NotificationEvent struct {
IsTriggerEvent bool `json:"trigger_event,omitempty"`
Timestamp int64 `json:"timestamp"`
Metric string `json:"metric"`
Value *float64 `json:"value,omitempty"`
State string `json:"state"`
TriggerID string `json:"trigger_id"`
SubscriptionID *string `json:"sub_id,omitempty"`
ContactID string `json:"contactId,omitempty"`
OldState string `json:"old_state"`
Message *string `json:"msg,omitempty"`
}
// NotificationEvents represents slice of NotificationEvent
type NotificationEvents []NotificationEvent
// TriggerData represents trigger object
type TriggerData struct {
ID string `json:"id"`
Name string `json:"name"`
Desc string `json:"desc"`
Targets []string `json:"targets"`
WarnValue float64 `json:"warn_value"`
ErrorValue float64 `json:"error_value"`
IsRemote bool `json:"is_remote"`
Tags []string `json:"__notifier_trigger_tags"`
}
// ContactData represents contact object
type ContactData struct {
Type string `json:"type"`
Value string `json:"value"`
ID string `json:"id"`
User string `json:"user"`
}
// SubscriptionData represents user subscription
type SubscriptionData struct {
Contacts []string `json:"contacts"`
Tags []string `json:"tags"`
Schedule ScheduleData `json:"sched"`
Plotting PlottingData `json:"plotting"`
ID string `json:"id"`
Enabled bool `json:"enabled"`
IgnoreWarnings bool `json:"ignore_warnings,omitempty"`
IgnoreRecoverings bool `json:"ignore_recoverings,omitempty"`
ThrottlingEnabled bool `json:"throttling"`
User string `json:"user"`
}
// PlottingData represents plotting settings
type PlottingData struct {
Enabled bool `json:"enabled"`
Theme string `json:"theme"`
}
// ScheduleData represents subscription schedule
type ScheduleData struct {
Days []ScheduleDataDay `json:"days"`
TimezoneOffset int64 `json:"tzOffset"`
StartOffset int64 `json:"startOffset"`
EndOffset int64 `json:"endOffset"`
}
// ScheduleDataDay represents week day of schedule
type ScheduleDataDay struct {
Enabled bool `json:"enabled"`
Name string `json:"name,omitempty"`
}
// ScheduledNotification represent notification object
type ScheduledNotification struct {
Event NotificationEvent `json:"event"`
Trigger TriggerData `json:"trigger"`
Contact ContactData `json:"contact"`
Plotting PlottingData `json:"plotting"`
Throttled bool `json:"throttled"`
SendFail int `json:"send_fail"`
Timestamp int64 `json:"timestamp"`
}
// MatchedMetric represents parsed and matched metric data
type MatchedMetric struct {
Metric string
Patterns []string
Value float64
Timestamp int64
RetentionTimestamp int64
Retention int
}
// MetricValue represents metric data
type MetricValue struct {
RetentionTimestamp int64 `json:"step,omitempty"`
Timestamp int64 `json:"ts"`
Value float64 `json:"value"`
}
const (
// FallingTrigger represents falling trigger type, in which OK > WARN > ERROR
FallingTrigger = "falling"
// RisingTrigger represents rising trigger type, in which OK < WARN < ERROR
RisingTrigger = "rising"
// ExpressionTrigger represents trigger type with custom user expression
ExpressionTrigger = "expression"
)
// Trigger represents trigger data object
type Trigger struct {
ID string `json:"id"`
Name string `json:"name"`
Desc *string `json:"desc,omitempty"`
Targets []string `json:"targets"`
WarnValue *float64 `json:"warn_value"`
ErrorValue *float64 `json:"error_value"`
TriggerType string `json:"trigger_type"`
Tags []string `json:"tags"`
TTLState *string `json:"ttl_state,omitempty"`
TTL int64 `json:"ttl,omitempty"`
Schedule *ScheduleData `json:"sched,omitempty"`
Expression *string `json:"expression,omitempty"`
PythonExpression *string `json:"python_expression,omitempty"`
Patterns []string `json:"patterns"`
IsRemote bool `json:"is_remote"`
MuteNewMetrics bool `json:"mute_new_metrics"`
}
// TriggerCheck represents trigger data with last check data and check timestamp
type TriggerCheck struct {
Trigger
Throttling int64 `json:"throttling"`
LastCheck CheckData `json:"last_check"`
}
// CheckData represents last trigger check data
type CheckData struct {
Metrics map[string]MetricState `json:"metrics"`
Score int64 `json:"score"`
State string `json:"state"`
Maintenance int64 `json:"maintenance,omitempty"`
Timestamp int64 `json:"timestamp,omitempty"`
EventTimestamp int64 `json:"event_timestamp,omitempty"`
LastSuccessfulCheckTimestamp int64 `json:"last_successful_check_timestamp"`
Suppressed bool `json:"suppressed,omitempty"`
SuppressedState string `json:"suppressed_state,omitempty"`
Message string `json:"msg,omitempty"`
}
// MetricState represents metric state data for given timestamp
type MetricState struct {
EventTimestamp int64 `json:"event_timestamp"`
State string `json:"state"`
Suppressed bool `json:"suppressed"`
SuppressedState string `json:"suppressed_state,omitempty"`
Timestamp int64 `json:"timestamp"`
Value *float64 `json:"value,omitempty"`
Maintenance int64 `json:"maintenance,omitempty"`
}
// MetricEvent represents filter metric event
type MetricEvent struct {
Metric string `json:"metric"`
Pattern string `json:"pattern"`
}
// GetSubjectState returns the most critical state of events
func (events NotificationEvents) GetSubjectState() string {
result := ""
states := make(map[string]bool)
for _, event := range events {
states[event.State] = true
}
for _, state := range eventStates {
if states[state] {
result = state
}
}
return result
}
// GetTags returns "[tag1][tag2]...[tagN]" string
func (trigger *TriggerData) GetTags() string {
var buffer bytes.Buffer
for _, tag := range trigger.Tags {
buffer.WriteString(fmt.Sprintf("[%s]", tag))
}
return buffer.String()
}
// GetKey return notification key to prevent duplication to the same contact
func (notification *ScheduledNotification) GetKey() string {
return fmt.Sprintf("%s:%s:%s:%s:%s:%d:%f:%d:%t:%d",
notification.Contact.Type,
notification.Contact.Value,
notification.Event.TriggerID,
notification.Event.Metric,
notification.Event.State,
notification.Event.Timestamp,
UseFloat64(notification.Event.Value),
notification.SendFail,
notification.Throttled,
notification.Timestamp,
)
}
// IsScheduleAllows check if the time is in the allowed schedule interval
func (schedule *ScheduleData) IsScheduleAllows(ts int64) bool {
if schedule == nil {
return true
}
endOffset, startOffset := schedule.EndOffset, schedule.StartOffset
if schedule.EndOffset < schedule.StartOffset {
endOffset = schedule.EndOffset + 24*60
}
timestamp := ts - ts%60 - schedule.TimezoneOffset*60
date := time.Unix(timestamp, 0).UTC()
if !schedule.Days[int(date.Weekday()+6)%7].Enabled {
return false
}
dayStart := time.Unix(timestamp-timestamp%(24*3600), 0).UTC()
startDayTime := dayStart.Add(time.Duration(startOffset) * time.Minute)
endDayTime := dayStart.Add(time.Duration(endOffset) * time.Minute)
if endOffset < 24*60 {
if (date.After(startDayTime) || date.Equal(startDayTime)) && (date.Before(endDayTime) || date.Equal(endDayTime)) {
return true
}
} else {
endDayTime = endDayTime.Add(-time.Hour * 24)
if date.Before(endDayTime) || date.After(startDayTime) {
return true
}
}
return false
}
func (eventData NotificationEvent) String() string {
return fmt.Sprintf("TriggerId: %s, Metric: %s, Value: %v, OldState: %s, State: %s, Message: '%s', Timestamp: %v", eventData.TriggerID, eventData.Metric, UseFloat64(eventData.Value), eventData.OldState, eventData.State, UseString(eventData.Message), eventData.Timestamp)
}
// GetOrCreateMetricState gets metric state from check data or create new if CheckData has no state for given metric
func (checkData *CheckData) GetOrCreateMetricState(metric string, emptyTimestampValue int64, muteNewMetric bool) MetricState {
_, ok := checkData.Metrics[metric]
if !ok {
checkData.Metrics[metric] = createEmptyMetricState(emptyTimestampValue, !muteNewMetric)
}
return checkData.Metrics[metric]
}
func createEmptyMetricState(defaultTimestampValue int64, firstStateIsNodata bool) MetricState {
if firstStateIsNodata {
return MetricState{
State: "NODATA",
Timestamp: defaultTimestampValue,
}
}
unixNow := time.Now().Unix()
return MetricState{
State: "OK",
Timestamp: unixNow,
EventTimestamp: unixNow,
}
}
// GetCheckPoint gets check point for given MetricState
// CheckPoint is the timestamp from which to start checking the current state of the metric
func (metricState *MetricState) GetCheckPoint(checkPointGap int64) int64 {
return int64(math.Max(float64(metricState.Timestamp-checkPointGap), float64(metricState.EventTimestamp)))
}
// GetEventTimestamp gets event timestamp for given metric
func (metricState MetricState) GetEventTimestamp() int64 {
if metricState.EventTimestamp == 0 {
return metricState.Timestamp
}
return metricState.EventTimestamp
}
// GetEventTimestamp gets event timestamp for given check
func (checkData CheckData) GetEventTimestamp() int64 {
if checkData.EventTimestamp == 0 {
return checkData.Timestamp
}
return checkData.EventTimestamp
}
// IsSimple checks triggers patterns
// If patterns more than one or it contains standard graphite wildcard symbols,
// when this target can contain more then one metrics, and is it not simple trigger
func (trigger *Trigger) IsSimple() bool {
if len(trigger.Targets) > 1 || len(trigger.Patterns) > 1 {
return false
}
for _, pattern := range trigger.Patterns {
if strings.ContainsAny(pattern, "*{?[") {
return false
}
}
return true
}
// UpdateScore update and return checkData score, based on metric states and checkData state
func (checkData *CheckData) UpdateScore() int64 {
checkData.Score = scores[checkData.State]
for _, metricData := range checkData.Metrics {
checkData.Score += scores[metricData.State]
}
return checkData.Score
}
// MustIgnore returns true if given state transition must be ignored
func (subscription *SubscriptionData) MustIgnore(eventData *NotificationEvent) bool {
if oldStateWeight, ok := eventStateWeight[eventData.OldState]; ok {
if newStateWeight, ok := eventStateWeight[eventData.State]; ok {
delta := newStateWeight - oldStateWeight
if delta < 0 {
if delta == -1 && (subscription.IgnoreRecoverings || subscription.IgnoreWarnings) {
return true
}
return subscription.IgnoreRecoverings
}
if delta == 1 {
return subscription.IgnoreWarnings
}
}
}
return false
}