@cryptotaxi247 / netdata-1 / commits / a6a776a03

fix(go.d/ping): fix "interface" option (#19016)

Ilya Mashchenko committed Nov 14, 2024 at 16:50 UTC a6a776a0395726ebba25387111df57c386ad9e04
6 files changed +26 -60
src/go/go.mod
+1 -1
@@ -39,7 +39,7 @@ require (
39 github.com/mattn/go-xmlrpc v0.0.3
40 github.com/miekg/dns v1.1.62
41 github.com/mitchellh/go-homedir v1.1.0
42 - github.com/prometheus-community/pro-bing v0.4.1
42 + github.com/prometheus-community/pro-bing v0.4.2-0.20241106090159-5a5f1d731cf5
43 github.com/prometheus/common v0.60.1
44 github.com/prometheus/prometheus v2.5.0+incompatible
45 github.com/redis/go-redis/v9 v9.7.0
src/go/go.sum
+2 -2
@@ -334,8 +334,8 @@ github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE
334 github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
335 github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
336 github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
337 -github.com/prometheus-community/pro-bing v0.4.1 h1:aMaJwyifHZO0y+h8+icUz0xbToHbia0wdmzdVZ+Kl3w=
338 -github.com/prometheus-community/pro-bing v0.4.1/go.mod h1:aLsw+zqCaDoa2RLVVSX3+UiCkBBXTMtZC3c7EkfWnAE=
337 +github.com/prometheus-community/pro-bing v0.4.2-0.20241106090159-5a5f1d731cf5 h1:zHS7rMBAaRLMQUlKPT/gFHQVn3RldvPBNNDSpIB6WtM=
338 +github.com/prometheus-community/pro-bing v0.4.2-0.20241106090159-5a5f1d731cf5/go.mod h1:zZf++wJG7OTBQY+deqxF1XgN9GqlbYPCPZPd1aX9wqQ=
339 github.com/prometheus/client_model v0.6.1 h1:ZKSh/rekM+n3CeS952MLRAdFwIKqeY8b62p8ais2e9E=
340 github.com/prometheus/client_model v0.6.1/go.mod h1:OrxVMOVHjw3lKMa8+x6HeMGkHMQyHDk9E3jmP2AmGiY=
341 github.com/prometheus/common v0.60.1 h1:FUas6GcOw66yB/73KC+BOZoFJmbo/1pojoILArPAaSc=
src/go/plugin/go.d/modules/ping/config_schema.json
+1 -1
@@ -58,7 +58,7 @@
58 },
59 "interface": {
60 "title": "Interface",
61 - "description": "The name of the network interface whose IP address will be used as the source for sending ping packets.",
61 + "description": "The network device name (e.g., `eth0`, `wlan0`) used as the source for ICMP echo requests.",
62 "type": "string",
63 "default": ""
64 }
src/go/plugin/go.d/modules/ping/init.go
+1 -1
@@ -30,7 +30,7 @@ func (p *Ping) initProber() (prober, error) {
30 conf := pingProberConfig{
31 privileged: p.Privileged,
32 packets: p.SendPackets,
33 - iface: p.Interface,
33 + ifaceName: p.Interface,
34 interval: p.Interval.Duration(),
35 deadline: deadline,
36 }
src/go/plugin/go.d/modules/ping/metadata.yaml
+4
@@ -89,6 +89,10 @@ modules:
89 description: "Allows configuration of DNS resolution. Supported options: ip (select IPv4 or IPv6), ip4 (select IPv4), ip6 (select IPv6)."
90 default_value: "ip"
91 required: false
92 + - name: interface
93 + description: "The network device name (e.g., `eth0`, `wlan0`) used as the source for ICMP echo requests."
94 + default_value: ""
95 + required: false
96 - name: privileged
97 description: Ping packets type. "no" means send an "unprivileged" UDP ping, "yes" - raw ICMP ping.
98 default_value: true
src/go/plugin/go.d/modules/ping/prober.go
+17 -55
@@ -3,9 +3,7 @@
3 package ping
4
5 import (
6 - "errors"
6 "fmt"
8 - "net"
7 "time"
8
9 "github.com/netdata/netdata/go/plugins/logger"
@@ -14,32 +12,22 @@ import (
12 )
13
14 func newPingProber(conf pingProberConfig, log *logger.Logger) prober {
17 - var source string
18 - if conf.iface != "" {
19 - if addr, err := getInterfaceIPAddress(conf.iface); err != nil {
20 - log.Warningf("error getting interface '%s' IP address: %v", conf.iface, err)
21 - } else {
22 - log.Infof("interface '%s' IP address '%s', will use it as the source", conf.iface, addr)
23 - source = addr
24 - }
25 - }
26 -
15 return &pingProber{
28 - network: conf.network,
29 - privileged: conf.privileged,
30 - packets: conf.packets,
31 - source: source,
32 - interval: conf.interval,
33 - deadline: conf.deadline,
34 - Logger: log,
16 + network: conf.network,
17 + interfaceName: conf.ifaceName,
18 + privileged: conf.privileged,
19 + packets: conf.packets,
20 + interval: conf.interval,
21 + deadline: conf.deadline,
22 + Logger: log,
23 }
24 }
25
26 type pingProberConfig struct {
27 network string
28 + ifaceName string
29 privileged bool
30 packets int
42 - iface string
31 interval time.Duration
32 deadline time.Duration
33 }
@@ -47,12 +35,12 @@ type pingProberConfig struct {
35 type pingProber struct {
36 *logger.Logger
37
50 - network string
51 - privileged bool
52 - packets int
53 - source string
54 - interval time.Duration
55 - deadline time.Duration
38 + network string
39 + interfaceName string
40 + privileged bool
41 + packets int
42 + interval time.Duration
43 + deadline time.Duration
44 }
45
46 func (p *pingProber) ping(host string) (*probing.Statistics, error) {
@@ -64,16 +52,17 @@ func (p *pingProber) ping(host string) (*probing.Statistics, error) {
52 return nil, fmt.Errorf("DNS lookup '%s' : %v", host, err)
53 }
54
67 - pr.Source = p.source
55 pr.RecordRtts = false
56 pr.Interval = p.interval
57 pr.Count = p.packets
58 pr.Timeout = p.deadline
59 + pr.InterfaceName = p.interfaceName
60 pr.SetPrivileged(p.privileged)
61 pr.SetLogger(nil)
62
63 if err := pr.Run(); err != nil {
76 - return nil, fmt.Errorf("pinging host '%s' (ip %s): %v", pr.Addr(), pr.IPAddr(), err)
64 + return nil, fmt.Errorf("pinging host '%s' (ip '%s' iface '%s'): %v",
65 + pr.Addr(), pr.IPAddr(), pr.InterfaceName, err)
66 }
67
68 stats := pr.Statistics()
@@ -82,30 +71,3 @@ func (p *pingProber) ping(host string) (*probing.Statistics, error) {
71
72 return stats, nil
73 }
85 -
86 -func getInterfaceIPAddress(ifaceName string) (ipaddr string, err error) {
87 - iface, err := net.InterfaceByName(ifaceName)
88 - if err != nil {
89 - return "", err
90 - }
91 -
92 - addresses, err := iface.Addrs()
93 - if err != nil {
94 - return "", err
95 - }
96 -
97 - // FIXME: add IPv6 support
98 - var v4Addr string
99 - for _, addr := range addresses {
100 - if ipnet, ok := addr.(*net.IPNet); ok && ipnet.IP.To4() != nil {
101 - v4Addr = ipnet.IP.To4().String()
102 - break
103 - }
104 - }
105 -
106 - if v4Addr == "" {
107 - return "", errors.New("ipv4 addresses not found")
108 - }
109 -
110 - return v4Addr, nil
111 -}