This repository has been archived by the owner on Mar 4, 2021. It is now read-only.
forked from moira-alert/moira
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatatypes.go
586 lines (522 loc) · 20.4 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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
package moira
import (
"bytes"
"fmt"
"math"
"sort"
"strconv"
"strings"
"time"
"github.com/moira-alert/moira/templating"
)
const (
// VariableContactID is used to render template with contact.ID
VariableContactID = "${contact_id}"
// VariableContactValue is used to render template with contact.Value
VariableContactValue = "${contact_value}"
// VariableContactType is used to render template with contact.Type
VariableContactType = "${contact_type}"
// VariableTriggerID is used to render template with trigger.ID
VariableTriggerID = "${trigger_id}"
// VariableTriggerName is used to render template with trigger.Name
VariableTriggerName = "${trigger_name}"
)
const (
format = "15:04 02.01.2006"
remindMessage = "This metric has been in bad state for more than %v hours - please, fix."
)
// 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"`
Values map[string]float64 `json:"values,omitempty"`
State State `json:"state"`
TriggerID string `json:"trigger_id"`
SubscriptionID *string `json:"sub_id,omitempty"`
ContactID string `json:"contactId,omitempty"`
OldState State `json:"old_state"`
Message *string `json:"msg,omitempty"`
MessageEventInfo *EventInfo `json:"event_message"`
}
// EventInfo - a base for creating messages.
type EventInfo struct {
Maintenance *MaintenanceInfo `json:"maintenance,omitempty"`
Interval *int64 `json:"interval,omitempty"`
}
// CreateMessage - creates a message based on EventInfo.
func (event *NotificationEvent) CreateMessage(location *time.Location) string { //nolint
// ToDo: DEPRECATED Message in NotificationEvent
if len(UseString(event.Message)) > 0 {
return *event.Message
}
if event.MessageEventInfo == nil {
return ""
}
if event.MessageEventInfo.Interval != nil && event.MessageEventInfo.Maintenance == nil {
return fmt.Sprintf(remindMessage, *event.MessageEventInfo.Interval)
}
if event.MessageEventInfo.Maintenance == nil {
return ""
}
messageBuffer := bytes.NewBuffer([]byte(""))
messageBuffer.WriteString("This metric changed its state during maintenance interval.")
if location == nil {
location = time.UTC
}
if event.MessageEventInfo.Maintenance.StartUser != nil || event.MessageEventInfo.Maintenance.StartTime != nil {
messageBuffer.WriteString(" Maintenance was set")
if event.MessageEventInfo.Maintenance.StartUser != nil {
messageBuffer.WriteString(" by ")
messageBuffer.WriteString(*event.MessageEventInfo.Maintenance.StartUser)
}
if event.MessageEventInfo.Maintenance.StartTime != nil {
messageBuffer.WriteString(" at ")
messageBuffer.WriteString(time.Unix(*event.MessageEventInfo.Maintenance.StartTime, 0).In(location).Format(format))
}
if event.MessageEventInfo.Maintenance.StopUser != nil || event.MessageEventInfo.Maintenance.StopTime != nil {
messageBuffer.WriteString(" and removed")
if event.MessageEventInfo.Maintenance.StopUser != nil && *event.MessageEventInfo.Maintenance.StopUser != *event.MessageEventInfo.Maintenance.StartUser {
messageBuffer.WriteString(" by ")
messageBuffer.WriteString(*event.MessageEventInfo.Maintenance.StopUser)
}
if event.MessageEventInfo.Maintenance.StopTime != nil {
messageBuffer.WriteString(" at ")
messageBuffer.WriteString(time.Unix(*event.MessageEventInfo.Maintenance.StopTime, 0).In(location).Format(format))
}
}
messageBuffer.WriteString(".")
}
return messageBuffer.String()
}
// NotificationEvents represents slice of NotificationEvent
type NotificationEvents []NotificationEvent
func (trigger *TriggerData) PopulatedDescription(events NotificationEvents) error {
description, err := templating.Populate(trigger.Name, trigger.Desc, NotificationEventsToTemplatingEvents(events))
if err != nil {
description = "Your description is using the wrong template. Since we were unable to populate your template with " +
"data, we return it so you can parse it.\n\n" + trigger.Desc
}
trigger.Desc = description
return err
}
func NotificationEventsToTemplatingEvents(events NotificationEvents) []templating.Event {
templatingEvents := make([]templating.Event, 0, len(events))
for _, event := range events {
templatingEvents = append(templatingEvents, templating.Event{
Metric: event.Metric,
MetricElements: strings.Split(event.Metric, "."),
Timestamp: event.Timestamp,
State: string(event.State),
Value: event.Value,
})
}
return templatingEvents
}
// 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"`
}
// GetTriggerURI gets frontUri and returns triggerUrl, returns empty string on selfcheck and test notifications
func (trigger TriggerData) GetTriggerURI(frontURI string) string {
if trigger.ID != "" {
return fmt.Sprintf("%s/trigger/%s", frontURI, trigger.ID)
}
return ""
}
// 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"`
AnyTags bool `json:"any_tags"`
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 *TTLState `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"`
AloneMetrics map[string]bool `json:"alone_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"`
Highlights map[string]string `json:"highlights"`
}
// MaintenanceCheck set maintenance user, time
type MaintenanceCheck interface {
SetMaintenance(maintenanceInfo *MaintenanceInfo, maintenance int64)
GetMaintenance() (MaintenanceInfo, int64)
}
// CheckData represents last trigger check data
type CheckData struct {
Metrics map[string]MetricState `json:"metrics"`
// MetricsToTargetRelation is a map that holds relation between metric names that was alone during last
// check and targets that fetched this metric
MetricsToTargetRelation map[string]string `json:"metrics_to_target_relation"`
Score int64 `json:"score"`
State State `json:"state"`
Maintenance int64 `json:"maintenance,omitempty"`
MaintenanceInfo MaintenanceInfo `json:"maintenance_info"`
Timestamp int64 `json:"timestamp,omitempty"`
EventTimestamp int64 `json:"event_timestamp,omitempty"`
LastSuccessfulCheckTimestamp int64 `json:"last_successful_check_timestamp"`
Suppressed bool `json:"suppressed,omitempty"`
SuppressedState State `json:"suppressed_state,omitempty"`
Message string `json:"msg,omitempty"`
}
// RemoveMetricState is a function that removes MetricState from map of states.
func (checkData CheckData) RemoveMetricState(metricName string) {
delete(checkData.Metrics, metricName)
}
// MetricState represents metric state data for given timestamp
type MetricState struct {
EventTimestamp int64 `json:"event_timestamp"`
State State `json:"state"`
Suppressed bool `json:"suppressed"`
SuppressedState State `json:"suppressed_state,omitempty"`
Timestamp int64 `json:"timestamp"`
Value *float64 `json:"value,omitempty"`
Values map[string]float64 `json:"values,omitempty"`
Maintenance int64 `json:"maintenance,omitempty"`
MaintenanceInfo MaintenanceInfo `json:"maintenance_info"`
// AloneMetrics map[string]string `json:"alone_metrics"` // represents a relation between name of alone metrics and their targets
}
// SetMaintenance set maintenance user, time for MetricState
func (metricState *MetricState) SetMaintenance(maintenanceInfo *MaintenanceInfo, maintenance int64) {
metricState.MaintenanceInfo = *maintenanceInfo
metricState.Maintenance = maintenance
}
// GetMaintenance return metricState MaintenanceInfo
func (metricState *MetricState) GetMaintenance() (MaintenanceInfo, int64) {
return metricState.MaintenanceInfo, metricState.Maintenance
}
// MaintenanceInfo represents user and time set/unset maintenance
type MaintenanceInfo struct {
StartUser *string `json:"setup_user"`
StartTime *int64 `json:"setup_time"`
StopUser *string `json:"remove_user"`
StopTime *int64 `json:"remove_time"`
}
// Set maintanace start and stop users and times
func (maintenanceInfo *MaintenanceInfo) Set(startUser *string, startTime *int64, stopUser *string, stopTime *int64) {
maintenanceInfo.StartUser = startUser
maintenanceInfo.StartTime = startTime
maintenanceInfo.StopUser = stopUser
maintenanceInfo.StopTime = stopTime
}
// MetricEvent represents filter metric event
type MetricEvent struct {
Metric string `json:"metric"`
Pattern string `json:"pattern"`
}
// SearchHighlight represents highlight
type SearchHighlight struct {
Field string
Value string
}
// SearchResult represents fulltext search result
type SearchResult struct {
ObjectID string
Highlights []SearchHighlight
}
// GetSubjectState returns the most critical state of events
func (events NotificationEvents) GetSubjectState() State {
result := StateOK
states := make(map[State]bool)
for _, event := range events {
states[event.State] = true
}
for _, state := range eventStatesPriority {
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:%s:%d:%t:%d",
notification.Contact.Type,
notification.Contact.Value,
notification.Event.TriggerID,
notification.Event.Metric,
notification.Event.State,
notification.Event.Timestamp,
notification.Event.GetMetricsValues(),
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 //nolint
}
timestamp := ts - ts%60 - schedule.TimezoneOffset*60 //nolint
date := time.Unix(timestamp, 0).UTC()
if !schedule.Days[int(date.Weekday()+6)%7].Enabled { //nolint
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) //nolint
if date.Before(endDayTime) || date.After(startDayTime) {
return true
}
}
return false
}
func (event NotificationEvent) String() string {
return fmt.Sprintf("TriggerId: %s, Metric: %s, Values: %s, OldState: %s, State: %s, Message: '%s', Timestamp: %v", event.TriggerID, event.Metric, event.GetMetricsValues(), event.OldState, event.State, event.CreateMessage(nil), event.Timestamp)
}
// GetMetricsValues gets event metric value and format it to human readable presentation
func (event NotificationEvent) GetMetricsValues() string {
var targetNames []string //nolint
for targetName := range event.Values {
targetNames = append(targetNames, targetName)
}
if len(targetNames) == 0 {
return "—"
}
if len(targetNames) == 1 {
return strconv.FormatFloat(event.Values[targetNames[0]], 'f', -1, 64)
}
var builder strings.Builder
sort.Strings(targetNames)
for i, targetName := range targetNames {
builder.WriteString(targetName)
builder.WriteString(": ")
value := strconv.FormatFloat(event.Values[targetName], 'f', -1, 64)
builder.WriteString(value)
if i < len(targetNames)-1 {
builder.WriteString(", ")
}
}
return builder.String()
}
// FormatTimestamp gets event timestamp and format it using given location to human readable presentation
func (event NotificationEvent) FormatTimestamp(location *time.Location) string {
return time.Unix(event.Timestamp, 0).In(location).Format("15:04")
}
// 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]
}
// SetMaintenance set maintenance user, time for CheckData
func (checkData *CheckData) SetMaintenance(maintenanceInfo *MaintenanceInfo, maintenance int64) {
checkData.MaintenanceInfo = *maintenanceInfo
checkData.Maintenance = maintenance
}
// GetMaintenance return metricState MaintenanceInfo
func (checkData *CheckData) GetMaintenance() (MaintenanceInfo, int64) {
return checkData.MaintenanceInfo, checkData.Maintenance
}
func createEmptyMetricState(defaultTimestampValue int64, firstStateIsNodata bool) MetricState {
if firstStateIsNodata {
return MetricState{
State: StateNODATA,
Timestamp: defaultTimestampValue,
}
}
unixNow := time.Now().Unix()
return MetricState{
State: StateOK,
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 = stateScores[checkData.State]
for _, metricData := range checkData.Metrics {
checkData.Score += stateScores[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
}
// isAnonymous checks if user is Anonymous or empty
func isAnonymous(user string) bool {
return user == "anonymous" || user == ""
}
// SetMaintenanceUserAndTime set startuser and starttime or stopuser and stoptime for MaintenanceInfo
func SetMaintenanceUserAndTime(maintenanceCheck MaintenanceCheck, maintenance int64, user string, callMaintenance int64) {
maintenanceInfo, _ := maintenanceCheck.GetMaintenance()
if maintenance < callMaintenance {
if (maintenanceInfo.StartUser != nil && !isAnonymous(*maintenanceInfo.StartUser)) || !isAnonymous(user) {
maintenanceInfo.StopUser = &user
maintenanceInfo.StopTime = &callMaintenance
}
if isAnonymous(user) {
maintenanceInfo.StopUser = nil
maintenanceInfo.StopTime = nil
}
} else {
if !isAnonymous(user) {
maintenanceInfo.Set(&user, &callMaintenance, nil, nil)
} else {
maintenanceInfo.Set(nil, nil, nil, nil)
}
}
maintenanceCheck.SetMaintenance(&maintenanceInfo, maintenance)
}