-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support to export dashboard to Beat
Currently to export a Dashboard from the Kibana exporter API it has to be done manually or the Beats repository must be used. To simplify the exporting of own dashboards the command `beat export dashboard --id="dashboard-id"` is added. This should allow all users to export their own dashboards. The output is written to stdout, so the expected usage is to pipe it into a file inside the `kibana/dashboards/6` directory. For the Kibana connection settings it uses the setting from `setup.kibana.*`. If none are set it uses the defaults.
- Loading branch information
Showing
5 changed files
with
115 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package export | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/spf13/cobra" | ||
|
||
"github.com/elastic/beats/libbeat/cmd/instance" | ||
"github.com/elastic/beats/libbeat/common" | ||
"github.com/elastic/beats/libbeat/kibana" | ||
) | ||
|
||
// GenDashboardCmd is the command used to export a dashboard. | ||
func GenDashboardCmd(name, idxPrefix, beatVersion string) *cobra.Command { | ||
genTemplateConfigCmd := &cobra.Command{ | ||
Use: "dashboard", | ||
Short: "Export defined dashboard to stdout", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
dashboard, _ := cmd.Flags().GetString("id") | ||
|
||
b, err := instance.NewBeat(name, idxPrefix, beatVersion) | ||
if err != nil { | ||
fmt.Fprintf(os.Stderr, "Error creating beat: %s\n", err) | ||
os.Exit(1) | ||
} | ||
err = b.Init() | ||
if err != nil { | ||
fmt.Fprintf(os.Stderr, "Error initializing beat: %s\n", err) | ||
os.Exit(1) | ||
} | ||
|
||
// Use empty config to use default configs if not set | ||
if b.Config.Kibana == nil { | ||
b.Config.Kibana = common.NewConfig() | ||
} | ||
|
||
client, err := kibana.NewKibanaClient(b.Config.Kibana) | ||
if err != nil { | ||
fmt.Fprintf(os.Stderr, "Error creating Kibana client: %+v\n", err) | ||
os.Exit(1) | ||
} | ||
|
||
result, err := client.GetDashboard(dashboard) | ||
if err != nil { | ||
fmt.Fprintf(os.Stderr, "Error getting dashboard: %+v\n", err) | ||
os.Exit(1) | ||
} | ||
fmt.Println(result.StringToPrint()) | ||
}, | ||
} | ||
|
||
genTemplateConfigCmd.Flags().String("id", "", "Dashboard id") | ||
|
||
return genTemplateConfigCmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package kibana | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
|
||
"github.com/elastic/beats/libbeat/common" | ||
) | ||
|
||
// RemoveIndexPattern removes the index pattern entry from a given dashboard export | ||
func RemoveIndexPattern(data []byte) (common.MapStr, error) { | ||
|
||
var kbResult struct { | ||
// Has to be defined as interface instead of Type directly as it has to be assigned again | ||
// and otherwise would not contain the full content. | ||
Objects []interface{} | ||
} | ||
|
||
err := json.Unmarshal(data, &kbResult) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var objs []interface{} | ||
|
||
for _, obj := range kbResult.Objects { | ||
t, ok := obj.(map[string]interface{})["type"].(string) | ||
if !ok { | ||
return nil, fmt.Errorf("type key not found or not string") | ||
} | ||
if t != "index-pattern" { | ||
objs = append(objs, obj) | ||
} | ||
} | ||
|
||
result := common.MapStr{ | ||
"objects": objs, | ||
} | ||
|
||
return result, nil | ||
} |