Influxdb 使用api创建grafana仪表板

Influxdb 使用api创建grafana仪表板,influxdb,grafana,Influxdb,Grafana,我正在尝试使用grafana的api从模板创建grafana仪表板。目前我使用的是grafana v2.0.2 我有一个api键,我可以用curl获取仪表盘,但我无法创建仪表盘 当我执行以下请求时:curl-I-H“Authorization:Bearer eyjriobfouscatedlkijoxfq==”http://localhost:3000/api/dashboards/db/webserver2 然后我得到了数据板的json 当我尝试创建在api示例中找到的最简单的仪表板时,它不起

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

我有一个api键,我可以用curl获取仪表盘,但我无法创建仪表盘

当我执行以下请求时:
curl-I-H“Authorization:Bearer eyjriobfouscatedlkijoxfq==”http://localhost:3000/api/dashboards/db/webserver2
然后我得到了数据板的json

当我尝试创建在api示例中找到的最简单的仪表板时,它不起作用:
curl-I-H“Authorization:Bearer eyjriobfouscatedlkijoxfq==”-d/tmp/simpledashhttp://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创建仪表板


谢谢

我昨晚发现了这个问题,网站上的例子在“schemaversation”之前缺少了一个逗号

正确的json应该是:

{
  "dashboard": {
    "id": null,
    "title": "Production Overview",
    "tags": [ "templated" ],
    "timezone": "browser",
    "rows": [
      {
      }
    ],
    "schemaVersion": 6,
    "version": 0
  },
  "overwrite": false
 }
如果您将json复制到此json验证器中,它将准确显示问题所在:


要使用curl发布文件中的数据,请在文件名前加一个@,如下所示:

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

失败的原因是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 }}"
和包含仪表板的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"
          }
        ]
}

用于从计算机中导入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_用户名、GRAFANA_密码和GRAFANA_主机。

我解决了如下问题:

curl -i -H "Authorization: Bearer eyJrIobfuscatedlkIjoxfQ==" -d @/tmp/simpledash http://localhost:3000/api/dashboards/db
1-首先像这样创建数据源(在我的例子中,我使用了collectd、prometheus和grafana的组合)

2-要添加定制的json仪表板,请编辑grafana.ini文件并启用仪表板json文件部分,如下所示:

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

3-然后将dashboard json文件复制到/var/lib/grafana/dashboards(您需要重新启动服务)

我添加了另一种使用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)

我发现如果“rows”数组中有一个空对象
[{}]
,就会出现JS错误,单独发送
[]
似乎已经纠正了这一点。JS似乎看到了这个对象并试图从中提取值。这对我来说也很有效。我必须将导出的仪表板Json源文件注释更改为数据源的文字名称,例如:将“${datasource}”替换为“test”