diff --git a/CHANGELOG.next.asciidoc b/CHANGELOG.next.asciidoc index 559f671decdd..d1f7ba1935b8 100644 --- a/CHANGELOG.next.asciidoc +++ b/CHANGELOG.next.asciidoc @@ -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* diff --git a/x-pack/auditbeat/module/system/host/host.go b/x-pack/auditbeat/module/system/host/host.go index 0d7f85ba0c15..d4d867ef6760 100644 --- a/x-pack/auditbeat/module/system/host/host.go +++ b/x-pack/auditbeat/module/system/host/host.go @@ -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. @@ -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. @@ -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 }