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

     1  // SPDX-License-Identifier: GPL-3.0-or-later
     2  
     3  package unbound
     4  
     5  import (
     6  	_ "embed"
     7  	"time"
     8  
     9  	"github.com/netdata/go.d.plugin/pkg/socket"
    10  	"github.com/netdata/go.d.plugin/pkg/tlscfg"
    11  	"github.com/netdata/go.d.plugin/pkg/web"
    12  
    13  	"github.com/netdata/go.d.plugin/agent/module"
    14  )
    15  
    16  //go:embed "config_schema.json"
    17  var configSchema string
    18  
    19  func init() {
    20  	module.Register("unbound", module.Creator{
    21  		JobConfigSchema: configSchema,
    22  		Create:          func() module.Module { return New() },
    23  	})
    24  }
    25  
    26  func New() *Unbound {
    27  	config := Config{
    28  		Address:    "127.0.0.1:8953",
    29  		ConfPath:   "/etc/unbound/unbound.conf",
    30  		Timeout:    web.Duration{Duration: time.Second},
    31  		Cumulative: false,
    32  		UseTLS:     true,
    33  		TLSConfig: tlscfg.TLSConfig{
    34  			TLSCert:            "/etc/unbound/unbound_control.pem",
    35  			TLSKey:             "/etc/unbound/unbound_control.key",
    36  			InsecureSkipVerify: true,
    37  		},
    38  	}
    39  
    40  	return &Unbound{
    41  		Config:   config,
    42  		curCache: newCollectCache(),
    43  		cache:    newCollectCache(),
    44  	}
    45  }
    46  
    47  type (
    48  	Config struct {
    49  		Address          string       `yaml:"address"`
    50  		ConfPath         string       `yaml:"conf_path"`
    51  		Timeout          web.Duration `yaml:"timeout"`
    52  		Cumulative       bool         `yaml:"cumulative_stats"`
    53  		UseTLS           bool         `yaml:"use_tls"`
    54  		tlscfg.TLSConfig `yaml:",inline"`
    55  	}
    56  	Unbound struct {
    57  		module.Base
    58  		Config `yaml:",inline"`
    59  
    60  		client   socket.Client
    61  		cache    collectCache
    62  		curCache collectCache
    63  
    64  		prevCacheMiss    float64 // needed for cumulative mode
    65  		extChartsCreated bool
    66  
    67  		charts *module.Charts
    68  	}
    69  )
    70  
    71  func (Unbound) Cleanup() {}
    72  
    73  func (u *Unbound) Init() bool {
    74  	if enabled := u.initConfig(); !enabled {
    75  		return false
    76  	}
    77  
    78  	if err := u.initClient(); err != nil {
    79  		u.Errorf("creating client: %v", err)
    80  		return false
    81  	}
    82  
    83  	u.charts = charts(u.Cumulative)
    84  
    85  	u.Debugf("using address: %s, cumulative: %v, use_tls: %v, timeout: %s", u.Address, u.Cumulative, u.UseTLS, u.Timeout)
    86  	if u.UseTLS {
    87  		u.Debugf("using tls_skip_verify: %v, tls_key: %s, tls_cert: %s", u.InsecureSkipVerify, u.TLSKey, u.TLSCert)
    88  	}
    89  	return true
    90  }
    91  
    92  func (u *Unbound) Check() bool {
    93  	return len(u.Collect()) > 0
    94  }
    95  
    96  func (u Unbound) Charts() *module.Charts {
    97  	return u.charts
    98  }
    99  
   100  func (u *Unbound) Collect() map[string]int64 {
   101  	mx, err := u.collect()
   102  	if err != nil {
   103  		u.Error(err)
   104  	}
   105  
   106  	if len(mx) == 0 {
   107  		return nil
   108  	}
   109  	return mx
   110  }