-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
heartbeat.go
178 lines (150 loc) · 5.07 KB
/
heartbeat.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
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package beater
import (
"fmt"
"time"
"github.com/elastic/beats/v7/heartbeat/hbregistry"
"github.com/pkg/errors"
"github.com/elastic/beats/v7/heartbeat/config"
"github.com/elastic/beats/v7/heartbeat/monitors"
"github.com/elastic/beats/v7/heartbeat/scheduler"
"github.com/elastic/beats/v7/libbeat/autodiscover"
"github.com/elastic/beats/v7/libbeat/beat"
"github.com/elastic/beats/v7/libbeat/cfgfile"
"github.com/elastic/beats/v7/libbeat/common"
"github.com/elastic/beats/v7/libbeat/common/reload"
"github.com/elastic/beats/v7/libbeat/logp"
"github.com/elastic/beats/v7/libbeat/management"
)
// Heartbeat represents the root datastructure of this beat.
type Heartbeat struct {
done chan struct{}
// config is used for iterating over elements of the config.
config config.Config
scheduler *scheduler.Scheduler
monitorReloader *cfgfile.Reloader
dynamicFactory *monitors.RunnerFactory
autodiscover *autodiscover.Autodiscover
}
// New creates a new heartbeat.
func New(b *beat.Beat, rawConfig *common.Config) (beat.Beater, error) {
parsedConfig := config.DefaultConfig
if err := rawConfig.Unpack(&parsedConfig); err != nil {
return nil, fmt.Errorf("Error reading config file: %v", err)
}
limit := parsedConfig.Scheduler.Limit
locationName := parsedConfig.Scheduler.Location
if locationName == "" {
locationName = "Local"
}
location, err := time.LoadLocation(locationName)
if err != nil {
return nil, err
}
scheduler := scheduler.NewWithLocation(limit, hbregistry.SchedulerRegistry, location)
bt := &Heartbeat{
done: make(chan struct{}),
config: parsedConfig,
scheduler: scheduler,
// dynamicFactory is the factory used for dynamic configs, e.g. autodiscover / reload
dynamicFactory: monitors.NewFactory(b.Info, scheduler, false),
}
return bt, nil
}
// Run executes the beat.
func (bt *Heartbeat) Run(b *beat.Beat) error {
logp.Info("heartbeat is running! Hit CTRL-C to stop it.")
err := bt.RunStaticMonitors(b)
if err != nil {
return err
}
if b.Manager.Enabled() {
bt.RunCentralMgmtMonitors(b)
}
if bt.config.ConfigMonitors.Enabled() {
bt.monitorReloader = cfgfile.NewReloader(b.Publisher, bt.config.ConfigMonitors)
defer bt.monitorReloader.Stop()
err := bt.RunReloadableMonitors(b)
if err != nil {
return err
}
}
if bt.config.Autodiscover != nil {
bt.autodiscover, err = bt.makeAutodiscover(b)
if err != nil {
return err
}
bt.autodiscover.Start()
defer bt.autodiscover.Stop()
}
if err := bt.scheduler.Start(); err != nil {
return err
}
defer bt.scheduler.Stop()
<-bt.done
logp.Info("Shutting down.")
return nil
}
// RunStaticMonitors runs the `heartbeat.monitors` portion of the yaml config if present.
func (bt *Heartbeat) RunStaticMonitors(b *beat.Beat) error {
factory := monitors.NewFactory(b.Info, bt.scheduler, true)
for _, cfg := range bt.config.Monitors {
created, err := factory.Create(b.Publisher, cfg)
if err != nil {
return errors.Wrap(err, "could not create monitor")
}
created.Start()
}
return nil
}
// RunCentralMgmtMonitors loads any central management configured configs.
func (bt *Heartbeat) RunCentralMgmtMonitors(b *beat.Beat) {
monitors := cfgfile.NewRunnerList(management.DebugK, bt.dynamicFactory, b.Publisher)
reload.Register.MustRegisterList(b.Info.Beat+".monitors", monitors)
inputs := cfgfile.NewRunnerList(management.DebugK, bt.dynamicFactory, b.Publisher)
reload.Register.MustRegisterList("inputs", inputs)
}
// RunReloadableMonitors runs the `heartbeat.config.monitors` portion of the yaml config if present.
func (bt *Heartbeat) RunReloadableMonitors(b *beat.Beat) (err error) {
// Check monitor configs
if err := bt.monitorReloader.Check(bt.dynamicFactory); err != nil {
logp.Error(errors.Wrap(err, "error loading reloadable monitors"))
}
// Execute the monitor
go bt.monitorReloader.Run(bt.dynamicFactory)
return nil
}
// makeAutodiscover creates an autodiscover object ready to be started.
func (bt *Heartbeat) makeAutodiscover(b *beat.Beat) (*autodiscover.Autodiscover, error) {
autodiscover, err := autodiscover.NewAutodiscover(
"heartbeat",
b.Publisher,
bt.dynamicFactory,
autodiscover.QueryConfig(),
bt.config.Autodiscover,
b.Keystore,
)
if err != nil {
return nil, err
}
return autodiscover, nil
}
// Stop stops the beat.
func (bt *Heartbeat) Stop() {
close(bt.done)
}