Creating grafana panels with api

I am trying to create grafana chart panels from a template with api from grafana. I am currently using grafana v2.0.2.

I have an api key and I can get dashboards with curls, but I can not create dashboards.

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

When I try to create the simplest panel that 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 answer:

 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 json options, but I always get this answer, and on the Internet I could not find a working example. Does anyone have a working example for me? I like to have this work, so I can create a control panel due to impossibility.

Thanks!

+5
source share
5 answers

The reason this happens is because 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 inconsequential

 - 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 a 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" } ] } 
+7
source

I realized this last night, in the example on the website, there is no comma before "schemaVersion"

The correct json should be:

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

if you copy json to this json validator, it will show you exactly where the problem is:

http://jsonlint.com/

+4
source

To use curl to send data from a file, put @ in front of the file name, for example:

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

Custom ONE-LINER to import JSON panels from your computer.

 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 

Change GRAFANA_USERNAME, GRAFANA_PASSWORD and GRAFANA_HOST from the above command.

0
source

I solved the problem as follows:

1- first create your data source like this (in my case I used a 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 - To add a custom json panel, edit the grafana.ini file and include the jash Dashboard section as shown below:

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

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

0
source

All Articles