forked from czerwonk/ping_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollector.go
63 lines (52 loc) · 2.38 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
package main
import (
"strings"
"sync"
mon "github.com/digineo/go-ping/monitor"
"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)
medianDesc = prometheus.NewDesc(prefix+"rtt_median_ms", "Median 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+"packet_loss", "Number of Packet loss", labelNames, nil)
sentDesc = prometheus.NewDesc(prefix+"packet_sent", "Number of Packet sent", labelNames, nil)
progDesc = prometheus.NewDesc(prefix+"up", "ping_exporter version", nil, prometheus.Labels{"version": version})
mutex = &sync.Mutex{}
)
type pingCollector struct {
monitor *mon.Monitor
metrics map[string]*mon.Metrics
}
func (p *pingCollector) Describe(ch chan<- *prometheus.Desc) {
ch <- sentDesc
ch <- lossDesc
ch <- bestDesc
ch <- worstDesc
ch <- medianDesc
ch <- meanDesc
ch <- stddevDesc
ch <- progDesc
}
func (p *pingCollector) Collect(ch chan<- prometheus.Metric) {
mutex.Lock()
defer mutex.Unlock()
if m := p.monitor.Export(); len(m) > 0 {
p.metrics = m
}
ch <- prometheus.MustNewConstMetric(progDesc, prometheus.GaugeValue, 1)
for target, metrics := range p.metrics {
l := strings.SplitN(target, " ", 3)
ch <- prometheus.MustNewConstMetric(sentDesc, prometheus.GaugeValue, float64(metrics.PacketsSent), l...)
ch <- prometheus.MustNewConstMetric(lossDesc, prometheus.GaugeValue, float64(metrics.PacketsLost), l...)
ch <- prometheus.MustNewConstMetric(bestDesc, prometheus.GaugeValue, float64(metrics.Best), l...)
ch <- prometheus.MustNewConstMetric(worstDesc, prometheus.GaugeValue, float64(metrics.Worst), l...)
ch <- prometheus.MustNewConstMetric(medianDesc, prometheus.GaugeValue, float64(metrics.Median), l...)
ch <- prometheus.MustNewConstMetric(meanDesc, prometheus.GaugeValue, float64(metrics.Mean), l...)
ch <- prometheus.MustNewConstMetric(stddevDesc, prometheus.GaugeValue, float64(metrics.StdDev), l...)
}
}