-
Notifications
You must be signed in to change notification settings - Fork 0
/
exporter.go
executable file
·446 lines (410 loc) · 12.4 KB
/
exporter.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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
package main
import (
"encoding/json"
"errors"
"io/ioutil"
"log"
"net"
"os"
"sort"
"strings"
"github.com/avinetworks/sdk/go/clients"
"github.com/avinetworks/sdk/go/session"
"github.com/prometheus/client_golang/prometheus"
"github.com/tidwall/pretty"
)
func formatAviRef(in string) string {
uriArr := strings.SplitAfter(in, "/")
return uriArr[len(uriArr)-1]
}
func fromJSONFile(path string, ob interface{}) (err error) {
toReturn := ob
openedFile, err := os.Open(path)
if err != nil {
log.Println(err)
return
}
byteValue, err := ioutil.ReadAll(openedFile)
if err != nil {
log.Println(err)
return
}
err = json.Unmarshal(byteValue, &toReturn)
if err != nil {
log.Println(err)
return
}
defer openedFile.Close()
return
}
func (o *Exporter) getDefaultMetrics(entityType string) (r DefaultMetrics, err error) {
var path string
r = DefaultMetrics{}
switch entityType {
case "virtualservice":
path = "lib/virtualservice_metrics.json"
case "serviceengine":
path = "lib/serviceengine_metrics.json"
case "controller":
path = "lib/controller_metrics.json"
default:
err = errors.New("entity type must be either: virtualserver, servicengine or controller")
log.Panic(err)
}
err = fromJSONFile(path, &r)
if err != nil {
log.Panic(err)
}
return
}
func (o *Exporter) setAllMetricsMap() (r GaugeOptsMap) {
r = make(GaugeOptsMap)
//////////////////////////////////////////////////////////////////////////////
// Get default metrics.
//////////////////////////////////////////////////////////////////////////////
vsDefaultMetrics, err := o.getDefaultMetrics("virtualservice")
if err != nil {
log.Panic(err)
}
seDefaultMetrics, err := o.getDefaultMetrics("serviceengine")
if err != nil {
log.Panic(err)
}
controllerDefaultMetrics, err := o.getDefaultMetrics("controller")
if err != nil {
log.Panic(err)
}
//////////////////////////////////////////////////////////////////////////////
// Populating default metrics. Leaving these as separate functions
// in the event we want different GaugeOpts in the future.
//////////////////////////////////////////////////////////////////////////////
for _, v := range vsDefaultMetrics {
fName := strings.ReplaceAll(v.Metric, ".", "_")
r[v.Metric] = GaugeOpts{CustomLabels: []string{"name", "fqdn", "ipaddress", "pool", "tenant_uuid", "units", "cluster"}, Type: "virtualservice", GaugeOpts: prometheus.GaugeOpts{Name: fName, Help: v.Help}}
}
for _, v := range seDefaultMetrics {
fName := strings.ReplaceAll(v.Metric, ".", "_")
r[v.Metric] = GaugeOpts{CustomLabels: []string{"name", "entity_uuid", "fqdn", "ipaddress", "tenant_uuid", "units", "cluster"}, Type: "serviceengine", GaugeOpts: prometheus.GaugeOpts{Name: fName, Help: v.Help}}
}
for _, v := range controllerDefaultMetrics {
fName := strings.ReplaceAll(v.Metric, ".", "_")
r[v.Metric] = GaugeOpts{CustomLabels: []string{"name", "entity_uuid", "fqdn", "ipaddress", "tenant_uuid", "units", "cluster"}, Type: "controller", GaugeOpts: prometheus.GaugeOpts{Name: fName, Help: v.Help}}
}
//////////////////////////////////////////////////////////////////////////////
return
}
func (o *Exporter) setPromMetricsMap() (r GaugeOptsMap) {
r = make(GaugeOptsMap)
all := o.setAllMetricsMap()
if o.userMetricString == "" {
r = all
return
}
/////////////////////////////////////////////////////////
// User provided metrics list
/////////////////////////////////////////////////////////
metrics := strings.Split(o.userMetricString, ",")
for _, v := range metrics {
r[v] = all[v]
}
return
}
func (o *Exporter) setUserMetrics() (r string) {
r = os.Getenv("AVI_METRICS")
return
}
// NewExporter constructor.
func NewExporter() (r *Exporter) {
r = new(Exporter)
r.userMetricString = r.setUserMetrics()
r.connectionOpts = r.setConnectionOpts()
r.GaugeOptsMap = r.setPromMetricsMap()
return
}
func (o *Exporter) setConnectionOpts() (r connectionOpts) {
r.username = os.Getenv("AVI_USERNAME")
r.password = os.Getenv("AVI_PASSWORD")
r.cluster = os.Getenv("AVI_CLUSTER")
r.tenant = os.Getenv("AVI_TENANT")
r.apiVersion = os.Getenv("AVI_APIVERSION")
return
}
// connect establishes the avi connection.
func (o *Exporter) connect() (r *clients.AviClient, err error) {
// simplify avi connection
r, err = clients.NewAviClient(o.connectionOpts.cluster, o.connectionOpts.username,
session.SetPassword(o.connectionOpts.password),
session.SetTenant(o.connectionOpts.tenant),
session.SetInsecure,
session.SetVersion(o.connectionOpts.apiVersion))
return
}
func (o *Exporter) registerGauges() {
o.guages = make(map[string]*prometheus.GaugeVec)
for k, v := range o.GaugeOptsMap {
g := prometheus.NewGaugeVec(v.GaugeOpts, v.CustomLabels)
prometheus.MustRegister(g)
o.guages[k] = g
}
}
// sortUniqueKeys sorts unique keys within a string array.
func sortUniqueKeys(in []string) ([]string, error) {
var err error
var resp []string
respMap := make(map[string]string)
for _, v := range in {
respMap[v] = v
}
for _, v := range respMap {
resp = append(resp, v)
}
sort.Strings(resp)
return resp, err
}
func (o *Exporter) getVirtualServices() (r map[string]virtualServiceDef, err error) {
vs, err := o.AviClient.VirtualService.GetAll()
var pooluuid string
if err != nil {
log.Panic(err)
}
r = make(map[string]virtualServiceDef)
for _, v := range vs {
vip := v.Vip[0]
address := *vip.IPAddress.Addr
dns, _ := net.LookupAddr(address)
for k, v := range dns {
dns[k] = strings.TrimSuffix(v, ".")
}
dns, err = sortUniqueKeys(dns)
if v.PoolRef != nil {
pooluuid = formatAviRef(*v.PoolRef)
}
r[*v.UUID] = virtualServiceDef{Name: *v.Name, IPAddress: address, FQDN: strings.Join(dns, ","), PoolUUID: pooluuid}
}
return
}
func (o *Exporter) getClusterRuntime() (r map[string]clusterDef, err error) {
resp := new(cluster)
err = o.AviClient.AviSession.Get("/api/cluster", &resp)
if err != nil {
log.Panic(err)
}
r = make(map[string]clusterDef)
for _, v := range resp.Nodes {
address := v.IP.Addr
dns, _ := net.LookupAddr(address)
r[v.VMUUID] = clusterDef{Name: v.Name, IPAddress: address, FQDN: strings.Join(dns, ",")}
}
return
}
func (o *Exporter) getServiceEngines() (r map[string]seDef, err error) {
se, err := o.AviClient.ServiceEngine.GetAll()
if err != nil {
log.Panic(err)
}
r = make(map[string]seDef)
for _, v := range se {
address := *v.MgmtVnic.VnicNetworks[0].IP.IPAddr.Addr
dns, _ := net.LookupAddr(address)
for k, v := range dns {
dns[k] = strings.TrimSuffix(v, ".")
}
r[*v.UUID] = seDef{Name: *v.Name, IPAddress: address, FQDN: strings.Join(dns, ",")}
}
return
}
func (o *Exporter) getPools() (r map[string]poolDef, err error) {
vs, err := o.AviClient.Pool.GetAll()
if err != nil {
log.Panic(err)
}
r = make(map[string]poolDef)
for _, v := range vs {
r[*v.UUID] = poolDef{Name: *v.Name}
}
return
}
// toPrettyJSON formats json output.
func toPrettyJSON(p interface{}) []byte {
bytes, err := json.Marshal(p)
if err != nil {
log.Println(err.Error())
}
return pretty.Pretty(bytes)
}
// Collect retrieves metrics for Avi.
func (o *Exporter) Collect() (err error) {
log.Println("polling")
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Connect to the cluster.
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
o.AviClient, err = o.connect()
if err != nil {
log.Panic(err)
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Set promMetrics.
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
err = o.setVirtualServiceMetrics()
if err != nil {
log.Print(err)
return
}
err = o.setServiceEngineMetrics()
if err != nil {
log.Print(err)
return
}
err = o.setControllerMetrics()
if err != nil {
log.Print(err)
return
}
return
}
func (o *Exporter) getVirtualServiceMetrics() (r [][]CollectionResponse, err error) {
req := Metrics{}
for k, v := range o.GaugeOptsMap {
if v.Type == "virtualservice" {
reqMetric := MetricRequest{}
reqMetric.EntityUUID = "*"
reqMetric.MetricEntity = "VSERVER_METRICS_ENTITY"
reqMetric.Limit = 1
reqMetric.MetricID = k
reqMetric.Step = 5
req.MetricRequests = append(req.MetricRequests, reqMetric)
}
}
resp := make(map[string]map[string][]CollectionResponse)
err = o.AviClient.AviSession.Post("/api/analytics/metrics/collection", req, &resp)
if err != nil {
log.Panic(err)
return
}
for _, s := range resp["series"] {
r = append(r, s)
}
return
}
func (o *Exporter) getServiceEngineMetrics() (r [][]CollectionResponse, err error) {
req := Metrics{}
for k, v := range o.GaugeOptsMap {
if v.Type == "serviceengine" {
reqMetric := MetricRequest{}
reqMetric.EntityUUID = "*"
reqMetric.MetricEntity = "SE_METRICS_ENTITY"
reqMetric.Limit = 1
reqMetric.MetricID = k
reqMetric.Step = 5
req.MetricRequests = append(req.MetricRequests, reqMetric)
}
}
resp := make(map[string]map[string][]CollectionResponse)
err = o.AviClient.AviSession.Post("/api/analytics/metrics/collection", req, &resp)
if err != nil {
log.Panic(err)
return
}
for _, s := range resp["series"] {
r = append(r, s)
}
return
}
func (o *Exporter) getControllerMetrics() (r [][]CollectionResponse, err error) {
req := Metrics{}
for k, v := range o.GaugeOptsMap {
if v.Type == "controller" {
reqMetric := MetricRequest{}
reqMetric.EntityUUID = "*"
reqMetric.MetricEntity = "CONTROLLER_METRICS_ENTITY"
reqMetric.Limit = 1
reqMetric.MetricID = k
reqMetric.Step = 5
req.MetricRequests = append(req.MetricRequests, reqMetric)
}
}
resp := make(map[string]map[string][]CollectionResponse)
err = o.AviClient.AviSession.Post("/api/analytics/metrics/collection", req, &resp)
if err != nil {
log.Panic(err)
return
}
for _, s := range resp["series"] {
r = append(r, s)
}
return
}
func (o *Exporter) setVirtualServiceMetrics() (err error) {
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Get lb objects for mapping.
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
vs, _ := o.getVirtualServices()
pools, _ := o.getPools()
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
results, err := o.getVirtualServiceMetrics()
if err != nil {
log.Panic(err)
return
}
for _, v := range results {
for _, v1 := range v {
var labels prometheus.Labels
labels = make(map[string]string)
labels["name"] = vs[v1.Header.EntityUUID].Name
labels["pool"] = pools[vs[v1.Header.EntityUUID].PoolUUID].Name
labels["tenant_uuid"] = v1.Header.TenantUUID
labels["cluster"] = o.connectionOpts.cluster
labels["units"] = v1.Header.Units
labels["fqdn"] = vs[v1.Header.EntityUUID].FQDN
labels["ipaddress"] = vs[v1.Header.EntityUUID].IPAddress
o.guages[v1.Header.Name].With(labels).Set(v1.Data[len(v1.Data)-1].Value)
}
}
return
}
func (o *Exporter) setServiceEngineMetrics() (err error) {
results, err := o.getServiceEngineMetrics()
ses, _ := o.getServiceEngines()
if err != nil {
log.Panic(err)
return
}
for _, v := range results {
for _, v1 := range v {
var labels prometheus.Labels
labels = make(map[string]string)
labels["tenant_uuid"] = v1.Header.TenantUUID
labels["entity_uuid"] = v1.Header.EntityUUID
labels["cluster"] = o.connectionOpts.cluster
labels["units"] = v1.Header.Units
labels["name"] = ses[v1.Header.EntityUUID].Name
labels["fqdn"] = ses[v1.Header.EntityUUID].FQDN
labels["ipaddress"] = ses[v1.Header.EntityUUID].IPAddress
o.guages[v1.Header.Name].With(labels).Set(v1.Data[len(v1.Data)-1].Value)
}
}
return
}
func (o *Exporter) setControllerMetrics() (err error) {
results, err := o.getControllerMetrics()
runtime, _ := o.getClusterRuntime()
if err != nil {
log.Panic(err)
return
}
for _, v := range results {
for _, v1 := range v {
var labels prometheus.Labels
labels = make(map[string]string)
labels["tenant_uuid"] = v1.Header.TenantUUID
labels["entity_uuid"] = v1.Header.EntityUUID
labels["cluster"] = o.connectionOpts.cluster
labels["units"] = v1.Header.Units
labels["name"] = runtime[v1.Header.EntityUUID].Name
labels["fqdn"] = runtime[v1.Header.EntityUUID].FQDN
labels["ipaddress"] = runtime[v1.Header.EntityUUID].IPAddress
o.guages[v1.Header.Name].With(labels).Set(v1.Data[len(v1.Data)-1].Value)
}
}
return
}