10

I'm trying to create grafana dashboards from a template with the api from grafana. I use grafana v2.0.2 at the moment.

I have an api key and I'm able to get the dashboards with curl, but I'm unable to create dashboards.

When I do the following request: curl -i -H "Authorization: Bearer eyJrIobfuscatedlkIjoxfQ==" http://localhost:3000/api/dashboards/db/webserver2 then I get the json back for the dasboard.

When I try to create the simplest dashboard I found in the api examples it does not work: curl -i -H "Authorization: Bearer eyJrIobfuscatedlkIjoxfQ==" -d /tmp/simpledash http://localhost:3000/api/dashboards/db where /tmp/simpledash contains:

{
  "dashboard": {
    "id": null,
    "title": "Production Overview",
    "tags": [ "templated" ],
    "timezone": "browser",
    "rows": [
      {
      }
    ]
    "schemaVersion": 6,
    "version": 0
  },
  "overwrite": false
 }

I get the following response:

HTTP/1.1 422 status code 422
Content-Type: application/json; charset=utf-8
Date: Wed, 01 Jul 2015 16:16:48 GMT
Content-Length: 84

[{"fieldNames":   ["Dashboard"],"classification":"RequiredError","message":"Required"}]

I tried some variations of the json, but I always get that response and on the internet I could not find a working example. Anyone have a working example for me? I like to have this working so I can create dashboard from ansible.

Thanks!

1
  • I found I got JS errors if the "rows" array has an empty object [{}] inside it, sending [] on its own seems to have corrected this. It seems the JS sees the object and attempts to extract values from it. Commented Feb 1, 2016 at 4:38

6 Answers 6

13

The reason why it's failing is that the API needs to know that the payload is json.

with cURL

curl -XPOST -i http://localhost:3000/api/dashboards/db --data-binary @./test.json -H "Content-Type: application/json"

with ansible

- name: postinstall::dashsetups
  uri:
    url: http://{{grafana.ip}}:{{grafana.bind}}/api/dashboards/db
    method: POST
    user: "{{ admin_usr }}"
    password: "{{ admin_pwd }}"
    body: "{{ lookup('template', item.file) }}"
    status_code: 200
    body_format: raw
    force_basic_auth: yes
    HEADER_Content-Type: "application/json"
  with_items: "{{ grafana.dashboards }}"

and vars file containing dashboards,

"grafana":{"dashboards": [
          {
            "name": "t1",
            "file": "./dashboards/filename.json.j2",
            "dash_name": "Test 1"
          },
          {
            "name": "t2",
            "file": "./dashboards/filename2.json.j2",
            "dash_name": "Test 2"
          },
          {
            "name": "t3",
            "file": "./dashboards/template3.json.j2",
            "dash_name": "Test 3"
          }
        ]
}
Sign up to request clarification or add additional context in comments.

Comments

5

I figured this out last night, the example on the website is missing a comma just before "schemaVersion"

correct json should be :

{
  "dashboard": {
    "id": null,
    "title": "Production Overview",
    "tags": [ "templated" ],
    "timezone": "browser",
    "rows": [
      {
      }
    ],
    "schemaVersion": 6,
    "version": 0
  },
  "overwrite": false
 }

if you copy your json into this json validator it'll show you exactly where the issue is :

http://jsonlint.com/

Comments

4

To use curl to post data from a file, put an @ before the filename, like this:

curl -i -H "Authorization: Bearer eyJrIobfuscatedlkIjoxfQ==" -d @/tmp/simpledash http://localhost:3000/api/dashboards/db

Comments

1

I solved the problem like this:

1- first create your datasource like this (In my case I used the combination of collectd, prometheus and grafana)

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}'

2-For adding the customized json dashboard, edit grafana.ini file and enable Dashboard json file section like below:

;##################### Dashboard JSON files #####################
 [dashboards.json]
 enabled = true
 path = /var/lib/grafana/dashboards

3- then copy dashboard json file to /var/lib/grafana/dashboards (you need to restart the service)

1 Comment

This worked for me too. I did have to change the exported dashboard Json source file annotation to be the literal name of the datasource eg: Replacing "${DATASOURCE}" with "test"
0

Userful ONE-LINER to import JSON dashboards from your machine

for i in `ls *.json` ;do curl -i -u GRAFANA_USERNAME:GRAFANA_PASSWORD -H "Content-Type: application/json" -X POST http://GRAFANA_HOST/api/dashboards/db -d @$i ; done

Do change GRAFANA_USERNAME , GRAFANA_PASSWORD and GRAFANA_HOST from the above command.

Comments

0

I am adding one more way that is using python script

import requests
url='http://admin:admin@localhost:3000/api/dashboards/db'
data='''{
  "dashboard": {
    "id": null,
    "uid": "mahadev",
    "title": "scriptedDashboard",
    "tags": [ "templated" ],
    "timezone": "browser",
    "schemaVersion": 16,
    "version": 0
  },
  "folderId": 48,
  "overwrite": false
}'''
headers={"Content-Type": 'application/json'}
response = requests.post(url, data=data,headers=headers)
print (response.text)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.