Skip to content

Commit

Permalink
Merge pull request #775 from bosun-monitor/fix_silence
Browse files Browse the repository at this point in the history
cmd/bosun: Fix silence logic
  • Loading branch information
maddyblue committed Mar 11, 2015
2 parents 1d8f36c + 4b63814 commit 7dc91ad
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
54 changes: 54 additions & 0 deletions cmd/bosun/sched/check_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package sched

import (
"fmt"
"net/http"
"net/http/httptest"
"net/url"
"testing"
"time"

"bosun.org/cmd/bosun/conf"
"bosun.org/cmd/bosun/expr"
Expand Down Expand Up @@ -73,3 +78,52 @@ func TestCheckFlapping(t *testing.T) {
t.Fatal("expected notification")
}
}

func TestCheckSilence(t *testing.T) {
s := new(Schedule)
done := make(chan bool, 1)
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
done <- true
}))
defer ts.Close()
u, err := url.Parse(ts.URL)
if err != nil {
t.Fatal(err)
}
c, err := conf.New("", fmt.Sprintf(`
template t {
subject = "test"
body = "test"
}
notification n {
post = http://%s/
}
alert a {
template = t
warnNotification = n
warn = 1
}
`, u.Host))
if err != nil {
t.Fatal(err)
}
err = s.Init(c)
if err != nil {
t.Fatal(err)
}
_, err = s.AddSilence(time.Now().Add(-time.Hour), time.Now().Add(time.Hour), "a", "", false, true, "")
if err != nil {
t.Fatal(err)
}
_, err = s.Check(nil, time.Now())
if err != nil {
t.Fatal(err)
}
s.CheckNotifications()
select {
case <-done:
t.Fatal("silenced notification was sent")
case <-time.After(time.Second * 2):
// Timeout *probably* means the silence worked
}
}
6 changes: 5 additions & 1 deletion cmd/bosun/sched/notify.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,16 @@ func (s *Schedule) sendNotifications(silenced map[expr.AlertKey]Silence) {
ustates := make(States)
for _, st := range states {
ak := st.AlertKey()
_, silenced := silenced[ak]
if st.Last().Status == StUnknown {
if _, ok := silenced[ak]; ok {
if silenced {
log.Println("silencing unknown", ak)
continue
}
ustates[ak] = st
}
if silenced {
log.Println("silencing", ak)
} else {
s.notify(st, n)
}
Expand Down

0 comments on commit 7dc91ad

Please sign in to comment.