-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
add_host_metadata.go
320 lines (275 loc) · 8.74 KB
/
add_host_metadata.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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
// 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 add_host_metadata
import (
"fmt"
"sync"
"time"
"github.com/gofrs/uuid"
"github.com/elastic/elastic-agent-libs/monitoring"
"github.com/elastic/beats/v7/libbeat/beat"
"github.com/elastic/beats/v7/libbeat/features"
"github.com/elastic/beats/v7/libbeat/processors"
jsprocessor "github.com/elastic/beats/v7/libbeat/processors/script/javascript/module/processor"
"github.com/elastic/beats/v7/libbeat/processors/util"
"github.com/elastic/elastic-agent-libs/config"
"github.com/elastic/elastic-agent-libs/logp"
"github.com/elastic/elastic-agent-libs/mapstr"
"github.com/elastic/elastic-agent-system-metrics/metric/system/host"
"github.com/elastic/go-sysinfo"
)
const processorName = "add_host_metadata"
const logName = "processor." + processorName
var (
reg *monitoring.Registry
)
func init() {
processors.RegisterPlugin(processorName, New)
jsprocessor.RegisterPlugin("AddHostMetadata", New)
reg = monitoring.Default.NewRegistry(logName, monitoring.DoNotReport)
}
type metrics struct {
FQDNLookupFailed *monitoring.Int
}
type addHostMetadata struct {
lastUpdate struct {
time.Time
sync.Mutex
}
data mapstr.Pointer
geoData mapstr.M
config Config
logger *logp.Logger
metrics metrics
}
// New constructs a new add_host_metadata processor.
func New(cfg *config.C) (beat.Processor, error) {
c := defaultConfig()
if err := cfg.Unpack(&c); err != nil {
return nil, fmt.Errorf("fail to unpack the %v configuration: %w", processorName, err)
}
p := &addHostMetadata{
config: c,
data: mapstr.NewPointer(nil),
logger: logp.NewLogger(logName),
metrics: metrics{
FQDNLookupFailed: monitoring.NewInt(reg, "fqdn_lookup_failed"),
},
}
if err := p.loadData(true, features.FQDN()); err != nil {
return nil, fmt.Errorf("failed to load data: %w", err)
}
if c.Geo != nil {
geoFields, err := util.GeoConfigToMap(*c.Geo)
if err != nil {
return nil, err
}
p.geoData = mapstr.M{"host": mapstr.M{"geo": geoFields}}
}
// create a unique ID for this instance of the processor
cbIDStr := ""
cbID, err := uuid.NewV4()
// if we fail, fall back to the processor name, hope for the best.
if err != nil {
p.logger.Errorf("error generating ID for FQDN callback, reverting to processor name: %w", err)
cbIDStr = processorName
} else {
cbIDStr = cbID.String()
}
// this is safe as New() returns a pointer, not the actual object.
// This matters as other pieces of code in libbeat, like libbeat/processors/processor.go,
// will do weird stuff like copy the entire list of global processors.
err = features.AddFQDNOnChangeCallback(p.handleFQDNReportingChange, cbIDStr)
if err != nil {
return nil, fmt.Errorf(
"could not register callback for FQDN reporting onChange from %s processor: %w",
processorName, err,
)
}
return p, nil
}
// Run enriches the given event with the host metadata
func (p *addHostMetadata) Run(event *beat.Event) (*beat.Event, error) {
// check replace_host_fields field
if !p.config.ReplaceFields && skipAddingHostMetadata(event) {
return event, nil
}
err := p.loadData(true, features.FQDN())
if err != nil {
return nil, fmt.Errorf("error loading data during event update: %w", err)
}
event.Fields.DeepUpdate(p.data.Get().Clone())
if len(p.geoData) > 0 {
event.Fields.DeepUpdate(p.geoData)
}
return event, nil
}
// Ideally we'd be able to implement the Closer interface here and
// deregister the callback. But processors that can be used with the
// `script` processor are not allowed to implement the Closer
// interface (@see https://github.com/elastic/beats/pull/16349).
//func (p *addHostMetadata) Close() error {
// features.RemoveFQDNOnChangeCallback(processorName)
// return nil
//}
func (p *addHostMetadata) expired() bool {
p.lastUpdate.Lock()
defer p.lastUpdate.Unlock()
if p.config.CacheTTL <= 0 {
return true
}
if p.lastUpdate.Add(p.config.CacheTTL).After(time.Now()) {
return false
}
p.lastUpdate.Time = time.Now()
return true
}
// loadData update's the processor's associated host metadata
func (p *addHostMetadata) loadData(checkCache bool, useFQDN bool) error {
if checkCache && !p.expired() {
return nil
}
h, err := sysinfo.Host()
if err != nil {
return fmt.Errorf("error collecting host info: %w", err)
}
hostname := h.Info().Hostname
if useFQDN {
fqdn, err := h.FQDN()
if err != nil {
// FQDN lookup is "best effort". If it fails, we monitor the failure, fallback to
// the OS-reported hostname, and move on.
p.metrics.FQDNLookupFailed.Inc()
p.logger.Warnf(
"unable to lookup FQDN (failed attempt counter: %d): %s, using hostname = %s as FQDN",
p.metrics.FQDNLookupFailed.Get(),
err.Error(),
hostname,
)
} else {
hostname = fqdn
}
}
data := host.MapHostInfo(h.Info(), hostname)
if p.config.NetInfoEnabled {
// IP-address and MAC-address
var ipList, hwList, err = util.GetNetInfo()
if err != nil {
p.logger.Infof("Error when getting network information %v", err)
}
if len(ipList) > 0 {
if _, err := data.Put("host.ip", ipList); err != nil {
return fmt.Errorf("could not set host.ip: %w", err)
}
}
if len(hwList) > 0 {
if _, err := data.Put("host.mac", hwList); err != nil {
return fmt.Errorf("could not set host.mac: %w", err)
}
}
}
if p.config.Name != "" {
if _, err := data.Put("host.name", p.config.Name); err != nil {
return fmt.Errorf("could not set host.name: %w", err)
}
}
p.data.Set(data)
return nil
}
func (p *addHostMetadata) String() string {
return fmt.Sprintf("%v=[netinfo.enabled=[%v], cache.ttl=[%v]]",
processorName, p.config.NetInfoEnabled, p.config.CacheTTL)
}
func (p *addHostMetadata) handleFQDNReportingChange(new, old bool) {
if new == old {
// Nothing to do
return
}
// update the data for the processor
p.updateOrExpire(new)
}
// updateOrExpire will attempt to update the data for the processor, or expire the cache
// if the config update fails, or times out
func (p *addHostMetadata) updateOrExpire(useFQDN bool) {
if p.config.CacheTTL <= 0 {
return
}
p.lastUpdate.Lock()
defer p.lastUpdate.Unlock()
// while holding the mutex, attempt to update loadData()
// doing this with the mutex means other events must wait until we have the correct host data, as we assume that
// a call to this function means something else wants to force an update, and thus all events must sync.
updateChanSuccess := make(chan bool)
timeout := time.After(p.config.ExpireUpdateTimeout)
go func() {
err := p.loadData(false, useFQDN)
if err != nil {
p.logger.Errorf("error updating data for processor: %w")
updateChanSuccess <- false
return
}
updateChanSuccess <- true
}()
// this additional timeout check is paranoid, but when it's method is called from handleFQDNReportingChange(),
// it's blocking, which means we can hold a mutex in features. In addition, we don't want to break the processor by
// having all the events wait for too long.
select {
case <-timeout:
p.logger.Errorf("got timeout while trying to update metadata")
p.lastUpdate.Time = time.Time{}
case success := <-updateChanSuccess:
// only expire the cache if update was failed
if !success {
p.lastUpdate.Time = time.Time{}
} else {
p.lastUpdate.Time = time.Now()
}
}
}
func skipAddingHostMetadata(event *beat.Event) bool {
// If host fields exist(besides host.name added by libbeat) in event, skip add_host_metadata.
hostFields, err := event.Fields.GetValue("host")
// Don't skip if there are no fields
if err != nil || hostFields == nil {
return false
}
switch m := hostFields.(type) {
case mapstr.M:
// if "name" is the only field, don't skip
hasName, _ := m.HasKey("name")
if hasName && len(m) == 1 {
return false
}
return true
case map[string]interface{}:
hostMapStr := mapstr.M(m)
// if "name" is the only field, don't skip
hasName, _ := hostMapStr.HasKey("name")
if hasName && len(m) == 1 {
return false
}
return true
case map[string]string:
// if "name" is the only field, don't skip
if m["name"] != "" && len(m) == 1 {
return false
}
return true
default:
return false
}
}