-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfastnetmon_collector.go
80 lines (63 loc) · 2.07 KB
/
fastnetmon_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
package main
import (
"time"
"github.com/lwlcom/fastnetmon_exporter/collector"
"github.com/lwlcom/fastnetmon_exporter/host"
"github.com/lwlcom/fastnetmon_exporter/network"
"github.com/lwlcom/fastnetmon_exporter/rpc"
"github.com/lwlcom/fastnetmon_exporter/totaltraffic"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/log"
)
const prefix = "fastnetmon_"
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 fastnetmonCollector struct {
collectors map[string]collector.RPCCollector
}
func newFastnetmonCollector() *fastnetmonCollector {
collectors := collectors()
return &fastnetmonCollector{collectors}
}
func collectors() map[string]collector.RPCCollector {
m := map[string]collector.RPCCollector{}
if *totalTrafficEnabled == true {
m["totalTraffic"] = totalTraffic.NewCollector()
}
if *networkEnabled == true {
m["network"] = network.NewCollector()
}
if *hostEnabled == true {
m["host"] = host.NewCollector()
}
return m
}
// Describe implements prometheus.Collector interface
func (c *fastnetmonCollector) Describe(ch chan<- *prometheus.Desc) {
ch <- upDesc
ch <- scrapeDurationDesc
for _, col := range c.collectors {
col.Describe(ch)
}
}
// Collect implements prometheus.Collector interface
func (c *fastnetmonCollector) Collect(ch chan<- prometheus.Metric) {
t := time.Now()
defer func() {
ch <- prometheus.MustNewConstMetric(scrapeDurationDesc, prometheus.GaugeValue, time.Since(t).Seconds(), *apiHost)
}()
rpc := rpc.NewClient(*apiHost, *apiUsername, *apiPassword, *debug)
ch <- prometheus.MustNewConstMetric(upDesc, prometheus.GaugeValue, 1, *apiHost)
for k, col := range c.collectors {
err := col.Collect(rpc, ch, []string{*apiHost})
if err != nil && err.Error() != "EOF" {
log.Errorln(k + ": " + err.Error())
}
}
}