Skip to content

Commit

Permalink
[vSphere][datastore_cluster] Add support for new metrics in datastore…
Browse files Browse the repository at this point in the history
…_cluster metricset (elastic#40694)

* initial commit

* add childEntity

* add changelog entry

* resolve review comments
  • Loading branch information
kush-elastic authored Sep 9, 2024
1 parent b06f7ce commit 83a880f
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,7 @@ https://github.com/elastic/beats/compare/v8.8.1\...main[Check the HEAD diff]

*Metricbeat*

- Add support for new metrics for vSphere module datastorecluster metricset. {pull}40694[40694]

*Osquerybeat*

Expand Down
20 changes: 20 additions & 0 deletions metricbeat/docs/fields.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -67175,6 +67175,26 @@ format: bytes

--

*`vsphere.datastorecluster.datastore.names`*::
+
--
List of all the Datastore names associated with the Datastore Cluster.


type: keyword

--

*`vsphere.datastorecluster.datastore.count`*::
+
--
Number of datastores in the Datastore Cluster.


type: long

--

[float]
=== host

Expand Down
6 changes: 6 additions & 0 deletions metricbeat/module/vsphere/datastorecluster/_meta/data.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@
},
"free_space": {
"bytes": 8788836876288
},
"datastore": {
"count": 1,
"names": [
"LocalDS_0"
]
}
}
}
Expand Down
10 changes: 9 additions & 1 deletion metricbeat/module/vsphere/datastorecluster/_meta/fields.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,12 @@
type: long
description: >
Total free space on this storage pod, in bytes.
format: bytes
format: bytes
- name: datastore.names
type: keyword
description: >
List of all the Datastore names associated with the Datastore Cluster.
- name: datastore.count
type: long
description: >
Number of datastores in the Datastore Cluster.
22 changes: 14 additions & 8 deletions metricbeat/module/vsphere/datastorecluster/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,18 @@ import (
"github.com/elastic/elastic-agent-libs/mapstr"
)

func (m *DatastoreClusterMetricSet) mapEvent(datastoreCluster mo.StoragePod) mapstr.M {
event := mapstr.M{}

event.Put("name", datastoreCluster.Name)
event.Put("capacity.bytes", datastoreCluster.Summary.Capacity)
event.Put("free_space.bytes", datastoreCluster.Summary.FreeSpace)

return event
func (m *DatastoreClusterMetricSet) mapEvent(datastoreCluster mo.StoragePod, data *metricData) mapstr.M {
return mapstr.M{
"name": datastoreCluster.Name,
"capacity": mapstr.M{
"bytes": datastoreCluster.Summary.Capacity,
},
"free_space": mapstr.M{
"bytes": datastoreCluster.Summary.FreeSpace,
},
"datastore": mapstr.M{
"names": data.assetNames.outputDsNames,
"count": len(data.assetNames.outputDsNames),
},
}
}
8 changes: 7 additions & 1 deletion metricbeat/module/vsphere/datastorecluster/data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func TestEventMapping(t *testing.T) {
},
}

event := (&DatastoreClusterMetricSet{}).mapEvent(datastoreClusterTest)
event := (&DatastoreClusterMetricSet{}).mapEvent(datastoreClusterTest, &metricData{assetNames: assetNames{outputDsNames: []string{"DCS_0"}}})

name, _ := event.GetValue("name")
assert.Equal(t, "Folder1", name)
Expand All @@ -48,4 +48,10 @@ func TestEventMapping(t *testing.T) {

freeSpace, _ := event.GetValue("free_space.bytes")
assert.Equal(t, int64(50), freeSpace)

datastoreNames, _ := event.GetValue("datastore.names")
assert.Equal(t, []string{"DCS_0"}, datastoreNames)

datastoreCount, _ := event.GetValue("datastore.count")
assert.Equal(t, 1, datastoreCount)
}
41 changes: 39 additions & 2 deletions metricbeat/module/vsphere/datastorecluster/datastorecluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ package datastorecluster
import (
"context"
"fmt"
"strings"

"github.com/vmware/govmomi"
"github.com/vmware/govmomi/property"
"github.com/vmware/govmomi/view"
"github.com/vmware/govmomi/vim25/mo"

Expand Down Expand Up @@ -55,6 +57,14 @@ func New(base mb.BaseMetricSet) (mb.MetricSet, error) {
return &DatastoreClusterMetricSet{ms}, nil
}

type metricData struct {
assetNames assetNames
}

type assetNames struct {
outputDsNames []string
}

func (m *DatastoreClusterMetricSet) Fetch(ctx context.Context, reporter mb.ReporterV2) error {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
Expand Down Expand Up @@ -84,18 +94,45 @@ func (m *DatastoreClusterMetricSet) Fetch(ctx context.Context, reporter mb.Repor
}()

var datastoreCluster []mo.StoragePod
err = v.Retrieve(ctx, []string{"StoragePod"}, nil, &datastoreCluster)
err = v.Retrieve(ctx, []string{"StoragePod"}, []string{"name", "summary", "childEntity"}, &datastoreCluster)
if err != nil {
return fmt.Errorf("error in retrieve from vsphere: %w", err)
}

pc := property.DefaultCollector(c)
for i := range datastoreCluster {
if ctx.Err() != nil {
return ctx.Err()
}

reporter.Event(mb.Event{MetricSetFields: m.mapEvent(datastoreCluster[i])})
assetNames, err := getAssetNames(ctx, pc, &datastoreCluster[i])
if err != nil {
m.Logger().Errorf("Failed to retrieve object from host %s: %w", datastoreCluster[i].Name, err)
}

reporter.Event(mb.Event{MetricSetFields: m.mapEvent(datastoreCluster[i], &metricData{assetNames: assetNames})})
}

return nil
}

func getAssetNames(ctx context.Context, pc *property.Collector, dsc *mo.StoragePod) (assetNames, error) {
var objects []mo.ManagedEntity
if len(dsc.ChildEntity) > 0 {
if err := pc.Retrieve(ctx, dsc.ChildEntity, []string{"name"}, &objects); err != nil {
return assetNames{}, err
}
}

outputDsNames := make([]string, 0)
for _, ob := range objects {
if ob.Reference().Type == "Datastore" {
name := strings.ReplaceAll(ob.Name, ".", "_")
outputDsNames = append(outputDsNames, name)
}
}

return assetNames{
outputDsNames: outputDsNames,
}, nil
}
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.

0 comments on commit 83a880f

Please sign in to comment.