通过HTTP API导出和导入Grafana仪表板



我正在尝试通过其HTTP API导出Grafana实例的仪表板,并使用HTTP API重新启动它们。

回归给我以下错误(412):

{
  "message": "The dashboard belongs to plugin Prometheus.",
  "status": "plugin-dashboard"
}
{
  "message": "The dashboard has been changed by someone else",
  "status": "version-mismatch"
}

我做什么

导出

curl --fail --insecure --user $USER:$PASSWORD --request "GET" "$TARGET/api/search" --header "Accept: application/json" | jq -r '.[].uri | select(. | startswith("db/"))' > /tmp/grafanaDashboards.txt
while read F  ; do
   FILENAME=$(basename "$F").json
   OUT=$(curl --silent --fail --insecure --user $USER:$PASSWORD --request "GET" "$TARGET/api/dashboards/$F" --header "Accept: application/json" --output "$FILENAME" --write-out '%{http_code}') 2>/dev/null
done </tmp/grafanaDashboards.txt || exit 1

导入

for FILENAME in $SOURCE/micro*; do
   OUT=$(curl --fail --insecure --user $USER:$PASSWORD --request "POST" "$TARGET/api/dashboards/db" --header "Content-Type: application/json" --data-binary @$FILENAME --write-out '%{http_code}')
done || exit 1

内容

导出的内容看起来像这样:

