Skip to content

Commit

Permalink
Issue elastic#7187/fix vsphere metricbeat host (elastic#7213)
Browse files Browse the repository at this point in the history
* added host.id and host.hostname fields for vsphere
  • Loading branch information
marian-craciunescu authored and Pablo Mercado committed Nov 19, 2019
1 parent 9913bbf commit 4288a56
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d
- Fix checking tagsFilter using length in cloudwatch metricset. {pull}14525[14525]
- Log bulk failures from bulk API requests to monitoring cluster. {issue}14303[14303] {pull}14356[14356]
- Fixed bug with `elasticsearch/cluster_stats` metricset not recording license expiration date correctly. {issue}14541[14541] {pull}14591[14591]
- Vshpere module splits `virtualmachine.host` into `virtualmachine.host.id` and `virtualmachine.host.hostname`. {issue}7187[7187] {pull}7213[7213]

*Packetbeat*

Expand Down
14 changes: 12 additions & 2 deletions metricbeat/docs/fields.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -32469,10 +32469,20 @@ virtualmachine



*`vsphere.virtualmachine.host`*::
*`vsphere.virtualmachine.host.id`*::
+
--
Host name
Host id


type: keyword

--

*`vsphere.virtualmachine.host.hostname`*::
+
--
Host name of the host


type: keyword
Expand Down
1 change: 0 additions & 1 deletion metricbeat/magefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ import (

func init() {
common.RegisterCheckDeps(update.Update)

devtools.BeatDescription = "Metricbeat is a lightweight shipper for metrics."
}

Expand Down
2 changes: 1 addition & 1 deletion metricbeat/module/vsphere/fields.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions metricbeat/module/vsphere/virtualmachine/_meta/data.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"mhz": 0
}
},
"host": "ha-host",
"host.id": "ha-host",
"host.hostname": "localhost.localdomain",
"memory": {
"free": {
"guest": {
Expand All @@ -41,4 +42,4 @@
"name": "ha-host_VM1"
}
}
}
}
8 changes: 6 additions & 2 deletions metricbeat/module/vsphere/virtualmachine/_meta/fields.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@
virtualmachine
release: beta
fields:
- name: host
- name: host.id
type: keyword
description: >
Host name
Host id
- name: host.hostname
type: keyword
description: >
Host name of the host
- name: name
type: keyword
description: >
Expand Down
23 changes: 20 additions & 3 deletions metricbeat/module/vsphere/virtualmachine/virtualmachine.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,12 +151,12 @@ func (m *MetricSet) Fetch(ctx context.Context, reporter mb.ReporterV2) error {
"bytes": (int64(vm.Summary.QuickStats.GuestMemoryUsage) * 1024 * 1024),
},
"host": common.MapStr{
"bytes": (int64(vm.Summary.QuickStats.HostMemoryUsage) * 1024 * 1024),
"bytes": int64(vm.Summary.QuickStats.HostMemoryUsage) * 1024 * 1024,
},
},
"total": common.MapStr{
"guest": common.MapStr{
"bytes": (int64(vm.Summary.Config.MemorySizeMB) * 1024 * 1024),
"bytes": int64(vm.Summary.Config.MemorySizeMB) * 1024 * 1024,
},
},
"free": common.MapStr{
Expand All @@ -168,13 +168,19 @@ func (m *MetricSet) Fetch(ctx context.Context, reporter mb.ReporterV2) error {
}

if vm.Summary.Runtime.Host != nil {
event["host"] = vm.Summary.Runtime.Host.Value
event["host.id"] = vm.Summary.Runtime.Host.Value
} else {
m.Logger().Debug("'Host', 'Runtime' or 'Summary' data not found. This is either a parsing error " +
"from vsphere library, an error trying to reach host/guest or incomplete information returned " +
"from host/guest")
}

hostSystem, err := getHostSystem(ctx, c, vm.Summary.Runtime.Host.Reference())
if err != nil {
m.Logger().Debug(err.Error())
} else {
event["host.hostname"] = hostSystem.Summary.Config.Name
}
// Get custom fields (attributes) values if get_custom_fields is true.
if m.GetCustomFields && vm.Summary.CustomValue != nil {
customFields := getCustomFields(vm.Summary.CustomValue, customFieldsMap)
Expand Down Expand Up @@ -285,3 +291,14 @@ func setCustomFieldsMap(ctx context.Context, client *vim25.Client) (map[int32]st

return customFieldsMap, nil
}

func getHostSystem(ctx context.Context, c *vim25.Client, ref types.ManagedObjectReference) (*mo.HostSystem, error) {
pc := property.DefaultCollector(c)

var hs mo.HostSystem
err := pc.RetrieveOne(ctx, ref, []string{"summary"}, &hs)
if err != nil {
return nil, fmt.Errorf("error retrieving host information: %v", err)
}
return &hs, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ func TestFetchEventContents(t *testing.T) {

t.Logf("%s/%s event: %+v", f.Module().Name(), f.Name(), event.StringToPrint())

assert.EqualValues(t, "ha-host", event["host"])
assert.EqualValues(t, "ha-host", event["host.id"])
assert.EqualValues(t, "localhost.localdomain", event["host.hostname"])
assert.True(t, strings.Contains(event["name"].(string), "ha-host_VM"))

cpu := event["cpu"].(common.MapStr)
Expand Down

0 comments on commit 4288a56

Please sign in to comment.