Skip to content

Commit

Permalink
Clean up Kibana, remove Elasticsearch loading and 5.x version (#10451)
Browse files Browse the repository at this point in the history
In 5.x the dashboards were loaded through Elasticsearch and not Kibana. For 6.x and 7.x dashboards are loaded through the Kibana API. The old code is removed in this PR.

Also some code is cleaned up around talking to 5.x Kibana APIs.
  • Loading branch information
ruflin authored Jan 31, 2019
1 parent 33b789c commit b054f08
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 539 deletions.
7 changes: 1 addition & 6 deletions libbeat/cmd/instance/beat.go
Original file line number Diff line number Diff line change
Expand Up @@ -705,13 +705,8 @@ func (b *Beat) loadDashboards(ctx context.Context, force bool) error {
}

if b.Config.Dashboards.Enabled() {
var esConfig *common.Config

if b.Config.Output.Name() == "elasticsearch" {
esConfig = b.Config.Output.Config()
}
err := dashboards.ImportDashboards(ctx, b.Info.Beat, b.Info.Hostname, paths.Resolve(paths.Home, ""),
b.Config.Kibana, esConfig, b.Config.Dashboards, nil)
b.Config.Kibana, b.Config.Dashboards, nil)
if err != nil {
return errw.Wrap(err, "Error importing Kibana dashboards")
}
Expand Down
87 changes: 3 additions & 84 deletions libbeat/dashboards/dashboards.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,11 @@ import (
"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(
ctx context.Context,
beatName, hostname, homePath string,
kibanaConfig, esConfig, dashboardsConfig *common.Config,
kibanaConfig, dashboardsConfig *common.Config,
msgOutputter MessageOutputter,
) error {
if dashboardsConfig == nil || !dashboardsConfig.Enabled() {
Expand All @@ -65,61 +53,12 @@ func ImportDashboards(
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
return errors.New("kibana configuration missing for loading dashboards.")
}

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)

if esLoader.version.Major < 6 {
importVia = importViaES
} else {
importVia = useKibana
}
}
return setupAndImportDashboardsViaKibana(ctx, hostname, kibanaConfig, &dashConfig, msgOutputter)

// Try to import dashboards.
switch importVia {
case importViaES:
return ImportDashboardsViaElasticsearch(esLoader)
case importViaKibana:
return setupAndImportDashboardsViaKibana(ctx, hostname, kibanaConfig, &dashConfig, msgOutputter)
default:
return errors.New("Elasticsearch or Kibana configuration missing for loading dashboards.")
}
}

func setupAndImportDashboardsViaKibana(ctx context.Context, hostname string, kibanaConfig *common.Config,
Expand Down Expand Up @@ -159,26 +98,6 @@ func ImportDashboardsViaKibana(kibanaLoader *KibanaLoader) error {
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 isKibanaAPIavailable(version common.Version) bool {
return (version.Major == 5 && version.Minor >= 6) || version.Major >= 6
}
Loading

0 comments on commit b054f08

Please sign in to comment.