forked from sensu/sensu-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
silenced.go
91 lines (76 loc) · 2.69 KB
/
silenced.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
package eventd
import (
"context"
"fmt"
"github.com/sensu/sensu-go/backend/store"
"github.com/sensu/sensu-go/types"
stringsutil "github.com/sensu/sensu-go/util/strings"
)
// addToSilencedBy takes a silenced entry ID and adds it to a silence of IDs if
// it's not already present in order to avoid duplicated elements
func addToSilencedBy(id string, ids []string) []string {
if !stringsutil.InArray(id, ids) {
ids = append(ids, id)
}
return ids
}
// getSilenced retrieves all silenced entries for a given event, using the
// entity subscription, the check subscription and the check name while
// supporting wildcard silenced entries (e.g. subscription:*)
func getSilenced(ctx context.Context, event *types.Event, s store.Store) error {
if !event.HasCheck() {
return nil
}
// Retrieve silenced entries using the entity subscription
entitySubscription := types.GetEntitySubscription(event.Entity.Name)
subscriptions := append(event.Check.Subscriptions, entitySubscription)
entries, err := s.GetSilencedEntriesBySubscription(ctx, subscriptions...)
if err != nil {
return fmt.Errorf("error setting silenced entries: %s", err)
}
// Retrieve silenced entries using the check name
results, err := s.GetSilencedEntriesByCheckName(ctx, event.Check.Name)
if err != nil {
return err
}
entries = append(entries, results...)
// Determine which entries silence this event
silencedIDs := silencedBy(event, entries)
// Add to the event all silenced entries ID that actually silence it
event.Check.Silenced = silencedIDs
return nil
}
// silencedBy determines which of the given silenced entries silenced a given
// event and return a list of silenced entry IDs
func silencedBy(event *types.Event, silencedEntries []*types.Silenced) []string {
silencedBy := event.SilencedBy(silencedEntries)
names := make([]string, 0, len(silencedBy))
for _, entry := range silencedBy {
names = addToSilencedBy(entry.Name, names)
}
return names
}
func handleExpireOnResolveEntries(ctx context.Context, event *types.Event, store store.Store) error {
// Make sure we have a check and that the event is a resolution
if !event.HasCheck() || !event.IsResolution() {
return nil
}
entries, err := store.GetSilencedEntriesByName(ctx, event.Check.Silenced...)
if err != nil {
return fmt.Errorf("couldn't resolve silences: %s", err)
}
toDelete := []string{}
toRetain := []string{}
for _, entry := range entries {
if entry.ExpireOnResolve {
toDelete = append(toDelete, entry.Name)
} else {
toRetain = append(toRetain, entry.Name)
}
}
if err := store.DeleteSilencedEntryByName(ctx, toDelete...); err != nil {
return fmt.Errorf("couldn't resolve silences: %s", err)
}
event.Check.Silenced = toRetain
return nil
}