“消息”:使用 HTTP API 导入 grafana 现有 json 文件时出现“无法导入仪表板”错误

Posted

技术标签:

【中文标题】“消息”:使用 HTTP API 导入 grafana 现有 json 文件时出现“无法导入仪表板”错误【英文标题】:"message":"Failed to import dashboard" error while import grafana existing json file using HTTP API 【发布时间】:2019-11-15 00:28:09 【问题描述】:

我正在尝试使用 HTTP API 将现有 json 文件导入 grafana,它正在成功读取 json 文件,但对于每一行,它都会抛出 command not found 错误。

我尝试执行以下命令,

curl -X POST --insecure -H "Authorization: Bearer eyJrIjoiOWwwUmpQYUFmczhRVG1CZ1VmdTZ1VlZLTlc2ba0xUb0QiLCJuIjoia2xhIiwiaWQiOjJ9" -H "Content-Type: application/json" -d '"dashboard":"id": 1,"title": "tested33","tags": ["templated"],"timezone": "browser","rows": [],"schemaVersion": 6,"version": 0,"overwrite":false' -d "$(/u04/my_metrics.json)"   localhost:3000/api/dashboards/db

它正在读取 json 文件中的每一行但抛出错误

"message":"导入仪表盘失败"


  "annotations": 
  "list": [
   
    "builtIn": 1,
    "datasource": "-- Grafana --",
    "enable": true,
    "hide": true,
    "iconColor": "rgba(0, 211, 255, 1)",
    "name": "Annotations & Alerts",
    "type": "dashboard"
  
 ]
,
"editable": true,
"gnetId": 1860,
"graphTooltip": 0,
"id": 35,
"iteration": 1562652036142,
"links": [],
"panels": [
 
  "collapsed": false,
  "gridPos": 
    "h": 1,
    "w": 24,
    "x": 0,
    "y": 0
  ,
  "id": 261,
  "panels": [],
  "repeat": null,
  "title": "General",
  "type": "row"
,

  "cacheTimeout": null,
  "colorBackground": false,
  "colorValue": false,
  "colors": [
    "rgba(245, 54, 54, 0.9)",
    "rgba(237, 129, 40, 0.89)",
    "rgba(50, 172, 45, 0.97)"
  ],
  "datasource": "Prometheus",
  "decimals": 2,
  "description": "System Load (1m avg)",
  "format": "short",
  "gauge": 
    "maxValue": 100,
    "minValue": 0,
    "show": false,
    "thresholdLabels": false,
    "thresholdMarkers": true
  ,
  "gridPos": 
    "h": 3,
    "w": 4,
    "x": 0,
    "y": 1
  ,
  "id": 17,
  "interval": null,
  "links": [],
  "mappingType": 1,
  "mappingTypes": [
    
      "name": "value to text",
      "value": 1
    ,
    
      "name": "range to text",
      "value": 2
    
  ],
  "maxDataPoints": 100,
  "maxPerRow": 12,
  "nullPointMode": "null",
  "nullText": null,
  "options": ,
  "postfix": "",
  "postfixFontSize": "50%",
  "prefix": "",
  "prefixFontSize": "50%",
  "rangeMaps": [
    
      "from": "null",
      "text": "N/A",
      "to": "null"
    
  ],
  "sparkline": 
    "fillColor": "rgba(31, 118, 189, 0.18)",
    "full": false,
    "lineColor": "rgb(31, 120, 193)",
    "show": false
  ,
  "tableColumn": "",
  "targets": [
    
      "expr": "node_load1instance=~\"$node:$port\",job=~\"$job\"",
      "hide": false,
      "intervalFactor": 1,
      "refId": "A",
      "step": 900
    
  ],
  "thresholds": "",
  "title": "System Load (1m avg)",
  "type": "singlestat",
  "valueFontSize": "50%",
  "valueMaps": [
    
      "op": "=",
      "text": "N/A",
      "value": "null"
    
  ],
  "valueName": "current"

],
"refresh": false,
"schemaVersion": 18,
"style": "dark",
"tags": [],
"templating": 
"list": [
  
    "allValue": null,
    "current": 
      "text": "limca",
      "value": "limca"
    ,
    "datasource": "Prometheus",
    "definition": "",
    "hide": 0,
    "includeAll": false,
    "label": "Job",
    "multi": false,
    "name": "job",
    "options": [],
    "query": "label_values(node_uname_info, job)",
    "refresh": 1,
    "regex": "",
    "skipUrlSync": false,
    "sort": 1,
    "tagValuesQuery": "",
    "tags": [],
    "tagsQuery": "",
    "type": "query",
    "useTags": false
  ,
  
    "allValue": null,
    "current": 
      "text": "limca",
      "value": "limca"
    ,
    "datasource": "Prometheus",
    "definition": "",
    "hide": 2,
    "includeAll": false,
    "label": "",
    "multi": false,
    "name": "name",
    "options": [],
    "query": "label_values(node_uname_infojob=~\"$job\", nodename)",
    "refresh": 1,
    "regex": "",
    "skipUrlSync": false,
    "sort": 1,
    "tagValuesQuery": "",
    "tags": [],
    "tagsQuery": "",
    "type": "query",
    "useTags": false
  ,
  
    "allValue": null,
    "current": 
      "text": "limca",
      "value": "limca"
    ,
    "datasource": "Prometheus",
    "definition": "",
    "hide": 2,
    "includeAll": false,
    "label": "Host:",
    "multi": false,
    "name": "node",
    "options": [],
    "query": "label_values(node_uname_infonodename=\"$name\", instance)",
    "refresh": 1,
    "regex": "/([^:]+):.*/",
    "skipUrlSync": false,
    "sort": 1,
    "tagValuesQuery": "",
    "tags": [],
    "tagsQuery": "",
    "type": "query",
    "useTags": false
  ,
  
    "allValue": null,
    "current": 
      "text": "9100",
      "value": "9100"
    ,
    "datasource": "Prometheus",
    "definition": "",
    "hide": 0,
    "includeAll": false,
    "label": "Port",
    "multi": false,
    "name": "port",
    "options": [],
    "query": "label_values(node_uname_infoinstance=~\"$node:(.*)\", instance)",
    "refresh": 1,
    "regex": "/[^:]+:(.*)/",
    "skipUrlSync": false,
    "sort": 3,
    "tagValuesQuery": "",
    "tags": [],
    "tagsQuery": "",
    "type": "query",
    "useTags": false
  
]
 ,
 "time": 
