无法通过Grafana API导入Grafana仪表板

7

我正在尝试使用HTTP API导入Grafana仪表板,按照Grafana文档的步骤操作。

Grafana版本:5.1.3 操作系统-Windows 10

这是我尝试的内容:

curl --user admin:admin "http://localhost:3000/api/dashboards/db" -X POST -H "Content-Type:application/json;charset=UTF-8" --data-binary @c:/Users/Mahadev/Desktop/Dashboard.json

这是我的 Python 代码:

import requests

headers = {
'Content-Type': 'application/json;charset=UTF-8',
}

data = open('C:/Users/Mahadev/Desktop/Dashboard.json', 'rb').read()
response = requests.post('http://admin:admin@localhost:3000/api/dashboards/db', headers=headers, data=data)
print (response.text)

两者的输出结果如下:

[{"fieldNames":["Dashboard"],"classification":"RequiredError","message":"Required"}]

在我的JSON有效载荷中,它正在请求名为“dashboard”的根属性。有人能建议我如何使用该属性以及我应提供什么数据吗?

如果有任何人想深入了解,请参阅以下链接。

https://github.com/grafana/grafana/issues/8193
https://github.com/grafana/grafana/issues/2816
https://github.com/grafana/grafana/issues/8193
https://community.grafana.com/t/how-can-i-import-a-dashboard-from-a-json-file/669
https://github.com/grafana/grafana/issues/273
https://github.com/grafana/grafana/issues/5811
https://dev59.com/IZvga4cB1Zd3GeqP24L2
https://dev59.com/fpvga4cB1Zd3GeqP2nyL#39954514
https://www.bountysource.com/issues/44431991-use-api-to-import-json-file-error
https://github.com/grafana/grafana/issues/7029

1
感谢您的支持,我已经解决了这个问题。 - Mahadev Gouda
2个回答

4
也许你应该尝试从API下载仪表板,这样你就会得到一个“正确”的JSON模型来推送?
你可以使用以下命令来下载它:
curl -H "Authorization: Bearer $TOKEN" https://grafana.domain.tld/api/dashboards/uid/$DASHBOARD_UID

另一种方法是,在grafana.com/dashboards下载一个仪表盘JSON文件,然后尝试使用您当前的代码上传它。 ;)
仪表板字段包含将显示的所有内容,警报、图形等等。
以下是dashboard.json的示例:
{
  "meta": {
    "type": "db",
    "canSave": true,
    "canEdit": true,
    "canAdmin": false,
    "canStar": true,
    "slug": "status-app",
    "url": "/d/lOy3lIImz/status-app",
    "expires": "0001-01-01T00:00:00Z",
    "created": "2018-06-04T11:40:20+02:00",
    "updated": "2018-06-14T17:51:23+02:00",
    "updatedBy": "jean",
    "createdBy": "jean",
    "version": 89,
    "hasAcl": false,
    "isFolder": false,
    "folderId": 0,
    "folderTitle": "General",
    "folderUrl": "",
    "provisioned": false
  },
  "dashboard": {
    "annotations": {
      "list": [
        {
          "builtIn": 1,
          "datasource": "-- Grafana --",
          "enable": true,
          "hide": true,
          "iconColor": "rgba(0, 211, 255, 1)",
          "name": "Annotations & Alerts",
          "type": "dashboard"
        }
      ]
    },
    "editable": true,
    "gnetId": null,
    "graphTooltip": 0,
    "id": 182,
    "links": [],
    "panels": [
      {
        "alert": {
          "conditions": [
            {
              "evaluator": {
                "params": [
                  1
                ],
                "type": "lt"
              },
              "operator": {
                "type": "and"
              },
              "query": {
                "params": [
                  "A",
                  "5m",
                  "now"
                ]
              },
              "reducer": {
                "params": [],
                "type": "avg"
              },
              "type": "query"
            }
          ],
          "executionErrorState": "alerting",
          "frequency": "60s",
          "handler": 1,
          "name": "Status of  alert",
          "noDataState": "alerting",
          "notifications": [
            {
              "id": 7
            }
          ]
        },
        "aliasColors": {},
        "bars": false,
        "dashLength": 10,
        "dashes": false,
        "datasource": "Collectd",
        "fill": 1,
        "gridPos": {
          "h": 7,
          "w": 8,
          "x": 0,
          "y": 0
        },
        "id": 4,
        "legend": {
          "alignAsTable": true,
          "avg": true,
          "current": true,
          "max": false,
          "min": false,
          "rightSide": false,
          "show": true,
          "total": false,
          "values": true
        },
        "lines": true,
        "linewidth": 1,
        "links": [],
        "nullPointMode": "connected",
        "percentage": false,
        "pointradius": 5,
        "points": false,
        "renderer": "flot",
        "seriesOverrides": [],
        "spaceLength": 10,
        "stack": false,
        "steppedLine": false,
        "targets": [
          {
            "alias": "Status",
            "groupBy": [
              {
                "params": [
                  "$__interval"
                ],
                "type": "time"
              },
              {
                "params": [
                  "null"
                ],
                "type": "fill"
              }
            ],
            "measurement": "processes_processes",
            "orderByTime": "ASC",
            "policy": "default",
            "query": "SELECT mean(value) FROM \"processes_processes\" WHERE (\"instance\" = '' AND \"host\" = 'Webp01') AND $timeFilter GROUP BY time($interval) fill(null)",
            "rawQuery": true,
            "refId": "A",
            "resultFormat": "time_series",
            "select": [
              [
                {
                  "params": [
                    "value"
                  ],
                  "type": "field"
                },
                {
                  "params": [],
                  "type": "mean"
                }
              ]
            ],
            "tags": [
              {
                "key": "instance",
                "operator": "=",
                "value": ""
              },
              {
                "condition": "AND",
                "key": "host",
                "operator": "=",
                "value": "Webp01"
              }
            ]
          }
        ],
        "thresholds": [
          {
            "colorMode": "critical",
            "fill": true,
            "line": true,
            "op": "lt",
            "value": 1
          }
        ],
        "timeFrom": null,
        "timeShift": null,
        "title": "Status of ",
        "tooltip": {
          "shared": true,
          "sort": 0,
          "value_type": "individual"
        },
        "type": "graph",
        "xaxis": {
          "buckets": null,
          "mode": "time",
          "name": null,
          "show": true,
          "values": []
        },
        "yaxes": [
          {
            "format": "short",
            "label": null,
            "logBase": 1,
            "max": null,
            "min": null,
            "show": true
          },
          {
            "format": "short",
            "label": null,
            "logBase": 1,
            "max": null,
            "min": null,
            "show": true
          }
        ],
        "yaxis": {
          "align": false,
          "alignLevel": null
        }
      }
    ],
    "refresh": "5m",
    "schemaVersion": 16,
    "style": "dark",
    "tags": [
      "web",
      "nodejs"
    ],
    "templating": {
      "list": []
    },
    "time": {
      "from": "now/d",
      "to": "now"
    },
    "timepicker": {
      "hidden": false,
      "refresh_intervals": [
        "5s",
        "10s",
        "30s",
        "1m",
        "5m",
        "15m",
        "30m",
        "1h",
        "2h",
        "1d"
      ],
      "time_options": [
        "5m",
        "15m",
        "1h",
        "6h",
        "12h",
        "24h",
        "2d",
        "7d",
        "30d"
      ]
    },
    "timezone": "",
    "title": "Status APP",
    "uid": "lOy3lIImz",
    "version": 89
  },
}

