forked from bosun-monitor/bosun
-
Notifications
You must be signed in to change notification settings - Fork 1
/
alertRunner.go
57 lines (52 loc) · 1.18 KB
/
alertRunner.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
package sched
import (
"fmt"
"time"
"bosun.org/cmd/bosun/cache"
"bosun.org/cmd/bosun/conf"
"bosun.org/slog"
)
// Run should be called once (and only once) to start all schedule activity.
func (s *Schedule) Run() error {
if s.Conf == nil {
return fmt.Errorf("sched: nil configuration")
}
s.nc = make(chan interface{}, 1)
if s.Conf.Ping {
go s.PingHosts()
}
go s.dispatchNotifications()
go s.performSave()
go s.updateCheckContext()
for _, a := range s.Conf.Alerts {
go s.RunAlert(a)
}
return nil
}
func (s *Schedule) updateCheckContext() {
for {
ctx := &checkContext{time.Now(), cache.New(0)}
s.ctx = ctx
time.Sleep(s.Conf.CheckFrequency)
s.Lock("CollectStates")
s.CollectStates()
s.Unlock()
}
}
func (s *Schedule) RunAlert(a *conf.Alert) {
for {
wait := time.After(s.Conf.CheckFrequency * time.Duration(a.RunEvery))
s.checkAlert(a)
s.LastCheck = time.Now()
<-wait
}
}
func (s *Schedule) checkAlert(a *conf.Alert) {
checkTime := s.ctx.runTime
checkCache := s.ctx.checkCache
rh := s.NewRunHistory(checkTime, checkCache)
s.CheckAlert(nil, rh, a)
start := time.Now()
s.RunHistory(rh)
slog.Infof("runHistory on %s took %v\n", a.Name, time.Since(start))
}