Data Source on Startup

Greetings Grafana Community!

We have a requirement from our users to have a data source setup for them when they login. We use Docker containers so Grafana starts up and ends based on customer needs (and to save money in the cloud :slight_smile: )

How can I configure Grafana’s database to be “pre-loaded” with the data source we want them to have? (This also cuts down on fat-fingering by users who can’t figure out how to make the connection work).

Again, I cannot log into Grafana as an admin and add the data source for all subsequent users because when the container goes away the database does away…and…each user has their own login credentials.

Thanks in advance!

In grafana v5+ you can use the provisioning method described here: http://docs.grafana.org/administration/provisioning/

An example using docker-compose.yml can look like this:

grafana:
  image: grafana/grafana:latest
  ports:
    - "3000:3000"
  volumes:
    - ./provisioning:/etc/grafana/provisioning

inside the provisioning directory:

provisioning/datasources/automatic.yml

apiVersion: 1

deleteDatasources:
  - name: MYINFLUX
    orgId: 1

datasources:
- name: MYINFLUXDB
  type: influxdb
  access: proxy
  url: https://myinfluxdb
  password:
  user:
  database: MYINFLUXDB
  basicAuth: false
  basicAuthUser:
  basicAuthPassword:
  withCredentials:
  isDefault: true
  jsonData:
     tlsAuth: false
     tlsAuthWithCACert: false
  secureJsonData:
    tlsCACert: ""
    tlsClientCert: ""
    tlsClientKey: ""
  version: 1
  editable: true

It’s easiest to login as admin, setup the datasource, then issue a GET /api/datasources the API to get the config, and then edit it as yaml, then drop into the provisioning/datasources directory, like this (using admin/admin username/pass):

curl -X GET
http://localhost:3000/api/datasources
-H ‘Authorization: Basic YWRtaW46YWRtaW4=’

You can also pre-create dashboards using the same method.

Hope this helps.

3 Likes