This repository has been archived by the owner on Sep 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathelastic_agent_checks.go
417 lines (363 loc) · 10.8 KB
/
elastic_agent_checks.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
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.
package main
import (
"context"
"fmt"
"strings"
"time"
"github.com/cenkalti/backoff/v4"
"github.com/elastic/e2e-testing/internal/common"
"github.com/elastic/e2e-testing/internal/deploy"
"github.com/elastic/e2e-testing/internal/elasticsearch"
"github.com/elastic/e2e-testing/internal/installer"
"github.com/elastic/e2e-testing/internal/kibana"
"github.com/elastic/e2e-testing/internal/utils"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
)
func (fts *FleetTestSuite) checkDataStream() error {
query := map[string]interface{}{
"query": map[string]interface{}{
"bool": map[string]interface{}{
"filter": []interface{}{
map[string]interface{}{
"exists": map[string]interface{}{
"field": "linux.memory.page_stats",
},
},
map[string]interface{}{
"exists": map[string]interface{}{
"field": "elastic_agent",
},
},
map[string]interface{}{
"range": map[string]interface{}{
"@timestamp": map[string]interface{}{
"gte": "now-1m",
},
},
},
map[string]interface{}{
"term": map[string]interface{}{
"data_stream.type": "metrics",
},
},
map[string]interface{}{
"term": map[string]interface{}{
"data_stream.dataset": "linux.memory",
},
},
map[string]interface{}{
"term": map[string]interface{}{
"data_stream.namespace": "default",
},
},
map[string]interface{}{
"term": map[string]interface{}{
"event.dataset": "linux.memory",
},
},
map[string]interface{}{
"term": map[string]interface{}{
"agent.type": "metricbeat",
},
},
map[string]interface{}{
"term": map[string]interface{}{
"metricset.period": 1000,
},
},
map[string]interface{}{
"term": map[string]interface{}{
"service.type": "linux",
},
},
},
},
},
}
indexName := "metrics-linux.memory-default"
_, err := elasticsearch.WaitForNumberOfHits(context.Background(), indexName, query, 1, 3*time.Minute)
if err != nil {
log.WithFields(log.Fields{
"error": err,
}).Warn(elasticsearch.WaitForIndices())
}
return err
}
func (fts *FleetTestSuite) systemPackageDashboardsAreListedInFleet() error {
log.Trace("Checking system Package dashboards in Fleet")
dataStreamsCount := 0
maxTimeout := time.Duration(utils.TimeoutFactor) * time.Minute
retryCount := 1
exp := utils.GetExponentialBackOff(maxTimeout)
countDataStreamsFn := func() error {
dataStreams, err := fts.kibanaClient.GetDataStreams(fts.currentContext)
if err != nil {
log.WithFields(log.Fields{
"retry": retryCount,
"elapsedTime": exp.GetElapsedTime(),
}).Warn(err.Error())
retryCount++
return err
}
count := len(dataStreams.Children())
if count == 0 {
err = fmt.Errorf("there are no datastreams yet")
log.WithFields(log.Fields{
"retry": retryCount,
"dataStreams": count,
"elapsedTime": exp.GetElapsedTime(),
}).Warn(err.Error())
retryCount++
return err
}
log.WithFields(log.Fields{
"elapsedTime": exp.GetElapsedTime(),
"datastreams": count,
"retries": retryCount,
}).Info("Datastreams are present")
dataStreamsCount = count
return nil
}
err := backoff.Retry(countDataStreamsFn, exp)
if err != nil {
return err
}
if dataStreamsCount == 0 {
err = fmt.Errorf("there are no datastreams. We expected to have more than one")
log.Error(err.Error())
return err
}
return nil
}
func (fts *FleetTestSuite) tagsAreInTheElasticAgentIndex() error {
var tagsArray []string
//ex of flags "--tag production,linux" or "--tag=production,linux"
if fts.ElasticAgentFlags != "" {
tags := strings.TrimPrefix(fts.ElasticAgentFlags, "--tag")
tags = strings.TrimPrefix(tags, "=")
tags = strings.ReplaceAll(tags, " ", "")
tagsArray = strings.Split(tags, ",")
}
if len(tagsArray) == 0 {
return errors.Errorf("no tags were found, ElasticAgentFlags value %s", fts.ElasticAgentFlags)
}
var tagTerms []map[string]interface{}
for _, tag := range tagsArray {
tagTerms = append(tagTerms, map[string]interface{}{
"term": map[string]interface{}{
"tags": tag,
},
})
}
query := map[string]interface{}{
"query": map[string]interface{}{
"bool": map[string]interface{}{
"must": tagTerms,
},
},
}
indexName := ".fleet-agents"
_, err := elasticsearch.WaitForNumberOfHits(context.Background(), indexName, query, 1, 3*time.Minute)
if err != nil {
log.WithFields(log.Fields{
"error": err,
}).Warn(elasticsearch.WaitForIndices())
}
return err
}
func (fts *FleetTestSuite) theAgentIsListedInFleetWithStatus(desiredStatus string) error {
agentService := deploy.NewServiceRequest(common.ElasticAgentServiceName)
manifest, _ := fts.getDeployer().GetServiceManifest(fts.currentContext, agentService)
err := theAgentIsListedInFleetWithStatus(fts.currentContext, desiredStatus, manifest.Hostname)
if err != nil {
return err
}
if desiredStatus == "online" {
//get Agent Default Key
err := fts.theAgentGetDefaultAPIKey()
if err != nil {
return err
}
}
return err
}
func (fts *FleetTestSuite) theFileSystemAgentFolderIsEmpty() error {
agentService := deploy.NewServiceRequest(common.ElasticAgentServiceName)
agentInstaller, _ := installer.Attach(fts.currentContext, fts.getDeployer(), agentService, fts.InstallerType)
pkgManifest, _ := agentInstaller.Inspect()
cmd := []string{
"ls", "-l", pkgManifest.WorkDir,
}
content, err := agentInstaller.Exec(fts.currentContext, cmd)
if err != nil {
if content == "" || strings.Contains(content, "No such file or directory") {
return nil
}
return err
}
log.WithFields(log.Fields{
"installer": agentInstaller,
"workingDir": pkgManifest.WorkDir,
"content": content,
}).Debug("Agent working dir content")
return fmt.Errorf("the file system directory is not empty")
}
func (fts *FleetTestSuite) thereIsNewDataInTheIndexFromAgent() error {
maxTimeout := time.Duration(utils.TimeoutFactor) * time.Minute * 2
minimumHitsCount := 20
agentService := deploy.NewServiceContainerRequest(common.ElasticAgentServiceName).WithFlavour(fts.Image)
manifest, _ := fts.getDeployer().GetServiceManifest(fts.currentContext, agentService)
result, err := searchAgentData(fts.currentContext, manifest.Hostname, fts.RuntimeDependenciesStartDate, minimumHitsCount, maxTimeout)
if err != nil {
return err
}
log.Tracef("Search result: %v", result)
return elasticsearch.AssertHitsArePresent(result)
}
func searchAgentData(ctx context.Context, hostname string, startDate time.Time, minimumHitsCount int, maxTimeout time.Duration) (elasticsearch.SearchResult, error) {
timezone := "America/New_York"
esQuery := map[string]interface{}{
"version": true,
"size": 500,
"docvalue_fields": []map[string]interface{}{
{
"field": "@timestamp",
"format": "date_time",
},
{
"field": "system.process.cpu.start_time",
"format": "date_time",
},
{
"field": "system.service.state_since",
"format": "date_time",
},
},
"_source": map[string]interface{}{
"excludes": []map[string]interface{}{},
},
"query": map[string]interface{}{
"bool": map[string]interface{}{
"must": []map[string]interface{}{},
"filter": []map[string]interface{}{
{
"bool": map[string]interface{}{
"filter": []map[string]interface{}{
{
"bool": map[string]interface{}{
"should": []map[string]interface{}{
{
"match_phrase": map[string]interface{}{
"host.name": hostname,
},
},
},
"minimum_should_match": 1,
},
},
{
"bool": map[string]interface{}{
"should": []map[string]interface{}{
{
"range": map[string]interface{}{
"@timestamp": map[string]interface{}{
"gte": startDate,
"time_zone": timezone,
},
},
},
},
"minimum_should_match": 1,
},
},
},
},
},
{
"range": map[string]interface{}{
"@timestamp": map[string]interface{}{
"gte": startDate,
"format": "strict_date_optional_time",
},
},
},
},
"should": []map[string]interface{}{},
"must_not": []map[string]interface{}{},
},
},
}
indexName := "logs-elastic_agent-default"
result, err := elasticsearch.WaitForNumberOfHits(ctx, indexName, esQuery, minimumHitsCount, maxTimeout)
if err != nil {
log.WithFields(log.Fields{
"error": err,
}).Warn(elasticsearch.WaitForIndices())
}
return result, err
}
func theAgentIsListedInFleetWithStatus(ctx context.Context, desiredStatus string, hostname string) error {
log.Tracef("Checking if agent is listed in Fleet as %s", desiredStatus)
kibanaClient, err := kibana.NewClient()
if err != nil {
return err
}
maxTimeout := time.Duration(utils.TimeoutFactor) * time.Minute * 2
retryCount := 1
exp := utils.GetExponentialBackOff(maxTimeout)
agentOnlineFn := func() error {
agentID, err := kibanaClient.GetAgentIDByHostname(ctx, hostname)
if err != nil {
retryCount++
return err
}
if agentID == "" {
// the agent is not listed in Fleet
if desiredStatus == "offline" || desiredStatus == "inactive" {
log.WithFields(log.Fields{
"elapsedTime": exp.GetElapsedTime(),
"hostname": hostname,
"retries": retryCount,
"status": desiredStatus,
}).Info("The Agent is not present in Fleet, as expected")
return nil
}
retryCount++
return fmt.Errorf("the agent is not present in Fleet in the '%s' status, but it should", desiredStatus)
}
agentStatus, err := kibanaClient.GetAgentStatusByHostname(ctx, hostname)
isAgentInStatus := strings.EqualFold(agentStatus, desiredStatus)
if err != nil || !isAgentInStatus {
if err == nil {
err = fmt.Errorf("the Agent is not in the %s status yet", desiredStatus)
}
log.WithFields(log.Fields{
"agentID": agentID,
"isAgentInStatus": isAgentInStatus,
"elapsedTime": exp.GetElapsedTime(),
"hostname": hostname,
"retry": retryCount,
"status": desiredStatus,
}).Warn(err.Error())
retryCount++
return err
}
log.WithFields(log.Fields{
"isAgentInStatus": isAgentInStatus,
"elapsedTime": exp.GetElapsedTime(),
"hostname": hostname,
"retries": retryCount,
"status": desiredStatus,
}).Info("The Agent is in the desired status")
return nil
}
err = backoff.Retry(agentOnlineFn, exp)
if err != nil {
return err
}
return nil
}