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

[8.15](backport #40707) Update fields to use mapstr in vSphere virtualmachine metricset #40832

Merged
merged 2 commits into from
Sep 15, 2024
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
3 changes: 2 additions & 1 deletion CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ https://github.com/elastic/beats/compare/v8.8.1\...main[Check the HEAD diff]
*Metricbeat*

- Setting period for counter cache for Prometheus remote_write at least to 60sec {pull}38553[38553]
- Add metrics for the vSphere Virtualmachine metricset. {pull}40485[40485]
- Add new metrics for the vSphere Datastore metricset. {pull}40441[40441]
- Update metrics for the vSphere Host metricset. {pull}40429[40429]

Expand Down Expand Up @@ -210,7 +209,9 @@ https://github.com/elastic/beats/compare/v8.8.1\...main[Check the HEAD diff]
- Add new metricset network for the vSphere module. {pull}40559[40559]
- Add new metricset resourcepool for the vSphere module. {pull}40456[40456]
- Add new metricset cluster for the vSphere module. {pull}40536[40536]
- Add metrics for the vSphere Virtualmachine metricset. {pull}40485[40485]
- Add support for snapshot in vSphere virtualmachine metricset {pull}40683[40683]
- Update fields to use mapstr in vSphere virtualmachine metricset {pull}40707[40707]

*Metricbeat*

Expand Down
1 change: 1 addition & 0 deletions metricbeat/docs/fields.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -67914,6 +67914,7 @@ virtualmachine




*`vsphere.virtualmachine.host.id`*::
+
--
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.

141 changes: 74 additions & 67 deletions metricbeat/module/vsphere/virtualmachine/_meta/data.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,73 +14,80 @@
"type": "vsphere"
},
"vsphere": {
"virtualmachine": {
"network.names": [
"PROD_VCF"
],
"os": "Microsoft Windows 10 (64-bit)",
"datastore": {
"count": 1
},
"memory": {
"used": {
"guest": {
"bytes": 0
},
"host": {
"bytes": 0
}
},
"total": {
"guest": {
"bytes": 4294967296
}
},
"free": {
"guest": {
"bytes": 4294967296
}
}
},
"network": {
"count": 1
},
"host.hostname": "phx-w1c1",
"name": "phx-14",
"cpu": {
"free": {
"mhz": 0
},
"used": {
"mhz": 0
},
"total": {
"mhz": 0
}
"virtualmachine": {
"name": "xt0nmfpv9",
"uptime": 5348978,
"status": "green",
"host": {
"id": "host-32",
"hostname": "phx-w1c1-esxi04.com"
},
"cpu": {
"free": {
"mhz": 0
},
"used": {
"mhz": 161
},
"total": {
"mhz": 0
}
},
"network": {
"names": [
"PROD_VCF_VMS"
],
"count": 1
},
"memory": {
"used": {
"guest": {
"bytes": 686817280
},
"uptime": 0,
"status": "green",
"network_names": [
"PROD_VCF"
],
"datastore.names": [
"VxRailtec-Virtual-SAN-Datastore-247df-bc1d-5aad2"
],
"host.id": "host-20",
"snapshot.info": [
{
"Name": "Snapshot_1",
"Description": "Test snapshot 1",
"CreateTime": "2024-09-01T12:34:56Z"

},
{
"Name": "Snapshot_2",
"Description": "Test snapshot 2",
"CreateTime": "2024-09-03T2:34:56Z"
}
],
"snapshot.count": 2
"host": {
"bytes": 29027729408
}
},
"total": {
"guest": {
"bytes": 68719476736
}
},
"free": {
"guest": {
"bytes": 68032659456
}
}
}
},
"network_names": [
"PROD_VCF_VMS"
],
"datastore": {
"count": 1,
"names": [
"VxRailtoup-Virtual-Datastore-bc1d-5aa310fb"
]
},
"os": "CentOS 4/5/6/7 (64-bit)",
"snapshot": {
"info": [
{
"id": 123,
"name": "Snapshot_1",
"description": "Test snapshot 1",
"createtime": "2024-09-01T12:34:56Z",
"state": "PoweredOff"
},
{
"id": 745,
"name": "Snapshot_2",
"description": "Test snapshot 2",
"createtime": "2024-09-03T2:34:56Z",
"state": "PoweredOn"
}
],
"count": 2
}
}
}
}
19 changes: 11 additions & 8 deletions metricbeat/module/vsphere/virtualmachine/_meta/fields.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@
virtualmachine
release: ga
fields:
- name: host.id
type: keyword
description: >
Host id.
- name: host.hostname
type: keyword
description: >
Hostname of the host.
- name: host
type: group
fields:
- name: id
type: keyword
description: >
Host id.
- name: hostname
type: keyword
description: >
Hostname of the host.
- name: name
type: keyword
description: >
Expand Down
30 changes: 14 additions & 16 deletions metricbeat/module/vsphere/virtualmachine/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,14 @@ func (m *MetricSet) mapEvent(data VMData) mapstr.M {
freeMemory := max(0, totalMemory-usedMemory)

event := mapstr.M{
"name": data.VM.Summary.Config.Name,
"os": data.VM.Summary.Config.GuestFullName,
"uptime": data.VM.Summary.QuickStats.UptimeSeconds,
"status": data.VM.Summary.OverallStatus,
"host.id": data.HostID,
"host.hostname": data.HostName,
"name": data.VM.Summary.Config.Name,
"os": data.VM.Summary.Config.GuestFullName,
"uptime": data.VM.Summary.QuickStats.UptimeSeconds,
"status": data.VM.Summary.OverallStatus,
"host": mapstr.M{
"id": data.HostID,
"hostname": data.HostName,
},
"cpu": mapstr.M{
"used": mapstr.M{"mhz": usedCPU},
"total": mapstr.M{"mhz": totalCPU},
Expand All @@ -55,26 +57,22 @@ func (m *MetricSet) mapEvent(data VMData) mapstr.M {
"guest": mapstr.M{"bytes": freeMemory},
},
},
"network": mapstr.M{
"count": len(data.NetworkNames),
},
"datastore": mapstr.M{
"count": len(data.DatastoreNames),
},
}
if len(data.CustomFields) > 0 {
event["custom_fields"] = data.CustomFields
}
if len(data.NetworkNames) > 0 {
event.Put("network.count", len(data.NetworkNames))
event.Put("network.names", data.NetworkNames)
event["network_names"] = data.NetworkNames
event["network.names"] = data.NetworkNames
}
if len(data.DatastoreNames) > 0 {
event["datastore.names"] = data.DatastoreNames
event.Put("datastore.count", len(data.DatastoreNames))
event.Put("datastore.names", data.DatastoreNames)
}
if len(data.Snapshots) > 0 {
event["snapshot.info"] = data.Snapshots
event["snapshot.count"] = len(data.Snapshots)
event.Put("snapshot.count", len(data.Snapshots))
event.Put("snapshot.info", data.Snapshots)
}

return event
Expand Down
52 changes: 28 additions & 24 deletions metricbeat/module/vsphere/virtualmachine/data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,10 @@ func TestEventMapping(t *testing.T) {

// Expected event structure
expectedEvent := mapstr.M{
"name": "localhost.localdomain",
"os": "otherGuest",
"uptime": int32(10),
"status": types.ManagedEntityStatus("green"),
"host.id": "host-1234",
"host.hostname": "test-host",
"name": "localhost.localdomain",
"os": "otherGuest",
"uptime": int32(10),
"status": types.ManagedEntityStatus("green"),
"cpu": mapstr.M{
"used": mapstr.M{"mhz": int32(30)},
"total": mapstr.M{"mhz": int32(2294)},
Expand All @@ -112,36 +110,42 @@ func TestEventMapping(t *testing.T) {
},
},
},
"host": mapstr.M{
"id": "host-1234",
"hostname": "test-host",
},
"network": mapstr.M{
"count": 2,
"names": []string{"network-1", "network-2"},
},
"datastore": mapstr.M{
"count": 2,
"names": []string{"ds1", "ds2"},
},
"custom_fields": mapstr.M{
"customField1": "value1",
"customField2": "value2",
},
"network.names": []string{"network-1", "network-2"},
"network_names": []string{"network-1", "network-2"},
"datastore.names": []string{"ds1", "ds2"},
"snapshot.info": []VMSnapshotData{
{
ID: 123,
Name: "Snapshot_1",
Description: "Test snapshot 1",
CreateTime: time.Time{},
State: types.VirtualMachinePowerStatePoweredOff,
},
{
ID: 745,
Name: "Snapshot_2",
Description: "Test snapshot 2",
CreateTime: time.Time{},
State: types.VirtualMachinePowerStatePoweredOn,
"network_names": []string{"network-1", "network-2"},
"snapshot": mapstr.M{
"info": []VMSnapshotData{
{
ID: 123,
Name: "Snapshot_1",
Description: "Test snapshot 1",
CreateTime: time.Time{},
State: types.VirtualMachinePowerStatePoweredOff,
},
{
ID: 745,
Name: "Snapshot_2",
Description: "Test snapshot 2",
CreateTime: time.Time{},
State: types.VirtualMachinePowerStatePoweredOn,
},
},
"count": 2,
},
"snapshot.count": 2,
}

// Assert that the output event matches the expected event
Expand Down
10 changes: 5 additions & 5 deletions metricbeat/module/vsphere/virtualmachine/virtualmachine.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,11 @@ type VMData struct {
}

type VMSnapshotData struct {
ID int32
Name string
Description string
CreateTime time.Time
State types.VirtualMachinePowerState
ID int32 `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
CreateTime time.Time `json:"createtime"`
State types.VirtualMachinePowerState `json:"state"`
}

// New creates a new instance of the MetricSet.
Expand Down
18 changes: 12 additions & 6 deletions metricbeat/module/vsphere/virtualmachine/virtualmachine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,21 @@ func TestFetchEventContents(t *testing.T) {
event := events[0].MetricSetFields

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

assert.EqualValues(t, "ha-host", event["host.id"])
assert.EqualValues(t, "localhost.localdomain", event["host.hostname"])
assert.EqualValues(t, "green", event["status"])
assert.EqualValues(t, 0, event["uptime"])
assert.True(t, strings.Contains(event["name"].(string), "ha-host_VM"))
expectedDatastoreNames := []string{"LocalDS_0"}
actualDatastoreNames := event["datastore.names"].([]string)
assert.EqualValues(t, expectedDatastoreNames, actualDatastoreNames)

host := event["host"].(mapstr.M)
assert.EqualValues(t, "ha-host", host["id"])
assert.EqualValues(t, "localhost.localdomain", host["hostname"])

datastore := event["datastore"].(mapstr.M)
assert.EqualValues(t, []string{"LocalDS_0"}, datastore["names"])
assert.EqualValues(t, 1, datastore["count"])

network := event["network"].(mapstr.M)
assert.EqualValues(t, []string{"VM Network"}, network["names"])
assert.EqualValues(t, 1, network["count"])

cpu := event["cpu"].(mapstr.M)

Expand Down
Loading