api - 通过 HTTP API 导出和导入 Grafana Dashboard

标签 api import export grafana prometheus

我正在尝试通过其 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 表示您正在尝试更新现有仪表板。

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

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

关于api - 通过 HTTP API 导出和导入 Grafana Dashboard,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47460284/

相关文章:

database - Virtuemart2 csv导入数据库不断崩溃

R:多个模型并排的方差分析输出

c++ - 在 Linux 中链接动态共享库 - 未定义的函数引用

react-native - npm 包导出路径定义

java - 如何将 Android 应用程序连接到 API 并在列表中显示数据?

json - 将 .txt 文件转换为 JSON

c++ - 有没有C++轻量级的跨平台文件系统库?

javascript - 在 HighCharts 上设置图标(图像)时出现问题

r - 将 RDS 文件从 github 导入 R Windows

r - 将 SPSS 读入 R 时 attr(*, "value.labels") 是什么?