使用api创建grafana仪表板

时间:2015-07-01 16:30:20

标签: influxdb grafana

我尝试使用grafana的api从模板创建grafana仪表板。我现在使用grafana v2.0.2。

我有一个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的一些变体,但我总是得到那个响应,在互联网上我找不到一个有效的例子。有人为我做过一个有效的例子吗?我喜欢这个工作,所以我可以从ansible创建仪表板。

谢谢!

6 个答案:

答案 0 :(得分:8)

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

使用cURL

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

和包含仪表板的vars文件

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

答案 1 :(得分: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/

答案 2 :(得分:3)

要使用curl从文件发布数据,请在文件名之前放置@,如下所示:

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

答案 3 :(得分:1)

我解决了这个问题:

1-首先创建这样的数据源(在我的情况下,我使用了collectd,prometheus和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文件并启用Dashboard json文件部分,如下所示:

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

3-然后将仪表板json文件复制到/ var / lib / grafana / dashboards(需要重启服务)

答案 4 :(得分:0)

Userful ONE-LINER从您的计算机导入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。

答案 5 :(得分:0)

我正在添加一种使用python脚本的方式

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)
相关问题