forked from bigpigeon/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
106 lines (90 loc) · 2.06 KB
/
client.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
package elasticsearch
import (
"encoding/json"
"fmt"
"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/monitoring/report"
esout "github.com/elastic/beats/libbeat/outputs/elasticsearch"
"github.com/elastic/beats/libbeat/publisher"
"github.com/elastic/beats/libbeat/testing"
)
type publishClient struct {
es *esout.Client
params map[string]string
}
var (
// monitoring beats action
actMonitoringBeats = common.MapStr{
"index": common.MapStr{
"_index": "",
"_type": "beats_stats",
"_routing": nil,
},
}
)
func newPublishClient(
es *esout.Client,
params map[string]string,
) *publishClient {
p := &publishClient{
es: es,
params: params,
}
return p
}
func (c *publishClient) Connect() error {
debugf("Monitoring client: connect.")
params := map[string]string{
"filter_path": "features.monitoring.enabled",
}
status, body, err := c.es.Request("GET", "/_xpack", "", params, nil)
if err != nil {
debugf("XPack capabilities query failed with: %v", err)
return err
}
if status != 200 {
err := fmt.Errorf("XPack capabilities query failed with status code: %v", status)
debugf("%s", err)
return err
}
resp := struct {
Features struct {
Monitoring struct {
Enabled bool
}
}
}{}
if err := json.Unmarshal(body, &resp); err != nil {
return err
}
if !resp.Features.Monitoring.Enabled {
debugf("XPack monitoring is disabled.")
return errNoMonitoring
}
debugf("XPack monitoring is enabled")
return nil
}
func (c *publishClient) Close() error {
return c.es.Close()
}
func (c *publishClient) Publish(batch publisher.Batch) error {
events := batch.Events()
bulk := make([]interface{}, 0, 2*len(events))
for _, event := range events {
bulk = append(bulk,
actMonitoringBeats, report.Event{
Timestamp: event.Content.Timestamp,
Fields: event.Content.Fields,
})
}
_, err := c.es.BulkWith("_xpack", "monitoring", c.params, nil, bulk)
if err != nil {
batch.Retry()
return err
}
batch.ACK()
return nil
}
func (c *publishClient) Test(d testing.Driver) {
c.es.Test(d)
}