Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Report total CPU for vSphere virtual machines #26167

Merged
merged 20 commits into from
Jun 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -957,6 +957,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 {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Issue created to be able to test with the simulator for different cases of reported metrics #26287

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