forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
manager.go
258 lines (215 loc) · 5.63 KB
/
manager.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
package beater
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"time"
"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/logp"
"github.com/elastic/beats/libbeat/publisher"
"github.com/elastic/beats/heartbeat/monitors"
"github.com/elastic/beats/heartbeat/scheduler"
"github.com/elastic/beats/heartbeat/scheduler/schedule"
"github.com/elastic/beats/heartbeat/watcher"
)
type MonitorManager struct {
monitors []Monitor
jobControl JobControl
client publisher.Client
}
type Monitor struct {
manager *MonitorManager
watcher watcher.Watch
name string
uniqueName string
factory monitors.Factory
config *common.Config
active map[string]MonitorTask
}
type MonitorTask struct {
job monitors.Job
schedule scheduler.Schedule
cancel JobCanceller
}
type JobControl interface {
Add(sched scheduler.Schedule, name string, f scheduler.TaskFunc) func() error
}
type JobCanceller func() error
var defaultFilePollInterval = 5 * time.Second
func newMonitorManager(
client publisher.Client,
jobControl JobControl,
registry *monitors.Registrar,
configs []*common.Config,
) (*MonitorManager, error) {
type watchConfig struct {
Path string `config:"watch.poll_file.path"`
Poll time.Duration `config:"watch.poll_file.interval" validate:"min=1"`
}
defaultWatchConfig := watchConfig{
Poll: defaultFilePollInterval,
}
m := &MonitorManager{
client: client,
jobControl: jobControl,
}
if len(configs) == 0 {
return nil, errors.New("no monitor configured")
}
// check monitors exist
for _, config := range configs {
plugin := struct {
Type string `config:"type" validate:"required"`
Enabled bool `config:"enabled"`
}{
Enabled: true,
}
if err := config.Unpack(&plugin); err != nil {
return nil, err
}
if !plugin.Enabled {
continue
}
info, found := registry.Query(plugin.Type)
if !found {
return nil, fmt.Errorf("Monitor type '%v' does not exist", plugin.Type)
}
logp.Info("Select (%v) monitor %v", info.Type, info.Name)
factory := registry.GetFactory(plugin.Type)
if factory == nil {
return nil, fmt.Errorf("Found non-runnable monitor %v", plugin.Type)
}
m.monitors = append(m.monitors, Monitor{
manager: m,
name: info.Name,
factory: factory,
config: config,
active: map[string]MonitorTask{},
})
}
// load watcher configs
watchConfigs := make([]watchConfig, len(m.monitors))
for i, monitor := range m.monitors {
watchConfigs[i] = defaultWatchConfig
if err := monitor.config.Unpack(&watchConfigs[i]); err != nil {
return nil, err
}
}
// load initial monitors
for _, monitor := range m.monitors {
err := monitor.Update([]*common.Config{monitor.config})
if err != nil {
logp.Err("failed to load monitor tasks: %v", err)
}
}
// start monitor resource watchers if configured (will drop registered monitoring tasks and install new one if resource is available)
for i := range m.monitors {
monitor := &m.monitors[i]
path := watchConfigs[i].Path
if path == "" {
continue
}
poll := watchConfigs[i].Poll
monitor.watcher, _ = watcher.NewFilePoller(path, poll, createWatchUpdater(monitor))
}
return m, nil
}
func (m *Monitor) Update(configs []*common.Config) error {
all := map[string]MonitorTask{}
for i, upd := range configs {
config, err := common.MergeConfigs(m.config, upd)
if err != nil {
logp.Err("Failed merging monitor config with updates: %v", err)
return err
}
shared := struct {
Schedule *schedule.Schedule `config:"schedule" validate:"required"`
}{}
if err := config.Unpack(&shared); err != nil {
logp.Err("Failed parsing job schedule: ", err)
return err
}
jobs, err := m.factory(config)
if err != nil {
err = fmt.Errorf("%v when initializing monitor %v(%v)", err, m.name, i)
return err
}
for _, job := range jobs {
all[job.Name()] = MonitorTask{
job: job,
schedule: shared.Schedule,
}
}
}
// stop all active jobs
for _, job := range m.active {
job.cancel()
}
m.active = map[string]MonitorTask{}
// start new and reconfigured tasks
for name, t := range all {
job := createJob(m.manager.client, name, t.job)
t.cancel = m.manager.jobControl.Add(t.schedule, name, job)
m.active[name] = t
}
return nil
}
func createWatchUpdater(monitor *Monitor) func(content []byte) {
return func(content []byte) {
defer logp.Recover("Failed applying monitor watch")
// read multiple json objects from content
dec := json.NewDecoder(bytes.NewBuffer(content))
var configs []*common.Config
for dec.More() {
var obj map[string]interface{}
err := dec.Decode(&obj)
if err != nil {
logp.Err("Failed parsing json object: %v", err)
return
}
logp.Info("load watch object: %v", obj)
cfg, err := common.NewConfigFrom(obj)
if err != nil {
logp.Err("Failed normalizing json input: %v", err)
return
}
configs = append(configs, cfg)
}
// apply read configurations
if err := monitor.Update(configs); err != nil {
logp.Err("Failed applying configuration: %v", err)
}
}
}
func createJob(
client publisher.Client,
name string,
r monitors.Job,
) scheduler.TaskFunc {
return createJobTask(client, name, r)
}
func createJobTask(
client publisher.Client,
name string,
r monitors.TaskRunner,
) scheduler.TaskFunc {
return func() []scheduler.TaskFunc {
event, next, err := r.Run()
if err != nil {
logp.Err("Job %v failed with: ", err)
}
if event != nil {
event["monitor"] = name
client.PublishEvent(event)
}
if len(next) == 0 {
return nil
}
cont := make([]scheduler.TaskFunc, len(next))
for i, n := range next {
cont[i] = createJobTask(client, name, n)
}
return cont
}
}