github.com/netdata/go.d.plugin@v0.58.1/modules/zookeeper/zookeeper.go (about) 1 // SPDX-License-Identifier: GPL-3.0-or-later 2 3 package zookeeper 4 5 import ( 6 "crypto/tls" 7 _ "embed" 8 "fmt" 9 "time" 10 11 "github.com/netdata/go.d.plugin/pkg/socket" 12 "github.com/netdata/go.d.plugin/pkg/tlscfg" 13 "github.com/netdata/go.d.plugin/pkg/web" 14 15 "github.com/netdata/go.d.plugin/agent/module" 16 ) 17 18 //go:embed "config_schema.json" 19 var configSchema string 20 21 func init() { 22 module.Register("zookeeper", module.Creator{ 23 JobConfigSchema: configSchema, 24 Create: func() module.Module { return New() }, 25 }) 26 } 27 28 // Config is the Zookeeper module configuration. 29 type Config struct { 30 Address string 31 Timeout web.Duration `yaml:"timeout"` 32 UseTLS bool `yaml:"use_tls"` 33 tlscfg.TLSConfig `yaml:",inline"` 34 } 35 36 // New creates Zookeeper with default values. 37 func New() *Zookeeper { 38 config := Config{ 39 Address: "127.0.0.1:2181", 40 Timeout: web.Duration{Duration: time.Second}, 41 UseTLS: false, 42 } 43 return &Zookeeper{Config: config} 44 } 45 46 type fetcher interface { 47 fetch(command string) ([]string, error) 48 } 49 50 // Zookeeper Zookeeper module. 51 type Zookeeper struct { 52 module.Base 53 fetcher 54 Config `yaml:",inline"` 55 } 56 57 // Cleanup makes cleanup. 58 func (Zookeeper) Cleanup() {} 59 60 func (z *Zookeeper) createZookeeperFetcher() (err error) { 61 var tlsConf *tls.Config 62 if z.UseTLS { 63 tlsConf, err = tlscfg.NewTLSConfig(z.TLSConfig) 64 if err != nil { 65 return fmt.Errorf("error on creating tls config : %v", err) 66 } 67 } 68 69 sock := socket.New(socket.Config{ 70 Address: z.Address, 71 ConnectTimeout: z.Timeout.Duration, 72 ReadTimeout: z.Timeout.Duration, 73 WriteTimeout: z.Timeout.Duration, 74 TLSConf: tlsConf, 75 }) 76 z.fetcher = &zookeeperFetcher{Client: sock} 77 return nil 78 } 79 80 // Init makes initialization. 81 func (z *Zookeeper) Init() bool { 82 err := z.createZookeeperFetcher() 83 if err != nil { 84 z.Error(err) 85 return false 86 } 87 88 return true 89 } 90 91 // Check makes check. 92 func (z *Zookeeper) Check() bool { 93 return len(z.Collect()) > 0 94 } 95 96 // Charts creates Charts. 97 func (Zookeeper) Charts() *Charts { 98 return charts.Copy() 99 } 100 101 // Collect collects metrics. 102 func (z *Zookeeper) Collect() map[string]int64 { 103 mx, err := z.collect() 104 if err != nil { 105 z.Error(err) 106 } 107 108 if len(mx) == 0 { 109 return nil 110 } 111 return mx 112 }