Skip to content

Commit

Permalink
add support for target names
Browse files Browse the repository at this point in the history
  • Loading branch information
adamcooke committed Jan 19, 2024
1 parent f3c7486 commit a5c7668
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 16 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ targets:
protocol: icmp # One of icmp, udp. Default: icmp (Requires privileged operation)
size: 56 # Packet data size in bytes. Default 56 (Range: 24 - 65535)
source: 127.0.1.1 # Souce IP address to use. Default: None (automatic selection)
name: example-target # Name for the target to include as a 'target' label (Default: none)
```
In each host group the `interval`, `network`, and `protocol` are optional.
Expand Down
33 changes: 20 additions & 13 deletions collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ package main
import (
"net"

"github.com/prometheus-community/pro-bing"
probing "github.com/prometheus-community/pro-bing"

"github.com/go-kit/log/level"
"github.com/prometheus/client_golang/prometheus"
Expand All @@ -29,7 +29,7 @@ const (
)

var (
labelNames = []string{"ip", "host", "source"}
labelNames = []string{"ip", "host", "source", "target"}

pingResponseTTL = promauto.NewGaugeVec(
prometheus.GaugeOpts{
Expand Down Expand Up @@ -79,29 +79,33 @@ func newPingResponseHistogram(buckets []float64, factor float64) *prometheus.His

// SmokepingCollector collects metrics from the pinger.
type SmokepingCollector struct {
pingers *[]*probing.Pinger
pingers *[]*probing.Pinger
targetGroupNames map[*probing.Pinger]string

requestsSent *prometheus.Desc
}

func NewSmokepingCollector(pingers *[]*probing.Pinger, pingResponseSeconds prometheus.HistogramVec) *SmokepingCollector {
func NewSmokepingCollector(pingers *[]*probing.Pinger, targetGroupNames map[*probing.Pinger]string, pingResponseSeconds prometheus.HistogramVec) *SmokepingCollector {
for _, pinger := range *pingers {

targetGroupName := targetGroupNames[pinger]

// Init all metrics to 0s.
ipAddr := pinger.IPAddr().String()
pingResponseDuplicates.WithLabelValues(ipAddr, pinger.Addr(), pinger.Source)
pingResponseSeconds.WithLabelValues(ipAddr, pinger.Addr(), pinger.Source)
pingResponseTTL.WithLabelValues(ipAddr, pinger.Addr(), pinger.Source)
pingSendErrors.WithLabelValues(ipAddr, pinger.Addr(), pinger.Source)
pingResponseDuplicates.WithLabelValues(ipAddr, pinger.Addr(), pinger.Source, targetGroupName)
pingResponseSeconds.WithLabelValues(ipAddr, pinger.Addr(), pinger.Source, targetGroupName)
pingResponseTTL.WithLabelValues(ipAddr, pinger.Addr(), pinger.Source, targetGroupName)
pingSendErrors.WithLabelValues(ipAddr, pinger.Addr(), pinger.Source, targetGroupName)

// Setup handler functions.
pinger.OnRecv = func(pkt *probing.Packet) {
pingResponseSeconds.WithLabelValues(pkt.IPAddr.String(), pkt.Addr, pinger.Source).Observe(pkt.Rtt.Seconds())
pingResponseTTL.WithLabelValues(pkt.IPAddr.String(), pkt.Addr, pinger.Source).Set(float64(pkt.TTL))
pingResponseSeconds.WithLabelValues(pkt.IPAddr.String(), pkt.Addr, pinger.Source, targetGroupName).Observe(pkt.Rtt.Seconds())
pingResponseTTL.WithLabelValues(pkt.IPAddr.String(), pkt.Addr, pinger.Source, targetGroupName).Set(float64(pkt.TTL))
level.Debug(logger).Log("msg", "Echo reply", "ip_addr", pkt.IPAddr,
"bytes_received", pkt.Nbytes, "icmp_seq", pkt.Seq, "time", pkt.Rtt, "ttl", pkt.TTL)
}
pinger.OnDuplicateRecv = func(pkt *probing.Packet) {
pingResponseDuplicates.WithLabelValues(pkt.IPAddr.String(), pkt.Addr, pinger.Source).Inc()
pingResponseDuplicates.WithLabelValues(pkt.IPAddr.String(), pkt.Addr, pinger.Source, targetGroupName).Inc()
level.Debug(logger).Log("msg", "Echo reply (DUP!)", "ip_addr", pkt.IPAddr,
"bytes_received", pkt.Nbytes, "icmp_seq", pkt.Seq, "time", pkt.Rtt, "ttl", pkt.TTL)
}
Expand All @@ -122,14 +126,15 @@ func NewSmokepingCollector(pingers *[]*probing.Pinger, pingResponseSeconds prome
level.Debug(logger).Log("msg", "Error receiving packet", "error", err)
}
pinger.OnSendError = func(pkt *probing.Packet, err error) {
pingSendErrors.WithLabelValues(pkt.IPAddr.String(), pkt.Addr, pinger.Source).Inc()
pingSendErrors.WithLabelValues(pkt.IPAddr.String(), pkt.Addr, pinger.Source, targetGroupName).Inc()
level.Debug(logger).Log("msg", "Error sending packet", "ip_addr", pkt.IPAddr,
"bytes_received", pkt.Nbytes, "icmp_seq", pkt.Seq, "time", pkt.Rtt, "ttl", pkt.TTL, "error", err)
}
}

return &SmokepingCollector{
pingers: pingers,
pingers: pingers,
targetGroupNames: targetGroupNames,
requestsSent: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "requests_total"),
"Number of ping requests sent",
Expand All @@ -145,6 +150,7 @@ func (s *SmokepingCollector) Describe(ch chan<- *prometheus.Desc) {

func (s *SmokepingCollector) Collect(ch chan<- prometheus.Metric) {
for _, pinger := range *s.pingers {
targetGroupName := s.targetGroupNames[pinger]
stats := pinger.Statistics()

ch <- prometheus.MustNewConstMetric(
Expand All @@ -154,6 +160,7 @@ func (s *SmokepingCollector) Collect(ch chan<- prometheus.Metric) {
stats.IPAddr.String(),
stats.Addr,
pinger.Source,
targetGroupName,
)
}
}
1 change: 1 addition & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ type TargetGroup struct {
Protocol string `yaml:"protocol,omitempty"`
Size int `yaml:"size,omitempty"`
Source string `yaml:"source,omitempty"`
Name string `yaml:"name,omitempty"`
// TODO: Needs work to fix MetricFamily consistency.
// Labels map[string]string `yaml:"labels,omitempty"`
}
Expand Down
11 changes: 8 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
"strings"
"time"

"github.com/prometheus-community/pro-bing"
probing "github.com/prometheus-community/pro-bing"
"github.com/superq/smokeping_prober/config"

"github.com/alecthomas/kingpin/v2"
Expand Down Expand Up @@ -138,6 +138,8 @@ func main() {
pingResponseSeconds := newPingResponseHistogram(bucketlist, *factor)
prometheus.MustRegister(pingResponseSeconds)

targetGroupNamesByPinger := make(map[*probing.Pinger]string)

pingers := make([]*probing.Pinger, len(*hosts))
var pinger *probing.Pinger
var host string
Expand Down Expand Up @@ -187,6 +189,7 @@ func main() {
os.Exit(1)
}
pingers = append(pingers, pinger)
targetGroupNamesByPinger[pinger] = targetGroup.Name
}
}
sc.Unlock()
Expand All @@ -200,12 +203,14 @@ func main() {
level.Info(logger).Log("msg", fmt.Sprintf("Waiting %s between starting pingers", splay))
g := new(errgroup.Group)
for _, pinger := range pingers {
level.Info(logger).Log("msg", "Starting prober", "address", pinger.Addr(), "interval", pinger.Interval, "size_bytes", pinger.Size, "source", pinger.Source)
level.Info(logger).Log("msg", "Starting prober", "address", pinger.Addr(),
"interval", pinger.Interval, "size_bytes", pinger.Size, "source", pinger.Source,
"target", targetGroupNamesByPinger[pinger])
g.Go(pinger.Run)
time.Sleep(splay)
}

prometheus.MustRegister(NewSmokepingCollector(&pingers, *pingResponseSeconds))
prometheus.MustRegister(NewSmokepingCollector(&pingers, targetGroupNamesByPinger, *pingResponseSeconds))

http.Handle(*metricsPath, promhttp.Handler())
if *metricsPath != "/" && *metricsPath != "" {
Expand Down

0 comments on commit a5c7668

Please sign in to comment.