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

     1  // SPDX-License-Identifier: GPL-3.0-or-later
     2  
     3  package chrony
     4  
     5  import (
     6  	_ "embed"
     7  	"time"
     8  
     9  	"github.com/facebook/time/ntp/chrony"
    10  	"github.com/netdata/go.d.plugin/agent/module"
    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("chrony", module.Creator{
    19  		JobConfigSchema: configSchema,
    20  		Create:          func() module.Module { return New() },
    21  	})
    22  }
    23  
    24  func New() *Chrony {
    25  	return &Chrony{
    26  		Config: Config{
    27  			Address: "127.0.0.1:323",
    28  			Timeout: web.Duration{Duration: time.Second},
    29  		},
    30  		charts:    charts.Copy(),
    31  		newClient: newChronyClient,
    32  	}
    33  }
    34  
    35  type Config struct {
    36  	Address string       `yaml:"address"`
    37  	Timeout web.Duration `yaml:"timeout"`
    38  }
    39  
    40  type (
    41  	Chrony struct {
    42  		module.Base
    43  		Config `yaml:",inline"`
    44  
    45  		charts *module.Charts
    46  
    47  		newClient func(c Config) (chronyClient, error)
    48  		client    chronyClient
    49  	}
    50  	chronyClient interface {
    51  		Tracking() (*chrony.ReplyTracking, error)
    52  		Activity() (*chrony.ReplyActivity, error)
    53  		Close()
    54  	}
    55  )
    56  
    57  func (c *Chrony) Init() bool {
    58  	if err := c.validateConfig(); err != nil {
    59  		c.Errorf("config validation: %v", err)
    60  		return false
    61  	}
    62  
    63  	return true
    64  }
    65  
    66  func (c *Chrony) Check() bool {
    67  	return len(c.Collect()) > 0
    68  }
    69  
    70  func (c *Chrony) Charts() *module.Charts {
    71  	return c.charts
    72  }
    73  
    74  func (c *Chrony) Collect() map[string]int64 {
    75  	mx, err := c.collect()
    76  	if err != nil {
    77  		c.Error(err)
    78  	}
    79  
    80  	if len(mx) == 0 {
    81  		return nil
    82  	}
    83  	return mx
    84  }
    85  
    86  func (c *Chrony) Cleanup() {
    87  	if c.client != nil {
    88  		c.client.Close()
    89  		c.client = nil
    90  	}
    91  }