github.com/netdata/go.d.plugin@v0.58.1/modules/unbound/init.go (about) 1 // SPDX-License-Identifier: GPL-3.0-or-later 2 3 package unbound 4 5 import ( 6 "crypto/tls" 7 "errors" 8 "net" 9 10 "github.com/netdata/go.d.plugin/modules/unbound/config" 11 "github.com/netdata/go.d.plugin/pkg/socket" 12 "github.com/netdata/go.d.plugin/pkg/tlscfg" 13 ) 14 15 func (u *Unbound) initConfig() (enabled bool) { 16 if u.ConfPath == "" { 17 u.Info("'conf_path' not set, skipping parameters auto detection") 18 return true 19 } 20 21 u.Infof("reading '%s'", u.ConfPath) 22 cfg, err := config.Parse(u.ConfPath) 23 if err != nil { 24 u.Warningf("%v, skipping parameters auto detection", err) 25 return true 26 } 27 28 if cfg.Empty() { 29 u.Debug("empty configuration") 30 return true 31 } 32 33 if enabled, ok := cfg.ControlEnabled(); ok && !enabled { 34 u.Info("remote control is disabled in the configuration file") 35 return false 36 } 37 38 u.applyConfig(cfg) 39 return true 40 } 41 42 func (u *Unbound) applyConfig(cfg *config.UnboundConfig) { 43 u.Infof("applying configuration: %s", cfg) 44 if cumulative, ok := cfg.Cumulative(); ok && cumulative != u.Cumulative { 45 u.Debugf("changing 'cumulative_stats': %v => %v", u.Cumulative, cumulative) 46 u.Cumulative = cumulative 47 } 48 if useCert, ok := cfg.ControlUseCert(); ok && useCert != u.UseTLS { 49 u.Debugf("changing 'use_tls': %v => %v", u.UseTLS, useCert) 50 u.UseTLS = useCert 51 } 52 if keyFile, ok := cfg.ControlKeyFile(); ok && keyFile != u.TLSKey { 53 u.Debugf("changing 'tls_key': '%s' => '%s'", u.TLSKey, keyFile) 54 u.TLSKey = keyFile 55 } 56 if certFile, ok := cfg.ControlCertFile(); ok && certFile != u.TLSCert { 57 u.Debugf("changing 'tls_cert': '%s' => '%s'", u.TLSCert, certFile) 58 u.TLSCert = certFile 59 } 60 if iface, ok := cfg.ControlInterface(); ok && adjustControlInterface(iface) != u.Address { 61 address := adjustControlInterface(iface) 62 u.Debugf("changing 'address': '%s' => '%s'", u.Address, address) 63 u.Address = address 64 } 65 if port, ok := cfg.ControlPort(); ok && !socket.IsUnixSocket(u.Address) { 66 if host, curPort, err := net.SplitHostPort(u.Address); err == nil && curPort != port { 67 address := net.JoinHostPort(host, port) 68 u.Debugf("changing 'address': '%s' => '%s'", u.Address, address) 69 u.Address = address 70 } 71 } 72 } 73 74 func (u *Unbound) initClient() (err error) { 75 var tlsCfg *tls.Config 76 useTLS := !socket.IsUnixSocket(u.Address) && u.UseTLS 77 78 if useTLS && (u.TLSConfig.TLSCert == "" || u.TLSConfig.TLSKey == "") { 79 return errors.New("'tls_cert' or 'tls_key' is missing") 80 } 81 82 if useTLS { 83 if tlsCfg, err = tlscfg.NewTLSConfig(u.TLSConfig); err != nil { 84 return err 85 } 86 } 87 88 u.client = socket.New(socket.Config{ 89 Address: u.Address, 90 ConnectTimeout: u.Timeout.Duration, 91 ReadTimeout: u.Timeout.Duration, 92 WriteTimeout: u.Timeout.Duration, 93 TLSConf: tlsCfg, 94 }) 95 return nil 96 } 97 98 func adjustControlInterface(value string) string { 99 if socket.IsUnixSocket(value) { 100 return value 101 } 102 if value == "0.0.0.0" { 103 value = "127.0.0.1" 104 } 105 return net.JoinHostPort(value, "8953") 106 }