master
go 177 lines 4.26 KB
Raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 package elasticsearch
4
5 import (
6 "context"
7 _ "embed"
8 "errors"
9 "fmt"
10 "net/http"
11 "sync"
12 "time"
13
14 "github.com/netdata/netdata/go/plugins/pkg/confopt"
15 "github.com/netdata/netdata/go/plugins/pkg/web"
16 "github.com/netdata/netdata/go/plugins/plugin/framework/collectorapi"
17 )
18
19 //go:embed "config_schema.json"
20 var configSchema string
21
22 func init() {
23 collectorapi.Register("elasticsearch", collectorapi.Creator{
24 JobConfigSchema: configSchema,
25 Defaults: collectorapi.Defaults{
26 UpdateEvery: 5,
27 },
28 Create: func() collectorapi.CollectorV1 { return New() },
29 Config: func() any { return &Config{} },
30 Methods: elasticsearchMethods,
31 MethodHandler: elasticsearchFunctionHandler,
32 })
33 }
34
35 func New() *Collector {
36 return &Collector{
37 Config: Config{
38 HTTPConfig: web.HTTPConfig{
39 RequestConfig: web.RequestConfig{
40 URL: "http://127.0.0.1:9200",
41 },
42 ClientConfig: web.ClientConfig{
43 Timeout: confopt.Duration(time.Second * 2),
44 },
45 },
46 ClusterMode: false,
47
48 DoNodeStats: true,
49 DoClusterStats: true,
50 DoClusterHealth: true,
51 DoIndicesStats: false,
52 Functions: FunctionsConfig{
53 TopQueries: TopQueriesConfig{
54 Limit: 500,
55 },
56 },
57 },
58
59 charts: &collectorapi.Charts{},
60 addClusterHealthChartsOnce: &sync.Once{},
61 addClusterStatsChartsOnce: &sync.Once{},
62 nodes: make(map[string]bool),
63 indices: make(map[string]bool),
64 }
65 }
66
67 type Config struct {
68 Vnode string `yaml:"vnode,omitempty" json:"vnode"`
69 UpdateEvery int `yaml:"update_every,omitempty" json:"update_every"`
70 AutoDetectionRetry int `yaml:"autodetection_retry,omitempty" json:"autodetection_retry"`
71 web.HTTPConfig `yaml:",inline" json:""`
72 ClusterMode bool `yaml:"cluster_mode" json:"cluster_mode"`
73 DoNodeStats bool `yaml:"collect_node_stats" json:"collect_node_stats"`
74 DoClusterHealth bool `yaml:"collect_cluster_health" json:"collect_cluster_health"`
75 DoClusterStats bool `yaml:"collect_cluster_stats" json:"collect_cluster_stats"`
76 DoIndicesStats bool `yaml:"collect_indices_stats" json:"collect_indices_stats"`
77 Functions FunctionsConfig `yaml:"functions,omitempty" json:"functions"`
78 }
79
80 type FunctionsConfig struct {
81 TopQueries TopQueriesConfig `yaml:"top_queries,omitempty" json:"top_queries"`
82 }
83
84 type TopQueriesConfig struct {
85 Disabled bool `yaml:"disabled" json:"disabled"`
86 Timeout confopt.Duration `yaml:"timeout,omitempty" json:"timeout"`
87 Limit int `yaml:"limit,omitempty" json:"limit"`
88 }
89
90 func (c Config) topQueriesTimeout() time.Duration {
91 if c.Functions.TopQueries.Timeout == 0 {
92 return c.Timeout.Duration()
93 }
94 return c.Functions.TopQueries.Timeout.Duration()
95 }
96
97 func (c Config) topQueriesLimit() int {
98 if c.Functions.TopQueries.Limit <= 0 {
99 return 500
100 }
101 return c.Functions.TopQueries.Limit
102 }
103
104 type Collector struct {
105 collectorapi.Base
106 Config `yaml:",inline" json:""`
107
108 charts *collectorapi.Charts
109 addClusterHealthChartsOnce *sync.Once
110 addClusterStatsChartsOnce *sync.Once
111
112 httpClient *http.Client
113
114 clusterName string
115 nodes map[string]bool
116 indices map[string]bool
117
118 funcRouter *funcRouter
119 }
120
121 func (c *Collector) Configuration() any {
122 return c.Config
123 }
124
125 func (c *Collector) Init(context.Context) error {
126 err := c.validateConfig()
127 if err != nil {
128 return fmt.Errorf("check configuration: %v", err)
129 }
130
131 httpClient, err := c.initHTTPClient()
132 if err != nil {
133 return fmt.Errorf("init HTTP client: %v", err)
134 }
135 c.httpClient = httpClient
136
137 c.funcRouter = newFuncRouter(c)
138
139 return nil
140 }
141
142 func (c *Collector) Check(context.Context) error {
143 mx, err := c.collect()
144 if err != nil {
145 return err
146 }
147 if len(mx) == 0 {
148 return errors.New("no metrics collected")
149
150 }
151 return nil
152 }
153
154 func (c *Collector) Charts() *collectorapi.Charts {
155 return c.charts
156 }
157
158 func (c *Collector) Collect(context.Context) map[string]int64 {
159 mx, err := c.collect()
160 if err != nil {
161 c.Error(err)
162 }
163
164 if len(mx) == 0 {
165 return nil
166 }
167 return mx
168 }
169
170 func (c *Collector) Cleanup(ctx context.Context) {
171 if c.funcRouter != nil {
172 c.funcRouter.Cleanup(ctx)
173 }
174 if c.httpClient != nil {
175 c.httpClient.CloseIdleConnections()
176 }
177 }