-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
Copy pathdata.go
328 lines (276 loc) · 9.83 KB
/
data.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package cluster_stats
import (
"encoding/json"
"fmt"
"hash/fnv"
"sort"
"strings"
s "github.com/elastic/beats/v7/libbeat/common/schema"
c "github.com/elastic/beats/v7/libbeat/common/schema/mapstriface"
"github.com/elastic/beats/v7/metricbeat/helper"
"github.com/elastic/beats/v7/metricbeat/helper/elastic"
"github.com/elastic/beats/v7/metricbeat/mb"
"github.com/elastic/beats/v7/metricbeat/module/elasticsearch"
"github.com/elastic/elastic-agent-libs/mapstr"
)
var (
schema = s.Schema{
"status": c.Str("status"),
"nodes": c.Dict("nodes", s.Schema{
"versions": c.Ifc("versions"),
"count": c.Int("count.total"),
"master": c.Int("count.master"),
"data": c.Int("count.data"),
"fs": c.Dict("fs", s.Schema{
"total": s.Object{
"bytes": c.Int("total_in_bytes"),
},
"available": s.Object{
"bytes": c.Int("available_in_bytes"),
},
}),
"jvm": c.Dict("jvm", s.Schema{
"max_uptime": s.Object{
"ms": c.Int("max_uptime_in_millis"),
},
"memory": c.Dict("mem", s.Schema{
"heap": s.Object{
"used": s.Object{
"bytes": c.Int("heap_used_in_bytes"),
},
"max": s.Object{
"bytes": c.Int("heap_max_in_bytes"),
},
},
}),
}),
}),
"indices": c.Dict("indices", s.Schema{
"docs": c.Dict("docs", s.Schema{
"total": c.Int("count"),
}),
"total": c.Int("count"),
"shards": c.Dict("shards", s.Schema{
"count": c.Int("total"),
"primaries": c.Int("primaries"),
}),
"store": c.Dict("store", s.Schema{
"size": s.Object{"bytes": c.Int("size_in_bytes")},
"total_data_set_size": s.Object{"bytes": c.Int("total_data_set_size_in_bytes", s.Optional)},
}),
"fielddata": c.Dict("fielddata", s.Schema{
"memory": s.Object{
"bytes": c.Int("memory_size_in_bytes"),
},
}),
}),
}
stackSchema = s.Schema{
"xpack": c.Dict("xpack", s.Schema{
"ccr": c.Dict("ccr", s.Schema{
"enabled": c.Bool("enabled"),
"available": c.Bool("available"),
}),
}),
"apm": c.Dict("apm", s.Schema{
"found": c.Bool("found"),
}),
}
)
func clusterNeedsTLSEnabled(license *elasticsearch.License, stackStats mapstr.M) (bool, error) {
// TLS does not need to be enabled if license type is something other than trial
if !license.IsOneOf("trial") {
return false, nil
}
// TLS does not need to be enabled if security is not enabled
value, err := stackStats.GetValue("security.enabled")
if err != nil {
return false, elastic.MakeErrorForMissingField("security.enabled", elastic.Elasticsearch)
}
isSecurityEnabled, ok := value.(bool)
if !ok {
return false, fmt.Errorf("security enabled flag is not a boolean")
}
if !isSecurityEnabled {
return false, nil
}
// TLS does not need to be enabled if TLS is already enabled on the transport protocol
value, err = stackStats.GetValue("security.ssl.transport.enabled")
if err != nil {
return false, elastic.MakeErrorForMissingField("security.ssl.transport.enabled", elastic.Elasticsearch)
}
isTLSAlreadyEnabled, ok := value.(bool)
if !ok {
return false, fmt.Errorf("transport protocol SSL enabled flag is not a boolean")
}
return !isTLSAlreadyEnabled, nil
}
// computeNodesHash computes a simple hash value that can be used to determine if the nodes listing has changed since the last report.
func computeNodesHash(clusterState mapstr.M) (int32, error) {
value, err := clusterState.GetValue("nodes")
if err != nil {
return 0, elastic.MakeErrorForMissingField("nodes", elastic.Elasticsearch)
}
nodes, ok := value.(map[string]interface{})
if !ok {
return 0, fmt.Errorf("nodes is not a map")
}
var nodeEphemeralIDs []string
for _, value := range nodes {
nodeData, ok := value.(map[string]interface{})
if !ok {
return 0, fmt.Errorf("node data is not a map")
}
value, ok := nodeData["ephemeral_id"]
if !ok {
return 0, fmt.Errorf("node data does not contain ephemeral ID")
}
ephemeralID, ok := value.(string)
if !ok {
return 0, fmt.Errorf("node ephemeral ID is not a string")
}
nodeEphemeralIDs = append(nodeEphemeralIDs, ephemeralID)
}
sort.Strings(nodeEphemeralIDs)
combinedNodeEphemeralIDs := strings.Join(nodeEphemeralIDs, "")
return hash(combinedNodeEphemeralIDs), nil
}
func hash(s string) int32 {
h := fnv.New32()
h.Write([]byte(s))
return int32(h.Sum32()) // This cast is needed because the ES mapping is for a 32-bit *signed* integer
}
func apmIndicesExist(clusterState mapstr.M) (bool, error) {
value, err := clusterState.GetValue("routing_table.indices")
if err != nil {
return false, elastic.MakeErrorForMissingField("routing_table.indices", elastic.Elasticsearch)
}
indices, ok := value.(map[string]interface{})
if !ok {
return false, fmt.Errorf("routing table indices is not a map")
}
for name := range indices {
if strings.HasPrefix(name, "apm-") {
return true, nil
}
}
return false, nil
}
func getClusterMetadataSettings(httpClient *helper.HTTP) (mapstr.M, error) {
// For security reasons we only get the display_name setting
filterPaths := []string{"*.cluster.metadata.display_name"}
clusterSettings, err := elasticsearch.GetClusterSettingsWithDefaults(httpClient, httpClient.GetURI(), filterPaths)
if err != nil {
return nil, fmt.Errorf("failure to get cluster settings: %w", err)
}
clusterSettings, err = elasticsearch.MergeClusterSettings(clusterSettings)
if err != nil {
return nil, fmt.Errorf("failure to merge cluster settings: %w", err)
}
return clusterSettings, nil
}
func eventMapping(r mb.ReporterV2, httpClient *helper.HTTP, info elasticsearch.Info, content []byte, isXpack bool) error {
var data map[string]interface{}
err := json.Unmarshal(content, &data)
if err != nil {
return fmt.Errorf("failure parsing Elasticsearch Cluster Stats API response: %w", err)
}
clusterStats := mapstr.M(data)
clusterStats.Delete("_nodes")
license, err := elasticsearch.GetLicense(httpClient, httpClient.GetURI())
if err != nil {
return fmt.Errorf("failed to get license from Elasticsearch: %w", err)
}
clusterStateMetrics := []string{"version", "master_node", "nodes", "routing_table"}
clusterState, err := elasticsearch.GetClusterState(httpClient, httpClient.GetURI(), clusterStateMetrics, []string{})
if err != nil {
return fmt.Errorf("failed to get cluster state from Elasticsearch: %w", err)
}
clusterState.Delete("cluster_name")
clusterStateReduced := mapstr.M{}
if err = elasticsearch.PassThruField("status", clusterStats, clusterStateReduced); err != nil {
return fmt.Errorf("failed to pass through status field: %w", err)
}
clusterStateReduced.Delete("status")
if err = elasticsearch.PassThruField("master_node", clusterState, clusterStateReduced); err != nil {
return fmt.Errorf("failed to pass through master_node field: %w", err)
}
if err = elasticsearch.PassThruField("state_uuid", clusterState, clusterStateReduced); err != nil {
return fmt.Errorf("failed to pass through state_uuid field: %w", err)
}
if err = elasticsearch.PassThruField("nodes", clusterState, clusterStateReduced); err != nil {
return fmt.Errorf("failed to pass through nodes field: %w", err)
}
nodesHash, err := computeNodesHash(clusterState)
if err != nil {
return fmt.Errorf("failed to compute nodes hash: %w", err)
}
clusterStateReduced.Put("nodes_hash", nodesHash)
usage, err := elasticsearch.GetStackUsage(httpClient, httpClient.GetURI())
if err != nil {
return fmt.Errorf("failed to get stack usage from Elasticsearch: %w", err)
}
clusterNeedsTLS, err := clusterNeedsTLSEnabled(license, usage)
if err != nil {
return fmt.Errorf("failed to determine if cluster needs TLS enabled: %w", err)
}
l := license.ToMapStr()
l["cluster_needs_tls"] = clusterNeedsTLS
isAPMFound, err := apmIndicesExist(clusterState)
if err != nil {
return fmt.Errorf("failed to determine if APM indices exist: %w", err)
}
delete(clusterState, "routing_table") // We don't want to index the routing table in monitoring indices
stackStats := map[string]interface{}{
"xpack": usage,
"apm": map[string]interface{}{
"found": isAPMFound,
},
}
stackData, _ := stackSchema.Apply(stackStats)
event := mb.Event{
ModuleFields: mapstr.M{},
RootFields: mapstr.M{},
}
event.ModuleFields.Put("cluster.name", info.ClusterName)
event.ModuleFields.Put("cluster.id", info.ClusterID)
clusterSettings, err := getClusterMetadataSettings(httpClient)
if err != nil {
return err
}
if clusterSettings != nil {
event.RootFields.Put("cluster_settings", clusterSettings)
}
metricSetFields, _ := schema.Apply(data)
metricSetFields.Put("stack", stackData)
metricSetFields.Put("license", l)
metricSetFields.Put("state", clusterStateReduced)
if err = elasticsearch.PassThruField("version", clusterState, event.ModuleFields); err != nil {
return fmt.Errorf("failed to pass through version field: %w", err)
}
event.MetricSetFields = metricSetFields
// xpack.enabled in config using standalone metricbeat writes to `.monitoring` instead of `metricbeat-*`
// When using Agent, the index name is overwritten anyways.
if isXpack {
index := elastic.MakeXPackMonitoringIndexName(elastic.Elasticsearch)
event.Index = index
}
r.Event(event)
return nil
}