| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package pinger |
| 4 | |
| 5 | import ( |
| 6 | "context" |
| 7 | "fmt" |
| 8 | |
| 9 | "github.com/netdata/netdata/go/plugins/logger" |
| 10 | |
| 11 | probing "github.com/prometheus-community/pro-bing" |
| 12 | ) |
| 13 | |
| 14 | type probeRunner interface { |
| 15 | probe(ctx context.Context, host string, cfg ProbeConfig) (*probing.Statistics, error) |
| 16 | } |
| 17 | |
| 18 | type defaultRunner struct { |
| 19 | log *logger.Logger |
| 20 | } |
| 21 | |
| 22 | func (r *defaultRunner) probe(ctx context.Context, host string, cfg ProbeConfig) (*probing.Statistics, error) { |
| 23 | pr := probing.New(host) |
| 24 | |
| 25 | pr.SetNetwork(cfg.Network) |
| 26 | |
| 27 | if err := pr.Resolve(); err != nil { |
| 28 | return nil, &ProbeError{Host: host, Stage: "resolve", Err: err} |
| 29 | } |
| 30 | |
| 31 | pr.RecordRtts = true |
| 32 | pr.RecordTTLs = false |
| 33 | pr.Interval = cfg.Interval.Duration() |
| 34 | pr.Count = cfg.Packets |
| 35 | pr.Timeout = cfg.Timeout |
| 36 | pr.InterfaceName = cfg.Interface |
| 37 | pr.SetPrivileged(cfg.Privileged) |
| 38 | pr.SetLogger(nil) |
| 39 | |
| 40 | if err := pr.RunWithContext(ctx); err != nil { |
| 41 | runErr := fmt.Errorf("ip %q iface %q: %w", pr.IPAddr(), pr.InterfaceName, err) |
| 42 | return nil, &ProbeError{Host: host, Stage: "run", Err: runErr} |
| 43 | } |
| 44 | |
| 45 | stats := pr.Statistics() |
| 46 | |
| 47 | r.log.Debugf("ping stats for host %q (ip %q): %+v", pr.Addr(), pr.IPAddr(), stats) |
| 48 | |
| 49 | return stats, nil |
| 50 | } |