Skip to content

Commit

Permalink
Fixes to metricbeat's KVM module (#7793) (#8706)
Browse files Browse the repository at this point in the history
Improper error handling caused a panic when connection to libvirtd
couldn't be stablished.

Cleaned up error handling a little bit.

Fixes #7792

The default settings for the kvm module didn't work.

Updated to connect to the unix socket by default, and provide a hint on
how to setup access to libvirtd running on remote hosts.

Co-authored-by: Adrian Serrano <[email protected]>

(cherry picked from commit 76d3949)
  • Loading branch information
jsoriano authored Oct 24, 2018
1 parent eea00c2 commit 1756cc3
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 19 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ https://github.com/elastic/beats/compare/v6.4.0...6.x[Check the HEAD diff]
- Fix issue that would prevent kafka module to find a proper broker when port is not set {pull}8613[8613]
- Fix range colors in multiple visualizations. {issue}8633[8633] {pull}8634[8634]
- Fix incorrect header parsing on http metricbeat module {issue}8564[8564] {pull}8585[8585]
- Fixed a panic when the kvm module cannot establish a connection to libvirtd. {issue}7792[7792].

*Packetbeat*

Expand Down
5 changes: 4 additions & 1 deletion metricbeat/docs/modules/kvm.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ metricbeat.modules:
metricsets: ["dommemstat"]
enabled: true
period: 10s
hosts: ["localhost"]
hosts: ["unix:///var/run/libvirt/libvirt-sock"]
# For remote hosts, setup network access in libvirtd.conf
# and use the tcp scheme:
# hosts: [ "tcp://<host>:16509" ]
# Timeout to connect to Libvirt server
#timeout: 1s
Expand Down
5 changes: 4 additions & 1 deletion metricbeat/metricbeat.reference.yml
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,10 @@ metricbeat.modules:
metricsets: ["dommemstat"]
enabled: true
period: 10s
hosts: ["localhost"]
hosts: ["unix:///var/run/libvirt/libvirt-sock"]
# For remote hosts, setup network access in libvirtd.conf
# and use the tcp scheme:
# hosts: [ "tcp://<host>:16509" ]

# Timeout to connect to Libvirt server
#timeout: 1s
Expand Down
5 changes: 4 additions & 1 deletion metricbeat/module/kvm/_meta/config.reference.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
metricsets: ["dommemstat"]
enabled: true
period: 10s
hosts: ["localhost"]
hosts: ["unix:///var/run/libvirt/libvirt-sock"]
# For remote hosts, setup network access in libvirtd.conf
# and use the tcp scheme:
# hosts: [ "tcp://<host>:16509" ]

# Timeout to connect to Libvirt server
#timeout: 1s
2 changes: 1 addition & 1 deletion metricbeat/module/kvm/_meta/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
#metricsets:
# - dommemstat
period: 10s
hosts: ["localhost"]
hosts: ["unix:///var/run/libvirt/libvirt-sock"]
35 changes: 21 additions & 14 deletions metricbeat/module/kvm/dommemstat/dommemstat.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,18 @@
package dommemstat

import (
"errors"
"net"
"net/url"
"time"

"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/common/cfgwarn"
"github.com/elastic/beats/metricbeat/mb"
"github.com/pkg/errors"

"github.com/digitalocean/go-libvirt"
"github.com/digitalocean/go-libvirt/libvirttest"

"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/common/cfgwarn"
"github.com/elastic/beats/metricbeat/mb"
)

const (
Expand Down Expand Up @@ -101,30 +102,40 @@ func (m *MetricSet) Fetch(report mb.ReporterV2) {

c, err = net.DialTimeout(u.Scheme, address, m.Timeout)
if err != nil {
report.Error(err)
report.Error(errors.Wrapf(err, "cannot connect to %v", u))
return
}
}

defer c.Close()

l := libvirt.New(c)
if err := l.Connect(); err != nil {
report.Error(err)
if err = l.Connect(); err != nil {
report.Error(errors.Wrap(err, "error connecting to libvirtd"))
return
}
defer func() {
if err = l.Disconnect(); err != nil {
report.Error(errors.Wrap(err, "failed to disconnect"))
}
}()

domains, err := l.Domains()
if err != nil {
report.Error(err)
report.Error(errors.Wrap(err, "error listing domains"))
return
}

for _, d := range domains {
gotDomainMemoryStats, err := l.DomainMemoryStats(d, maximumStats, flags)
if err != nil {
report.Error(err)
report.Error(errors.Wrapf(err, "error fetching memory stats for domain %s", d.Name))
continue
}

if len(gotDomainMemoryStats) == 0 {
report.Error(errors.New("no domain memory stats found"))
report.Error(errors.Errorf("no memory stats for domain %s", d.Name))
continue
}

for i := range gotDomainMemoryStats {
Expand All @@ -140,10 +151,6 @@ func (m *MetricSet) Fetch(report mb.ReporterV2) {
})
}
}

if err := l.Disconnect(); err != nil {
report.Error(errors.New("failed to disconnect"))
}
}

func getDomainMemoryStatName(tag int32) string {
Expand Down
2 changes: 1 addition & 1 deletion metricbeat/modules.d/kvm.yml.disabled
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
#metricsets:
# - dommemstat
period: 10s
hosts: ["localhost"]
hosts: ["unix:///var/run/libvirt/libvirt-sock"]

0 comments on commit 1756cc3

Please sign in to comment.