This repository has been archived by the owner on Nov 8, 2022. It is now read-only.
/
collector.go
339 lines (292 loc) · 8.88 KB
/
collector.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/*
http://www.apache.org/licenses/LICENSE-2.0.txt
Copyright 2016 Intel Corporation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package collector
import (
"fmt"
"strings"
"sync"
"time"
"github.com/rackspace/gophercloud"
"github.com/intelsdi-x/snap/control/plugin"
"github.com/intelsdi-x/snap/control/plugin/cpolicy"
"github.com/intelsdi-x/snap/core"
"github.com/intelsdi-x/snap-plugin-utilities/config"
"github.com/intelsdi-x/snap-plugin-utilities/ns"
"github.com/intelsdi-x/snap-plugin-utilities/str"
openstackintel "github.com/intelsdi-x/snap-plugin-collector-cinder/openstack"
"github.com/intelsdi-x/snap-plugin-collector-cinder/openstack/services"
"github.com/intelsdi-x/snap-plugin-collector-cinder/types"
)
const (
name = "cinder"
version = 3
plgtype = plugin.CollectorPluginType
vendor = "intel"
fs = "openstack"
)
// New creates initialized instance of Cinder collector
func New() *collector {
providers := map[string]*gophercloud.ProviderClient{}
allTenants := map[string]string{}
allLimits := map[string]types.Limits{}
return &collector{
allTenants: allTenants,
providers: providers,
allLimits: allLimits,
}
}
// GetMetricTypes returns list of available metric types
// It returns error in case retrieval was not successful
func (c *collector) GetMetricTypes(cfg plugin.ConfigType) ([]plugin.MetricType, error) {
mts := []plugin.MetricType{}
var err error
c.allTenants, err = getTenants(cfg)
if err != nil {
return nil, err
}
// Generate available namespace for limits
namespaces := []string{}
for _, tenantName := range c.allTenants {
// Construct temporary struct to generate namespace based on tags
var metrics struct {
S types.Snapshots `json:"snapshots"`
V types.Volumes `json:"volumes"`
L types.Limits `json:"limits"`
}
current := strings.Join([]string{vendor, fs, name, tenantName}, "/")
ns.FromCompositionTags(metrics, current, &namespaces)
}
for _, namespace := range namespaces {
mts = append(mts, plugin.MetricType{
Namespace_: core.NewNamespace(strings.Split(namespace, "/")...),
Config_: cfg.ConfigDataNode,
})
}
return mts, nil
}
// CollectMetrics returns list of requested metric values
// It returns error in case retrieval was not successful
func (c *collector) CollectMetrics(metricTypes []plugin.MetricType) ([]plugin.MetricType, error) {
// get admin tenant from configuration. admin tenant is needed for gathering volumes and snapshots metrics at once
item, err := config.GetConfigItem(metricTypes[0], "tenant")
if err != nil {
return nil, err
}
admin := item.(string)
// populate information about all available tenants
if len(c.allTenants) == 0 {
c.allTenants, err = getTenants(metricTypes[0])
if err != nil {
return nil, err
}
}
// iterate over metric types to resolve needed collection calls
// for requested tenants
collectTenants := str.InitSet()
var collectLimits, collectVolumes, collectSnapshots bool
for _, metricType := range metricTypes {
namespace := metricType.Namespace()
if len(namespace) < 6 {
return nil, fmt.Errorf("Incorrect namespace lenth. Expected 6 is %d", len(namespace))
}
tenant := namespace[3].Value
collectTenants.Add(tenant)
if str.Contains(namespace.Strings(), "limits") {
collectLimits = true
} else if str.Contains(namespace.Strings(), "volumes") {
collectVolumes = true
} else {
collectSnapshots = true
}
}
allSnapshots := map[string]types.Snapshots{}
allVolumes := map[string]types.Volumes{}
// collect volumes and snapshots separately by authenticating to admin
{
if err := c.authenticate(metricTypes[0], admin); err != nil {
return nil, err
}
provider := c.providers[admin]
var done sync.WaitGroup
errChn := make(chan error, 2)
// Collect volumes
if collectVolumes {
done.Add(1)
go func() {
defer done.Done()
volumes, err := c.service.GetVolumes(provider)
if err != nil {
errChn <- err
}
for tenantId, volumeCount := range volumes {
tenantName := c.allTenants[tenantId]
allVolumes[tenantName] = volumeCount
}
}()
}
// Collect snapshots
if collectSnapshots {
done.Add(1)
go func() {
defer done.Done()
snapshots, err := c.service.GetSnapshots(provider)
if err != nil {
errChn <- err
}
for tenantId, snapshotCount := range snapshots {
tenantName := c.allTenants[tenantId]
allSnapshots[tenantName] = snapshotCount
}
}()
}
done.Wait()
close(errChn)
if e := <-errChn; e != nil {
return nil, e
}
}
// Collect limits per each tenant only if not already collected (plugin lifetime scope)
{
var done sync.WaitGroup
errChn := make(chan error, collectTenants.Size())
for _, tenant := range collectTenants.Elements() {
_, found := c.allLimits[tenant]
if collectLimits && !found {
if err := c.authenticate(metricTypes[0], tenant); err != nil {
return nil, err
}
provider := c.providers[tenant]
done.Add(1)
go func(p *gophercloud.ProviderClient, t string) {
defer done.Done()
limits, err := c.service.GetLimits(p)
if err != nil {
errChn <- err
}
c.allLimits[t] = limits
}(provider, tenant)
}
}
done.Wait()
close(errChn)
if e := <-errChn; e != nil {
return nil, e
}
}
metrics := []plugin.MetricType{}
for _, metricType := range metricTypes {
namespace := metricType.Namespace().Strings()
tenant := namespace[3]
// Construct temporary struct to accommodate all gathered metrics
metricContainer := struct {
S types.Snapshots `json:"snapshots"`
V types.Volumes `json:"volumes"`
L types.Limits `json:"limits"`
}{
allSnapshots[tenant],
allVolumes[tenant],
c.allLimits[tenant],
}
// Extract values by namespace from temporary struct and create metrics
metric := plugin.MetricType{
Timestamp_: time.Now(),
Namespace_: metricType.Namespace(),
Data_: ns.GetValueByNamespace(metricContainer, namespace[4:]),
}
metrics = append(metrics, metric)
}
return metrics, nil
}
// GetConfigPolicy returns config policy
// It returns error in case retrieval was not successful
func (c *collector) GetConfigPolicy() (*cpolicy.ConfigPolicy, error) {
cp := cpolicy.New()
return cp, nil
}
// Commenting exported items is very important
func Meta() *plugin.PluginMeta {
return plugin.NewPluginMeta(
name,
version,
plgtype,
[]string{plugin.SnapGOBContentType},
[]string{plugin.SnapGOBContentType},
plugin.RoutingStrategy(plugin.StickyRouting),
)
}
type collector struct {
allTenants map[string]string
service services.Service
common openstackintel.Commoner
allLimits map[string]types.Limits
providers map[string]*gophercloud.ProviderClient
}
func (c *collector) authenticate(cfg interface{}, tenant string) error {
if _, found := c.providers[tenant]; !found {
domain_name := ""
domain_id := ""
// get credentials and endpoint from configuration
items, err := config.GetConfigItems(cfg, "endpoint", "user", "password")
if err != nil {
return err
}
endpoint := items["endpoint"].(string)
user := items["user"].(string)
password := items["password"].(string)
dom_name, _ := config.GetConfigItem(cfg, "domain_name")
dom_id, _ := config.GetConfigItem(cfg, "domain_id")
if dom_name != nil {
domain_name = dom_name.(string)
}
if dom_id != nil {
domain_id = dom_id.(string)
}
provider, err := openstackintel.Authenticate(endpoint, user, password, tenant, domain_name, domain_id)
if err != nil {
return err
}
// set provider and dispatch API version based on priority
c.providers[tenant] = provider
c.service = services.Dispatch(provider)
// set Commoner interface
c.common = openstackintel.Common{}
}
return nil
}
func getTenants(cfg interface{}) (map[string]string, error) {
items, err := config.GetConfigItems(cfg, "endpoint", "user", "password")
domain_name := ""
domain_id := ""
if err != nil {
return nil, err
}
endpoint := items["endpoint"].(string)
user := items["user"].(string)
password := items["password"].(string)
dom_name, _ := config.GetConfigItem(cfg, "domain_name")
dom_id, _ := config.GetConfigItem(cfg, "domain_id")
if dom_name != nil {
domain_name = dom_name.(string)
}
if dom_id != nil {
domain_id = dom_id.(string)
}
// retrieve list of all available tenants for provided endpoint, user and password
cmn := openstackintel.Common{}
allTenants, err := cmn.GetTenants(endpoint, user, password, domain_name, domain_id)
if err != nil {
return nil, err
}
return allTenants, nil
}