Grafana: Adding datasource without using the web gui

Created on 15 Apr 2015  ·  40Comments  ·  Source: grafana/grafana

Hello,
I just wonder if it is possible to add datasources 'from command line or config file' instead of using the web interface 'Add Source' option...
I would like to make an installation process that automates influxdb installation + grafana installation and automatically creates the 'localhost' influxdb datasource inside grafana. Is this feasible?
Thanks

Most helpful comment

Can this issue be reopened?
I'd very much like an option similar to dashboards.json to have a directory where the desired datasources can be defined

All 40 comments

It is, through the web api:

curl 'http://localhost:3000/api/datasources' -X PUT --data-binary '{"name":"test","type":"influxdb_08","url":"http://localhost:8086","access":"proxy","isDefault":true,"database":"asd","user":"asd","password":"asd"}'

The problem is for the above to work you need an API key, and to create an api key you need UI. The solution is to be able to use the api with username/password (for example with the admin user's username/password)

Thanks,I'll try this and see :-)
The main issue is that I can't find any doc about the web api... maybe I'm missing something.

Here's some python code to do this that I derived from watching the http requests:

import requests

[ provide credential variables etc ]

grafana_url = os.path.join('http://', '%s:%u' % (grafana_host, grafana_port))
session = requests.Session()
login_post = session.post(
   os.path.join(grafana_url, 'login'),
   data=json.dumps({
      'user': grafana_user,
      'email': '',
      'password': grafana_password }),
   headers={'content-type': 'application/json'})

# Get list of datasources
datasources_get = session.get(os.path.join(grafana_url, 'api', 'datasources'))
datasources = datasources_get.json()

# Add new datasource
datasources_put = session.put(
   os.path.join(grafana_url, 'api', 'datasources'),
   data=json.dumps({
      'access': 'direct',
      'database': ifdb_database,
      'name': datasource_name,
      'password': ifdb_password,
      'type': 'influxdb_08',
      'url': 'http://%s:%u' % (ifdb_host, ifdb_port),
      'user': ifdb_user}),
      headers={'content-type': 'application/json'})

Thanks,
As it will be part of install, I think the 'Curl' way will make the jab. But I'll also try the python stuff as it could be useful too. Thank again, I'll let you know :-)
Christian

I will try to make this easier in 2.1 and also make a CLI tool that makes interacting with the Web api easier

Hi again torkedo,
You said "The problem is for the above to work you need an API key, and to create an api key you need UI."
and effectively when I try to send such curl command, I get {"message":"Access denied"}
Could you explain what you mean by "The solution is to be able to use the api with username/password (for example with the admin user's username/password)"
Thanks...

Hi all,
PRI-mcallen's Python solution worked fine for me :-) I'll go on that way.
I close the topic.
Thanks!!

@PRI-mcallen Thanks for the python script, based on that I came up with the following (mostly Bash around curl with cookies/cookie-jar) as I don't have a python interpreter in my env where I need to run this, maybe it's useful for someone else.

This is a real hassle for running grafana in docker. We use a docker orchestration tool (maestro-ng). We have a docker image for grafana and we would like to be able to automatically configure the graphite datasource. The orchestration tool creates graphite container and then grafana container that depends on it. The tool injects the address of the graphite container as env variables into grafana container. We want the startup script that runs in grafana container to automatically configure the graphite datasource. We used to be able to do this using config.js in grafana 1.9 but now there is only the HTTP API (which is not documented) to do this.

@iangkent, see the python snippet I posted above for an example of how to use the HTTP API (reverse engineered from watching the transactions).

Hi

I'm testing the python snippet that @PRI-mcallen posted in 2.1.0-pre1, and it seems something is broken (because in 2.0.2 and 2.0.3-pre1 it works)

The response I'm getting from adding the datasource is

datasources_put.json()
{u'message': u'Not found'}

And from grafana logs:

2015/07/13 11:27:49 [I] Completed /api/datasources 404 Not Found in 1.169711ms

I tried using curl directly and same behaviour (using cookie authentication or user/pass)

curl --user admin:admin \
-X PUT \
-H 'Content-Type: application/json;charset=UTF-8' \
--data-binary "{\"name\":\"test4\",\"isDefault\":\"true\",\"type\":\"influxdb_08\",\"url\":\"http://localhost:8086\",\"access\":\"proxy\",\"database\":\"test4\",\"user\":\"test4\",\"password\":\"test4\"}" \
"http://172.16.149.149:3000/api/datasources".

