| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package ping |
| 4 | |
| 5 | import ( |
| 6 | "errors" |
| 7 | "time" |
| 8 | |
| 9 | "github.com/netdata/netdata/go/plugins/plugin/go.d/pkg/pinger" |
| 10 | ) |
| 11 | |
| 12 | func (c *Collector) validateConfig() error { |
| 13 | if len(c.Hosts) == 0 { |
| 14 | return errors.New("'hosts' can't be empty") |
| 15 | } |
| 16 | seenHosts := make(map[string]struct{}, len(c.Hosts)) |
| 17 | for _, host := range c.Hosts { |
| 18 | if _, ok := seenHosts[host]; ok { |
| 19 | return errors.New("'hosts' contains duplicate entries") |
| 20 | } |
| 21 | seenHosts[host] = struct{}{} |
| 22 | } |
| 23 | if c.Packets <= 0 { |
| 24 | return errors.New("'send_packets' can't be <= 0") |
| 25 | } |
| 26 | if c.JitterEWMASamples <= 0 { |
| 27 | c.JitterEWMASamples = 16 |
| 28 | } |
| 29 | if c.JitterSMAWindow <= 0 { |
| 30 | c.JitterSMAWindow = 10 |
| 31 | } |
| 32 | return nil |
| 33 | } |
| 34 | |
| 35 | func (c *Collector) initPinger() (pinger.Client, error) { |
| 36 | mul := 0.9 |
| 37 | if c.UpdateEvery > 1 { |
| 38 | mul = 0.95 |
| 39 | } |
| 40 | timeout := time.Millisecond * time.Duration(float64(c.UpdateEvery)*mul*1000) |
| 41 | if timeout.Milliseconds() == 0 { |
| 42 | return nil, errors.New("zero ping timeout") |
| 43 | } |
| 44 | return c.newPinger(pinger.Config{ |
| 45 | Probe: pinger.ProbeConfig{ |
| 46 | Network: c.Network, |
| 47 | Interface: c.Interface, |
| 48 | Privileged: c.Privileged, |
| 49 | Packets: c.Packets, |
| 50 | Interval: c.Interval, |
| 51 | Timeout: timeout, |
| 52 | }, |
| 53 | Analysis: c.AnalysisConfig, |
| 54 | }, c.Logger) |
| 55 | } |