forked from bosun-monitor/bosun
-
Notifications
You must be signed in to change notification settings - Fork 0
/
silence.go
155 lines (143 loc) · 3.16 KB
/
silence.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
package sched
import (
"crypto/sha1"
"encoding/json"
"fmt"
"sync"
"time"
"bosun.org/cmd/bosun/expr"
"bosun.org/opentsdb"
)
type Silence struct {
Start, End time.Time
Alert string
Tags opentsdb.TagSet
Forget bool
User string
Message string
}
func (s *Silence) MarshalJSON() ([]byte, error) {
return json.Marshal(struct {
Start, End time.Time
Alert string
Tags string
Forget bool
User string
Message string
}{
Start: s.Start,
End: s.End,
Alert: s.Alert,
Tags: s.Tags.Tags(),
Forget: s.Forget,
User: s.User,
Message: s.Message,
})
}
func (s *Silence) Silenced(now time.Time, alert string, tags opentsdb.TagSet) bool {
if !s.ActiveAt(now) {
return false
}
return s.Matches(alert, tags)
}
func (s *Silence) ActiveAt(now time.Time) bool {
if now.Before(s.Start) || now.After(s.End) {
return false
}
return true
}
func (s *Silence) Matches(alert string, tags opentsdb.TagSet) bool {
if s.Alert != "" && s.Alert != alert {
return false
}
for k, pattern := range s.Tags {
tagv, ok := tags[k]
if !ok {
return false
}
matched, _ := Match(pattern, tagv)
if !matched {
return false
}
}
return true
}
func (s Silence) ID() string {
h := sha1.New()
fmt.Fprintf(h, "%s|%s|%s%s", s.Start, s.End, s.Alert, s.Tags)
return fmt.Sprintf("%x", h.Sum(nil))
}
// Silenced returns all currently silenced AlertKeys and the time they will be
// unsilenced.
func (s *Schedule) Silenced() map[expr.AlertKey]Silence {
aks := make(map[expr.AlertKey]Silence)
now := time.Now()
silenceLock.RLock()
defer silenceLock.RUnlock()
for _, si := range s.Silence {
if !si.ActiveAt(now) {
continue
}
s.Lock("Silence")
for ak := range s.status {
if si.Silenced(now, ak.Name(), ak.Group()) {
if aks[ak].End.Before(si.End) {
aks[ak] = *si
}
}
}
s.Unlock()
}
return aks
}
var silenceLock = sync.RWMutex{}
func (s *Schedule) AddSilence(start, end time.Time, alert, tagList string, forget, confirm bool, edit, user, message string) (map[expr.AlertKey]bool, error) {
if start.IsZero() || end.IsZero() {
return nil, fmt.Errorf("both start and end must be specified")
}
if start.After(end) {
return nil, fmt.Errorf("start time must be before end time")
}
if time.Since(end) > 0 {
return nil, fmt.Errorf("end time must be in the future")
}
if alert == "" && tagList == "" {
return nil, fmt.Errorf("must specify either alert or tags")
}
si := &Silence{
Start: start,
End: end,
Alert: alert,
Tags: make(opentsdb.TagSet),
Forget: forget,
User: user,
Message: message,
}
if tagList != "" {
tags, err := opentsdb.ParseTags(tagList)
if err != nil && tags == nil {
return nil, err
}
si.Tags = tags
}
silenceLock.Lock()
defer silenceLock.Unlock()
if confirm {
delete(s.Silence, edit)
s.Silence[si.ID()] = si
return nil, nil
}
aks := make(map[expr.AlertKey]bool)
for ak := range s.status {
if si.Matches(ak.Name(), ak.Group()) {
aks[ak] = s.status[ak].IsActive()
}
}
return aks, nil
}
func (s *Schedule) ClearSilence(id string) error {
silenceLock.Lock()
defer silenceLock.Unlock()
delete(s.Silence, id)
return nil
}