forked from czerwonk/ping_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
collector.go
66 lines (54 loc) · 2.13 KB
/
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
package main
import (
"strconv"
"sync"
"github.com/prometheus/client_golang/prometheus"
)
const prefix = "ping_"
var (
labelNames = []string{"target", "ip", "ip_version"}
bestDesc = prometheus.NewDesc(prefix+"rtt_best_ms", "Best round trip time in millis", labelNames, nil)
worstDesc = prometheus.NewDesc(prefix+"rtt_worst_ms", "Worst round trip time in millis", labelNames, nil)
meanDesc = prometheus.NewDesc(prefix+"rtt_mean_ms", "Mean round trip time in millis", labelNames, nil)
stddevDesc = prometheus.NewDesc(prefix+"rtt_std_deviation_ms", "Standard deviation in millis", labelNames, nil)
lossDesc = prometheus.NewDesc(prefix+"loss_percent", "Packet loss in percent", labelNames, nil)
progDesc = prometheus.NewDesc(prefix+"up", "ping_exporter version", nil, prometheus.Labels{"version": version})
mutex = &sync.Mutex{}
)
type pingCollector struct {
targets []*target
}
func (p *pingCollector) Describe(ch chan<- *prometheus.Desc) {
ch <- lossDesc
ch <- bestDesc
ch <- worstDesc
ch <- meanDesc
ch <- stddevDesc
ch <- progDesc
}
func (p *pingCollector) Collect(ch chan<- prometheus.Metric) {
mutex.Lock()
defer mutex.Unlock()
ch <- prometheus.MustNewConstMetric(progDesc, prometheus.GaugeValue, 1)
for _, t := range p.targets {
for _, r := range t.runners {
p.collectForRunner(t, r, ch)
}
}
}
func (p *pingCollector) collectForRunner(t *target, r *runner, ch chan<- prometheus.Metric) {
stats := r.stats
if stats == nil {
return
}
ipVersion := 6
if r.ip.To4() != nil {
ipVersion = 4
}
l := []string{t.host, r.ip.String(), strconv.Itoa(ipVersion)}
ch <- prometheus.MustNewConstMetric(bestDesc, prometheus.GaugeValue, float64(stats.MinRtt.Milliseconds()), l...)
ch <- prometheus.MustNewConstMetric(worstDesc, prometheus.GaugeValue, float64(stats.MaxRtt.Milliseconds()), l...)
ch <- prometheus.MustNewConstMetric(meanDesc, prometheus.GaugeValue, float64(stats.AvgRtt.Milliseconds()), l...)
ch <- prometheus.MustNewConstMetric(stddevDesc, prometheus.GaugeValue, float64(stats.StdDevRtt.Milliseconds()), l...)
ch <- prometheus.MustNewConstMetric(lossDesc, prometheus.GaugeValue, stats.PacketLoss, l...)
}