forked from 42wim/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dashboards.go
205 lines (164 loc) · 5.38 KB
/
dashboards.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
package dashboards
import (
"errors"
"fmt"
"path/filepath"
"strconv"
"strings"
"github.com/elastic/beats/libbeat/common"
)
type importMethod uint8
// check import route
const (
importNone importMethod = iota
importViaKibana
importViaES
)
// ImportDashboards tries to import the kibana dashboards.
// If the Elastic Stack is at version 6.0+, the dashboards should be installed
// via the kibana dashboard loader plugin. For older versions of the Elastic Stack
// we write the dashboards directly into the .kibana index.
func ImportDashboards(
beatName, hostname, homePath string,
kibanaConfig, esConfig, dashboardsConfig *common.Config,
msgOutputter MessageOutputter,
) error {
if dashboardsConfig == nil || !dashboardsConfig.Enabled() {
return nil
}
// unpack dashboard config
dashConfig := defaultConfig
dashConfig.Beat = beatName
dashConfig.Dir = filepath.Join(homePath, defaultDirectory)
err := dashboardsConfig.Unpack(&dashConfig)
if err != nil {
return err
}
// init kibana config object
if kibanaConfig == nil {
kibanaConfig = common.NewConfig()
}
if esConfig.Enabled() {
username, _ := esConfig.String("username", -1)
password, _ := esConfig.String("password", -1)
if !kibanaConfig.HasField("username") && username != "" {
kibanaConfig.SetString("username", -1, username)
}
if !kibanaConfig.HasField("password") && password != "" {
kibanaConfig.SetString("password", -1, password)
}
}
var esLoader *ElasticsearchLoader
importVia := importNone
useKibana := importViaKibana
if !kibanaConfig.Enabled() {
useKibana = importNone
}
requiresKibana := dashConfig.AlwaysKibana || !esConfig.Enabled()
if requiresKibana {
importVia = useKibana
} else {
// Check import route via elasticsearch version. If Elasticsearch major
// version is >6, we assume Kibana also being at versions >6.0. In this
// case dashboards will be imported using the new kibana dashboard loader
// plugin.
// XXX(urso): Why do we test the Elasticsearch version? If kibana is
// configured, why not test the kibana version and plugin
// availability first?
esLoader, err = NewElasticsearchLoader(esConfig, &dashConfig, msgOutputter)
if err != nil {
return fmt.Errorf("fail to create the Elasticsearch loader: %v", err)
}
defer esLoader.Close()
esLoader.statusMsg("Elasticsearch URL %v", esLoader.client.Connection.URL)
majorVersion, _, err := getMajorAndMinorVersion(esLoader.version)
if err != nil {
return fmt.Errorf("wrong Elasticsearch version: %v", err)
}
if majorVersion < 6 {
importVia = importViaES
} else {
importVia = useKibana
}
}
// Try to import dashboards.
switch importVia {
case importViaES:
return ImportDashboardsViaElasticsearch(esLoader)
case importViaKibana:
return setupAndImportDashboardsViaKibana(hostname, kibanaConfig, &dashConfig, msgOutputter)
default:
return errors.New("Elasticsearch or Kibana configuration missing for loading dashboards.")
}
}
func setupAndImportDashboardsViaKibana(hostname string, kibanaConfig *common.Config,
dashboardsConfig *Config, msgOutputter MessageOutputter) error {
kibanaLoader, err := NewKibanaLoader(kibanaConfig, dashboardsConfig, hostname, msgOutputter)
if err != nil {
return fmt.Errorf("fail to create the Kibana loader: %v", err)
}
defer kibanaLoader.Close()
kibanaLoader.statusMsg("Kibana URL %v", kibanaLoader.client.Connection.URL)
return ImportDashboardsViaKibana(kibanaLoader)
}
func ImportDashboardsViaKibana(kibanaLoader *KibanaLoader) error {
if !isKibanaAPIavailable(kibanaLoader.version) {
return fmt.Errorf("Kibana API is not available in Kibana version %s", kibanaLoader.version)
}
version, err := common.NewVersion(kibanaLoader.version)
if err != nil {
return fmt.Errorf("Invalid Kibana version: %s", kibanaLoader.version)
}
importer, err := NewImporter(*version, kibanaLoader.config, kibanaLoader)
if err != nil {
return fmt.Errorf("fail to create a Kibana importer for loading the dashboards: %v", err)
}
if err := importer.Import(); err != nil {
return fmt.Errorf("fail to import the dashboards in Kibana: %v", err)
}
return nil
}
func ImportDashboardsViaElasticsearch(esLoader *ElasticsearchLoader) error {
if err := esLoader.CreateKibanaIndex(); err != nil {
return fmt.Errorf("fail to create the kibana index: %v", err)
}
version, _ := common.NewVersion("5.0.0")
importer, err := NewImporter(*version, esLoader.config, esLoader)
if err != nil {
return fmt.Errorf("fail to create an Elasticsearch importer for loading the dashboards: %v", err)
}
if err := importer.Import(); err != nil {
return fmt.Errorf("fail to import the dashboards in Elasticsearch: %v", err)
}
return nil
}
func getMajorAndMinorVersion(version string) (int, int, error) {
fields := strings.Split(version, ".")
if len(fields) != 3 {
return 0, 0, fmt.Errorf("wrong version %s", version)
}
majorVersion := fields[0]
minorVersion := fields[1]
majorVersionInt, err := strconv.Atoi(majorVersion)
if err != nil {
return 0, 0, err
}
minorVersionInt, err := strconv.Atoi(minorVersion)
if err != nil {
return 0, 0, err
}
return majorVersionInt, minorVersionInt, nil
}
func isKibanaAPIavailable(version string) bool {
majorVersion, minorVersion, err := getMajorAndMinorVersion(version)
if err != nil {
return false
}
if majorVersion == 5 && minorVersion >= 6 {
return true
}
if majorVersion >= 6 {
return true
}
return false
}