{
   "meta":{
      "type":"db",
      "canSave":true,
      "canEdit":true,
      "canStar":true,
      "slug":"microservice-xyz-overview-current-values-m",
      "expires":"0001-01-01T00:00:00Z",
      "created":"2017-11-22T17:41:12Z",
      "updated":"2017-11-22T17:41:12Z",
      "updatedBy":"admin",
      "createdBy":"admin",
      "version":1
   },
   "dashboard":{
      "annotations":{
         "list":[
            {
               "builtIn":1,
               "datasource":"-- Grafana --",
               "enable":true,
               "hide":true,
               "iconColor":"rgba(0, 211, 255, 1)",
               "name":"Annotations u0026 Alerts",
               "type":"dashboard"
            }
         ]
      },
      "editable":true,
      "gnetId":null,
      "graphTooltip":0,
      "hideControls":false,
      "id":3,
      "links":[
      ],
      "refresh":"5s",
      "rows":[
         {
            "collapse":false,
            "height":"180",
            "panels":[
               {
                  "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":"Prometheus",
                  "description":"Average of all 95th percentile of last minute.",
                  "format":"s",
                  "gauge":{
                     "maxValue":1000,
                     "minValue":0,
                     "show":false,
                     "thresholdLabels":true,
                     "thresholdMarkers":true
                  },
                  "hideTimeOverride":false,
                  "id":11,
                  "interval":null,
                  "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"
                     }
                  ],
                  "span":4,
                  "sparkline":{
                     "fillColor":"rgba(255, 255, 255, 0.08)",
                     "full":true,
                     "lineColor":"rgb(255, 255, 255)",
                     "show":true
                  },
                  "tableColumn":"handler",
                  "targets":[
                     {
                        "expr":"avg(http_response_time{status=~"2[0-9]{2}", handler="xyz", quantile="0.95"})",
                        "format":"time_series",
                        "interval":"15s",
                        "intervalFactor":1,
                        "refId":"A",
                        "step":15
                     }
                  ],
                  "thresholds":"400,500",
                  "timeFrom":null,
                  "title":"95-th response time",
                  "type":"singlestat",
                  "valueFontSize":"120%",
                  "valueMaps":[
                     {
                        "op":"=",
                        "text":"0",
                        "value":"null"
                     }
                  ],
                  "valueName":"current"
               },
               {
                  "cacheTimeout":null,
                  "colorBackground":true,
                  "colorValue":false,
                  "colors":[
                     "rgba(245, 54, 54, 0.9)",
                     "rgba(237, 129, 40, 0.89)",
                     "rgba(50, 172, 45, 0.97)"
                  ],
                  "datasource":"Prometheus",
                  "description":"Number of running xyzz/xyzzz services.",
                  "format":"short",
                  "gauge":{
                     "maxValue":10,
                     "minValue":0,
                     "show":false,
                     "thresholdLabels":false,
                     "thresholdMarkers":true
                  },
                  "hideTimeOverride":false,
                  "id":17,
                  "interval":null,
                  "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"
                     }
                  ],
                  "span":4,
                  "sparkline":{
                     "fillColor":"rgba(255, 255, 255, 0.18)",
                     "full":true,
                     "lineColor":"rgb(255, 255, 255)",
                     "show":true
                  },
                  "tableColumn":"",
                  "targets":[
                     {
                        "expr":"sum(up{job=~"xyzz|xyzzz"})",
                        "format":"time_series",
                        "interval":"",
                        "intervalFactor":1,
                        "refId":"A",
                        "step":1
                     }
                  ],
                  "thresholds":"2,2",
                  "timeFrom":null,
                  "title":"running instances",
                  "type":"singlestat",
                  "valueFontSize":"120%",
                  "valueMaps":[
                     {
                        "op":"=",
                        "text":"N/A",
                        "value":"null"
                     }
                  ],
                  "valueName":"current"
               },
               {
                  "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":"Prometheus",
                  "format":"none",
                  "gauge":{
                     "maxValue":100,
                     "minValue":0,
                     "show":false,
                     "thresholdLabels":false,
                     "thresholdMarkers":true
                  },
                  "id":22,
                  "interval":null,
                  "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"
                     }
                  ],
                  "span":4,
                  "sparkline":{
                     "fillColor":"rgba(255, 255, 255, 0.18)",
                     "full":true,
                     "lineColor":"rgb(255, 255, 255)",
                     "show":true
                  },
                  "tableColumn":"handler",
                  "targets":[
                     {
                        "expr":"sum(rate(http_response_time_count{handler="xyz", status=~"2[0-9]{2}"}[1m]))",
                        "format":"time_series",
                        "intervalFactor":1,
                        "legendFormat":"",
                        "metric":"http_response_time_count",
                        "refId":"A",
                        "step":30
                     }
                  ],
                  "thresholds":"1000,3000",
                  "timeFrom":null,
                  "title":"HTTP-2XX / min",
                  "type":"singlestat",
                  "valueFontSize":"120%",
                  "valueMaps":[
                     {
                        "op":"=",
                        "text":"N/A",
                        "value":"null"
                     }
                  ],
                  "valueName":"current"
               }
            ],
            "repeat":null,
            "repeatIteration":null,
            "repeatRowId":null,
            "showTitle":false,
            "title":"Dashboard Row",
            "titleSize":"h6"
         },
         {
            "collapse":false,
            "height":"180",
            "panels":[
               {
                  "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":"Prometheus",
                  "decimals":null,
                  "format":"short",
                  "gauge":{
                     "maxValue":100,
                     "minValue":0,
                     "show":false,
                     "thresholdLabels":false,
                     "thresholdMarkers":true
                  },
                  "hideTimeOverride":false,
                  "id":20,
                  "interval":null,
                  "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"
                     }
                  ],
                  "span":4,
                  "sparkline":{
                     "fillColor":"rgba(255, 255, 255, 0.18)",
                     "full":true,
                     "lineColor":"rgb(255, 255, 255)",
                     "show":true
                  },
                  "tableColumn":"",
                  "targets":[
                     {
                        "expr":"sum(rate(http_response_time_count{handler="xyz", status=~"4[0-9]{2}"}[1m]))",
                        "format":"time_series",
                        "intervalFactor":1,
                        "refId":"A",
                        "step":2
                     }
                  ],
                  "thresholds":"100",
                  "timeFrom":null,
                  "timeShift":null,
                  "title":"HTTP-4XX / min",
                  "type":"singlestat",
                  "valueFontSize":"120%",
                  "valueMaps":[
                     {
                        "op":"=",
                        "text":"N/A",
                        "value":"null"
                     }
                  ],
                  "valueName":"diff"
               },
               {
                  "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":"Prometheus",
                  "format":"short",
                  "gauge":{
                     "maxValue":100,
                     "minValue":0,
                     "show":false,
                     "thresholdLabels":false,
                     "thresholdMarkers":true
                  },
                  "hideTimeOverride":false,
                  "id":18,
                  "interval":null,
                  "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"
                     }
                  ],
                  "span":4,
                  "sparkline":{
                     "fillColor":"rgba(255, 255, 255, 0.18)",
                     "full":true,
                     "lineColor":"rgb(255, 255, 255)",
                     "show":true
                  },
                  "tableColumn":"",
                  "targets":[
                     {
                        "expr":"sum(rate(http_response_time_count{handler="xyz", status=~"5[0-9]{2}"}[1m]))",
                        "format":"time_series",
                        "intervalFactor":1,
                        "refId":"A",
                        "step":2
                     }
                  ],
                  "thresholds":"10,100",
                  "timeFrom":null,
                  "timeShift":null,
                  "title":"HTTP-5XX / min",
                  "type":"singlestat",
                  "valueFontSize":"120%",
                  "valueMaps":[
                     {
                        "op":"=",
                        "text":"0",
                        "value":"null"
                     }
                  ],
                  "valueName":"diff"
               },
               {
                  "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":"Prometheus",
                  "format":"none",
                  "gauge":{
                     "maxValue":100,
                     "minValue":0,
                     "show":false,
                     "thresholdLabels":false,
                     "thresholdMarkers":true
                  },
                  "hideTimeOverride":false,
                  "id":21,
                  "interval":null,
                  "links":[
                  ],
                  "mappingType":2,
                  "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":"Come on. Not a single error?",
                        "to":"null"
                     },
                     {
                        "from":"0",
                        "text":"Come on. Not a single error?",
                        "to":"1"
                     },
                     {
                        "from":"1",
                        "text":"Repair that!",
                        "to":"100"
                     },
                     {
                        "from":"100",
                        "text":"That will not end good for you...",
                        "to":"1000"
                     },
                     {
                        "from":"1000",
                        "text":"You have lost the match.",
                        "to":"99999999"
                     }
                  ],
                  "span":4,
                  "sparkline":{
                     "fillColor":"rgba(31, 118, 189, 0.18)",
                     "full":false,
                     "lineColor":"rgb(31, 120, 193)",
                     "show":false
                  },
                  "tableColumn":"",
                  "targets":[
                     {
                        "expr":"sum(rate(http_response_time_count{handler="xyz", status=~"4[0-9]{2}"}[1m]))",
                        "format":"time_series",
                        "intervalFactor":1,
                        "refId":"A",
                        "step":2
                     }
                  ],
                  "thresholds":"1,1000",
                  "timeFrom":null,
                  "timeShift":null,
                  "title":"",
                  "type":"singlestat",
                  "valueFontSize":"70%",
                  "valueMaps":[
                     {
                        "op":"=",
                        "text":"0",
                        "value":"null"
                     }
                  ],
                  "valueName":"current"
               }
            ],
            "repeat":null,
            "repeatIteration":null,
            "repeatRowId":null,
            "showTitle":false,
            "title":"SLA quantiles",
            "titleSize":"h6"
         }
      ],
      "schemaVersion":14,
      "style":"dark",
      "tags":[
      ],
      "templating":{
         "list":[
         ]
      },
      "time":{
         "from":"now-1h",
         "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"
         ]
      },
      "timezone":"utc",
      "title":"Microservice | XYZ | Overview | Current Values | M",
      "version":1
   }
}

有人知道如何解决这个问题吗?更改版本有助于我防止版本不匹配的错误(这只能是解决方法)!

信息:导入的Grafana实例与导出实例不同。但这是与完全相同的数据源的同一版本。

您要导入的仪表板JSON具有id字段("id": 3),并且JSON中的id字段意味着您正在尝试更新现有的仪表板。

The dashboard belongs to plugin Prometheus.错误是因为不允许您更新属于数据源或插件的仪表板。

要导入一个新的仪表板,只需删除ID字段,Grafana将使用导入的JSON创建一个新的仪表板(并为其分配新ID)。

最新更新