7
votes

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!

6
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.Rebs

6 Answers

11
votes

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"
          }
        ]
}
4
votes

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/

3
votes

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
1
votes

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)

0
votes

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.

0
votes

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)