This repository has been archived by the owner on Jun 21, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathevent.go
151 lines (140 loc) · 3.91 KB
/
event.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
package notifier
import (
"sync"
)
var (
eventStates = [...]string{"OK", "WARN", "ERROR", "NODATA", "TEST"}
eventStateWeight = map[string]int{
"OK": 0,
"WARN": 1,
"ERROR": 100,
"NODATA": 10000,
}
eventHighDegradationTag = "HIGH DEGRADATION"
eventDegradationTag = "DEGRADATION"
eventProgressTag = "PROGRESS"
)
//GetPseudoTags returns additional subscription tags based on trigger state
func (event *EventData) GetPseudoTags() []string {
tags := []string{event.State, event.OldState}
if oldStateWeight, ok := eventStateWeight[event.OldState]; ok {
if newStateWeight, ok := eventStateWeight[event.State]; ok {
if newStateWeight > oldStateWeight {
if newStateWeight-oldStateWeight >= 100 {
tags = append(tags, eventHighDegradationTag)
}
tags = append(tags, eventDegradationTag)
}
if newStateWeight < oldStateWeight {
tags = append(tags, eventProgressTag)
}
}
}
return tags
}
// ProcessEvent generate notifications from EventData
func ProcessEvent(event EventData) error {
var (
subscriptions []SubscriptionData
tags []string
trigger TriggerData
err error
)
if event.State != "TEST" {
log.Debugf("Processing trigger id %s for metric %s == %f, %s -> %s", event.TriggerID, event.Metric, event.Value, event.OldState, event.State)
trigger, err = db.GetTrigger(event.TriggerID)
if err != nil {
return err
}
tags, err = db.GetTriggerTags(event.TriggerID)
if err != nil {
return err
}
trigger.Tags = tags
tags = append(tags, event.GetPseudoTags()...)
log.Debugf("Getting subscriptions for tags %v", tags)
subscriptions, err = db.GetTagsSubscriptions(tags)
if err != nil {
return err
}
} else {
log.Debugf("Getting subscription id %s for test message", event.SubscriptionID)
sub, err := db.GetSubscription(event.SubscriptionID)
if err != nil {
return err
}
subscriptions = []SubscriptionData{sub}
}
duplications := make(map[string]bool)
for _, subscription := range subscriptions {
if event.State == "TEST" || (subscription.Enabled && subset(subscription.Tags, tags)) {
log.Debugf("Processing contact ids %v for subscription %s", subscription.Contacts, subscription.ID)
for _, contactID := range subscription.Contacts {
contact, err := db.GetContact(contactID)
if err != nil {
log.Warning(err.Error())
continue
}
event.SubscriptionID = subscription.ID
notification := scheduleNotification(event, trigger, contact, false, 0)
key := notification.GetKey()
if _, exist := duplications[key]; !exist {
if err := db.AddNotification(notification); err != nil {
log.Errorf("Failed to save scheduled notification: %s", err)
}
duplications[key] = true
} else {
log.Debugf("Skip duplicated notification for contact %s", notification.Contact)
}
}
} else if !subscription.Enabled {
log.Debugf("Subscription %s is disabled", subscription.ID)
} else {
log.Debugf("Subscription %s has extra tags", subscription.ID)
}
}
return nil
}
// FetchEvents is a cycle that fetches events from database
func FetchEvents(shutdown chan bool, wg *sync.WaitGroup) {
defer wg.Done()
log.Debug("Start Fetching Events")
for {
select {
case <-shutdown:
{
log.Debug("Stop Fetching Events")
return
}
default:
{
event, err := db.FetchEvent()
if err != nil {
eventsMalformed.Mark(1)
continue
}
if event != nil {
eventsReceived.Mark(1)
if err := ProcessEvent(*event); err != nil {
eventsProcessingFailed.Mark(1)
log.Errorf("Failed processEvent. %s", err)
}
}
}
}
}
}
// GetSubjectState returns the most critial state of events
func (events EventsData) 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
}