使用api创建grafana仪表板

Vin*_*ent 5 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/dbwhere /tmp/simpledash contains:

{
  "dashboard": {
    "id": null,
    "title": "Production Overview",
    "tags": [ "templated" ],
    "timezone": "browser",
    "rows": [
      {
      }
    ]
    "schemaVersion": 6,
    "version": 0
  },
  "overwrite": false
 }
Run Code Online (Sandbox Code Playgroud)

我收到以下回复:

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"}]
Run Code Online (Sandbox Code Playgroud)

我尝试了json的一些变体,但我总是得到那个响应,在互联网上我找不到一个有效的例子.有人为我做过一个有效的例子吗?我喜欢这个工作,所以我可以从ansible创建仪表板.

谢谢!

小智 9

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

与cURL

curl -XPOST -i http://localhost:3000/api/dashboards/db --data-binary @./test.json -H "Content-Type: application/json"
Run Code Online (Sandbox Code Playgroud)

与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 }}"
Run Code Online (Sandbox Code Playgroud)

和包含仪表板的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"
          }
        ]
}
Run Code Online (Sandbox Code Playgroud)


Ale*_*rty 5

我昨晚发现了这一点,网站上的示例在“schemaVersion”之前缺少一个逗号

正确的 json 应该是:

{
  "dashboard": {
    "id": null,
    "title": "Production Overview",
    "tags": [ "templated" ],
    "timezone": "browser",
    "rows": [
      {
      }
    ],
    "schemaVersion": 6,
    "version": 0
  },
  "overwrite": false
 }
Run Code Online (Sandbox Code Playgroud)

如果您将 json 复制到此 json 验证器中,它会准确显示问题所在:

http://jsonlint.com/