如何在 Grafana 图像中添加仪表板配置 json 文件?

How to add dashboard configuration json file in Grafana image?

我有一个 grafana docker 图像,它使用配置文件预先配置了 hawkular-datasource。

在运行ning grafana实例之后,我有一个队友给的json,可以在grafana中导入,json文件在导入时创建仪表板。

如何让仪表板默认显示在 Grafana 实例中?

我尝试将 json 文件复制到 /etc/grafana/provisioning/dashboards/ 文件夹并创建了一个新的 docker 图像。但是当我 运行 图片时,实例不包含主页或其中任何地方的仪表板。

如何在 docker 图像中添加此 json 文件。我走的路对吗?

我试过了 http://docs.grafana.org/administration/provisioning/ 但它并没有多大帮助。有什么建议吗?

这是 json 文件。

{
    "id": null,
    "title": "Openshift Metrics",
    "tags": [],
    "style": "dark",
    "timezone": "browser",
    "editable": true,
    "hideControls": false,
    "sharedCrosshair": false,
    "rows": [
      {
        "collapse": false,
        "editable": true,
        "height": "322px",
        "panels": [
          {
            "content": "<center><p style='font-size: 40pt'>$app</p></center>",
            "editable": true,
            "error": false,
            "id": 23,
            "isNew": true,
            "links": [],
            "mode": "html",
            "repeatIteration": 1476706310439,
            "scopedVars": {},
            "span": 2,
            "style": {
              "font-size": "36pt"
            },
            "title": "",
            "type": "text"
          },
          {
            "aliasColors": {},
            "bars": false,
            "datasource": "Hawk-DS",
            "editable": true,
            "error": false,
            "fill": 1,
            "grid": {
              "threshold1": null,
              "threshold1Color": "rgba(216, 200, 27, 0.27)",
              "threshold2": null,
              "threshold2Color": "rgba(234, 112, 112, 0.22)"
            },
            "id": 9,
            "isNew": true,
            "legend": {
              "avg": false,
              "current": false,
              "max": false,
              "min": false,
              "show": true,
              "total": false,
              "values": false
            },
            "lines": true,
            "linewidth": 2,
            "links": [],
            "nullPointMode": "connected",
            "percentage": false,
            "pointradius": 5,
            "points": false,
            "renderer": "flot",
            "repeatIteration": 1476706310439,
            "scopedVars": {},
            "seriesOverrides": [],
            "span": 6,
            "stack": false,
            "steppedLine": false,
            "targets": [
              {
                "queryBy": "tags",
                "rate": false,
                "refId": "A",
                "seriesAggFn": "none",
                "tags": [
                  {
                    "name": "container_name",
                    "value": "$app"
                  },
                  {
                    "name": "descriptor_name",
                    "value": "memory/usage"
                  }
                ],
                "target": "select metric",
                "tagsQL": "container_name IN [$app] AND descriptor_name='memory/usage'",
                "timeAggFn": "avg",
                "type": "gauge"
              }
            ],
            "timeFrom": null,
            "timeShift": null,
            "title": "Memory usage",
            "tooltip": {
              "msResolution": true,
              "shared": true,
              "sort": 0,
              "value_type": "cumulative"
            },
            "type": "graph",
            "xaxis": {
              "show": true
            },
            "yaxes": [
              {
                "format": "bytes",
                "label": null,
                "logBase": 1,
                "max": null,
                "min": 0,
                "show": true
              },
              {
                "format": "short",
                "label": null,
                "logBase": 1,
                "max": null,
                "min": null,
                "show": true
              }
            ]
          },
          {
            "cacheTimeout": null,
            "colorBackground": true,
            "colorValue": false,
            "colors": [
              "rgba(50, 172, 45, 0.97)",
              "rgba(237, 129, 40, 0.89)",
              "rgba(245, 54, 54, 0.9)"
            ],
            "datasource": "Hawk-DS",
            "editable": true,
            "error": false,
            "format": "bytes",
            "gauge": {
              "maxValue": 100,
              "minValue": 0,
              "show": false,
              "thresholdLabels": false,
              "thresholdMarkers": true
            },
            "height": "100px",
            "id": 12,
            "interval": null,
            "isNew": true,
            "links": [],
            "mappingType": 1,
            "mappingTypes": [
              {
                "name": "value to text",
                "value": 1
              },
              {
                "name": "range to text",
                "value": 2
              }
            ],
            "maxDataPoints": 100,
            "nullPointMode": "connected",
            "nullText": null,
            "postfix": "",
            "postfixFontSize": "50%",
            "prefix": "",
            "prefixFontSize": "50%",
            "rangeMaps": [
              {
                "from": "null",
                "text": "N/A",
                "to": "null"
              }
            ],
            "repeatIteration": 1476706310439,
            "scopedVars": {},
            "span": 2,
            "sparkline": {
              "fillColor": "rgba(31, 118, 189, 0.18)",
              "full": false,
              "lineColor": "rgb(31, 120, 193)",
              "show": false
            },
            "targets": [
              {
                "queryBy": "tags",
                "rate": false,
                "refId": "A",
                "seriesAggFn": "sum",
                "tags": [
                  {
                    "name": "container_name",
                    "value": "$app"
                  },
                  {
                    "name": "descriptor_name",
                    "value": "memory/usage"
                  }
                ],
                "target": "select metric",
                "tagsQL": "container_name IN [$app] AND descriptor_name='memory/usage'",
                "timeAggFn": "live",
                "type": "gauge"
              }
            ],
            "thresholds": "140000000,180000000",
            "title": "Live, all pods",
            "type": "singlestat",
            "valueFontSize": "80%",
            "valueMaps": [
              {
                "op": "=",
                "text": "N/A",
                "value": "null"
              }
            ],
            "valueName": "avg"
          },
          {
            "cacheTimeout": null,
            "colorBackground": true,
            "colorValue": false,
            "colors": [
              "rgba(50, 172, 45, 0.97)",
              "rgba(237, 129, 40, 0.89)",
              "rgba(245, 54, 54, 0.9)"
            ],
            "datasource": "Hawk-DS",
            "editable": true,
            "error": false,
            "format": "bytes",
            "gauge": {
              "maxValue": 100,
              "minValue": 0,
              "show": false,
              "thresholdLabels": false,
              "thresholdMarkers": true
            },
            "height": "100px",
            "id": 15,
            "interval": null,
            "isNew": true,
            "links": [],
            "mappingType": 1,
            "mappingTypes": [
              {
                "name": "value to text",
                "value": 1
              },
              {
                "name": "range to text",
                "value": 2
              }
            ],
            "maxDataPoints": 100,
            "nullPointMode": "connected",
            "nullText": null,
            "postfix": "",
            "postfixFontSize": "50%",
            "prefix": "",
            "prefixFontSize": "50%",
            "rangeMaps": [
              {
                "from": "null",
                "text": "N/A",
                "to": "null"
              }
            ],
            "repeatIteration": 1476706310439,
            "scopedVars": {},
            "span": 2,
            "sparkline": {
              "fillColor": "rgba(31, 118, 189, 0.18)",
              "full": false,
              "lineColor": "rgb(31, 120, 193)",
              "show": false
            },
            "targets": [
              {
                "queryBy": "tags",
                "rate": false,
                "refId": "A",
                "seriesAggFn": "avg",
                "tags": [
                  {
                    "name": "container_name",
                    "value": "$app"
                  },
                  {
                    "name": "descriptor_name",
                    "value": "memory/usage"
                  }
                ],
                "target": "select metric",
                "tagsQL": "container_name IN [$app] AND descriptor_name='memory/usage'",
                "timeAggFn": "live",
                "type": "gauge"
              }
            ],
            "thresholds": "140000000,180000000",
            "title": "Live per pod",
            "type": "singlestat",
            "valueFontSize": "80%",
            "valueMaps": [
              {
                "op": "=",
                "text": "N/A",
                "value": "null"
              }
            ],
            "valueName": "avg"
          },
          {
            "cacheTimeout": null,
            "colorBackground": true,
            "colorValue": false,
            "colors": [
              "rgba(50, 172, 45, 0.97)",
              "rgba(237, 129, 40, 0.89)",
              "rgba(245, 54, 54, 0.9)"
            ],
            "datasource": "Hawk-DS",
            "editable": true,
            "error": false,
            "format": "bytes",
            "gauge": {
              "maxValue": 100,
              "minValue": 0,
              "show": false,
              "thresholdLabels": false,
              "thresholdMarkers": true
            },
            "height": "100px",
            "id": 10,
            "interval": null,
            "isNew": true,
            "links": [],
            "mappingType": 1,
            "mappingTypes": [
              {
                "name": "value to text",
                "value": 1
              },
              {
                "name": "range to text",
                "value": 2
              }
            ],
            "maxDataPoints": 100,
            "nullPointMode": "connected",
            "nullText": null,
            "postfix": "",
            "postfixFontSize": "50%",
            "prefix": "",
            "prefixFontSize": "50%",
            "rangeMaps": [
              {
                "from": "null",
                "text": "N/A",
                "to": "null"
              }
            ],
            "repeatIteration": 1476706310439,
            "scopedVars": {},
            "span": 2,
            "sparkline": {
              "fillColor": "rgba(31, 118, 189, 0.18)",
              "full": false,
              "lineColor": "rgb(31, 120, 193)",
              "show": false
            },
            "targets": [
              {
                "queryBy": "tags",
                "rate": false,
                "refId": "A",
                "seriesAggFn": "sum",
                "tags": [
                  {
                    "name": "container_name",
                    "value": "$app"
                  },
                  {
                    "name": "descriptor_name",
                    "value": "memory/usage"
                  }
                ],
                "target": "select metric",
                "tagsQL": "container_name IN [$app] AND descriptor_name='memory/usage'",
                "timeAggFn": "avg",
                "type": "gauge"
              }
            ],
            "thresholds": "140000000,180000000",
            "title": "Average, all pods",
            "type": "singlestat",
            "valueFontSize": "80%",
            "valueMaps": [
              {
                "op": "=",
                "text": "N/A",
                "value": "null"
              }
            ],
            "valueName": "avg"
          },
          {
            "cacheTimeout": null,
            "colorBackground": true,
            "colorValue": false,
            "colors": [
              "rgba(50, 172, 45, 0.97)",
              "rgba(237, 129, 40, 0.89)",
              "rgba(245, 54, 54, 0.9)"
            ],
            "datasource": "Hawk-DS",
            "editable": true,
            "error": false,
            "format": "bytes",
            "gauge": {
              "maxValue": 100,
              "minValue": 0,
              "show": false,
              "thresholdLabels": false,
              "thresholdMarkers": true
            },
            "height": "100px",
            "id": 13,
            "interval": null,
            "isNew": true,
            "links": [],
            "mappingType": 1,
            "mappingTypes": [
              {
                "name": "value to text",
                "value": 1
              },
              {
                "name": "range to text",
                "value": 2
              }
            ],
            "maxDataPoints": 100,
            "nullPointMode": "connected",
            "nullText": null,
            "postfix": "",
            "postfixFontSize": "50%",
            "prefix": "",
            "prefixFontSize": "50%",
            "rangeMaps": [
              {
                "from": "null",
                "text": "N/A",
                "to": "null"
              }
            ],
            "repeatIteration": 1476706310439,
            "scopedVars": {},
            "span": 2,
            "sparkline": {
              "fillColor": "rgba(31, 118, 189, 0.18)",
              "full": false,
              "lineColor": "rgb(31, 120, 193)",
              "show": false
            },
            "targets": [
              {
                "queryBy": "tags",
                "rate": false,
                "refId": "A",
                "seriesAggFn": "avg",
                "tags": [
                  {
                    "name": "container_name",
                    "value": "$app"
                  },
                  {
                    "name": "descriptor_name",
                    "value": "memory/usage"
                  }
                ],
                "target": "select metric",
                "tagsQL": "container_name IN [$app] AND descriptor_name='memory/usage'",
                "timeAggFn": "avg",
                "type": "gauge"
              }
            ],
            "thresholds": "140000000,180000000",
            "title": "Average per pod",
            "type": "singlestat",
            "valueFontSize": "80%",
            "valueMaps": [
              {
                "op": "=",
                "text": "N/A",
                "value": "null"
              }
            ],
            "valueName": "avg"
          },
          {
            "cacheTimeout": null,
            "colorBackground": true,
            "colorValue": false,
            "colors": [
              "rgba(50, 172, 45, 0.97)",
              "rgba(237, 129, 40, 0.89)",
              "rgba(245, 54, 54, 0.9)"
            ],
            "datasource": "Hawk-DS",
            "editable": true,
            "error": false,
            "format": "bytes",
            "gauge": {
              "maxValue": 100,
              "minValue": 0,
              "show": false,
              "thresholdLabels": false,
              "thresholdMarkers": true
            },
            "height": "100px",
            "id": 11,
            "interval": null,
            "isNew": true,
            "links": [],
            "mappingType": 1,
            "mappingTypes": [
              {
                "name": "value to text",
                "value": 1
              },
              {
                "name": "range to text",
                "value": 2
              }
            ],
            "maxDataPoints": 100,
            "nullPointMode": "connected",
            "nullText": null,
            "postfix": "",
            "postfixFontSize": "50%",
            "prefix": "",
            "prefixFontSize": "50%",
            "rangeMaps": [
              {
                "from": "null",
                "text": "N/A",
                "to": "null"
              }
            ],
            "repeatIteration": 1476706310439,
            "scopedVars": {},
            "span": 2,
            "sparkline": {
              "fillColor": "rgba(31, 118, 189, 0.18)",
              "full": false,
              "lineColor": "rgb(31, 120, 193)",
              "show": false
            },
            "targets": [
              {
                "queryBy": "tags",
                "rate": false,
                "refId": "A",
                "seriesAggFn": "sum",
                "tags": [
                  {
                    "name": "container_name",
                    "value": "$app"
                  },
                  {
                    "name": "descriptor_name",
                    "value": "memory/usage"
                  }
                ],
                "target": "select metric",
                "tagsQL": "container_name IN [$app] AND descriptor_name='memory/usage'",
                "timeAggFn": "max",
                "type": "gauge"
              }
            ],
            "thresholds": "140000000,180000000",
            "title": "Max, all pods",
            "type": "singlestat",
            "valueFontSize": "80%",
            "valueMaps": [
              {
                "op": "=",
                "text": "N/A",
                "value": "null"
              }
            ],
            "valueName": "avg"
          },
          {
            "cacheTimeout": null,
            "colorBackground": true,
            "colorValue": false,
            "colors": [
              "rgba(50, 172, 45, 0.97)",
              "rgba(237, 129, 40, 0.89)",
              "rgba(245, 54, 54, 0.9)"
            ],
            "datasource": "Hawk-DS",
            "editable": true,
            "error": false,
            "format": "bytes",
            "gauge": {
              "maxValue": 100,
              "minValue": 0,
              "show": false,
              "thresholdLabels": false,
              "thresholdMarkers": true
            },
            "height": "100px",
            "id": 14,
            "interval": null,
            "isNew": true,
            "links": [],
            "mappingType": 1,
            "mappingTypes": [
              {
                "name": "value to text",
                "value": 1
              },
              {
                "name": "range to text",
                "value": 2
              }
            ],
            "maxDataPoints": 100,
            "nullPointMode": "connected",
            "nullText": null,
            "postfix": "",
            "postfixFontSize": "50%",
            "prefix": "",
            "prefixFontSize": "50%",
            "rangeMaps": [
              {
                "from": "null",
                "text": "N/A",
                "to": "null"
              }
            ],
            "repeatIteration": 1476706310439,
            "scopedVars": {},
            "span": 2,
            "sparkline": {
              "fillColor": "rgba(31, 118, 189, 0.18)",
              "full": false,
              "lineColor": "rgb(31, 120, 193)",
              "show": false
            },
            "targets": [
              {
                "queryBy": "tags",
                "rate": false,
                "refId": "A",
                "seriesAggFn": "avg",
                "tags": [
                  {
                    "name": "container_name",
                    "value": "$app"
                  },
                  {
                    "name": "descriptor_name",
                    "value": "memory/usage"
                  }
                ],
                "target": "select metric",
                "tagsQL": "container_name IN [$app] AND descriptor_name='memory/usage'",
                "timeAggFn": "max",
                "type": "gauge"
              }
            ],
            "thresholds": "140000000,180000000",
            "title": "Max per pod",
            "type": "singlestat",
            "valueFontSize": "80%",
            "valueMaps": [
              {
                "op": "=",
                "text": "N/A",
                "value": "null"
              }
            ],
            "valueName": "avg"
          }
        ],
        "repeat": "app",
        "scopedVars": {
          "app": {
            "text": "aloha",
            "value": "aloha",
            "selected": true
          }
        },
        "title": "New row"
      }
    ],
    "time": {
      "from": "now-30m",
      "to": "now"
    },
    "timepicker": {
      "refresh_intervals": [
        "5s",
        "10s",
        "30s",
        "1m",
        "5m",
        "15m",
        "30m",
        "1h",
        "2h",
        "1d"
      ],
      "time_options": [
        "5m",
        "15m",
        "1h",
        "6h",
        "12h",
        "24h",
        "2d",
        "7d",
        "30d"
      ]
    },
    "templating": {
      "list": [
        {
          "current": {},
          "datasource": "Hawk-DS",
          "hide": 0,
          "includeAll": true,
          "label": "Application",
          "multi": true,
          "name": "app",
          "options": [],
          "query": "tags/container_name:*",
          "refresh": 1,
          "regex": "",
          "type": "query"
        }
      ]
    },
    "annotations": {
      "list": []
    },
    "schemaVersion": 12,
    "version": 32,
    "links": [],
    "gnetId": null
}

