Tuesday, September 10, 2019

How to automatically load dashboards into Kibana?

Kibana is a great tool to visualize your ElasticSearch data and I've used it as part of my kubernetes cluster deployment. You can configure your index patterns, visualizations, and dashboards to fully cover the product needs.



However, once the product is reinstalled, the index patterns, visualizations, and dashboards configuration are lost. You don't really want to reinstall everything from scratch upon each resinstall of the Kibana.

I've handled it using export and import of the Kibana dashboards.

Export of the Kibana configuration

After configuration kibana dashboard to your needs, you need to manually export its configuration to a JSON file.
Use the following command to locate your dashboard ID:

curl -s 'http://localhost:5601/api/saved_objects/_find?type=dashboard'

In case the Kibana is running within a kubernetes, you can do the same using kubectl
kubectl exec KIBANA_POD_NAME -- \
  curl -s 'http://localhost:5601/api/saved_objects/_find?type=dashboard' 

The output will indicate the dashboard ID:
{
  "page": 1,
  "per_page": 20,
  "total": 1,
  "saved_objects": [
    {
      "type": "dashboard",
      "id": "44532f10-d211-11e9-9c96-fbd87ecb6df2", <== This is the ID
      "attributes": {
        "title": "MyDashboard",

Next use the DASHBOARD_ID to export it:
curl -s \
'http://localhost:5601/api/kibana/dashboards/export?dashboard=DASHBOARD_ID' \
> dashboard.json

Import of the Kibana configuration

To import the dashboard use the dashboard.json file that you've had created as part of the export, and run the following command:

curl -s -X POST -H 'kbn-xsrf: true' -H 'Content-Type: application/json' \ 
  'http://localhost:5601/api/kibana/dashboards/import?force=true' \
 --data-binary "@/dashboard.json"

In case of a kubernetes installation, create an image containing the dashboard.json on the root folder, and running the following entrypoint:
#!/usr/bin/env bash
/import_dashboard.sh &
exec /usr/local/bin/kibana-docker
Where the import_dashboard.sh script should loop with the import command until it is success (as it should wait for kibana startup).
function importDashboard {
	result=$(curl -s -X POST -H 'kbn-xsrf: true' \
	  -H 'Content-Type: application/json' \
	  'http://localhost:5601/api/kibana/dashboards/import?force=true' \
	  --data-binary "@/dashboard.json")
	  
	rc=$?

	if [[ "$rc" == "0" ]]; then
		if [[ $result == *"objects"* ]]; then
			return 0
		fi
	fi
	return 1
}

function importLoop {
    until importDashboard
    do
        echo "Waiting for Kibana startup"
        sleep 3
    done
}




For more details:

No comments:

Post a Comment