github.com/netdata/go.d.plugin@v0.58.1/modules/elasticsearch/elasticsearch.go (about) 1 // SPDX-License-Identifier: GPL-3.0-or-later 2 3 package elasticsearch 4 5 import ( 6 _ "embed" 7 "net/http" 8 "sync" 9 "time" 10 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("elasticsearch", module.Creator{ 21 JobConfigSchema: configSchema, 22 Defaults: module.Defaults{ 23 UpdateEvery: 5, 24 }, 25 Create: func() module.Module { return New() }, 26 }) 27 } 28 29 func New() *Elasticsearch { 30 return &Elasticsearch{ 31 Config: Config{ 32 HTTP: web.HTTP{ 33 Request: web.Request{ 34 URL: "http://127.0.0.1:9200", 35 }, 36 Client: web.Client{ 37 Timeout: web.Duration{Duration: time.Second * 5}, 38 }, 39 }, 40 ClusterMode: false, 41 42 DoNodeStats: true, 43 DoClusterStats: true, 44 DoClusterHealth: true, 45 DoIndicesStats: false, 46 }, 47 48 charts: &module.Charts{}, 49 addClusterHealthChartsOnce: &sync.Once{}, 50 addClusterStatsChartsOnce: &sync.Once{}, 51 nodes: make(map[string]bool), 52 indices: make(map[string]bool), 53 } 54 } 55 56 type Config struct { 57 web.HTTP `yaml:",inline"` 58 ClusterMode bool `yaml:"cluster_mode"` 59 DoNodeStats bool `yaml:"collect_node_stats"` 60 DoClusterHealth bool `yaml:"collect_cluster_health"` 61 DoClusterStats bool `yaml:"collect_cluster_stats"` 62 DoIndicesStats bool `yaml:"collect_indices_stats"` 63 } 64 65 type Elasticsearch struct { 66 module.Base 67 Config `yaml:",inline"` 68 69 httpClient *http.Client 70 charts *module.Charts 71 72 clusterName string 73 74 addClusterHealthChartsOnce *sync.Once 75 addClusterStatsChartsOnce *sync.Once 76 77 nodes map[string]bool 78 indices map[string]bool 79 } 80 81 func (es *Elasticsearch) Init() bool { 82 err := es.validateConfig() 83 if err != nil { 84 es.Errorf("check configuration: %v", err) 85 return false 86 } 87 88 httpClient, err := es.initHTTPClient() 89 if err != nil { 90 es.Errorf("init HTTP client: %v", err) 91 return false 92 } 93 es.httpClient = httpClient 94 95 return true 96 } 97 98 func (es *Elasticsearch) Check() bool { 99 return len(es.Collect()) > 0 100 } 101 102 func (es *Elasticsearch) Charts() *module.Charts { 103 return es.charts 104 } 105 106 func (es *Elasticsearch) Collect() map[string]int64 { 107 mx, err := es.collect() 108 if err != nil { 109 es.Error(err) 110 } 111 112 if len(mx) == 0 { 113 return nil 114 } 115 return mx 116 } 117 118 func (es *Elasticsearch) Cleanup() { 119 if es.httpClient != nil { 120 es.httpClient.CloseIdleConnections() 121 } 122 }