github.com/netdata/go.d.plugin@v0.58.1/modules/ntpd/ntpd.go (about)

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