From 06ddd48cdf5d8907f1d59c872ab79c109cb697a2 Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Mon, 14 Jun 2021 19:18:42 +0200 Subject: [PATCH] Report total and free CPU for vSphere virtual machines (#26167) (#26290) Use configured CPU as total, use it to calculate free too. Total and free resources are only reported if they are configured with a total greater than zero. (cherry picked from commit 6c0f28bd055c4c886c564b4174dc00f64ff0c3b9) Co-authored-by: Jaime Soriano Pastor --- CHANGELOG.next.asciidoc | 1 + metricbeat/docs/fields.asciidoc | 20 ++++++++++ metricbeat/module/vsphere/fields.go | 2 +- .../vsphere/virtualmachine/_meta/data.json | 25 +++++++----- .../vsphere/virtualmachine/_meta/fields.yml | 8 ++++ .../vsphere/virtualmachine/virtualmachine.go | 40 ++++++++++++------- .../virtualmachine/virtualmachine_test.go | 6 +-- 7 files changed, 75 insertions(+), 27 deletions(-) diff --git a/CHANGELOG.next.asciidoc b/CHANGELOG.next.asciidoc index 2b58dd0eacb..7c9b1aa67d8 100644 --- a/CHANGELOG.next.asciidoc +++ b/CHANGELOG.next.asciidoc @@ -661,6 +661,7 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d - Reduce number of requests done by kubernetes metricsets to kubelet. {pull}25782[25782] - Migrate rds metricsets to use cloudwatch input. {pull}26077[26077] - Migrate sqs metricsets to use cloudwatch input. {pull}26117[26117] +- Add total CPU to vSphere virtual machine metrics. {pull}26167[26167] *Packetbeat* diff --git a/metricbeat/docs/fields.asciidoc b/metricbeat/docs/fields.asciidoc index 29ac68a0a08..5c524fba549 100644 --- a/metricbeat/docs/fields.asciidoc +++ b/metricbeat/docs/fields.asciidoc @@ -47456,6 +47456,26 @@ type: keyword Used CPU in Mhz +type: long + +-- + +*`vsphere.virtualmachine.cpu.total.mhz`*:: ++ +-- +Total CPU in Mhz + + +type: long + +-- + +*`vsphere.virtualmachine.cpu.free.mhz`*:: ++ +-- +Available CPU in Mhz + + type: long -- diff --git a/metricbeat/module/vsphere/fields.go b/metricbeat/module/vsphere/fields.go index 97a810ae68c..99cf119cd81 100644 --- a/metricbeat/module/vsphere/fields.go +++ b/metricbeat/module/vsphere/fields.go @@ -32,5 +32,5 @@ func init() { // AssetVsphere returns asset data. // This is the base64 encoded gzipped contents of module/vsphere. func AssetVsphere() string { - return "eJzMlsFymzAQhu9+ip3c4wfg0Es6aS9uO5OmV48Ci61asIy0JEOfviMJPCDkximijQ6eMRL//4HYf3ULJ+wyeDbNETVuAFiywgxunh/clZsNQIEm17JhSXUGHzYAAP0sVFS0yt6mUaEwmMFBbABKiaowmVt6C7WocGxhB3eNXaypbforEZep0FisECwM01kuLnlRtp+KiEyfYxghxhjF/k4mBpITdi+ki2DuDzx2fByY5rqDYWmsfjrLe6nQdIaxgpnw4JmLRuSSuy0TC7V96hhNlEBRfXib/XerCE4RqAQ+YnRj7ChJV4IzmNvPOEuNmBTzXiMmp2wNFkkpHw0W61A2OUcZTS4UFvtSkQgXXMPaoM6x5mtp++Xn2VkwHMnwkkwI7v/fcfCZDF9Ogrxp/eZUx18pP6C7b48ga9gFqmNbnwPpfH0KXGHsCjudryvrV2wrrEivVaw7J27tY9KvV2kPt1YuJ8JbJ46XwtXIL6RPe/svDvZXRfvFy8JU9nwOkppboSqRH2W96PxyUentqWVjbyvD51waXIHgxMz+rJSVQzMJsnytjP7htwF2fh8uxzUl/MZC068NasGyPsCDP8q9t6YxDtFDi4ZXi1Iq4ZM1WBxajtV9rCui+kJJkv7pX+ukByR7r64ZpIcdt4TlrHlrmKq9D80oIT39xNmh2F/cL6jsO2cMEeN/2rZ+BwAA//8rvBJ+" + return "eJzsl8+OmzAQxu95itHedx+AQ6Vqq20vaSttt9fIgSG4MQyyh6zo01e2IeKP002KaS/1ASnYfN8PzHxD7uGIbQInUxeocQPAkhUmcHd6dmfuNgAZmlTLmiVVCbzbAAB0s1BS1ih7mUaFwmACB7EByCWqzCRu6T1UosShhR3c1naxpqbuzgRcxkJDsUywMExnubDkRdluKiAyvo9+TDGGKPY4muhJjti+ks4mc7/hseNDzzTX7Q1zY/XjWT5JhaY1jCXMhHvPVNQildw+MLFQD/uW0QQJFFWH2+y/WUVwikA5cIHBjbEjJ10KTmBuP+PMNWJUzCeNGJ2yMZhFpXwxmK1DWaccZDSpUJjtckViuuAa1hp1ihVfS9stP8/OgqEgw0syYXL9v46DT2T4chKkdeM3pyx+xnyBHr++gKxgO1Ed2vociOfrU+AKY1fY8XxdWb9hW2JJeq1i3Tpxax+SfrtKO7i1cjkS3jpxvBSuQn4lfdzZX2GwPyraz14WxrLn7yCpuRGqFGkhq0XfLxeVbk8tG3sPcnqfS4NrIjgys4eVsrJvJpMsXyujv/ttgK3fh8txTRHfsanplxq1YFkd4Nl/yv1vGus1jfcnIZXYq5s6x6FBw6v1D8rhozVYnNSO1VXoiqg+HaK0vPiPddT4oj1X9wrGhx32weWsaWOYyp3vFEFC2v/A2T8Bf3K3IM4enTEEjP9qr/4VAAD//9YITTs=" } diff --git a/metricbeat/module/vsphere/virtualmachine/_meta/data.json b/metricbeat/module/vsphere/virtualmachine/_meta/data.json index cae229ceaeb..4dfa302758c 100644 --- a/metricbeat/module/vsphere/virtualmachine/_meta/data.json +++ b/metricbeat/module/vsphere/virtualmachine/_meta/data.json @@ -1,14 +1,17 @@ { "@timestamp": "2017-10-12T08:05:34.853Z", - "beat": { - "hostname": "host.example.com", - "name": "host.example.com" + "event": { + "dataset": "vsphere.virtualmachine", + "duration": 115000, + "module": "vsphere" }, "metricset": { - "host": "http://127.0.0.1:35887/sdk", - "module": "vsphere", "name": "virtualmachine", - "rtt": 115 + "period": 10000 + }, + "service": { + "address": "http://127.0.0.1:37231/sdk", + "type": "vsphere" }, "vsphere": { "virtualmachine": { @@ -17,8 +20,8 @@ "mhz": 0 } }, - "host.id": "ha-host", "host.hostname": "localhost.localdomain", + "host.id": "ha-host", "memory": { "free": { "guest": { @@ -39,7 +42,11 @@ } } }, - "name": "ha-host_VM1" + "name": "ha-host_VM0", + "network_names": [ + "VM Network" + ], + "os": "otherGuest" } } -} +} \ No newline at end of file diff --git a/metricbeat/module/vsphere/virtualmachine/_meta/fields.yml b/metricbeat/module/vsphere/virtualmachine/_meta/fields.yml index fbbbeec5691..723d611b2e1 100644 --- a/metricbeat/module/vsphere/virtualmachine/_meta/fields.yml +++ b/metricbeat/module/vsphere/virtualmachine/_meta/fields.yml @@ -24,6 +24,14 @@ type: long description: > Used CPU in Mhz + - name: cpu.total.mhz + type: long + description: > + Total CPU in Mhz + - name: cpu.free.mhz + type: long + description: > + Available CPU in Mhz - name: memory.used.guest.bytes type: long description: > diff --git a/metricbeat/module/vsphere/virtualmachine/virtualmachine.go b/metricbeat/module/vsphere/virtualmachine/virtualmachine.go index 1179b36b5b8..938bf91c97b 100644 --- a/metricbeat/module/vsphere/virtualmachine/virtualmachine.go +++ b/metricbeat/module/vsphere/virtualmachine/virtualmachine.go @@ -132,38 +132,50 @@ func (m *MetricSet) Fetch(ctx context.Context, reporter mb.ReporterV2) error { } for _, vm := range vmt { - freeMemory := (int64(vm.Summary.Config.MemorySizeMB) * 1024 * 1024) - (int64(vm.Summary.QuickStats.GuestMemoryUsage) * 1024 * 1024) - + usedMemory := int64(vm.Summary.QuickStats.GuestMemoryUsage) * 1024 * 1024 + usedCPU := vm.Summary.QuickStats.OverallCpuUsage event := common.MapStr{ "name": vm.Summary.Config.Name, "os": vm.Summary.Config.GuestFullName, "cpu": common.MapStr{ "used": common.MapStr{ - "mhz": vm.Summary.QuickStats.OverallCpuUsage, + "mhz": usedCPU, }, }, "memory": common.MapStr{ "used": common.MapStr{ "guest": common.MapStr{ - "bytes": (int64(vm.Summary.QuickStats.GuestMemoryUsage) * 1024 * 1024), + "bytes": usedMemory, }, "host": common.MapStr{ "bytes": int64(vm.Summary.QuickStats.HostMemoryUsage) * 1024 * 1024, }, }, - "total": common.MapStr{ - "guest": common.MapStr{ - "bytes": int64(vm.Summary.Config.MemorySizeMB) * 1024 * 1024, - }, - }, - "free": common.MapStr{ - "guest": common.MapStr{ - "bytes": freeMemory, - }, - }, }, } + totalCPU := vm.Summary.Config.CpuReservation + if totalCPU > 0 { + freeCPU := totalCPU - usedCPU + // Avoid negative values if reported used CPU is slightly over total configured. + if freeCPU < 0 { + freeCPU = 0 + } + event.Put("cpu.total.mhz", totalCPU) + event.Put("cpu.free.mhz", freeCPU) + } + + totalMemory := int64(vm.Summary.Config.MemorySizeMB) * 1024 * 1024 + if totalMemory > 0 { + freeMemory := totalMemory - usedMemory + // Avoid negative values if reported used memory is slightly over total configured. + if freeMemory < 0 { + freeMemory = 0 + } + event.Put("memory.total.guest.bytes", totalMemory) + event.Put("memory.free.guest.bytes", freeMemory) + } + if host := vm.Summary.Runtime.Host; host != nil { event["host.id"] = host.Value hostSystem, err := getHostSystem(ctx, c, host.Reference()) diff --git a/metricbeat/module/vsphere/virtualmachine/virtualmachine_test.go b/metricbeat/module/vsphere/virtualmachine/virtualmachine_test.go index e3b12feaf4b..83a2db898dd 100644 --- a/metricbeat/module/vsphere/virtualmachine/virtualmachine_test.go +++ b/metricbeat/module/vsphere/virtualmachine/virtualmachine_test.go @@ -21,11 +21,11 @@ import ( "strings" "testing" - "github.com/elastic/beats/v7/libbeat/common" - mbtest "github.com/elastic/beats/v7/metricbeat/mb/testing" - "github.com/stretchr/testify/assert" "github.com/vmware/govmomi/simulator" + + "github.com/elastic/beats/v7/libbeat/common" + mbtest "github.com/elastic/beats/v7/metricbeat/mb/testing" ) func TestFetchEventContents(t *testing.T) {