Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cache.ttl to add_host_metadata #9359

Merged
merged 4 commits into from
Dec 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha1...master[Check the HEAD d

- Fix autodiscover configurations stopping when metadata is missing. {pull}8851[8851]
- Log events at the debug level when dropped by encoding problems. {pull}9251[9251]
- Refresh host metadata in add_host_metadata. {pull}9359[9359]

*Auditbeat*

Expand Down Expand Up @@ -63,6 +64,7 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha1...master[Check the HEAD d
*Affecting all Beats*
- Unify dashboard exporter tools. {pull}9097[9097]
- Use _doc as document type of the Elasticsearch major version is 7. {pull}9056[9056]
- Add cache.ttl to add_host_metadata. {pull}9359[9359]

*Auditbeat*

Expand Down
5 changes: 3 additions & 2 deletions libbeat/docs/processors-using.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -894,12 +894,15 @@ beta[]
processors:
- add_host_metadata:
netinfo.enabled: false
cache.ttl: 5m
-------------------------------------------------------------------------------

It has the following settings:

`netinfo.enabled`:: (Optional) Default false. Include IP addresses and MAC addresses as fields host.ip and host.mac

`cache.ttl`:: (Optional) The processor uses an internal cache for the host metadata. This sets the cache expiration time. The default is 5m, negative values disable caching altogether.

The `add_host_metadata` processor annotates each event with relevant metadata from the host machine.
The fields added to the event are looking as following:

Expand All @@ -923,8 +926,6 @@ The fields added to the event are looking as following:
}
-------------------------------------------------------------------------------

NOTE: The host information is refreshed every 5 minutes.

[[dissect]]
=== Dissect strings

Expand Down
38 changes: 22 additions & 16 deletions libbeat/processors/add_host_metadata/add_host_metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,13 @@ import (
"github.com/elastic/beats/libbeat/metric/system/host"
"github.com/elastic/beats/libbeat/processors"
"github.com/elastic/go-sysinfo"
"github.com/elastic/go-sysinfo/types"
)

func init() {
processors.RegisterPlugin("add_host_metadata", newHostMetadataProcessor)
}

type addHostMetadata struct {
info types.HostInfo
lastUpdate struct {
time.Time
sync.Mutex
Expand All @@ -50,8 +48,7 @@ type addHostMetadata struct {
}

const (
processorName = "add_host_metadata"
cacheExpiration = time.Minute * 5
processorName = "add_host_metadata"
)

func newHostMetadataProcessor(cfg *common.Config) (processors.Processor, error) {
Expand All @@ -60,12 +57,7 @@ func newHostMetadataProcessor(cfg *common.Config) (processors.Processor, error)
return nil, errors.Wrapf(err, "fail to unpack the %v configuration", processorName)
}

h, err := sysinfo.Host()
if err != nil {
return nil, err
}
p := &addHostMetadata{
info: h.Info(),
config: config,
data: common.NewMapStrPointer(nil),
}
Expand All @@ -75,28 +67,41 @@ func newHostMetadataProcessor(cfg *common.Config) (processors.Processor, error)

// Run enriches the given event with the host meta data
func (p *addHostMetadata) Run(event *beat.Event) (*beat.Event, error) {
p.loadData()
err := p.loadData()
if err != nil {
return nil, err
}

event.Fields.DeepUpdate(p.data.Get().Clone())
return event, nil
}

func (p *addHostMetadata) expired() bool {
if p.config.CacheTTL <= 0 {
return true
}

p.lastUpdate.Lock()
defer p.lastUpdate.Unlock()

if p.lastUpdate.Add(cacheExpiration).After(time.Now()) {
if p.lastUpdate.Add(p.config.CacheTTL).After(time.Now()) {
return false
}
p.lastUpdate.Time = time.Now()
return true
}

func (p *addHostMetadata) loadData() {
func (p *addHostMetadata) loadData() error {
if !p.expired() {
return
return nil
}

h, err := sysinfo.Host()
if err != nil {
return err
}

data := host.MapHostInfo(p.info)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIUC the problem was here b/c it never fetched updated info

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct

data := host.MapHostInfo(h.Info())
if p.config.NetInfoEnabled {
// IP-address and MAC-address
var ipList, hwList, err = p.getNetInfo()
Expand All @@ -113,6 +118,7 @@ func (p *addHostMetadata) loadData() {
}

p.data.Set(data)
return nil
}

func (p *addHostMetadata) getNetInfo() ([]string, []string, error) {
Expand Down Expand Up @@ -161,6 +167,6 @@ func (p *addHostMetadata) getNetInfo() ([]string, []string, error) {
}

func (p *addHostMetadata) String() string {
return fmt.Sprintf("%v=[netinfo.enabled=[%v]]",
processorName, p.config.NetInfoEnabled)
return fmt.Sprintf("%v=[netinfo.enabled=[%v], cache.ttl=[%v]]",
processorName, p.config.NetInfoEnabled, p.config.CacheTTL)
}
8 changes: 7 additions & 1 deletion libbeat/processors/add_host_metadata/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,19 @@

package add_host_metadata

import (
"time"
)

// Config for add_host_metadata processor.
type Config struct {
NetInfoEnabled bool `config:"netinfo.enabled"` // Add IP and MAC to event
NetInfoEnabled bool `config:"netinfo.enabled"` // Add IP and MAC to event
CacheTTL time.Duration `config:"cache.ttl"`
}

func defaultConfig() Config {
return Config{
NetInfoEnabled: false,
CacheTTL: 5 * time.Minute,
}
}