2015-07-01 104 views
5

我正尝试使用grafana的api模板创建grafana仪表板。我目前使用grafana v2.0.2。使用api创建grafana仪表板

我有一个API密钥,我可以用curl获得仪表板,但我无法创建仪表板。

当我做了以下的请求:curl -i -H "Authorization: Bearer eyJrIobfuscatedlkIjoxfQ==" http://localhost:3000/api/dashboards/db/webserver2 然后我得到的JSON回到dasboard。

当我尝试创建我的API的例子中发现的最简单的仪表盘它不工作:curl -i -H "Authorization: Bearer eyJrIobfuscatedlkIjoxfQ==" -d /tmp/simpledash http://localhost:3000/api/dashboards/db其中/tmp/simpledash包含:

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

我得到如下回应:

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

我试着JSON的一些变体,但我总是得到这种反应,并在互联网上找不到一个工作的例子。任何人都有我的例子吗?我喜欢这个工作,所以我可以从可靠的方面创建仪表板。

谢谢!

+0

我发现我有JS如果“rows”数组里面有一个空对象'[{}]',发送'[]'似乎已经纠正了这个错误。 JS似乎看到了这个对象,并试图从中提取值。 – Rebs

回答

7

它失败的原因是API需要知道有效负载是json。

与卷曲

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

与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 }}" 

和VAR文件,其中包含仪表板,

"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

昨晚我想通了这一点,网站上的例子只是“schemaVersion”前缺少一个逗号

正确的JSON应该是:

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

如果您复制的JSON到这个JSON验证它会告诉你究竟在何处的问题是:

http://jsonlint.com/

3

要使用curl从文件发布的数据,把一个@之前的文件名,如下所示:

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

的Userful的一行从您的机器导入JSON仪表盘

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 

请从上述命令更改GRAFANA_USERNAME,GRAFANA_PASSWORD和GRAFANA_HOST。

0

我解决了这样的问题:

1先创建数据源这样的(在我来说,我使用collectd,普罗米修斯与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,对于添加定制json仪表板,编辑grafana。ini文件并启用仪表盘JSON文件部分象下面这样:

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

3-接下来仪表板JSON文件复制到/ var/lib中/ grafana /仪表板(需要重新启动该服务)