Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create grafana dashboards with api

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!

like image 357
Vincent Avatar asked Jul 01 '15 16:07

Vincent


People also ask

What is an example of a Grafana dashboard?

Here is an example of what a dashboard could be with a little bit of work. Here is an example with a more futuristic theme on disk monitoring. Most of the API requests are authenticated within Grafana. In order to call the Grafana API to create a dashboard, you will have to get a token.

How do I authenticate the Grafana API?

Most of the API requests are authenticated within Grafana. In order to call the Grafana API to create a dashboard, you will have to get a token. If you don’t own the Grafana instance, you have to ask your administrator a token. Hover the ‘Configuration’ icon in the left menu and click on the “API Keys” option. Click on “Add API Key”.

What is the Grafana backend?

The Grafana backend exposes an HTTP API, which is the same API that is used by the frontend to do everything from saving dashboards, creating users, and updating data sources. Grafana Enterprise includes all of the Grafana OSS APIs as well as those that follow:

How do I change the visual of a Grafana panel?

By default, Grafana sets new panels to “Graph” types. Choose the visualization that fits your query the best. You have to choose between ten different visualizations (or more if you have plugins installed!) Tweak your dashboard with display options until you’re satisfied with the visual of your panel.


2 Answers

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"
          }
        ]
}
like image 61
Danie Avatar answered Oct 20 '22 01:10

Danie


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/

like image 25
Alex Laverty Avatar answered Oct 20 '22 00:10

Alex Laverty