-
Notifications
You must be signed in to change notification settings - Fork 23
/
collectors.go
79 lines (63 loc) · 2.2 KB
/
collectors.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
package main
import (
"github.com/lwlcom/cisco_exporter/bgp"
"github.com/lwlcom/cisco_exporter/collector"
"github.com/lwlcom/cisco_exporter/config"
"github.com/lwlcom/cisco_exporter/connector"
"github.com/lwlcom/cisco_exporter/environment"
"github.com/lwlcom/cisco_exporter/facts"
"github.com/lwlcom/cisco_exporter/interfaces"
"github.com/lwlcom/cisco_exporter/optics"
)
type collectors struct {
collectors map[string]collector.RPCCollector
devices map[string][]collector.RPCCollector
cfg *config.Config
}
func collectorsForDevices(devices []*connector.Device, cfg *config.Config) *collectors {
c := &collectors{
collectors: make(map[string]collector.RPCCollector),
devices: make(map[string][]collector.RPCCollector),
cfg: cfg,
}
for _, d := range devices {
c.initCollectorsForDevice(d)
}
return c
}
func (c *collectors) initCollectorsForDevice(device *connector.Device) {
f := c.cfg.FeaturesForDevice(device.Host)
c.devices[device.Host] = make([]collector.RPCCollector, 0)
c.addCollectorIfEnabledForDevice(device, "bgp", f.BGP, bgp.NewCollector)
c.addCollectorIfEnabledForDevice(device, "environment", f.Environment, environment.NewCollector)
c.addCollectorIfEnabledForDevice(device, "facts", f.Facts, facts.NewCollector)
c.addCollectorIfEnabledForDevice(device, "interfaces", f.Interfaces, interfaces.NewCollector)
c.addCollectorIfEnabledForDevice(device, "optics", f.Optics, optics.NewCollector)
}
func (c *collectors) addCollectorIfEnabledForDevice(device *connector.Device, key string, enabled *bool, newCollector func() collector.RPCCollector) {
if !*enabled {
return
}
col, found := c.collectors[key]
if !found {
col = newCollector()
c.collectors[key] = col
}
c.devices[device.Host] = append(c.devices[device.Host], col)
}
func (c *collectors) allEnabledCollectors() []collector.RPCCollector {
collectors := make([]collector.RPCCollector, len(c.collectors))
i := 0
for _, collector := range c.collectors {
collectors[i] = collector
i++
}
return collectors
}
func (c *collectors) collectorsForDevice(device *connector.Device) []collector.RPCCollector {
cols, found := c.devices[device.Host]
if !found {
return []collector.RPCCollector{}
}
return cols
}