forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
monitors.go
111 lines (87 loc) · 1.93 KB
/
monitors.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
package monitors
import (
"fmt"
"sort"
"strings"
"github.com/elastic/beats/libbeat/common"
)
type Factory func(*common.Config) ([]Job, error)
type ActiveBuilder func(Info, *common.Config) ([]Job, error)
type Job interface {
Name() string
TaskRunner
}
type TaskRunner interface {
Run() (common.MapStr, []TaskRunner, error)
}
type Type uint8
type Info struct {
Name string
Type Type
}
const (
ActiveMonitor Type = iota + 1
PassiveMonitor
)
var Registry = newRegistrar()
type Registrar struct {
modules map[string]entry
}
type entry struct {
info Info
builder ActiveBuilder
}
func newRegistrar() *Registrar {
return &Registrar{
modules: map[string]entry{},
}
}
func RegisterActive(name string, builder ActiveBuilder) {
if err := Registry.AddActive(name, builder); err != nil {
panic(err)
}
}
func (r *Registrar) Register(name string, t Type, builder ActiveBuilder) error {
if _, found := r.modules[name]; found {
return fmt.Errorf("monitor type %v already exists", name)
}
info := Info{Name: name, Type: t}
r.modules[name] = entry{info: info, builder: builder}
return nil
}
func (r *Registrar) Query(name string) (Info, bool) {
e, found := r.modules[name]
return e.info, found
}
func (r *Registrar) GetFactory(name string) Factory {
e, found := r.modules[name]
if !found {
return nil
}
return e.Create
}
func (r *Registrar) AddActive(name string, builder ActiveBuilder) error {
return r.Register(name, ActiveMonitor, builder)
}
func (r *Registrar) String() string {
var monitors []string
for m := range r.modules {
monitors = append(monitors, m)
}
sort.Strings(monitors)
return fmt.Sprintf("Registry, monitors: %v",
strings.Join(monitors, ", "))
}
func (e *entry) Create(cfg *common.Config) ([]Job, error) {
return e.builder(e.info, cfg)
}
func (t Type) String() string {
switch t {
case ActiveMonitor:
return "active"
case PassiveMonitor:
return "passive"
default:
return "unknown type"
}
}