您应该将 YAML 文件指向该文件夹中的 JSON 文件。例如写 /etc/grafana/provisioning/dashboards/local.yml:

apiVersion: 1

providers:
- name: 'default'
  orgId: 1
  folder: ''
  type: file
  disableDeletion: false
  updateIntervalSeconds: 10 #how often Grafana will scan for changed dashboards
  options:
    path: /var/lib/grafana/dashboards

然后将您的 JSON 文件写入 /var/lib/grafana/dashboards/openshift.json


在 Grafana 5 之前,我之前的解决方案是将整个 Docker 过程包装在一个脚本中,该脚本使用 API 在 Docker 容器启动后创建仪表板。您可以使用 GF_SECURITY_ADMIN_PASSWORD 环境变量来设置密码。您也可以使用 GF_AUTH_ANONYMOUS_ENABLED,但您需要确保它不能被外界访问。

docker run -p 3000:3000 -e GF_AUTH_ANONYMOUS_ENABLED=true grafana/grafana ...
sleep 10  # wait for grafana to load (use a better method of waiting in production)
curl -skfS -XPOST --header "Content-Type: application/json" "http://localhost:3000/grafana/api/dashboards/db" --data-binary @dashboard.json

我遇到了同样的问题,我希望下一个解决方案可以帮助别人。 例如,您有下一个工作目录:

