-
Notifications
You must be signed in to change notification settings - Fork 34
/
bucket.go
121 lines (103 loc) · 3.25 KB
/
bucket.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package bucket
import (
"github.com/netapp/harvest/v2/cmd/collectors/storagegrid/rest"
"github.com/netapp/harvest/v2/cmd/poller/plugin"
"github.com/netapp/harvest/v2/pkg/matrix"
"github.com/netapp/harvest/v2/pkg/util"
"github.com/tidwall/gjson"
)
type Bucket struct {
*plugin.AbstractPlugin
client *rest.Client
data *matrix.Matrix
}
func New(p *plugin.AbstractPlugin) plugin.Plugin {
return &Bucket{AbstractPlugin: p}
}
func (b *Bucket) Init() error {
var err error
if err := b.InitAbc(); err != nil {
return err
}
clientTimeout := b.ParentParams.GetChildContentS("client_timeout")
if b.client, err = rest.NewClient(b.Options.Poller, clientTimeout, b.Auth); err != nil {
b.Logger.Error().Stack().Err(err).Msg("connecting")
return err
}
if err := b.client.Init(5); err != nil {
return err
}
b.data = matrix.New(b.Parent+".Bucket", "bucket", "bucket")
_, _ = b.data.NewMetricFloat64("objects")
_, _ = b.data.NewMetricFloat64("bytes")
return nil
}
func (b *Bucket) Run(dataMap map[string]*matrix.Matrix) ([]*matrix.Matrix, *util.Metadata, error) {
var (
instanceKey string
)
data := dataMap[b.Object]
metricToJSON := map[string]string{
"objects": "objectCount",
"bytes": "dataBytes",
}
// Purge and reset data
b.data.PurgeInstances()
b.data.Reset()
b.client.Metadata.Reset()
// Set all global labels from Rest.go if already not exist
b.data.SetGlobalLabels(data.GetGlobalLabels())
// request the buckets for each tenant
for instKey, inst := range data.GetInstances() {
href := "grid/accounts/" + instKey + "/usage?includeBucketDetail=true"
var records []gjson.Result
err := b.client.Fetch(href, &records)
tenantName := inst.GetLabel("tenant")
if err != nil {
b.Logger.Error().Err(err).
Str("id", instKey).
Str("tenantName", tenantName).
Msg("Unable to fetch bucket details")
continue
}
for _, record := range records {
if !record.IsObject() {
b.Logger.Warn().Str("type", record.Type.String()).Msg("Bucket is not object, skipping")
continue
}
bucketsJSON := record.Get("buckets")
for _, bucketJSON := range bucketsJSON.Array() {
bucket := bucketJSON.Get("name").String()
region := bucketJSON.Get("region").String()
instanceKey = instKey + "#" + bucket
bucketInstance, err2 := b.data.NewInstance(instanceKey)
if err2 != nil {
b.Logger.Error().Err(err).Str("instanceKey", instanceKey).Msg("Failed to add instance")
break
}
b.Logger.Debug().Str("instanceKey", instanceKey).Msg("add instance")
bucketInstance.SetLabel("bucket", bucket)
bucketInstance.SetLabel("tenant", tenantName)
bucketInstance.SetLabel("region", region)
for metricKey, m := range b.data.GetMetrics() {
jsonKey := metricToJSON[metricKey]
if value := bucketJSON.Get(jsonKey); value.Exists() {
if err = m.SetValueString(bucketInstance, value.String()); err != nil {
b.Logger.Error().Err(err).
Str("key", metricKey).
Str("metric", m.GetName()).
Str("value", value.String()).
Msg("Unable to set float key on metric")
} else {
b.Logger.Debug().
Str("metricKey", metricKey).
Str("value", value.String()).
Msg("added")
}
}
}
}
}
}
return []*matrix.Matrix{b.data}, b.client.Metadata, nil
}