-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
collector.go
88 lines (73 loc) · 2.03 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
package pingexporter
import (
"github.com/go-ping/ping"
"github.com/prometheus/client_golang/prometheus"
)
var _ prometheus.Collector = &collector{}
type collector struct {
PacketsRecv *prometheus.Desc
PacketsSent *prometheus.Desc
RTT *prometheus.Desc
PacketLoss *prometheus.Desc
Jitter *prometheus.Desc
pingers []*ping.Pinger
}
func NewCollector(pingers []*ping.Pinger) prometheus.Collector {
return &collector{
PacketsRecv: prometheus.NewDesc(
"ping_packets_recv",
"Number of packets received.",
[]string{"host"},
nil,
),
PacketsSent: prometheus.NewDesc(
"ping_packets_sent",
"Number of packets sent.",
[]string{"host"},
nil,
),
RTT: prometheus.NewDesc(
"ping_rtt",
"Running average of the RTT.",
[]string{"host"},
nil,
),
PacketLoss: prometheus.NewDesc(
"ping_packet_loss",
"Percentage of packet loss.",
[]string{"host"},
nil,
),
Jitter: prometheus.NewDesc(
"ping_jitter",
"RTT jitter.",
[]string{"host"},
nil,
),
pingers: pingers,
}
}
// Describe implements prometheus.Collector.
func (c *collector) Describe(ch chan<- *prometheus.Desc) {
ds := []*prometheus.Desc{
c.PacketsRecv,
c.PacketsSent,
c.RTT,
c.PacketLoss,
c.Jitter,
}
for _, d := range ds {
ch <- d
}
}
// Collect implements prometheus.Collector.
func (c *collector) Collect(ch chan<- prometheus.Metric) {
for _, pinger := range c.pingers {
stats := pinger.Statistics()
ch <- prometheus.MustNewConstMetric(c.PacketsSent, prometheus.CounterValue, float64(stats.PacketsSent), pinger.Addr())
ch <- prometheus.MustNewConstMetric(c.PacketsRecv, prometheus.CounterValue, float64(stats.PacketsRecv), pinger.Addr())
ch <- prometheus.MustNewConstMetric(c.RTT, prometheus.GaugeValue, float64(stats.AvgRtt.Milliseconds()), pinger.Addr())
ch <- prometheus.MustNewConstMetric(c.PacketLoss, prometheus.GaugeValue, float64(stats.PacketLoss), pinger.Addr())
ch <- prometheus.MustNewConstMetric(c.Jitter, prometheus.GaugeValue, float64(stats.StdDevRtt.Milliseconds()), pinger.Addr())
}
}