| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package ntpd |
| 4 | |
| 5 | import ( |
| 6 | "context" |
| 7 | _ "embed" |
| 8 | "errors" |
| 9 | "fmt" |
| 10 | "time" |
| 11 | |
| 12 | "github.com/netdata/netdata/go/plugins/pkg/confopt" |
| 13 | "github.com/netdata/netdata/go/plugins/plugin/framework/collectorapi" |
| 14 | "github.com/netdata/netdata/go/plugins/plugin/go.d/pkg/iprange" |
| 15 | ) |
| 16 | |
| 17 | //go:embed "config_schema.json" |
| 18 | var configSchema string |
| 19 | |
| 20 | func init() { |
| 21 | collectorapi.Register("ntpd", collectorapi.Creator{ |
| 22 | JobConfigSchema: configSchema, |
| 23 | Create: func() collectorapi.CollectorV1 { return New() }, |
| 24 | Config: func() any { return &Config{} }, |
| 25 | }) |
| 26 | } |
| 27 | |
| 28 | func New() *Collector { |
| 29 | return &Collector{ |
| 30 | Config: Config{ |
| 31 | Address: "127.0.0.1:123", |
| 32 | Timeout: confopt.Duration(time.Second), |
| 33 | CollectPeers: false, |
| 34 | }, |
| 35 | charts: systemCharts.Copy(), |
| 36 | newClient: newNTPClient, |
| 37 | findPeersEvery: time.Minute * 3, |
| 38 | peerAddr: make(map[string]bool), |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | type Config struct { |
| 43 | Vnode string `yaml:"vnode,omitempty" json:"vnode"` |
| 44 | UpdateEvery int `yaml:"update_every,omitempty" json:"update_every"` |
| 45 | AutoDetectionRetry int `yaml:"autodetection_retry,omitempty" json:"autodetection_retry"` |
| 46 | Address string `yaml:"address" json:"address"` |
| 47 | Timeout confopt.Duration `yaml:"timeout,omitempty" json:"timeout"` |
| 48 | CollectPeers bool `yaml:"collect_peers" json:"collect_peers"` |
| 49 | } |
| 50 | |
| 51 | type Collector struct { |
| 52 | collectorapi.Base |
| 53 | Config `yaml:",inline" json:""` |
| 54 | |
| 55 | charts *collectorapi.Charts |
| 56 | |
| 57 | client ntpConn |
| 58 | newClient func(c Config) (ntpConn, error) |
| 59 | |
| 60 | findPeersTime time.Time |
| 61 | findPeersEvery time.Duration |
| 62 | peerAddr map[string]bool |
| 63 | peerIDs []uint16 |
| 64 | peerIPAddrFilter *iprange.Pool |
| 65 | } |
| 66 | |
| 67 | func (c *Collector) Configuration() any { |
| 68 | return c.Config |
| 69 | } |
| 70 | |
| 71 | func (c *Collector) Init(context.Context) error { |
| 72 | if c.Address == "" { |
| 73 | return errors.New("config: 'address' can not be empty") |
| 74 | } |
| 75 | |
| 76 | txt := "0.0.0.0 127.0.0.0/8" |
| 77 | r, err := iprange.ParseRanges(txt) |
| 78 | if err != nil { |
| 79 | return fmt.Errorf("error on parsing ip range '%s': %v", txt, err) |
| 80 | } |
| 81 | |
| 82 | c.peerIPAddrFilter = iprange.NewPool(r...) |
| 83 | |
| 84 | return nil |
| 85 | } |
| 86 | |
| 87 | func (c *Collector) Check(context.Context) error { |
| 88 | mx, err := c.collect() |
| 89 | if err != nil { |
| 90 | return err |
| 91 | } |
| 92 | if len(mx) == 0 { |
| 93 | return errors.New("no metrics collected") |
| 94 | } |
| 95 | return nil |
| 96 | } |
| 97 | |
| 98 | func (c *Collector) Charts() *collectorapi.Charts { |
| 99 | return c.charts |
| 100 | } |
| 101 | |
| 102 | func (c *Collector) Collect(context.Context) map[string]int64 { |
| 103 | mx, err := c.collect() |
| 104 | if err != nil { |
| 105 | c.Error(err) |
| 106 | } |
| 107 | |
| 108 | if len(mx) == 0 { |
| 109 | return nil |
| 110 | } |
| 111 | return mx |
| 112 | } |
| 113 | |
| 114 | func (c *Collector) Cleanup(context.Context) { |
| 115 | if c.client != nil { |
| 116 | c.client.close() |
| 117 | c.client = nil |
| 118 | } |
| 119 | } |