From 7bd59fe8f71ec90a339b268fd2e318379ded5691 Mon Sep 17 00:00:00 2001 From: hardikl Date: Tue, 19 Nov 2024 17:59:22 +0530 Subject: [PATCH] feat: Adding plugin to tracking cluster image update --- .../plugins/clusterupdate/clusterupdate.go | 92 +++++++++++++++++++ cmd/collectors/rest/rest.go | 3 + conf/rest/9.6.0/clusterupdate.yaml | 16 ++-- 3 files changed, 101 insertions(+), 10 deletions(-) create mode 100644 cmd/collectors/rest/plugins/clusterupdate/clusterupdate.go diff --git a/cmd/collectors/rest/plugins/clusterupdate/clusterupdate.go b/cmd/collectors/rest/plugins/clusterupdate/clusterupdate.go new file mode 100644 index 000000000..b002ccced --- /dev/null +++ b/cmd/collectors/rest/plugins/clusterupdate/clusterupdate.go @@ -0,0 +1,92 @@ +package clusterupdate + +import ( + "github.com/netapp/harvest/v2/cmd/poller/plugin" + "github.com/netapp/harvest/v2/pkg/conf" + "github.com/netapp/harvest/v2/pkg/matrix" + "github.com/netapp/harvest/v2/pkg/slogx" + "github.com/netapp/harvest/v2/pkg/tree/node" + "github.com/netapp/harvest/v2/pkg/util" + "github.com/tidwall/gjson" + "log/slog" +) + +type ClusterUpdate struct { + *plugin.AbstractPlugin + data *matrix.Matrix +} + +func New(p *plugin.AbstractPlugin) plugin.Plugin { + return &ClusterUpdate{AbstractPlugin: p} +} + +func (c *ClusterUpdate) Init(conf.Remote) error { + if err := c.InitAbc(); err != nil { + return err + } + + c.data = matrix.New(c.Parent+".ClusterUpdate", "cluster_update", "cluster_update") + exportOptions := node.NewS("export_options") + instanceKeys := exportOptions.NewChildS("instance_keys", "") + instanceKeys.NewChildS("", "phase") + instanceKeys.NewChildS("", "state") + instanceKeys.NewChildS("", "node") + c.data.SetExportOptions(exportOptions) + + if _, err := c.data.NewMetricFloat64("status", "status"); err != nil { + c.SLogger.Error("Failed to create metric", slogx.Err(err), slog.String("metric", "status")) + return err + } + + return nil +} + +func (c *ClusterUpdate) Run(dataMap map[string]*matrix.Matrix) ([]*matrix.Matrix, *util.Metadata, error) { + var ( + clusterUpdateInstance *matrix.Instance + key string + err error + ) + // Purge and reset data + c.data.PurgeInstances() + c.data.Reset() + + // Set all global labels + data := dataMap[c.Object] + c.data.SetGlobalLabels(data.GetGlobalLabels()) + + for _, instance := range data.GetInstances() { + instance.SetExportable(false) + updateDetails := instance.GetLabel("update_details") + updateDetailsJSON := gjson.Result{Type: gjson.JSON, Raw: "[" + updateDetails + "]"} + for _, updateDetail := range updateDetailsJSON.Array() { + phase := updateDetail.Get("phase").String() + state := updateDetail.Get("state").String() + nodeName := updateDetail.Get("node.name").String() + key = phase + state + nodeName + + if clusterUpdateInstance, err = c.data.NewInstance(key); err != nil { + c.SLogger.Error("Failed to create instance", slogx.Err(err), slog.String("key", key)) + continue + } + clusterUpdateInstance.SetLabel("node", nodeName) + clusterUpdateInstance.SetLabel("state", state) + clusterUpdateInstance.SetLabel("phase", phase) + + // populate numeric data + value := 0.0 + if state == "completed" { + value = 1.0 + } + + met := c.data.GetMetric("status") + if err := met.SetValueFloat64(clusterUpdateInstance, value); err != nil { + c.SLogger.Error("Failed to parse value", slogx.Err(err), slog.Float64("value", value)) + } else { + c.SLogger.Debug("added value", slog.Float64("value", value)) + } + } + } + + return []*matrix.Matrix{c.data}, nil, nil +} diff --git a/cmd/collectors/rest/rest.go b/cmd/collectors/rest/rest.go index b723bb1bc..de92ae737 100644 --- a/cmd/collectors/rest/rest.go +++ b/cmd/collectors/rest/rest.go @@ -7,6 +7,7 @@ import ( "github.com/netapp/harvest/v2/cmd/collectors/rest/plugins/aggregate" "github.com/netapp/harvest/v2/cmd/collectors/rest/plugins/certificate" "github.com/netapp/harvest/v2/cmd/collectors/rest/plugins/cluster" + "github.com/netapp/harvest/v2/cmd/collectors/rest/plugins/clusterupdate" "github.com/netapp/harvest/v2/cmd/collectors/rest/plugins/disk" "github.com/netapp/harvest/v2/cmd/collectors/rest/plugins/health" "github.com/netapp/harvest/v2/cmd/collectors/rest/plugins/metroclustercheck" @@ -487,6 +488,8 @@ func (r *Rest) LoadPlugin(kind string, abc *plugin.AbstractPlugin) plugin.Plugin return aggregate.New(abc) case "Cluster": return cluster.New(abc) + case "ClusterUpdate": + return clusterupdate.New(abc) case "Disk": return disk.New(abc) case "Health": diff --git a/conf/rest/9.6.0/clusterupdate.yaml b/conf/rest/9.6.0/clusterupdate.yaml index dba72b979..386e27351 100644 --- a/conf/rest/9.6.0/clusterupdate.yaml +++ b/conf/rest/9.6.0/clusterupdate.yaml @@ -1,16 +1,12 @@ name: ClusterUpdate -query: api/private/cli/cluster/image/show-update-progress +query: api/cluster/software object: cluster_update counters: - - ^^ndu_phase => phase - - ^^phase_description => phase_name - - ^^phase_status => status + - ^update_details => update_details + +plugins: + - ClusterUpdate + -export_options: - instance_keys: - - phase - - status - instance_labels: - - phase_name