Skip to content

Commit

Permalink
Report total and free CPU for vSphere virtual machines (#26167)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jsoriano authored Jun 14, 2021
1 parent 08eaadb commit 6c0f28b
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 27 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -958,6 +958,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*

Expand Down
20 changes: 20 additions & 0 deletions metricbeat/docs/fields.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -53624,6 +53624,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

--
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.

25 changes: 16 additions & 9 deletions metricbeat/module/vsphere/virtualmachine/_meta/data.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand All @@ -17,8 +20,8 @@
"mhz": 0
}
},
"host.id": "ha-host",
"host.hostname": "localhost.localdomain",
"host.id": "ha-host",
"memory": {
"free": {
"guest": {
Expand All @@ -39,7 +42,11 @@
}
}
},
"name": "ha-host_VM1"
"name": "ha-host_VM0",
"network_names": [
"VM Network"
],
"os": "otherGuest"
}
}
}
}
8 changes: 8 additions & 0 deletions metricbeat/module/vsphere/virtualmachine/_meta/fields.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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: >
Expand Down
40 changes: 26 additions & 14 deletions metricbeat/module/vsphere/virtualmachine/virtualmachine.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 6c0f28b

Please sign in to comment.