Экспорт и импорт Grafana Dashboard через HTTP API

Я пытаюсь экспортировать информационные панели экземпляра Grafana через его HTTP API и повторно импортировать их с помощью 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
   }
}

Есть ли у кого-нибудь идеи, как я могу это исправить? Смена версии помогает мне предотвратить ошибку несовпадения версий (это должно быть только временным решением)!

ИНФОРМАЦИЯ: Экземпляр графаны для импорта отличается от экземпляра экспорта. Но это та же версия с точно таким же источником данных.


person eventhorizon    schedule 23.11.2017    source источник


Ответы (1)


Панель мониторинга json, которую вы пытаетесь импортировать, имеет поле id ("id": 3), а наличие поля id в json означает, что вы пытаетесь обновить существующую панель мониторинга.

Ошибка The dashboard belongs to plugin Prometheus. возникает из-за того, что вам не разрешено обновлять информационные панели, принадлежащие источнику данных или плагину.

Чтобы импортировать новую панель мониторинга, просто удалите поле id, и Grafana создаст новую панель мониторинга с импортированным json (и назначит ей новый идентификатор).

person Daniel Lee    schedule 27.11.2017
comment
... но как обновить существующую информационную панель с помощью идентификатора? Веб-интерфейс дает запрос на перезапись. - person bryn; 04.10.2018
comment
@bryn вы имеете в виду, что после обновления через Grafana API вы получаете предупреждение о перезаписи при открытии обновленной панели инструментов в Grafana? Или вы о другом спрашиваете? - person Daniel Lee; 04.10.2018
comment
Нет, я просто хочу перезаписать через API. Теперь я вижу, что есть свойство, которое вы можете установить в своем запросе: "overwrite": true - это должно решить эту проблему для меня :) - person bryn; 04.10.2018