-
Notifications
You must be signed in to change notification settings - Fork 3
/
check.go
87 lines (67 loc) · 1.92 KB
/
check.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
package poller
import (
"fmt"
"github.com/marcw/bag"
"time"
)
type CheckType string
const (
CheckTypeUDP CheckType = "udp"
CheckTypeHTTP CheckType = "http"
)
type Check struct {
Key string // Key (should be unique among same Scheduler
checkType CheckType // Type of check
Interval time.Duration // Interval between each check
UpSince time.Time // Time since the service is up
DownSince time.Time // Time since the service is down
WasDownFor time.Duration // Time since the service was down
WasUpFor time.Duration // Time since the service was up
Alert bool // Raise alert if service is down
Alerted bool // Is backend already alerted?
NotifyFix bool // Notify if service is back up
AlertDelay time.Duration // Delay before raising an alert (zero value = NOW)
Config *bag.Bag
}
func newCheck() *Check {
return &Check{Config: bag.NewBag()}
}
func NewCheck(key, interval string, alert bool, alertDelay string, notifyFix bool, config map[string]interface{}) (*Check, error) {
d, err := time.ParseDuration(interval)
if err != nil {
return nil, err
}
var ad time.Duration
if alert {
ad, err = time.ParseDuration(alertDelay)
if err != nil {
return nil, err
}
}
return &Check{Key: key, Interval: d, Alert: alert, AlertDelay: ad, NotifyFix: notifyFix, Config: bag.From(config)}, nil
}
// Check if it's time to send the alert. Returns true if it is.
func (c *Check) ShouldAlert() bool {
return c.Alert && !c.Alerted && c.DownSince.Add(c.AlertDelay).Before(time.Now())
}
func (c *Check) ShouldNotifyFix() bool {
if !c.NotifyFix {
return false
}
if c.WasDownFor == 0 {
return false
}
if c.WasDownFor > 0 && c.NotifyFix && c.Alerted {
return true
}
return false
}
func (c *Check) Type() CheckType {
return c.checkType
}
func (c *Check) AlertDescription() string {
if c.Type() == CheckTypeHTTP {
return fmt.Sprintf("%s (%s)", c.Key, c.Config.GetString("url"))
}
return ""
}