-
Notifications
You must be signed in to change notification settings - Fork 23
/
cisco_collector.go
100 lines (78 loc) · 2.66 KB
/
cisco_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
package main
import (
"time"
"sync"
"github.com/lwlcom/cisco_exporter/connector"
"github.com/lwlcom/cisco_exporter/rpc"
"github.com/prometheus/client_golang/prometheus"
log "github.com/sirupsen/logrus"
)
const prefix = "cisco_"
var (
scrapeCollectorDurationDesc *prometheus.Desc
scrapeDurationDesc *prometheus.Desc
upDesc *prometheus.Desc
)
func init() {
upDesc = prometheus.NewDesc(prefix+"up", "Scrape of target was successful", []string{"target"}, nil)
scrapeDurationDesc = prometheus.NewDesc(prefix+"collector_duration_seconds", "Duration of a collector scrape for one target", []string{"target"}, nil)
scrapeCollectorDurationDesc = prometheus.NewDesc(prefix+"collect_duration_seconds", "Duration of a scrape by collector and target", []string{"target", "collector"}, nil)
}
type ciscoCollector struct {
devices []*connector.Device
collectors *collectors
}
func newCiscoCollector(devices []*connector.Device) *ciscoCollector {
return &ciscoCollector{
devices: devices,
collectors: collectorsForDevices(devices, cfg),
}
}
// Describe implements prometheus.Collector interface
func (c *ciscoCollector) Describe(ch chan<- *prometheus.Desc) {
ch <- upDesc
ch <- scrapeDurationDesc
ch <- scrapeCollectorDurationDesc
for _, col := range c.collectors.allEnabledCollectors() {
col.Describe(ch)
}
}
// Collect implements prometheus.Collector interface
func (c *ciscoCollector) Collect(ch chan<- prometheus.Metric) {
wg := &sync.WaitGroup{}
wg.Add(len(c.devices))
for _, d := range c.devices {
go c.collectForHost(d, ch, wg)
}
wg.Wait()
}
func (c *ciscoCollector) collectForHost(device *connector.Device, ch chan<- prometheus.Metric, wg *sync.WaitGroup) {
defer wg.Done()
l := []string{device.Host}
t := time.Now()
defer func() {
ch <- prometheus.MustNewConstMetric(scrapeDurationDesc, prometheus.GaugeValue, time.Since(t).Seconds(), l...)
}()
conn, err := connector.NewSSSHConnection(device, cfg)
if err != nil {
log.Errorln(err)
ch <- prometheus.MustNewConstMetric(upDesc, prometheus.GaugeValue, 0, l...)
return
}
defer conn.Close()
ch <- prometheus.MustNewConstMetric(upDesc, prometheus.GaugeValue, 1, l...)
client := rpc.NewClient(conn, cfg.Debug)
err = client.Identify()
if err != nil {
log.Errorln(device.Host + ": " + err.Error())
return
}
for _, col := range c.collectors.collectorsForDevice(device) {
ct := time.Now()
err := col.Collect(client, ch, l)
if err != nil && err.Error() != "EOF" {
log.Errorln(col.Name() + ": " + err.Error())
}
ch <- prometheus.MustNewConstMetric(scrapeCollectorDurationDesc, prometheus.GaugeValue, time.Since(ct).Seconds(), append(l, col.Name())...)
}
}