Skip to content

Commit

Permalink
[Auditbeat] Cherry-pick #10897 to 6.6: System module: Fix and unify b…
Browse files Browse the repository at this point in the history
…ucket closing logic (#11029)

Cherry-pick of PR #10897 to 6.6 branch. Original message: 

The `host` dataset is erroneously trying to save state in its `Close()` method. It should have saved the state earlier - usually at the end of `Fetch()` - and then should only close the bucket (something it is not doing at all). At the same time, it is not saving state in its `reportState()` method. Combined, this can lead to an error when the dataset is terminated before the first regular `reportChanges()` is run.

This fixes both issues and furthermore unifies the bucket closing logic across all six datasets of the System module.
  • Loading branch information
Christoph Wurm authored Mar 5, 2019
1 parent 8c103f2 commit 4573965
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ https://github.com/elastic/beats/compare/v6.6.1...6.6[Check the HEAD diff]

*Auditbeat*

- System module: Fix and unify bucket closing logic. {pull}10897[10897]

*Filebeat*

*Heartbeat*
Expand Down
27 changes: 16 additions & 11 deletions x-pack/auditbeat/module/system/host/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,10 @@ func New(base mb.BaseMetricSet) (mb.MetricSet, error) {

// Close cleans up the MetricSet when it finishes.
func (ms *MetricSet) Close() error {
return ms.saveStateToDisk()
if ms.bucket != nil {
return ms.bucket.Close()
}
return nil
}

// Fetch collects data about the host. It is invoked periodically.
Expand Down Expand Up @@ -224,7 +227,7 @@ func (ms *MetricSet) reportState(report mb.ReporterV2) error {

report.Event(hostEvent(host, eventTypeState, eventActionHost))

return nil
return ms.saveStateToDisk()
}

// reportChanges detects and reports any changes to this host since the last call.
Expand Down Expand Up @@ -375,17 +378,19 @@ func inflect(noun string, count int) string {
func (ms *MetricSet) saveStateToDisk() error {
var buf bytes.Buffer
encoder := gob.NewEncoder(&buf)
err := encoder.Encode(*ms.lastHost)
if err != nil {
return errors.Wrap(err, "error encoding host information")
}
if ms.lastHost != nil {
err := encoder.Encode(*ms.lastHost)
if err != nil {
return errors.Wrap(err, "error encoding host information")
}

err = ms.bucket.Store(bucketKeyLastHost, buf.Bytes())
if err != nil {
return errors.Wrap(err, "error writing host information to disk")
}
err = ms.bucket.Store(bucketKeyLastHost, buf.Bytes())
if err != nil {
return errors.Wrap(err, "error writing host information to disk")
}

ms.log.Debug("Wrote host information to disk.")
ms.log.Debug("Wrote host information to disk.")
}
return nil
}

Expand Down

0 comments on commit 4573965

Please sign in to comment.