Skip to content

Commit

Permalink
feat: quota template for zapi
Browse files Browse the repository at this point in the history
  • Loading branch information
Hardikl committed Jul 15, 2024
1 parent 0fea81b commit 6223848
Show file tree
Hide file tree
Showing 13 changed files with 483 additions and 69 deletions.
7 changes: 0 additions & 7 deletions cmd/collectors/commonutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,10 +265,3 @@ func SplitVscanName(ontapName string) (string, string, string, bool) {
}
return ontapName[:firstColon], ontapName[firstColon+1 : lastColon], ontapName[lastColon+1:], true
}

func RunPlugin(runPluginIfNoData bool, mat map[string]*matrix.Matrix, err error) (map[string]*matrix.Matrix, error) {
if runPluginIfNoData {
return mat, nil
}
return nil, err
}
34 changes: 0 additions & 34 deletions cmd/collectors/commonutils_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package collectors

import (
"errors"
"github.com/netapp/harvest/v2/pkg/errs"
"github.com/netapp/harvest/v2/pkg/logging"
"github.com/netapp/harvest/v2/pkg/matrix"
"github.com/netapp/harvest/v2/pkg/tree/node"
Expand Down Expand Up @@ -398,35 +396,3 @@ func Test_SplitVscanName(t *testing.T) {
})
}
}

func TestRunPlugin(t *testing.T) {
type test struct {
name string
runPluginIfNoData bool
mat map[string]*matrix.Matrix
err error
wantMatrix bool
wantErr error
}

testMat := make(map[string]*matrix.Matrix)
err := errs.New(errs.ErrConfig, "test Error")

tests := []test{
{"runPluginIfNoData_false", false, testMat, err, false, err},
{"runPluginIfNoData_true", true, testMat, err, true, nil},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := RunPlugin(tt.runPluginIfNoData, tt.mat, tt.err)
if !errors.Is(err, tt.wantErr) {
t.Errorf("RunPlugin() error = %v, wantErr %v", err, tt.wantErr)
return
}
if (got != nil) != tt.wantMatrix {
t.Errorf("RunPlugin() got = %v, wantMatrix %v", got, tt.wantMatrix)
}
})
}
}
17 changes: 8 additions & 9 deletions cmd/collectors/rest/plugins/quota/quota.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func (q *Quota) Run(dataMap map[string]*matrix.Matrix) ([]*matrix.Matrix, *util.
}
}

