Skip to content
This repository has been archived by the owner on Sep 7, 2018. It is now read-only.

[Feature request] Add dashboard import by environment variable #74

Closed
bvis opened this issue Jan 27, 2017 · 7 comments
Closed

[Feature request] Add dashboard import by environment variable #74

bvis opened this issue Jan 27, 2017 · 7 comments

Comments

@bvis
Copy link

bvis commented Jan 27, 2017

One important feature in containers is the automation of initial configuration. In the grafana docker image this possibility is missing.

One container that runs this image is not useful without an initial manual configuration. This wouldn't be a problem if we don't count with the volatility of containers.

Adding the option of automatically import dashboards by environment variables would be a huge improvement.

For example:

docker run \
  --name grafana \
  -e GF_INSTALL_DASHBOARDS=123,456 \
  grafana/grafana

Which would import the dashboards available in grafana.net with these ids. If when they are imported the references to the datasources already exist in the container it would use them directly. If not, the first time, it should show the option of which datasources to use.

@zplizzi
Copy link

zplizzi commented Sep 2, 2017

Also, the option to import a dashboard from file (say, in a mounted volume) would be useful. That way initial configurations can be checked into source control.

@mccormd
Copy link

mccormd commented Sep 4, 2017

Hi, one option would be to update the run.sh to do it. Here is a script that I am using, which I mount into the official container and call by overriding --entrypoint:

#!/bin/bash

# Script to configure grafana datasources and dashboards.
# Intended to be run before grafana entrypoint...
# Image: grafana/grafana:4.1.2
# ENTRYPOINT [\"/run.sh\"]"

GRAFANA_URL=${GRAFANA_URL:-http://localhost:3000}
#GRAFANA_URL=http://grafana-plain.k8s.playground1.aws.ad.zopa.com
DATASOURCES_PATH=${DATASOURCES_PATH:-/etc/grafana/datasources}
DASHBOARDS_PATH=${DASHBOARDS_PATH:-/etc/grafana/dashboards}

# Generic function to call the Vault API
grafana_api() {
  local verb=$1
  local url=$2
  local params=$3
  local bodyfile=$4
  local response
  local cmd

  cmd="curl -L -s --fail -H \"Accept: application/json\" -H \"Content-Type: application/json\" -X ${verb} -k ${GRAFANA_URL}${url}"
  [[ -n "${params}" ]] && cmd="${cmd} -d \"${params}\""
  [[ -n "${bodyfile}" ]] && cmd="${cmd} --data @${bodyfile}"
  echo "Running ${cmd}"
  eval ${cmd} || return 1
  return 0
}

wait_for_api() {
  while ! grafana_api GET /api/user/preferences
  do
    sleep 5
  done 
}

install_datasources() {
  local datasource

  for datasource in ${DATASOURCES_PATH}/*.json
  do
    if [[ -f "${datasource}" ]]; then
      echo "Installing datasource ${datasource}"
      if grafana_api POST /api/datasources "" "${datasource}"; then
        echo "installed ok"
      else
        echo "install failed"
      fi
    fi
  done
}

install_dashboards() {
  local dashboard

  for dashboard in ${DASHBOARDS_PATH}/*.json
  do
    if [[ -f "${dashboard}" ]]; then
      echo "Installing dashboard ${dashboard}"
      if grafana_api POST /api/dashboards/db "" "${dashboard}"; then
        echo "installed ok"
      else
        echo "install failed"
      fi
    fi
  done
}

configure_grafana() {
  wait_for_api
  install_datasources
  install_dashboards
}

echo "Running configure_grafana in the background..."
configure_grafana &
/run.sh
exit 0

Hope it helps.

@Aakansha-Talati
Copy link

Is the environment variable now available to import the dashboard?

@xlson
Copy link
Contributor

xlson commented Mar 29, 2018

Since Grafana 5.0 it's possible to provision dashboards from file, read about it here. Automatic import directly from grafana.net is still not available but might perhaps be a useful extension to provisioning.

@bvis
Copy link
Author

bvis commented Mar 29, 2018

@xlson It would be nice to have automatic import from grafana.net, it would be awesome!

@xlson
Copy link
Contributor

xlson commented Mar 29, 2018

This feature depends on grafana/grafana#10786

@xlson
Copy link
Contributor

xlson commented Aug 13, 2018

Moved to grafana/grafana#12895

@xlson xlson closed this as completed Aug 13, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

6 participants