forked from lwlcom/cisco_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcisco_collector.go
125 lines (98 loc) · 2.94 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
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
package main
import (
"strings"
"time"
"sync"
"github.com/lwlcom/cisco_exporter/bgp"
"github.com/lwlcom/cisco_exporter/collector"
"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"
"github.com/lwlcom/cisco_exporter/rpc"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/log"
)
const prefix = "cisco_"
var (
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)
}
type ciscoCollector struct {
targets []string
collectors map[string]collector.RPCCollector
}
func newCiscoCollector(targets []string) *ciscoCollector {
collectors := collectors()
return &ciscoCollector{targets, collectors}
}
func collectors() map[string]collector.RPCCollector {
m := map[string]collector.RPCCollector{}
if *bgpEnabled == true {
m["bgp"] = bgp.NewCollector()
}
if *environmetEnabled == true {
m["environment"] = environment.NewCollector()
}
if *factsEnabled == true {
m["facts"] = facts.NewCollector()
}
if *interfacesEnabled == true {
m["interfaces"] = interfaces.NewCollector()
}
if *opticsEnabled == true {
m["optics"] = optics.NewCollector()
}
return m
}
// Describe implements prometheus.Collector interface
func (c *ciscoCollector) Describe(ch chan<- *prometheus.Desc) {
ch <- upDesc
ch <- scrapeDurationDesc
for _, col := range c.collectors {
col.Describe(ch)
}
}
// Collect implements prometheus.Collector interface
func (c *ciscoCollector) Collect(ch chan<- prometheus.Metric) {
hosts := c.targets
wg := &sync.WaitGroup{}
wg.Add(len(hosts))
for _, h := range hosts {
go c.collectForHost(strings.Trim(h, " "), ch, wg)
}
wg.Wait()
}
func (c *ciscoCollector) collectForHost(host string, ch chan<- prometheus.Metric, wg *sync.WaitGroup) {
defer wg.Done()
l := []string{host}
t := time.Now()
defer func() {
ch <- prometheus.MustNewConstMetric(scrapeDurationDesc, prometheus.GaugeValue, time.Since(t).Seconds(), l...)
}()
conn, err := connector.NewSSSHConnection(host, *sshUsername, *sshKeyFile, *legacyCiphers, *sshTimeout, *sshBatchSize)
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...)
rpc := rpc.NewClient(conn, *debug)
err = rpc.Identify()
if err != nil {
log.Errorln(host + ": " + err.Error())
return
}
for k, col := range c.collectors {
err = col.Collect(rpc, ch, l)
if err != nil && err.Error() != "EOF" {
log.Errorln(k + ": " + err.Error())
}
}
}