Skip to content

Commit e7637c0

Browse files
authored
Update fields to use mapstr in vSphere virtualmachine metricset (#40707)
* Convert fields to mapstr * lint changes * Address comments, update changelog * lint change * Address CI failure
1 parent 7176eec commit e7637c0

File tree

9 files changed

+150
-129
lines changed

9 files changed

+150
-129
lines changed

CHANGELOG.next.asciidoc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,11 @@ https://github.com/elastic/beats/compare/v8.8.1\...main[Check the HEAD diff]
5555
- Allow metricsets to report their status via control v2 protocol. {pull}40025[40025]
5656
- Remove fallback to the node limit for the `kubernetes.pod.cpu.usage.limit.pct` and `kubernetes.pod.memory.usage.limit.pct` metrics calculation
5757
- Add support for Kibana status metricset in v8 format {pull}40275[40275]
58-
- Add metrics for the vSphere Virtualmachine metricset. {pull}40485[40485]
5958
- Add new metrics for the vSphere Datastore metricset. {pull}40441[40441]
6059
- Update metrics for the vSphere Host metricset. {pull}40429[40429]
6160
- Mark system process metricsets as running if metrics are partially available {pull}40565[40565]
6261
- Added back `elasticsearch.node.stats.jvm.mem.pools.*` to the `node_stats` metricset {pull}40571[40571]
63-
- Add support for snapshot in vSphere virtualmachine metricset {pull}40683[40683]
62+
6463

6564
*Osquerybeat*
6665

@@ -327,6 +326,9 @@ https://github.com/elastic/beats/compare/v8.8.1\...main[Check the HEAD diff]
327326
- Add new metricset network for the vSphere module. {pull}40559[40559]
328327
- Add new metricset resourcepool for the vSphere module. {pull}40456[40456]
329328
- Log the total time taken for GCP `ListTimeSeries` and `AggregatedList` requests {pull}40661[40661]
329+
- Add metrics for the vSphere Virtualmachine metricset. {pull}40485[40485]
330+
- Add support for snapshot in vSphere virtualmachine metricset {pull}40683[40683]
331+
- Update fields to use mapstr in vSphere virtualmachine metricset {pull}40707[40707]
330332

331333
*Metricbeat*
332334

metricbeat/docs/fields.asciidoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67914,6 +67914,7 @@ virtualmachine
6791467914

6791567915

6791667916

67917+
6791767918
*`vsphere.virtualmachine.host.id`*::
6791867919
+
6791967920
--

metricbeat/module/vsphere/fields.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

metricbeat/module/vsphere/virtualmachine/_meta/data.json

Lines changed: 74 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -14,73 +14,80 @@
1414
"type": "vsphere"
1515
},
1616
"vsphere": {
17-
"virtualmachine": {
18-
"network.names": [
19-
"PROD_VCF"
20-
],
21-
"os": "Microsoft Windows 10 (64-bit)",
22-
"datastore": {
23-
"count": 1
24-
},
25-
"memory": {
26-
"used": {
27-
"guest": {
28-
"bytes": 0
29-
},
30-
"host": {
31-
"bytes": 0
32-
}
33-
},
34-
"total": {
35-
"guest": {
36-
"bytes": 4294967296
37-
}
38-
},
39-
"free": {
40-
"guest": {
41-
"bytes": 4294967296
42-
}
43-
}
44-
},
45-
"network": {
46-
"count": 1
47-
},
48-
"host.hostname": "phx-w1c1",
49-
"name": "phx-14",
50-
"cpu": {
51-
"free": {
52-
"mhz": 0
53-
},
54-
"used": {
55-
"mhz": 0
56-
},
57-
"total": {
58-
"mhz": 0
59-
}
17+
"virtualmachine": {
18+
"name": "xt0nmfpv9",
19+
"uptime": 5348978,
20+
"status": "green",
21+
"host": {
22+
"id": "host-32",
23+
"hostname": "phx-w1c1-esxi04.com"
24+
},
25+
"cpu": {
26+
"free": {
27+
"mhz": 0
28+
},
29+
"used": {
30+
"mhz": 161
31+
},
32+
"total": {
33+
"mhz": 0
34+
}
35+
},
36+
"network": {
37+
"names": [
38+
"PROD_VCF_VMS"
39+
],
40+
"count": 1
41+
},
42+
"memory": {
43+
"used": {
44+
"guest": {
45+
"bytes": 686817280
6046
},
61-
"uptime": 0,
62-
"status": "green",
63-
"network_names": [
64-
"PROD_VCF"
65-
],
66-
"datastore.names": [
67-
"VxRailtec-Virtual-SAN-Datastore-247df-bc1d-5aad2"
68-
],
69-
"host.id": "host-20",
70-
"snapshot.info": [
71-
{
72-
"Name": "Snapshot_1",
73-
"Description": "Test snapshot 1",
74-
"CreateTime": "2024-09-01T12:34:56Z"
75-
76-
},
77-
{
78-
"Name": "Snapshot_2",
79-
"Description": "Test snapshot 2",
80-
"CreateTime": "2024-09-03T2:34:56Z"
81-
}
82-
],
83-
"snapshot.count": 2
47+
"host": {
48+
"bytes": 29027729408
49+
}
50+
},
51+
"total": {
52+
"guest": {
53+
"bytes": 68719476736
54+
}
55+
},
56+
"free": {
57+
"guest": {
58+
"bytes": 68032659456
59+
}
8460
}
85-
}
61+
},
62+
"network_names": [
63+
"PROD_VCF_VMS"
64+
],
65+
"datastore": {
66+
"count": 1,
67+
"names": [
68+
"VxRailtoup-Virtual-Datastore-bc1d-5aa310fb"
69+
]
70+
},
71+
"os": "CentOS 4/5/6/7 (64-bit)",
72+
"snapshot": {
73+
"info": [
74+
{
75+
"id": 123,
76+
"name": "Snapshot_1",
77+
"description": "Test snapshot 1",
78+
"createtime": "2024-09-01T12:34:56Z",
79+
"state": "PoweredOff"
80+
},
81+
{
82+
"id": 745,
83+
"name": "Snapshot_2",
84+
"description": "Test snapshot 2",
85+
"createtime": "2024-09-03T2:34:56Z",
86+
"state": "PoweredOn"
87+
}
88+
],
89+
"count": 2
90+
}
91+
}
8692
}
93+
}

