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

Add support of snapshot in vSphere virtualmachine metricset #40683

Merged
merged 14 commits into from
Sep 6, 2024
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 virtualmachine {pull}40683[40683]
ishleenk17 marked this conversation as resolved.
Show resolved Hide resolved

*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
}
26 changes: 26 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,18 @@ func TestEventMapping(t *testing.T) {
"customField1": "value1",
"customField2": "value2",
},
Snapshots: []VMSnapshotData{
{
Name: "Snapshot_1",
Description: "Test snapshot 1",
CreateTime: time.Time{},
},
{
Name: "Snapshot_2",
Description: "Test snapshot 2",
ishleenk17 marked this conversation as resolved.
Show resolved Hide resolved
CreateTime: time.Time{},
},
},
}

event := m.mapEvent(data)
Expand Down Expand Up @@ -108,6 +121,19 @@ 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{
{
Name: "Snapshot_1",
Description: "Test snapshot 1",
CreateTime: time.Time{},
},
{
Name: "Snapshot_2",
Description: "Test snapshot 2",
CreateTime: time.Time{},
},
},
"snapshot.count": 2,
}

// Assert that the output event matches the expected event
Expand Down
31 changes: 31 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,13 @@ type VMData struct {
NetworkNames []string
DatastoreNames []string
CustomFields mapstr.M
Snapshots []VMSnapshotData
}

type VMSnapshotData struct {
Name string
Description string
CreateTime time.Time
}

// New creates a new instance of the MetricSet.
Expand Down Expand Up @@ -136,6 +144,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 +188,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 +284,20 @@ func getHostSystem(ctx context.Context, c *vim25.Client, ref types.ManagedObject
}
return &hs, nil
}

func fetchSnapshots(snapshotTree []types.VirtualMachineSnapshotTree) []VMSnapshotData {
var snapshots []VMSnapshotData
for _, snapshot := range snapshotTree {
snapshots = append(snapshots, VMSnapshotData{
Name: snapshot.Name,
Description: snapshot.Description,
CreateTime: snapshot.CreateTime,
})

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