-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgochecks.go
80 lines (69 loc) · 2.14 KB
/
gochecks.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
// Package gochecks provide a monitoring checks engine to create or embbed
// small monitoring system that can publish the periodic check result to
// other monitoring plataforms
package gochecks
import (
"time"
"github.com/aleasoluciones/goaleasoluciones/scheduledtask"
)
// Event is the check result
type Event struct {
Host string
Service string
State string
Metric interface{}
Description string
Tags []string
Attributes map[string]string
TTL float32
}
type EventFilterFunction func(event Event) (bool, Event)
func NoopEventFilter(event Event) (bool, Event) {
return true, event
}
// CheckEngine monitoring check engine to schedule periodics checks and publish
// the results
type CheckEngine struct {
checkPublishers []CheckPublisher
filterFunc EventFilterFunction
results chan Event
}
// NewCheckEngine return a CheckEngine that publish the results of the
// periodic checks to the given publishers
func NewCheckEngine(publishers []CheckPublisher) *CheckEngine {
checkEngine := CheckEngine{publishers, NoopEventFilter, make(chan Event)}
go func() {
for result := range checkEngine.results {
ok, result := checkEngine.filterFunc(result)
if ok {
for _, publisher := range checkEngine.checkPublishers {
publisher.PublishCheckResult(result)
}
}
}
}()
return &checkEngine
}
func (ce *CheckEngine) SetFilter(f EventFilterFunction) {
ce.filterFunc = f
}
// AddResult publish the given check result as if it was generated by a
// scheduled check
func (ce *CheckEngine) AddResult(event Event) {
ce.results <- event
}
// AddCheck schedule a new check to be executed with the given period
func (ce *CheckEngine) AddCheck(check CheckFunction, period time.Duration) {
scheduledtask.NewScheduledTask(func() {
ce.results <- check()
}, period, 0)
}
// AddMultiCheck schedule a new multi check to be executed with the given period
// the muli check can return an array of events/results
func (ce *CheckEngine) AddMultiCheck(check MultiCheckFunction, period time.Duration) {
scheduledtask.NewScheduledTask(func() {
for _, result := range check() {
ce.results <- result
}
}, period, 0)
}