metricbeat/module/vsphere/virtualmachine/_meta/fields.yml

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,17 @@
44
virtualmachine
55
release: ga
66
fields:
7-
- name: host.id
8-
type: keyword
9-
description: >
10-
Host id.
11-
- name: host.hostname
12-
type: keyword
13-
description: >
14-
Hostname of the host.
7+
- name: host
8+
type: group
9+
fields:
10+
- name: id
11+
type: keyword
12+
description: >
13+
Host id.
14+
- name: hostname
15+
type: keyword
16+
description: >
17+
Hostname of the host.
1518
- name: name
1619
type: keyword
1720
description: >

metricbeat/module/vsphere/virtualmachine/data.go

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,14 @@ func (m *MetricSet) mapEvent(data VMData) mapstr.M {
3232
freeMemory := max(0, totalMemory-usedMemory)
3333

3434
event := mapstr.M{
35-
"name": data.VM.Summary.Config.Name,
36-
"os": data.VM.Summary.Config.GuestFullName,
37-
"uptime": data.VM.Summary.QuickStats.UptimeSeconds,
38-
"status": data.VM.Summary.OverallStatus,
39-
"host.id": data.HostID,
40-
"host.hostname": data.HostName,
35+
"name": data.VM.Summary.Config.Name,
36+
"os": data.VM.Summary.Config.GuestFullName,
37+
"uptime": data.VM.Summary.QuickStats.UptimeSeconds,
38+
"status": data.VM.Summary.OverallStatus,
39+
"host": mapstr.M{
40+
"id": data.HostID,
41+
"hostname": data.HostName,
42+
},
4143
"cpu": mapstr.M{
4244
"used": mapstr.M{"mhz": usedCPU},
4345
"total": mapstr.M{"mhz": totalCPU},
@@ -55,26 +57,22 @@ func (m *MetricSet) mapEvent(data VMData) mapstr.M {
5557
"guest": mapstr.M{"bytes": freeMemory},
5658
},
5759
},
58-
"network": mapstr.M{
59-
"count": len(data.NetworkNames),
60-
},
61-
"datastore": mapstr.M{
62-
"count": len(data.DatastoreNames),
63-
},
6460
}
6561
if len(data.CustomFields) > 0 {
6662
event["custom_fields"] = data.CustomFields
6763
}
6864
if len(data.NetworkNames) > 0 {
65+
event.Put("network.count", len(data.NetworkNames))
66+
event.Put("network.names", data.NetworkNames)
6967
event["network_names"] = data.NetworkNames
70-
event["network.names"] = data.NetworkNames
7168
}
7269
if len(data.DatastoreNames) > 0 {
73-
event["datastore.names"] = data.DatastoreNames
70+
event.Put("datastore.count", len(data.DatastoreNames))
71+
event.Put("datastore.names", data.DatastoreNames)
7472
}
7573
if len(data.Snapshots) > 0 {
76-
event["snapshot.info"] = data.Snapshots
77-
event["snapshot.count"] = len(data.Snapshots)
74+
event.Put("snapshot.count", len(data.Snapshots))
75+
event.Put("snapshot.info", data.Snapshots)
7876
}
7977

8078
return event

metricbeat/module/vsphere/virtualmachine/data_test.go

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,10 @@ func TestEventMapping(t *testing.T) {
8181

8282
// Expected event structure
8383
expectedEvent := mapstr.M{
84-
"name": "localhost.localdomain",
85-
"os": "otherGuest",
86-
"uptime": int32(10),
87-
"status": types.ManagedEntityStatus("green"),
88-
"host.id": "host-1234",
89-
"host.hostname": "test-host",
84+
"name": "localhost.localdomain",
85+
"os": "otherGuest",
86+
"uptime": int32(10),
87+
"status": types.ManagedEntityStatus("green"),
9088
"cpu": mapstr.M{
9189
"used": mapstr.M{"mhz": int32(30)},
9290
"total": mapstr.M{"mhz": int32(2294)},
@@ -112,36 +110,42 @@ func TestEventMapping(t *testing.T) {
112110
},
113111
},
114112
},
113+
"host": mapstr.M{
114+
"id": "host-1234",
115+
"hostname": "test-host",
116+
},
115117
"network": mapstr.M{
116118
"count": 2,
119+
"names": []string{"network-1", "network-2"},
117120
},
118121
"datastore": mapstr.M{
119122
"count": 2,
123+
"names": []string{"ds1", "ds2"},
120124
},
121125
"custom_fields": mapstr.M{
122126
"customField1": "value1",
123127
"customField2": "value2",
124128
},
125-
"network.names": []string{"network-1", "network-2"},
126-
"network_names": []string{"network-1", "network-2"},
127-
"datastore.names": []string{"ds1", "ds2"},
128-
"snapshot.info": []VMSnapshotData{
129-
{
130-
ID: 123,
131-
Name: "Snapshot_1",
132-
Description: "Test snapshot 1",
133-
CreateTime: time.Time{},
134-
State: types.VirtualMachinePowerStatePoweredOff,
135-
},
136-
{
137-
ID: 745,
138-
Name: "Snapshot_2",
139-
Description: "Test snapshot 2",
140-
CreateTime: time.Time{},
141-
State: types.VirtualMachinePowerStatePoweredOn,
129+
"network_names": []string{"network-1", "network-2"},
130+
"snapshot": mapstr.M{
131+
"info": []VMSnapshotData{
132+
{
133+
ID: 123,
134+
Name: "Snapshot_1",
135+
Description: "Test snapshot 1",
136+
CreateTime: time.Time{},
137+
State: types.VirtualMachinePowerStatePoweredOff,
138+
},
139+
{
140+
ID: 745,
141+
Name: "Snapshot_2",
142+
Description: "Test snapshot 2",
143+
CreateTime: time.Time{},
144+
State: types.VirtualMachinePowerStatePoweredOn,
145+
},
142146
},
147+
"count": 2,
143148
},
144-
"snapshot.count": 2,
145149
}
146150

147151
// Assert that the output event matches the expected event

metricbeat/module/vsphere/virtualmachine/virtualmachine.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,11 @@ type VMData struct {
6161
}
6262

6363
type VMSnapshotData struct {
64-
ID int32
65-
Name string
66-
Description string
67-
CreateTime time.Time
68-
State types.VirtualMachinePowerState
64+
ID int32 `json:"id"`
65+
Name string `json:"name"`
66+
Description string `json:"description"`
67+
CreateTime time.Time `json:"createtime"`
68+
State types.VirtualMachinePowerState `json:"state"`
6969
}
7070

7171
// New creates a new instance of the MetricSet.

metricbeat/module/vsphere/virtualmachine/virtualmachine_test.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,21 @@ func TestFetchEventContents(t *testing.T) {
4747
event := events[0].MetricSetFields
4848

4949
t.Logf("%s/%s event: %+v", f.Module().Name(), f.Name(), event.StringToPrint())
50-
51-
assert.EqualValues(t, "ha-host", event["host.id"])
52-
assert.EqualValues(t, "localhost.localdomain", event["host.hostname"])
5350
assert.EqualValues(t, "green", event["status"])
5451
assert.EqualValues(t, 0, event["uptime"])
5552
assert.True(t, strings.Contains(event["name"].(string), "ha-host_VM"))
56-
expectedDatastoreNames := []string{"LocalDS_0"}
57-
actualDatastoreNames := event["datastore.names"].([]string)
58-
assert.EqualValues(t, expectedDatastoreNames, actualDatastoreNames)
53+
54+
host := event["host"].(mapstr.M)
55+
assert.EqualValues(t, "ha-host", host["id"])
56+
assert.EqualValues(t, "localhost.localdomain", host["hostname"])
57+
58+
datastore := event["datastore"].(mapstr.M)
59+
assert.EqualValues(t, []string{"LocalDS_0"}, datastore["names"])
60+
assert.EqualValues(t, 1, datastore["count"])
61+
62+
network := event["network"].(mapstr.M)
63+
assert.EqualValues(t, []string{"VM Network"}, network["names"])
64+
assert.EqualValues(t, 1, network["count"])
5965

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

0 commit comments

Comments
 (0)