"from": "now-24h",
"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": "browser",
"title": "importTest",
"uid": "importjson",
"version": 1

请检查 json 文件一次并帮助我解决此问题

【问题讨论】:

curl 更改为 curl -v 以获取详细输出并附加完整响应的 pastebin @TarunLalwani: * 即将 connect() 到 localhost 端口 3000 (#0) * 正在尝试 ::1... * 已连接到 localhost (::1) 端口 3000 (#0) > POST /api/dashboards/import HTTP/1.1 > User-Agent: curl/7.29.0 > Host: localhost:3000 > Accept: / > Authorization: Bearer eyJrIjoiOWwwUmpQYUFmczhRVG1CZ1VmdTZ1VlZLTlc2b0xUb0QiLCJuIjoia2xhIiwiaWQiOjJ9 > Content-Type: application/json;接受:应用程序/json > 内容长度:144 > * 上传完全发送:144 个字节中的 144 个 我收到了上述消息 您可以查看任何服务器端日志吗? 【参考方案1】:

您正在尝试导入在 Grafana UI 中导出的仪表板 json /u04/my_metrics.json。您还必须使用 Grafana UI 进行导入 - https://grafana.com/docs/reference/export_import/。

如果要使用 Grafana 仪表板 API (https://grafana.com/docs/http_api/dashboard/),则需要从 Grafana API 中“导出”(正确的 API 名称为“获取仪表板”)。

Grafana UI 和 Grafana 仪表板 API 生成不同的仪表板 JSON 正文架构,因此您无法轻松互换它们。理论上您可以将 json 转换为正确的格式,但您需要熟悉使用的仪表板 JSON 正文模式。

【讨论】:

感谢您提供信息,作为初学者,我在从 api 获取仪表板时遇到困难,在文档中,它提到我需要发送请求,例如 'GET /api/dashboards/uid/cIBgcSjkk HTTP / 1.1'如何为此形成curl请求以及上面命令中的'cIBgcSjkk'是什么,如果我是对的,我认为我需要形成类似'curl -X GET --insecure -H'的请求“授权:承载eyJrIjoiOWwwUmpQYUFmczhRVG1CZ1VmdTZ1VlZLTlc2ba0xUb0QiLCJuIjoia2xhIi '' 能否请您协助如何形成 curl 命令来获取仪表板 api 我终于找到了解决方案,我从这个链接github.com/hagen1778/grafana-import-export使用了API导出器并使用curl命令导入仪表板 curl -XPOST -i localhost:3000/api/dashboards/db -u username:password --data-binary @/jsonfilepath/my.json -H "Content-Type: application/json"【参考方案2】:

请从 json 中删除 "id": 17。它将开始工作

【讨论】:

请解释为什么这样可以解决问题 这是使用我们正在做的API和创建时的后期操作。导入仪表板时,将在内部生成“id”。所以,我们不应该放它。【参考方案3】:

我需要使用配置了必要仪表板的 grafana 自动启动 docker 容器,因此我按照以下步骤操作:

首先,我配置仪表板,在我的案例中使用存储在 Prometheus,仪表板设计完成后,我使用导出 显示仪表板的 UID 的选项(它也存在于 网址)。

接下来,我使用 Postman 工具并调用 Grafana REST API 来获取 通过其 UID 以 JSON 格式显示仪表板的详细信息。你可以获得更多 following link 中的信息。

在我的例子中,我使用默认的用户名和密码(admin / admin)以及配置 Docker 容器时映射的端口 3000。

网址如下:

http://admin:admin@localhost:3000/api/dashboards/uid/lNCIQZdGk

如您所见,它以兼容的格式返回仪表板的元数据,以便通过 Grafana REST API 导入。

作为最后一步,有必要删除 JSON 的 “id”属性,以避免在导入时收到 404,然后继续使用该配置创建新仪表板,为其分配一个新身份证。

如您所见,删除该属性将创建一个新仪表板并为其分配 id 4。

使用类似于以下的 CURL 命令通过脚本自动创建仪表板会更合适:

curl --verbose -L -s --fail -H "Accept: application/json" -H "Content-Type: application/json" -X POST -k http://admin:admin@localhost:3000/api/dashboards/db --data @/etc/grafana/dashboards/kafka_exporter_overview.json

我与您分享我在创建 docker 容器期间用于配置仪表板和数据源的脚本:

GRAFANA_URL=$GRAFANA_URL:-http://$GF_SECURITY_ADMIN_USER:$GF_SECURITY_ADMIN_PASSWORD@localhost:3000
#GRAFANA_URL=http://grafana-plain.k8s.playground1.aws.ad.zopa.com
DATASOURCES_PATH=$DATASOURCES_PATH:-/etc/grafana/datasources
DASHBOARDS_PATH=$DASHBOARDS_PATH:-/etc/grafana/dashboards

# Generic function to call the Vault API
grafana_api() 
  local verb=$1
  local url=$2
  local params=$3
  local bodyfile=$4
  local response
  local cmd

  cmd="curl --verbose -L -s --fail -H \"Accept: application/json\" -H \"Content-Type: application/json\" -X $verb -k $GRAFANA_URL$url"
  [[ -n "$params" ]] && cmd="$cmd -d \"$params\""
  [[ -n "$bodyfile" ]] && cmd="$cmd --data @$bodyfile"
  echo "Running $cmd"
  eval $cmd || return 1
  return 0


wait_for_api() 
  while ! grafana_api GET /api/user/preferences
  do
    sleep 5
  done 


install_datasources() 
  local datasource

  for datasource in $DATASOURCES_PATH/*.json
  do
    if [[ -f "$datasource" ]]; then
      echo "Installing datasource $datasource"
      if grafana_api POST /api/datasources "" "$datasource"; then
        echo "installed ok"
      else
        echo "install failed"
      fi
    fi
  done


install_dashboards() 
  local dashboard

  for dashboard in $DASHBOARDS_PATH/*.json
  do
    if [[ -f "$dashboard" ]]; then
      echo "Installing dashboard $dashboard"

      if grafana_api POST /api/dashboards/db "" "$dashboard"; then
        echo "installed ok"
      else
        echo "install failed"
      fi

    fi
  done


configure_grafana() 
  wait_for_api
  install_datasources
  install_dashboards


echo "Running configure_grafana in the background..."
configure_grafana &
/run.sh
exit 0

希望这个答案可以帮助别人。

【讨论】:

以上是关于“消息”:使用 HTTP API 导入 grafana 现有 json 文件时出现“无法导入仪表板”错误的主要内容,如果未能解决你的问题,请参考以下文章

Gmail API 导入 - 错误 413 请求实体太大

使用 EWS API 发送电子邮件时控制消息编码

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

如何在 SOLR DIH HTTP API 中强制中止数据导入

如何将 swagger 导入带有环境的邮递员?

ASP.NET 4.5 Web API 2.0,JWT 消息处理程序将状态 0 返回到 Angular 7 HTTP 拦截器