quotaCount := 0
qtreeCount := 0
if q.historicalLabels {
// In 22.05, populate metrics with qtree prefix and old labels
filter := []string{"qtree=!\"\""}
Expand All @@ -115,20 +115,20 @@ func (q *Quota) Run(dataMap map[string]*matrix.Matrix) ([]*matrix.Matrix, *util.
if result, err = collectors.InvokeRestCall(q.client, href, q.Logger); err != nil {
return nil, nil, err
}
err = q.handlingHistoricalMetrics(result, instanceMap, metricsMap, data, &quotaCount, &numMetrics)
err = q.handlingHistoricalMetrics(result, instanceMap, metricsMap, data, &qtreeCount, &numMetrics)
} else {
// Populate metrics with quota prefix and current labels
err = q.handlingQuotaMetrics(instanceMap, metricsMap, data, &quotaCount, &numMetrics)
err = q.handlingQuotaMetrics(instanceMap, metricsMap, data, &numMetrics)
}

if err != nil {
return nil, nil, err
}

q.client.Metadata.PluginInstances = uint64(quotaCount)
q.client.Metadata.PluginInstances = uint64(qtreeCount)

q.Logger.Info().
Int("numQuotas", quotaCount).
Int("numQtrees", qtreeCount).
Int("metrics", numMetrics).
Msg("Collected")

Expand All @@ -143,7 +143,7 @@ func (q *Quota) Run(dataMap map[string]*matrix.Matrix) ([]*matrix.Matrix, *util.
return nil, q.client.Metadata, nil
}

func (q *Quota) handlingHistoricalMetrics(result []gjson.Result, instanceMap map[string]*matrix.Instance, metricMap map[string]*matrix.Metric, data *matrix.Matrix, quotaCount *int, numMetrics *int) error {
func (q *Quota) handlingHistoricalMetrics(result []gjson.Result, instanceMap map[string]*matrix.Instance, metricMap map[string]*matrix.Metric, data *matrix.Matrix, qtreeCount *int, numMetrics *int) error {
qtreeMap := make(map[string]QtreeData)
for _, qtree := range result {
if !qtree.IsObject() {
Expand All @@ -162,6 +162,7 @@ func (q *Quota) handlingHistoricalMetrics(result []gjson.Result, instanceMap map
// Ex. InstanceKey: vserver1vol1qtree31
qtreeInstanceKey := svm + volume + qtreeName
qtreeMap[qtreeInstanceKey] = QtreeData{oplocks: oplockMode, status: status, exportPolicy: exportPolicy, securityStyle: securityStyle}
*qtreeCount++
}

for _, quota := range instanceMap {
Expand All @@ -171,7 +172,6 @@ func (q *Quota) handlingHistoricalMetrics(result []gjson.Result, instanceMap map
volume := quota.GetLabel("volume")
qtreeInstanceKey := svm + volume + qtreeName
qtreeInstance := qtreeMap[qtreeInstanceKey]
*quotaCount++

for metricName, m := range metricMap {
// set 0 for unlimited
Expand Down Expand Up @@ -223,7 +223,7 @@ func (q *Quota) handlingHistoricalMetrics(result []gjson.Result, instanceMap map
return nil
}

func (q *Quota) handlingQuotaMetrics(instanceMap map[string]*matrix.Instance, metricMap map[string]*matrix.Metric, data *matrix.Matrix, quotaCount *int, numMetrics *int) error {
func (q *Quota) handlingQuotaMetrics(instanceMap map[string]*matrix.Instance, metricMap map[string]*matrix.Metric, data *matrix.Matrix, numMetrics *int) error {
for _, quota := range instanceMap {
index := quota.GetLabel("index")
uName := quota.GetLabel("userName")
Expand All @@ -244,7 +244,6 @@ func (q *Quota) handlingQuotaMetrics(instanceMap map[string]*matrix.Instance, me
quota.SetLabel("group", uid)
}
}
*quotaCount++

for metricName, m := range metricMap {
// set -1 for unlimited
Expand Down
8 changes: 2 additions & 6 deletions cmd/collectors/rest/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ type Rest struct {
Prop *prop
endpoints []*endPoint
isIgnoreUnknownFieldsEnabled bool
RunPluginsIfNoData bool
}

type endPoint struct {
Expand Down Expand Up @@ -194,9 +193,6 @@ func (r *Rest) InitVars(config *node.Node) {
} else {
r.Logger.Info().Str("timeout", rest.DefaultTimeout).Msg("Using default timeout")
}

// if the object template includes a run_plugins_if_no_data, then honour that
r.RunPluginsIfNoData = r.Params.HasChildS("run_plugins_if_no_data")
}

func (r *Rest) InitClient() error {
Expand Down Expand Up @@ -401,12 +397,12 @@ func (r *Rest) PollData() (map[string]*matrix.Matrix, error) {

if records, err = r.GetRestData(r.Prop.Href); err != nil {
r.Logger.Warn().Msgf("error while fetching " + r.Object + " records on cluster")
return collectors.RunPlugin(r.RunPluginsIfNoData, r.Matrix, err)
return nil, err
}

if len(records) == 0 {
r.Logger.Warn().Msgf("no " + r.Object + " instances on cluster")
return collectors.RunPlugin(r.RunPluginsIfNoData, r.Matrix, errs.New(errs.ErrNoInstance, "no "+r.Object+" instances on cluster"))
return nil, errs.New(errs.ErrNoInstance, "no "+r.Object+" instances on cluster")
}

return r.pollData(startTime, records, func(e *endPoint) ([]gjson.Result, time.Duration, error) {
Expand Down
15 changes: 9 additions & 6 deletions cmd/collectors/zapi/collector/zapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/netapp/harvest/v2/cmd/collectors/zapi/plugins/qospolicyadaptive"
"github.com/netapp/harvest/v2/cmd/collectors/zapi/plugins/qospolicyfixed"
"github.com/netapp/harvest/v2/cmd/collectors/zapi/plugins/qtree"
"github.com/netapp/harvest/v2/cmd/collectors/zapi/plugins/quota"
"github.com/netapp/harvest/v2/cmd/collectors/zapi/plugins/security"
"github.com/netapp/harvest/v2/cmd/collectors/zapi/plugins/shelf"
"github.com/netapp/harvest/v2/cmd/collectors/zapi/plugins/snapmirror"
Expand Down Expand Up @@ -50,7 +51,6 @@ type Zapi struct {
instanceKeyPaths [][]string
instanceLabelPaths map[string]string
shortestPathPrefix []string
RunPluginsIfNoData bool
}

func init() {
Expand Down Expand Up @@ -149,8 +149,6 @@ func (z *Zapi) InitVars() error {
z.Client.SetTimeout(timeout)
}

// if the object template includes a run_plugins_if_no_data, then honour that
z.RunPluginsIfNoData = z.Params.HasChildS("run_plugins_if_no_data")
return nil
}

Expand All @@ -162,6 +160,8 @@ func (z *Zapi) LoadPlugin(kind string, abc *plugin.AbstractPlugin) plugin.Plugin
return shelf.New(abc)
case "Qtree":
return qtree.New(abc)
case "Quota":
return quota.New(abc)
case "Volume":
return volume.New(abc)
case "Sensor":
Expand Down Expand Up @@ -295,6 +295,9 @@ func (z *Zapi) PollData() (map[string]*matrix.Matrix, error) {
}
count++
} else if metric := mat.GetMetric(key); metric != nil {
if value == "-" && z.Object == "Quota" {
value = "-1"
}
if err := metric.SetValueString(instance, value); err != nil {
z.Logger.Error().Msgf("%smetric (%s) set value (%s): %v%s", color.Red, key, value, err, color.End)
skipped++
Expand Down Expand Up @@ -341,7 +344,7 @@ func (z *Zapi) PollData() (map[string]*matrix.Matrix, error) {

if err != nil {
z.Logger.Warn().Msgf("error while fetching " + z.Object + " records on cluster")
return collectors.RunPlugin(z.RunPluginsIfNoData, z.Matrix, err)
return nil, err
}

if response == nil {
Expand All @@ -362,7 +365,7 @@ func (z *Zapi) PollData() (map[string]*matrix.Matrix, error) {
if instance == nil {
if instance, err = mat.NewInstance("cluster"); err != nil {
z.Logger.Warn().Msgf("error while creating " + z.Object + " instances on cluster")
return collectors.RunPlugin(z.RunPluginsIfNoData, z.Matrix, err)
return nil, err
}
}
fetch(instance, instances[0], make([]string, 0), false)
Expand Down Expand Up @@ -412,7 +415,7 @@ func (z *Zapi) PollData() (map[string]*matrix.Matrix, error) {

if numInstances == 0 {
z.Logger.Warn().Msgf("no " + z.Object + " instances on cluster")
return collectors.RunPlugin(z.RunPluginsIfNoData, z.Matrix, errs.New(errs.ErrNoInstance, ""))
return nil, errs.New(errs.ErrNoInstance, "")
}

return z.Matrix, nil
Expand Down
Loading

0 comments on commit 6223848

Please sign in to comment.