Grafana adds dashboard and access url through rest

aims

  • Configure Grafana to allow anonymous access
  • Register dashbord through rest interface
  • Get dashbord url for visualization

mac installation configuration

  • installation
brew update
brew install grafana
  • Configure anonymous access: /usr/local/etc/grafana/grafana.ini
[auth]
# 要删掉前边的分号
disable_login_form = true

[auth.anonymous]
enabled = true
org_name = Main Org.
org_role = Viewer
  • Start and stop
// 启动
brew services start grafana

// 关闭
brew services stop grafana

// 重启
brew services restart grafana

Register Dashboard

https://github.com/grafana/grafana/blob/master/docs/sources/http_api/dashboard.md

  • Post request Url: /api/dashboards/db

  • You need to create an Api key first, create one in Configuration -> API Keys, you will get a string, which is added to the post request header.

    https://blog.csdn.net/u012062455/article/details/79214927

  • The following important items are explained

    • targets.target: query time series
    • id: the id of the dashboard in a single Grafana instance
    • uid: The uidid of the dashboard in multiple Grafana instances, just make the two the same
    • title: dashboard name
    • refresh: refresh frequency
    • time: query time period, such as the last five minutes
    • panel: a time series dashboard, the panels in this example are all line charts
      • datasource: data source, need to be created in advance
      • gridPos: The position of the dashboard, x and y are the starting position, h is the height, and w is the width. Multiple panels need to be typeset by themselves.
      • title: panel name
  • After registration, you can view the dashboard through http://ip:3000/d/uid/title.

  • Create the post request body of the dashboard

{
  "dashboard": {
    "id": "imei",
    "uid": "imei",
    "title": "userName",
    "timezone": "browser",
    "refresh": "5s",
    "schemaVersion": 16,
    "time": {
    "from": "now-5m",
    "to": "now"
    },
    "panels": [
    {
      "aliasColors": {},
      "bars": false,
      "dashLength": 10,
      "dashes": false,
      "datasource": "IoTDB-Raspberry",
      "fill": 1,
      "gridPos": {
        "h": 6,
        "w": 24,
        "x": 0,
        "y": 0
      },
      "id": 2,
      "legend": {
        "avg": false,
        "current": false,
        "max": false,
        "min": false,
        "show": true,
        "total": false,
        "values": false
      },
      "lines": true,
      "linewidth": 1,
      "links": [],
      "nullPointMode": "null",
      "percentage": false,
      "pointradius": 5,
      "points": false,
      "renderer": "flot",
      "seriesOverrides": [],
      "spaceLength": 10,
      "stack": false,
      "steppedLine": false,
      "targets": [
        {
          "refId": "A",
          "target": "raspberry.pi1.distance",
          "type": "timeserie"
        }
      ],
      "thresholds": [],
      "timeFrom": null,
      "timeShift": null,
      "title": "超声波测距(米)",
      "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
      }
    }
  ],
    "version": 0
  },
  "folderId": 0,
  "overwrite": true
}

java constructs post request to send


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.URL;
import java.net.URLConnection;


		URL url = new URL(getGrafanaUrl);
        URLConnection connection = url.openConnection();
        connection.setRequestProperty("accept", "application/json");
        connection.setRequestProperty("connection", "Keep-Alive");
        connection.setRequestProperty("Content-Type", "application/json");
        connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
       
        // apiKey 是上边生成的那个
        connection.setRequestProperty("Authorization", "Bearer " + apiKey);
        connection.setDoInput(true);
        connection.setDoOutput(true);

        // request 是上边那个json
        try (PrintWriter out = new PrintWriter(connection.getOutputStream())){
            out.print(request.toString());
            out.flush();
        }

        try (BufferedReader in = new BufferedReader(
            new InputStreamReader(connection.getInputStream()))) {
            StringBuilder result = new StringBuilder();
            String line;
            while ((line = in.readLine()) != null) {
                result.append(line);
            }
            logger.info("Received response: {}", result);
        }

Dashboard URL

The url of Grafana's dashboard is all in the same style http://localhost:3000/d/good/goodgood?refresh=5s&orgId=1

Just replace those good with your own

Guess you like

Origin blog.csdn.net/qiaojialin/article/details/89482394