编辑:

这里是一个用于模板化您的仪表板的 JSON 段落:

  "templating": {
    "list": [
      {
        "allValue": null,
        "current": {
          "text": "PRD_Web01",
          "value": "PRD_Web01"
        },
        "datasource": "Collectd",
        "hide": 0,
        "includeAll": false,
        "label": null,
        "multi": false,
        "name": "host",
        "options": [],
        "query": "SHOW TAG VALUES WITH KEY=host",
        "refresh": 1,
        "regex": "",
        "sort": 0,
        "tagValuesQuery": "",
        "tags": [],
        "tagsQuery": "",
        "type": "query",
        "useTags": false
      },
      {
        "allValue": null,
        "current": {
          "text": "sda",
          "value": "sda"
        },
        "datasource": "Collectd",
        "hide": 0,
        "includeAll": false,
        "label": null,
        "multi": false,
        "name": "device",
        "options": [],
        "query": "SHOW TAG VALUES FROM \"disk_read\" WITH KEY = \"instance\"",
        "refresh": 1,
        "regex": "",
        "sort": 0,
        "tagValuesQuery": "",
        "tags": [],
        "tagsQuery": "",
        "type": "query",
        "useTags": false
      }
    ]
  },

根据我阅读您的回答,我猜您应该会心满意足的;)。我会更加关注这个讨论串。


1
感谢您的回复。我有一个从Grafana导出的json文件。并且我能够创建仪表板,同时保持(“query”:“”)空白。当我尝试发送influx查询“query”:“SELECT \”responseTime\” FROM \”requestsRaw\” WHERE $timeFilter”时,我会收到错误消息 [{"classification":"DeserializationError","message":"invalid character 'r' after object key:value pair"},{"fieldNames":["Dashboard"],"classification":"RequiredError","message":"Required"}]。 - Mahadev Gouda
1
Influx查询问题-https://community.grafana.com/t/can-not-send-influx-query-via-grafana-api-while-creating-the-dashboard/8139/2 - Mahadev Gouda

2

你能展示一下你的仪表板JSON是什么样子吗?JSON必须包含一个名为“dashboard”的键,并且其值内包含所有细节,如以下所示:

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

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