| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package ping |
| 4 | |
| 5 | import ( |
| 6 | "context" |
| 7 | _ "embed" |
| 8 | "errors" |
| 9 | "fmt" |
| 10 | "time" |
| 11 | |
| 12 | "github.com/netdata/netdata/go/plugins/logger" |
| 13 | "github.com/netdata/netdata/go/plugins/pkg/confopt" |
| 14 | "github.com/netdata/netdata/go/plugins/pkg/metrix" |
| 15 | "github.com/netdata/netdata/go/plugins/plugin/framework/collectorapi" |
| 16 | "github.com/netdata/netdata/go/plugins/plugin/go.d/pkg/pinger" |
| 17 | ) |
| 18 | |
| 19 | //go:embed "config_schema.json" |
| 20 | var configSchema string |
| 21 | |
| 22 | //go:embed "charts.yaml" |
| 23 | var pingChartTemplateV2 string |
| 24 | |
| 25 | func init() { |
| 26 | collectorapi.Register("ping", collectorapi.Creator{ |
| 27 | JobConfigSchema: configSchema, |
| 28 | Defaults: collectorapi.Defaults{ |
| 29 | UpdateEvery: 5, |
| 30 | }, |
| 31 | CreateV2: func() collectorapi.CollectorV2 { return New() }, |
| 32 | Config: func() any { return &Config{} }, |
| 33 | }) |
| 34 | } |
| 35 | |
| 36 | func New() *Collector { |
| 37 | store := metrix.NewCollectorStore() |
| 38 | |
| 39 | return &Collector{ |
| 40 | Config: Config{ |
| 41 | ProbeConfig: pinger.ProbeConfig{ |
| 42 | Network: "ip", |
| 43 | Privileged: true, |
| 44 | Packets: 5, |
| 45 | Interval: confopt.Duration(time.Millisecond * 100), |
| 46 | }, |
| 47 | AnalysisConfig: pinger.AnalysisConfig{ |
| 48 | JitterEWMASamples: 16, |
| 49 | JitterSMAWindow: 10, |
| 50 | }, |
| 51 | }, |
| 52 | |
| 53 | newPinger: pinger.New, |
| 54 | store: store, |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | type Config struct { |
| 59 | Vnode string `yaml:"vnode,omitempty" json:"vnode"` |
| 60 | UpdateEvery int `yaml:"update_every,omitempty" json:"update_every"` |
| 61 | Hosts []string `yaml:"hosts" json:"hosts"` |
| 62 | pinger.ProbeConfig `yaml:",inline" json:",inline"` |
| 63 | pinger.AnalysisConfig `yaml:",inline" json:",inline"` |
| 64 | } |
| 65 | |
| 66 | type Collector struct { |
| 67 | collectorapi.Base |
| 68 | Config `yaml:",inline" json:""` |
| 69 | |
| 70 | client pinger.Client |
| 71 | newPinger func(pinger.Config, *logger.Logger) (pinger.Client, error) |
| 72 | |
| 73 | store metrix.CollectorStore |
| 74 | } |
| 75 | |
| 76 | func (c *Collector) Configuration() any { |
| 77 | return c.Config |
| 78 | } |
| 79 | |
| 80 | func (c *Collector) Init(context.Context) error { |
| 81 | err := c.validateConfig() |
| 82 | if err != nil { |
| 83 | return fmt.Errorf("config validation: %v", err) |
| 84 | } |
| 85 | |
| 86 | pr, err := c.initPinger() |
| 87 | if err != nil { |
| 88 | return fmt.Errorf("init ping client: %v", err) |
| 89 | } |
| 90 | c.client = pr |
| 91 | |
| 92 | return nil |
| 93 | } |
| 94 | |
| 95 | func (c *Collector) Check(ctx context.Context) error { |
| 96 | samples := c.collectSamples(ctx, false) |
| 97 | if len(samples) == 0 { |
| 98 | return errors.New("no metrics collected") |
| 99 | } |
| 100 | return nil |
| 101 | } |
| 102 | |
| 103 | func (c *Collector) Collect(ctx context.Context) error { return c.collect(ctx) } |
| 104 | |
| 105 | func (c *Collector) Cleanup(context.Context) {} |
| 106 | |
| 107 | func (c *Collector) MetricStore() metrix.CollectorStore { return c.store } |
| 108 | |
| 109 | func (c *Collector) ChartTemplateYAML() string { return pingChartTemplateV2 } |