Skip to content

Commit

Permalink
Add support of snapshot in vSphere virtualmachine metricset (elastic#…
Browse files Browse the repository at this point in the history
…40683)

* Add support of snapshot in virtualmachine

* mage update and add fields.yml

* Update data_test.go

* address comments, mage update

* Lint error

* Add createtime

* Update CHANGELOG.next.asciidoc

* Add state and id for snapshot

* update changelog

* lint change

* lint change
  • Loading branch information
ishleenk17 authored Sep 6, 2024
1 parent 41b1c4d commit 7f29f60
Show file tree
Hide file tree
Showing 8 changed files with 121 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ https://github.com/elastic/beats/compare/v8.8.1\...main[Check the HEAD diff]
- Update metrics for the vSphere Host metricset. {pull}40429[40429]
- Mark system process metricsets as running if metrics are partially available {pull}40565[40565]
- Added back `elasticsearch.node.stats.jvm.mem.pools.*` to the `node_stats` metricset {pull}40571[40571]
- Add support for snapshot in vSphere virtualmachine metricset {pull}40683[40683]

*Osquerybeat*

Expand Down
21 changes: 20 additions & 1 deletion metricbeat/docs/fields.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -67947,7 +67947,7 @@ type: long
*`vsphere.virtualmachine.cpu.total.mhz`*::
+
--
Total CPU in Mhz.
Total Reserved CPU in Mhz.


type: long
Expand Down Expand Up @@ -68090,6 +68090,25 @@ type: keyword
The uptime of the VM in seconds.


type: long

--


*`vsphere.virtualmachine.snapshot.info`*::
+
--
Deatils of the snapshots of this virtualmachine.

type: object

--

*`vsphere.virtualmachine.snapshot.count`*::
+
--
The number of snapshots of this virtualmachine.

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.

16 changes: 15 additions & 1 deletion metricbeat/module/vsphere/virtualmachine/_meta/data.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,21 @@
"datastore.names": [
"VxRailtec-Virtual-SAN-Datastore-247df-bc1d-5aad2"
],
"host.id": "host-20"
"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
}
}
}
11 changes: 10 additions & 1 deletion metricbeat/module/vsphere/virtualmachine/_meta/fields.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
- name: cpu.total.mhz
type: long
description: >
Total CPU in Mhz.
Total Reserved CPU in Mhz.
- name: cpu.free.mhz
type: long
description: >
Expand Down Expand Up @@ -91,5 +91,14 @@
type: long
description: >
The uptime of the VM in seconds.
- name: snapshot
type: group
fields:
- name: info
type: object
description: Deatils of the snapshots of this virtualmachine.
- name: count
type: long
description: The number of snapshots of this virtualmachine.


5 changes: 5 additions & 0 deletions metricbeat/module/vsphere/virtualmachine/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,10 @@ func (m *MetricSet) mapEvent(data VMData) mapstr.M {
if len(data.DatastoreNames) > 0 {
event["datastore.names"] = data.DatastoreNames
}
if len(data.Snapshots) > 0 {
event["snapshot.info"] = data.Snapshots
event["snapshot.count"] = len(data.Snapshots)
}

return event
}
34 changes: 34 additions & 0 deletions metricbeat/module/vsphere/virtualmachine/data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package virtualmachine

import (
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/vmware/govmomi/vim25/mo"
Expand Down Expand Up @@ -58,6 +59,22 @@ func TestEventMapping(t *testing.T) {
"customField1": "value1",
"customField2": "value2",
},
Snapshots: []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,
},
},
}

event := m.mapEvent(data)
Expand Down Expand Up @@ -108,6 +125,23 @@ func TestEventMapping(t *testing.T) {
"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,
},
},
"snapshot.count": 2,
}

// Assert that the output event matches the expected event
Expand Down
35 changes: 35 additions & 0 deletions metricbeat/module/vsphere/virtualmachine/virtualmachine.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"errors"
"fmt"
"strings"
"time"

"github.com/elastic/beats/v7/metricbeat/mb"
"github.com/elastic/beats/v7/metricbeat/module/vsphere"
Expand Down Expand Up @@ -56,6 +57,15 @@ type VMData struct {
NetworkNames []string
DatastoreNames []string
CustomFields mapstr.M
Snapshots []VMSnapshotData
}

type VMSnapshotData struct {
ID int32
Name string
Description string
CreateTime time.Time
State types.VirtualMachinePowerState
}

// New creates a new instance of the MetricSet.
Expand Down Expand Up @@ -136,6 +146,7 @@ func (m *MetricSet) Fetch(ctx context.Context, reporter mb.ReporterV2) error {
var hostID, hostName string
var networkNames, datastoreNames []string
var customFields mapstr.M
var snapshots []VMSnapshotData

if host := vm.Summary.Runtime.Host; host != nil {
hostID = host.Value
Expand Down Expand Up @@ -179,13 +190,18 @@ func (m *MetricSet) Fetch(ctx context.Context, reporter mb.ReporterV2) error {
}
}

if vm.Snapshot != nil {
snapshots = fetchSnapshots(vm.Snapshot.RootSnapshotList)
}

data := VMData{
VM: vm,
HostID: hostID,
HostName: hostName,
NetworkNames: networkNames,
DatastoreNames: datastoreNames,
CustomFields: customFields,
Snapshots: snapshots,
}

reporter.Event(mb.Event{
Expand Down Expand Up @@ -270,3 +286,22 @@ func getHostSystem(ctx context.Context, c *vim25.Client, ref types.ManagedObject
}
return &hs, nil
}

func fetchSnapshots(snapshotTree []types.VirtualMachineSnapshotTree) []VMSnapshotData {
snapshots := make([]VMSnapshotData, 0, len(snapshotTree))
for _, snapshot := range snapshotTree {
snapshots = append(snapshots, VMSnapshotData{
ID: snapshot.Id,
Name: snapshot.Name,
Description: snapshot.Description,
CreateTime: snapshot.CreateTime,
State: snapshot.State,
})

// Recursively add child snapshots
if len(snapshot.ChildSnapshotList) > 0 {
snapshots = append(snapshots, fetchSnapshots(snapshot.ChildSnapshotList)...)
}
}
return snapshots
}

0 comments on commit 7f29f60

Please sign in to comment.