forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata.go
510 lines (436 loc) · 16.5 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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
// 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 index
import (
"encoding/json"
"fmt"
"github.com/joeshaw/multierror"
"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/logp"
"github.com/elastic/elastic-agent-libs/mapstr"
)
// Based on https://github.com/elastic/elasticsearch/blob/master/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/collector/indices/IndexStatsMonitoringDoc.java#L127-L203
type stats struct {
Indices map[string]Index `json:"indices"`
}
type Index struct {
UUID string `json:"uuid"`
Primaries primaries `json:"primaries"`
Total total `json:"total"`
Index string `json:"index"`
Status string `json:"status"`
TierPreference string `json:"tier_preference"`
CreationDate string `json:"creation_date"`
Version string `json:"version"`
Shards shardStats `json:"shards"`
}
type primaries struct {
Docs struct {
Count int `json:"count"`
Deleted int `json:"deleted"`
} `json:"docs"`
Indexing struct {
IndexTotal int `json:"index_total"`
IndexTimeInMillis int `json:"index_time_in_millis"`
ThrottleTimeInMillis int `json:"throttle_time_in_millis"`
} `json:"indexing"`
Merges struct {
TotalSizeInBytes int `json:"total_size_in_bytes"`
} `json:"merges"`
Segments struct {
Count int `json:"count"`
MemoryInBytes int `json:"memory_in_bytes"`
TermsMemoryInBytes int `json:"terms_memory_in_bytes"`
StoredFieldsMemoryInBytes int `json:"stored_fields_memory_in_bytes"`
TermVectorsMemoryInBytes int `json:"term_vectors_memory_in_bytes"`
NormsMemoryInBytes int `json:"norms_memory_in_bytes"`
PointsMemoryInBytes int `json:"points_memory_in_bytes"`
DocValuesMemoryInBytes int `json:"doc_values_memory_in_bytes"`
IndexWriterMemoryInBytes int `json:"index_writer_memory_in_bytes"`
VersionMapMemoryInBytes int `json:"version_map_memory_in_bytes"`
FixedBitSetMemoryInBytes int `json:"fixed_bit_set_memory_in_bytes"`
} `json:"segments"`
Store struct {
SizeInBytes int `json:"size_in_bytes"`
TotalDataSetSizeInBytes int `json:"total_data_set_size_in_bytes"`
} `json:"store"`
Refresh struct {
TotalTimeInMillis int `json:"total_time_in_millis"`
ExternalTotalTimeInMillis int `json:"external_total_time_in_millis"`
} `json:"refresh"`
QueryCache struct {
MemorySizeInBytes int `json:"memory_size_in_bytes"`
HitCount int `json:"hit_count"`
MissCount int `json:"miss_count"`
} `json:"query_cache"`
RequestCache struct {
MemorySizeInBytes int `json:"memory_size_in_bytes"`
HitCount int `json:"hit_count"`
MissCount int `json:"miss_count"`
Evictions int `json:"evictions"`
} `json:"request_cache"`
Search struct {
QueryTotal int `json:"query_total"`
QueryTimeInMillis int `json:"query_time_in_millis"`
} `json:"search"`
}
type total struct {
Docs struct {
Count int `json:"count"`
Deleted int `json:"deleted"`
} `json:"docs"`
FieldData struct {
MemorySizeInBytes int `json:"memory_size_in_bytes"`
Evictions int `json:"evictions"`
} `json:"fielddata"`
Indexing struct {
IndexTotal int `json:"index_total"`
IndexTimeInMillis int `json:"index_time_in_millis"`
ThrottleTimeInMillis int `json:"throttle_time_in_millis"`
} `json:"indexing"`
Bulk *bulkStats `json:"bulk,omitempty"`
Merges struct {
TotalSizeInBytes int `json:"total_size_in_bytes"`
} `json:"merges"`
Search struct {
QueryTotal int `json:"query_total"`
QueryTimeInMillis int `json:"query_time_in_millis"`
} `json:"search"`
Segments struct {
Count int `json:"count"`
MemoryInBytes int `json:"memory_in_bytes"`
TermsMemoryInBytes int `json:"terms_memory_in_bytes"`
StoredFieldsMemoryInBytes int `json:"stored_fields_memory_in_bytes"`
TermVectorsMemoryInBytes int `json:"term_vectors_memory_in_bytes"`
NormsMemoryInBytes int `json:"norms_memory_in_bytes"`
PointsMemoryInBytes int `json:"points_memory_in_bytes"`
DocValuesMemoryInBytes int `json:"doc_values_memory_in_bytes"`
IndexWriterMemoryInBytes int `json:"index_writer_memory_in_bytes"`
VersionMapMemoryInBytes int `json:"version_map_memory_in_bytes"`
FixedBitSetMemoryInBytes int `json:"fixed_bit_set_memory_in_bytes"`
} `json:"segments"`
Store struct {
SizeInBytes int `json:"size_in_bytes"`
TotalDataSetSizeInBytes int `json:"total_data_set_size_in_bytes"`
} `json:"store"`
Refresh struct {
TotalTimeInMillis int `json:"total_time_in_millis"`
ExternalTotalTimeInMillis int `json:"external_total_time_in_millis"`
} `json:"refresh"`
QueryCache struct {
MemorySizeInBytes int `json:"memory_size_in_bytes"`
HitCount int `json:"hit_count"`
MissCount int `json:"miss_count"`
Evictions int `json:"evictions"`
} `json:"query_cache"`
RequestCache struct {
MemorySizeInBytes int `json:"memory_size_in_bytes"`
HitCount int `json:"hit_count"`
MissCount int `json:"miss_count"`
Evictions int `json:"evictions"`
} `json:"request_cache"`
}
type shardStats struct {
Total int `json:"total"`
Primaries int `json:"primaries"`
Replicas int `json:"-"`
ActiveTotal int `json:"-"`
ActivePrimaries int `json:"-"`
ActiveReplicas int `json:"-"`
UnassignedTotal int `json:"-"`
UnassignedPrimaries int `json:"-"`
UnassignedReplicas int `json:"-"`
Initializing int `json:"-"`
Relocating int `json:"-"`
}
type bulkStats struct {
TotalOperations int `json:"total_operations"`
TotalTimeInMillis int `json:"total_time_in_millis"`
TotalSizeInBytes int `json:"total_size_in_bytes"`
AvgTimeInMillis int `json:"avg_time_in_millis"`
AvgSizeInBytes int `json:"avg_size_in_bytes"`
}
var logger = logp.NewLogger("elasticsearch.index")
func eventsMapping(r mb.ReporterV2, httpClient *helper.HTTP, info elasticsearch.Info, content []byte, isXpack bool) error {
clusterStateMetrics := []string{"routing_table"}
clusterStateFilterPaths := []string{"routing_table"}
clusterState, err := elasticsearch.GetClusterState(httpClient, httpClient.GetURI(), clusterStateMetrics, clusterStateFilterPaths)
if err != nil {
return fmt.Errorf("failure retrieving cluster state from Elasticsearch: %w", err)
}
indicesSettingsPattern := "*,.*"
indicesSettingsFilterPaths := []string{"*.settings.index.creation_date", "*.settings.index.**._tier_preference", "*.settings.index.version.created"}
indicesSettings, err := elasticsearch.GetIndexSettings(httpClient, httpClient.GetURI(), indicesSettingsPattern, indicesSettingsFilterPaths)
if err != nil {
return fmt.Errorf("failure retrieving index settings from Elasticsearch: %w", err)
}
// Under some very rare circumstances, an index in the stats response might not have an entry in the settings or cluster state.
// This can happen if the index got deleted between the time the settings and cluster state were retrieved and the time the stats were retrieved.
var indicesStats stats
if err := parseAPIResponse(content, &indicesStats); err != nil {
return fmt.Errorf("failure parsing Indices Stats Elasticsearch API response: %w", err)
}
var errs multierror.Errors
for name := range indicesStats.Indices {
event := mb.Event{
ModuleFields: mapstr.M{},
}
idx := indicesStats.Indices[name]
idx.Index = name
err = addClusterStateFields(&idx, clusterState)
if err != nil {
// We can't continue processing this index, so we skip it.
errs = append(errs, fmt.Errorf("failure adding cluster state fields: %w", err))
continue
}
err = addIndexSettings(&idx, indicesSettings)
if err != nil {
// Failure to add index settings is sometimes expected and won't be breaking,
// so we log it as debug and carry on with regular processing.
logger.Debugf("failure adding index settings: %v", err)
}
event.ModuleFields.Put("cluster.id", info.ClusterID)
event.ModuleFields.Put("cluster.name", info.ClusterName)
// Convert struct to common.Mapstr by passing it to JSON first so we can store the data in the root of the
// metricset level
indexBytes, err := json.Marshal(idx)
if err != nil {
errs = append(errs, fmt.Errorf("failure trying to convert metrics results to JSON: %w", err))
continue
}
var indexOutput mapstr.M
if err = json.Unmarshal(indexBytes, &indexOutput); err != nil {
errs = append(errs, fmt.Errorf("failure trying to convert JSON metrics back to mapstr: %w", err))
continue
}
event.MetricSetFields = indexOutput
event.MetricSetFields.Put("name", name)
delete(event.MetricSetFields, "index")
// 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 errs.Err()
}
func parseAPIResponse(content []byte, indicesStats *stats) error {
return json.Unmarshal(content, indicesStats)
}
// Fields added here are based on same fields being added by internal collection in
// https://github.com/elastic/elasticsearch/blob/master/x-pack/plugin/monitoring/src/main/java/org/elasticsearch/xpack/monitoring/collector/indices/IndexStatsMonitoringDoc.java#L62-L124
func addClusterStateFields(idx *Index, clusterState mapstr.M) error {
indexRoutingTable, err := getClusterStateMetricForIndex(clusterState, idx.Index, "routing_table")
if err != nil {
return fmt.Errorf("failed to get index routing table from cluster state: %w", err)
}
shards, err := getShardsFromRoutingTable(indexRoutingTable)
if err != nil {
return fmt.Errorf("failed to get shards from routing table: %w", err)
}
// "index_stats.version.created", <--- don't think this is being used in the UI, so can we skip it?
// "index_stats.version.upgraded", <--- don't think this is being used in the UI, so can we skip it?
status, err := getIndexStatus(shards)
if err != nil {
return fmt.Errorf("failed to get index status: %w", err)
}
idx.Status = status
shardStats, err := getIndexShardStats(shards)
if err != nil {
return fmt.Errorf("failed to get index shard stats: %w", err)
}
idx.Shards = *shardStats
return nil
}
func addIndexSettings(idx *Index, indicesSettings mapstr.M) error {
// Recover the index settings for our specific index
indexSettingsValue, err := indicesSettings.GetValue(idx.Index)
if err != nil {
return fmt.Errorf("failed to get index settings for index %s: %w", idx.Index, err)
}
indexSettings, ok := indexSettingsValue.(map[string]interface{})
if !ok {
return fmt.Errorf("index settings is not a map for index: %s", idx.Index)
}
indexCreationDate, err := getIndexSettingForIndex(indexSettings, idx.Index, "index.creation_date")
if err != nil {
return fmt.Errorf("failed to get index creation date: %w", err)
}
idx.CreationDate = indexCreationDate
indexTierPreference, err := getIndexSettingForIndex(indexSettings, idx.Index, "index.routing.allocation.require._tier_preference")
if err != nil {
indexTierPreference, err = getIndexSettingForIndex(indexSettings, idx.Index, "index.routing.allocation.include._tier_preference")
if err != nil {
return fmt.Errorf("failed to get index tier preference: %w", err)
}
}
idx.TierPreference = indexTierPreference
indexVersion, err := getIndexSettingForIndex(indexSettings, idx.Index, "index.version.created")
if err != nil {
return fmt.Errorf("failed to get index version: %w", err)
}
idx.Version = indexVersion
return nil
}
func getIndexSettingForIndex(indexSettings mapstr.M, index, settingKey string) (string, error) {
fieldKey := "settings." + settingKey
value, err := indexSettings.GetValue(fieldKey)
if err != nil {
return "", fmt.Errorf("'"+fieldKey+"': %w", err)
}
setting, ok := value.(string)
if !ok {
return "", elastic.MakeErrorForMissingField(fieldKey, elastic.Elasticsearch)
}
return setting, nil
}
func getClusterStateMetricForIndex(clusterState mapstr.M, index, metricKey string) (mapstr.M, error) {
fieldKey := metricKey + ".indices." + index
value, err := clusterState.GetValue(fieldKey)
if err != nil {
return nil, fmt.Errorf("'"+fieldKey+"': %w", err)
}
metric, ok := value.(map[string]interface{})
if !ok {
return nil, elastic.MakeErrorForMissingField(fieldKey, elastic.Elasticsearch)
}
return mapstr.M(metric), nil
}
func getIndexStatus(shards map[string]interface{}) (string, error) {
if len(shards) == 0 {
// No shards, index is red
return "red", nil
}
areAllPrimariesStarted := true
areAllReplicasStarted := true
for indexName, indexShard := range shards {
is, ok := indexShard.([]interface{})
if !ok {
return "", fmt.Errorf("shards is not an array")
}
for shardIdx, shard := range is {
s, ok := shard.(map[string]interface{})
if !ok {
return "", fmt.Errorf("%v.shards[%v] is not a map", indexName, shardIdx)
}
shard := mapstr.M(s)
isPrimary, ok := shard["primary"].(bool)
if !ok {
return "", fmt.Errorf("%v.shards[%v].primary is not a boolean", indexName, shardIdx)
}
state, ok := shard["state"].(string)
if !ok {
return "", fmt.Errorf("%v.shards[%v].state is not a string", indexName, shardIdx)
}
if isPrimary {
areAllPrimariesStarted = areAllPrimariesStarted && (state == "STARTED")
} else {
areAllReplicasStarted = areAllReplicasStarted && (state == "STARTED")
}
}
}
if areAllPrimariesStarted && areAllReplicasStarted {
return "green", nil
}
if areAllPrimariesStarted && !areAllReplicasStarted {
return "yellow", nil
}
return "red", nil
}
func getIndexShardStats(shards mapstr.M) (*shardStats, error) {
primaries := 0
replicas := 0
activePrimaries := 0
activeReplicas := 0
unassignedPrimaries := 0
unassignedReplicas := 0
initializing := 0
relocating := 0
for indexName, indexShard := range shards {
is, ok := indexShard.([]interface{})
if !ok {
return nil, fmt.Errorf("shards is not an array")
}
for shardIdx, shard := range is {
s, ok := shard.(map[string]interface{})
if !ok {
return nil, fmt.Errorf("%v.shards[%v] is not a map", indexName, shardIdx)
}
shard := mapstr.M(s)
isPrimary, ok := shard["primary"].(bool)
if !ok {
return nil, fmt.Errorf("%v.shards[%v].primary is not a boolean", indexName, shardIdx)
}
state, ok := shard["state"].(string)
if !ok {
return nil, fmt.Errorf("%v.shards[%v].state is not a string", indexName, shardIdx)
}
if isPrimary {
primaries++
switch state {
case "STARTED":
activePrimaries++
case "UNASSIGNED":
unassignedPrimaries++
}
} else {
replicas++
switch state {
case "STARTED":
activeReplicas++
case "UNASSIGNED":
unassignedReplicas++
}
}
switch state {
case "INITIALIZING":
initializing++
case "RELOCATING":
relocating++
}
}
}
return &shardStats{
Total: primaries + replicas,
Primaries: primaries,
Replicas: replicas,
ActiveTotal: activePrimaries + activeReplicas,
ActivePrimaries: activePrimaries,
ActiveReplicas: activeReplicas,
UnassignedTotal: unassignedPrimaries + unassignedReplicas,
UnassignedPrimaries: unassignedPrimaries,
UnassignedReplicas: unassignedReplicas,
Initializing: initializing,
Relocating: relocating,
}, nil
}
func getShardsFromRoutingTable(indexRoutingTable mapstr.M) (map[string]interface{}, error) {
s, err := indexRoutingTable.GetValue("shards")
if err != nil {
return nil, err
}
shards, ok := s.(map[string]interface{})
if !ok {
return nil, elastic.MakeErrorForMissingField("shards", elastic.Elasticsearch)
}
return shards, nil
}