{"message":"Not found"}

Does anyone have an idea about how to troubleshooting or fix it?

Thanks in advance
Efrain

@3fr61n there is a small breaking change in the HTTP api in 2.1 (its in the changelog)

Datasource HTTP api breaking change, ADD datasource is now POST /api/datasources/, update is now PUT /api/datasources/:id

Thanks!!! it's working now

In grafana 2.1 you can now authenticate against the API using basic auth, so you do not need to login and use cookies to create an api key (or to add the data sources)
https://github.com/grafana/grafana/issues/2218#issuecomment-117041541

curl -i -XPOST 'http://admin:admin@hostname:3000/api/datasources' --data-binary '{\"name\":\"influxdb_09x\",\"type\":\"influxdb\",\"access\":\"proxy\",\"url\":\"http://hostname:8086\",\"password\":\"root\",\"user\":\"root\",\"database\":\"cadvisor\",\"basicAuth\":true,\"basicAuthUser\":\"admin\",\"basicAuthPassword\":\"admin\",\"isDefault\":true,\"jsonData\":null}'

I'm getting "[{"fieldNames":["Name"],"classification":"RequiredError","message":"Required"},{"fieldNames":["Type"],"classification":"RequiredError","message":"Required"},{"fieldNames":["Access"],"classification":"RequiredError","message":"Required"}]"

You need header ContentType application/json

curl -i -XPOST 'http://admin:admin@hostname:3000/api/datasources' --header "ContentType application/json" --data-binary '{\"name\":\"influxdb2\",\"type\":\"influxdb_09x\",\"access\":\"proxy\",\"url\":\"http://hostname:8086\",\"password\":\"root\",\"user\":\"root\",\"database\":\"cadvisor\",\"basicAuth\":true,\"basicAuthUser\":\"admin\",\"basicAuthPassword\":\"admin\",\"isDefault\":true,\"jsonData\":null}'

Still returning the error above :/

That does not look right, missing colon ,-H 'Content-Type: application/json;charset=UTF-8' \

I'm using the official docker graphite (hopsoft/graphite-statsd) and grafana (grafana/grafana) images and the following command worked for me:

curl 'http://admin:[email protected]:3000/api/datasources' -X POST -H 'Content-Type: application/json;charset=UTF-8' --data-binary '{"name":"localGraphite","type":"graphite","url":"http://192.168.99.100","access":"proxy","isDefault":true,"database":"asd"}'

In grafana 2.6 and influxdb 0.10 the following curl is working correctly:

curl 'http://admin:[email protected]:3000/api/datasources' -X POST -H 'Content-Type: application/json;charset=UTF-8' --data-binary '{"name":"influx","type":"influxdb","url":"http://localhost:8086","access":"proxy","isDefault":true,"database":"mydb","user":"admin","password":"admin"}'

you can check the datasources configration with:
curl 'http://admin:[email protected]:3000/api/datasources'

This issue was closed in March but is there any way to automate this in a cleaner way for use in Docker?

This issue was closed in March but is there any way to automate this in a cleaner way for use in Docker?

What we do in Docker in run2.sh (there is already a run.sh in grafana/grafana):

echo 'Starting Grafana...'
/run.sh "$@" &
AddDataSource() {
  curl 'http://localhost:3000/api/datasources' \
    -X POST \
    -H 'Content-Type: application/json;charset=UTF-8' \
    --data-binary \
    '{"name":"Prometheus","type":"prometheus","url":"http://prometheus:9090","access":"proxy","isDefault":true}'
}
until AddDataSource; do
  echo 'Configuring Grafana...'
  sleep 1
done
echo 'Done!'
wait

But I agree this is a very sad design. Any other application can be configured before starting it. Grafana you have to start first and then configure. Weird.

Hey @torkelo , great job with the Grafana tool!
I get the error "user not found" when I try to add a user in an Organisation using the Grafana API using the command-

curl 'http://:@:3000/api/orgs/6/users' -X POST -H 'Content-Type: application/json;charset=UTF-8' --data-binary '{"loginOrEmail":"user1","role":"Viewer"}'

I've entered the correct Basic Auth credentials. Any solutions to this problem?
Thanks!

