Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot import grafana dashboard via Grafana API

Tags:

python

grafana

I am trying to import the Grafana dashboard using HTTP API by following Grafana

Grafana Version: 5.1.3 OS -Windows 10

This is what i tried

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

and Here is my python code

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)

And output of both is:

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

It is asking for root property called dashboard in my json payload. Can anybody suggest me how to use that porperty and what data should i provide.

If any one want to dig more here are some links.

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://stackoverflow.com/questions/39968111/unable-to-post-to-grafana-using-python3-module-requests
https://stackoverflow.com/questions/39954475/post-request-works-in-postman-but-not-in-python/39954514#39954514
https://www.bountysource.com/issues/44431991-use-api-to-import-json-file-error
https://github.com/grafana/grafana/issues/7029
like image 702
Mahadev Gouda Avatar asked Jun 14 '18 06:06

Mahadev Gouda


People also ask

Can Grafana connect to API?

You can use Grafana HTTP APIs with Amazon Managed Grafana workspaces. The following sections list which Grafana APIs are supported.

How do you connect Grafana dashboard?

Log into Grafana and, from the main page, click + | Create | Dashboard. In the resulting window (Figure A), click Add a New Panel. The first thing to add to your dashboard is a panel.

Which format Grafana dashboard can be exported imported?

Dashboards are exported in Grafana JSON format, and contain everything you need (layout, variables, styles, data sources, queries, etc.) to import the dashboard at a later time.


2 Answers

Maybe you should try to download your dashboard from the API so you will a "proper" json model to push after?

You can download it with the following command :

curl -H "Authorization: Bearer $TOKEN" https://grafana.domain.tld/api/dashboards/uid/$DASHBOARD_UID

An other way to do it , you can download a dashboard JSON on grafana website => grafana.com/dashboards and try to upload it with your current code? ;)

The dashboard field contain everything that will be display, alerts, graph etc....

Here is an example of 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
  },
}

Edit:

Here is a JSON snipper for templating your dashboard :

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

As I read your answer, I guess you will be OK with this ;). I will try to keep a better eye on this thread

like image 96
Kristy Avatar answered Oct 19 '22 23:10

Kristy


Can you show how your dashboard json looks like ? The json MUST contain a key dashboard in it with all the details inside its value like the following:

{
  "dashboard": {
    "id": null,
    "uid": null,
    "title": "Production Overview",
    "tags": [ "templated" ],
    "timezone": "browser",
    "schemaVersion": 16,
    "version": 0
  },
  "folderId": 0,
  "overwrite": false
}
like image 25
KaiwalyaPowle Avatar answered Oct 20 '22 01:10

KaiwalyaPowle