forked from czerwonk/ping_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
collector.go
99 lines (80 loc) · 2.85 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
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
// SPDX-License-Identifier: MIT
package main
import (
"strings"
"sync"
mon "github.com/digineo/go-ping/monitor"
"github.com/prometheus/client_golang/prometheus"
"github.com/czerwonk/ping_exporter/config"
)
var (
labelNames []string
rttDesc scaledMetrics
bestDesc scaledMetrics
worstDesc scaledMetrics
meanDesc scaledMetrics
stddevDesc scaledMetrics
lossDesc *prometheus.Desc
progDesc *prometheus.Desc
mutex *sync.Mutex
)
type pingCollector struct {
cfg *config.Config
customLabels *customLabelSet
monitor *mon.Monitor
metrics map[string]*mon.Metrics
}
func (p *pingCollector) Describe(ch chan<- *prometheus.Desc) {
p.createDesc()
if enableDeprecatedMetrics {
rttDesc.Describe(ch)
}
bestDesc.Describe(ch)
worstDesc.Describe(ch)
meanDesc.Describe(ch)
stddevDesc.Describe(ch)
ch <- lossDesc
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)
targetConfig := p.cfg.TargetConfigByAddr(l[0])
l = append(l, p.customLabels.labelValues(targetConfig)...)
if metrics.PacketsSent > metrics.PacketsLost {
if enableDeprecatedMetrics {
rttDesc.Collect(ch, metrics.Best, append(l, "best")...)
rttDesc.Collect(ch, metrics.Worst, append(l, "worst")...)
rttDesc.Collect(ch, metrics.Mean, append(l, "mean")...)
rttDesc.Collect(ch, metrics.StdDev, append(l, "std_dev")...)
}
bestDesc.Collect(ch, metrics.Best, l...)
worstDesc.Collect(ch, metrics.Worst, l...)
meanDesc.Collect(ch, metrics.Mean, l...)
stddevDesc.Collect(ch, metrics.StdDev, l...)
}
loss := float64(metrics.PacketsLost) / float64(metrics.PacketsSent)
ch <- prometheus.MustNewConstMetric(lossDesc, prometheus.GaugeValue, loss, l...)
}
}
func (p *pingCollector) createDesc() {
labelNames = []string{"target", "ip", "ip_version"}
labelNames = append(labelNames, p.customLabels.labelNames()...)
rttDesc = newScaledDesc("rtt", "Round trip time", append(labelNames, "type"))
bestDesc = newScaledDesc("rtt_best", "Best round trip time", labelNames)
worstDesc = newScaledDesc("rtt_worst", "Worst round trip time", labelNames)
meanDesc = newScaledDesc("rtt_mean", "Mean round trip time", labelNames)
stddevDesc = newScaledDesc("rtt_std_deviation", "Standard deviation", labelNames)
lossDesc = newDesc("loss_ratio", "Packet loss from 0.0 to 1.0", labelNames, nil)
progDesc = newDesc("up", "ping_exporter version", nil, prometheus.Labels{"version": version})
mutex = &sync.Mutex{}
}
func newDesc(name, help string, variableLabels []string, constLabels prometheus.Labels) *prometheus.Desc {
return prometheus.NewDesc("ping_"+name, help, variableLabels, constLabels)
}