使用API创建Grafana仪表板

9
我正在尝试使用grafana的API从模板创建仪表盘。目前我使用的是grafana v2.0.2。
我有一个api密钥,并且能够使用curl获取仪表板,但我无法创建仪表板。
当我执行以下请求时:curl -i -H"Authorization:Bearer eyJrIobfuscatedlkIjoxfQ == "http:// localhost:3000 / api / dashboards / db / webserver2 ,然后我得到仪表板的JSON返回。
当我尝试创建我在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 创建仪表板了。

谢谢!


我发现如果“rows”数组中有一个空对象[{}],就会出现JS错误,但是单独发送[]似乎可以纠正这个问题。看起来JS会看到这个对象并尝试从中提取值。 - Rebs
6个回答

13

它失败的原因是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"
          }
        ]
}

5

我昨晚找到了答案,网站上的例子在"schemaVersion"之前缺少了一个逗号。

正确的JSON应该是:

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

如果您将json复制到此json验证器中,它将准确地显示问题所在:http://jsonlint.com/

4
要使用curl从文件中发送数据,请在文件名前加上@,像这样:

使用curl从文件中发送数据,请在文件名前加@符号,例如:

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

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(您需要重新启动服务)


这对我也起作用了。我确实不得不更改导出的仪表板Json源文件注释,以成为数据源的字面名称,例如:将“$ {DATASOURCE}”替换为“test”。 - Leo

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)

0

导入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。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接