the user has to exist with that API call, you can create an user invite with the invite api

Or via ansible:

# Check that a page returns a status 200 and fail if the word AWESOME is not
# in the page contents.
- uri:
    url: http://localhost:3000/api/datasources/name/Prometheus
    user: admin
    password: changeme
    force_basic_auth: yes
  register: grafana_prometheus
  failed_when: false
  changed_when: false

- name: Enable Prometheus Datasource
  uri:
    url: http://localhost:3000/api/datasources
    method: POST
    user: admin
    password: changeme
    body:
      name: "Prometheus"
      type: "prometheus"
      url: "http://prometheus:9090"
      access: "proxy"
      isDefault: true
    force_basic_auth: yes
    status_code: 201
    body_format: json
  when: grafana_prometheus.status == 404

Another possible course of action, is to write directly into Grafana's database. I usually start Grafana in order to create its database, stop it and use sqlite to add the source to the database.

For Grafana 4.0.2 it would be something like:

cat <<EOF | sqlite3 grafana.db || echo "Failed to add data source."
INSERT INTO data_source VALUES (2,1,0,'prometheus','prometheus','proxy','http://address',NULL,NULL,NULL,0,NULL,NULL,0,'{}','2017-01-15 20:00:00','2017-01-15 20:00:00',0,'{}');
EOF

I do this by using a CLI. tool which I wrote for Grafana - wizzy https://github.com/utkarshcmu/wizzy

Can this issue be reopened?
I'd very much like an option similar to dashboards.json to have a directory where the desired datasources can be defined

Here's a chunk of saltstack state for it too (very similar to the ansible code above). It's pretty ugly, but seems to do the job:

  http.query:
    - name: http://localhost:3000/api/datasources
    - method: POST
    - username: admin
    - password: admin
    - data: |
        {"name": "Prometheus",
        "type": "prometheus",
        "url": "http://localhost:9090",
        "access": "proxy",
        "isDefault": true}
    - header_list:
      - 'Content-Type: application/json'
    - status: 200
    - unless:
      - curl -f http://admin:admin@localhost:3000/api/datasources/name/Prometheus

Please reopen this, messing around with curl and sqlite is more then annoying with docker. Just a clean solution as with dashboard.json would be awesome!

+1 we need this to be configurable in a static config (for example when using docker-compose to set up grafana automatically using an influxdb datasource)

+1 with json file we could easily generate a datasource for each ES index right from puppet, instead of running side scripts

+1

On Thu, May 25, 2017 at 5:36 PM, Anton Timofieiev notifications@github.com
wrote:

+1 with json file we could easily generate a datasource for each ES index
right from puppet, instead of running side scripts


You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
https://github.com/grafana/grafana/issues/1789#issuecomment-304026003,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AFrnv_CDln7rDTlqZK6r_stAFiek7jaHks5r9ZHfgaJpZM4EBJaL
.

👍 with being able to query consul for datasource (its already json)

I wonder how do I configure elastic search details ( such as index name, time field name and version) whether via HTTP API or conf file. I just can't find the key for those configurations. What is the default behavior? Does it create a data source for each index like @timtofan said?

curl --user admin:admin 'http://IPADDR:3000/api/datasources' -X POST -H 'Content-Type: application/json;charset=UTF-8' --data-binary '{"name":"test","type":"prometheus","url":"http://localhost:9090","access":"proxy","basicAuth":false}'

"isDefault": true if u want to make the datasource default

Here is the way that i add a prometheus datasource:
curl --user admin:admin 'http://IPADDR:3000/api/datasources' -X POST -H 'Content-Type: application/json;charset=UTF-8' --data-binary '{"name":"test","isDefault":true ,"type":"prometheus","url":"http://localhost:9090","access":"proxy","basicAuth":false}'

Can I add a data source from the configuration file to all organizations?
It would be very convenient.

Ok, I know that this is already closed, but I just wanted to share that Grafana 5 allows defining Dashboard from code. More info here: http://docs.grafana.org/administration/provisioning/#datasources

Was this page helpful?
0 / 5 - 0 ratings

Related issues

7lives83 picture 7lives83  ·  82Comments

dutchiechris picture dutchiechris  ·  78Comments

german-bortoli picture german-bortoli  ·  95Comments

torkelo picture torkelo  ·  97Comments

torkelo picture torkelo  ·  90Comments