forked from elastic/beats
/
es_loader.go
311 lines (254 loc) · 7.86 KB
/
es_loader.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
package dashboards
import (
"encoding/json"
"fmt"
"io/ioutil"
"path"
"path/filepath"
"strings"
"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/logp"
"github.com/elastic/beats/libbeat/outputs/elasticsearch"
)
type ElasticsearchLoader struct {
client *elasticsearch.Client
config *Config
version string
msgOutputter MessageOutputter
}
func NewElasticsearchLoader(cfg *common.Config, dashboardsConfig *Config, msgOutputter MessageOutputter) (*ElasticsearchLoader, error) {
if cfg == nil || !cfg.Enabled() {
return nil, fmt.Errorf("Elasticsearch output is not configured/enabled")
}
esClient, err := elasticsearch.NewConnectedClient(cfg)
if err != nil {
return nil, fmt.Errorf("Error creating Elasticsearch client: %v", err)
}
version := esClient.GetVersion()
loader := ElasticsearchLoader{
client: esClient,
config: dashboardsConfig,
version: version,
msgOutputter: msgOutputter,
}
loader.statusMsg("Initialize the Elasticsearch %s loader", version)
return &loader, nil
}
// CreateKibanaIndex creates the kibana index if it doesn't exists and sets
// some index properties which are needed as a workaround for:
// https://github.com/elastic/beats-dashboards/issues/94
func (loader ElasticsearchLoader) CreateKibanaIndex() error {
status, err := loader.client.IndexExists(loader.config.KibanaIndex)
if err != nil {
if status != 404 {
return err
}
_, _, err = loader.client.CreateIndex(loader.config.KibanaIndex, nil)
if err != nil {
return fmt.Errorf("Failed to create index: %v", err)
}
_, _, err = loader.client.CreateIndex(loader.config.KibanaIndex+"/_mapping/search",
common.MapStr{
"search": common.MapStr{
"properties": common.MapStr{
"hits": common.MapStr{
"type": "integer",
},
"version": common.MapStr{
"type": "integer",
},
},
},
})
if err != nil {
return fmt.Errorf("Failed to set the mapping: %v", err)
}
}
return nil
}
func (loader ElasticsearchLoader) ImportIndex(file string) error {
reader, err := ioutil.ReadFile(file)
if err != nil {
return err
}
var indexContent common.MapStr
err = json.Unmarshal(reader, &indexContent)
if err != nil {
return fmt.Errorf("fail to unmarshal index content: %v", err)
}
indexName, ok := indexContent["title"].(string)
if !ok {
return fmt.Errorf("Missing title in the index-pattern file at %s", file)
}
if loader.config.Index != "" {
// change index pattern name
loader.statusMsg("Change index in index-pattern %s", indexName)
indexContent["title"] = loader.config.Index
}
path := "/" + loader.config.KibanaIndex + "/index-pattern/" + indexName
if _, err = loader.client.LoadJSON(path, indexContent); err != nil {
return err
}
return nil
}
func (loader ElasticsearchLoader) importJSONFile(fileType string, file string) error {
path := "/" + loader.config.KibanaIndex + "/" + fileType
reader, err := ioutil.ReadFile(file)
if err != nil {
return fmt.Errorf("Failed to read %s. Error: %s", file, err)
}
var jsonContent map[string]interface{}
err = json.Unmarshal(reader, &jsonContent)
if err != nil {
return fmt.Errorf("fail to unmarshal json file: %v", err)
}
fileBase := strings.TrimSuffix(filepath.Base(file), filepath.Ext(file))
body, err := loader.client.LoadJSON(path+"/"+fileBase, jsonContent)
if err != nil {
return fmt.Errorf("Failed to load %s under %s/%s: %s. Response body: %s", file, path, fileBase, err, body)
}
return nil
}
func (loader ElasticsearchLoader) importPanelsFromDashboard(file string) (err error) {
// directory with the dashboards
dir := filepath.Dir(file)
// main directory with dashboard, search, visualizations directories
mainDir := filepath.Dir(dir)
reader, err := ioutil.ReadFile(file)
if err != nil {
return
}
type record struct {
Title string `json:"title"`
PanelsJSON string `json:"panelsJSON"`
}
type panel struct {
ID string `json:"id"`
Type string `json:"type"`
}
var jsonContent record
err = json.Unmarshal(reader, &jsonContent)
if err != nil {
return fmt.Errorf("fail to unmarshal json content: %v", err)
}
var widgets []panel
err = json.Unmarshal([]byte(jsonContent.PanelsJSON), &widgets)
if err != nil {
return fmt.Errorf("fail to unmarshal panels content: %v", err)
}
for _, widget := range widgets {
if widget.Type == "visualization" {
err = loader.importVisualization(path.Join(mainDir, "visualization", widget.ID+".json"))
if err != nil {
return err
}
} else if widget.Type == "search" {
err = loader.importSearch(path.Join(mainDir, "search", widget.ID+".json"))
if err != nil {
return err
}
} else {
loader.statusMsg("Widgets: %v", widgets)
return fmt.Errorf("Unknown panel type %s in %s", widget.Type, file)
}
}
return
}
func (loader ElasticsearchLoader) importVisualization(file string) error {
loader.statusMsg("Import visualization %s", file)
reader, err := ioutil.ReadFile(file)
if err != nil {
return err
}
var vizContent common.MapStr
err = json.Unmarshal(reader, &vizContent)
if err != nil {
return fmt.Errorf("fail to unmarshal visualization content %s: %v", file, err)
}
if loader.config.Index != "" {
if savedObject, ok := vizContent["kibanaSavedObjectMeta"].(map[string]interface{}); ok {
vizContent["kibanaSavedObjectMeta"] = ReplaceIndexInSavedObject(loader.config.Index, savedObject)
}
}
vizName := strings.TrimSuffix(filepath.Base(file), filepath.Ext(file))
path := "/" + loader.config.KibanaIndex + "/visualization/" + vizName
if _, err := loader.client.LoadJSON(path, vizContent); err != nil {
return err
}
return loader.importSearchFromVisualization(file)
}
func (loader ElasticsearchLoader) importSearch(file string) error {
reader, err := ioutil.ReadFile(file)
if err != nil {
return err
}
searchName := strings.TrimSuffix(filepath.Base(file), filepath.Ext(file))
var searchContent common.MapStr
err = json.Unmarshal(reader, &searchContent)
if err != nil {
return fmt.Errorf("fail to unmarshal search content %s: %v", searchName, err)
}
if loader.config.Index != "" {
// change index pattern name
if savedObject, ok := searchContent["kibanaSavedObjectMeta"].(map[string]interface{}); ok {
searchContent["kibanaSavedObjectMeta"] = ReplaceIndexInSavedObject(loader.config.Index, savedObject)
}
}
path := "/" + loader.config.KibanaIndex + "/search/" + searchName
loader.statusMsg("Import search %s", file)
if _, err = loader.client.LoadJSON(path, searchContent); err != nil {
return err
}
return nil
}
func (loader ElasticsearchLoader) importSearchFromVisualization(file string) error {
type record struct {
Title string `json:"title"`
SavedSearchID string `json:"savedSearchId"`
}
reader, err := ioutil.ReadFile(file)
if err != nil {
return nil
}
var jsonContent record
err = json.Unmarshal(reader, &jsonContent)
if err != nil {
return fmt.Errorf("fail to unmarshal the search content: %v", err)
}
id := jsonContent.SavedSearchID
if len(id) == 0 {
// no search used
return nil
}
// directory with the visualizations
dir := filepath.Dir(file)
// main directory
mainDir := filepath.Dir(dir)
searchFile := path.Join(mainDir, "search", id+".json")
if searchFile != "" {
// visualization depends on search
if err := loader.importSearch(searchFile); err != nil {
return err
}
}
return nil
}
func (loader ElasticsearchLoader) ImportDashboard(file string) error {
/* load dashboard */
err := loader.importJSONFile("dashboard", file)
if err != nil {
return err
}
/* load the visualizations and searches that depend on the dashboard */
return loader.importPanelsFromDashboard(file)
}
func (loader ElasticsearchLoader) Close() error {
return loader.client.Close()
}
func (loader ElasticsearchLoader) statusMsg(msg string, a ...interface{}) {
if loader.msgOutputter != nil {
loader.msgOutputter(msg, a...)
} else {
logp.Debug("dashboards", msg, a...)
}
}