├── docker-compose.yml          
├── grafana                     
│   ├── Dockerfile
│   ├── dashboards
│   │   └── exported_dashboard.json    #dashboard you saved before
│   └── provisioning            
│       │                       
│       ├── dashboards          
│       │   └── all.yml         
│       └── datasources
│           └── all.yml         
  1. Dockerfile 中,您必须将 provisioningdashboards 文件夹添加到 grafana 容器中:
FROM grafana/grafana:latest
ADD ./provisioning /etc/grafana/provisioning
ADD ./dashboards /var/lib/grafana/dashboards
  1. provisioning/dashboards/all.yml 中,您必须设置导入的仪表板文件夹的路径 exported_dashboard.json实际放在哪里:
- name: 'default'
  org_id: 1
  folder: ''
  type: 'file'
  options:
    folder: '/var/lib/grafana/dashboards'
  1. [可选] 然后你也可以在provisioning/datasources/all.yml,prometheus中配置默认​​的datasourse,例如:
datasources:
-  access: 'proxy'
   editable: true
   is_default: true
   name: 'prom1'
   org_id: 1
   type: 'prometheus'
   url: 'http://prometheus:9090'
   version: 1
  1. 最后,docker-compose.yml
version: '3.8'

services:
  grafana:
    image: awesome-grafana:latest
    build:
      context: grafana
    ports:
      - "3000:3000"
    environment:
      GF_DASHBOARDS_DEFAULT_HOME_DASHBOARD_PATH: "/var/lib/grafana/dashboards/exported_dashboard.json"
  • 至于HOME仪表盘,可以使用GF_DASHBOARDS_DEFAULT_HOME_DASHBOARD_PATH环境变量,如上图。 有关 GF_DASHBOARDS_DEFAULT_HOME_DASHBOARD_PATH 环境变量的更多信息,您可以在 github issue.
  • 中阅读
  • 您可以在下一个 github repository
  • 中找到完整的综合代码示例
  • 带解释的原创文章there