3

I am trying to import to grafana existing json file using HTTP API, it's reading the json file successfully but for each and every line, it's throwing command not found error.

I tried to execute the below command,

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

It's reading each and every line in json file but throwing error

"message":"Failed to import dashboard"

{
  "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_load1{instance=~\"$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_info{job=~\"$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_info{nodename=\"$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_info{instance=~\"$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
}

Kindly review the json file once and assist me to resolve this issue

sivanesan1
  • 779
  • 4
  • 20
  • 44
  • Change `curl` to `curl -v` to get verbose output and attached pastebin of the complete response – Tarun Lalwani Jul 08 '19 at 06:07
  • @TarunLalwani: * About to connect() to localhost port 3000 (#0) * Trying ::1... * Connected to localhost (::1) port 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;Accept:application/json > Content-Length: 144 > – sivanesan1 Jul 08 '19 at 06:14
  • * upload completely sent off: 144 out of 144 bytes < HTTP/1.1 500 Internal Server Error < Cache-Control: no-cache < Content-Type: application/json < Expires: -1 < Pragma: no-cache < X-Frame-Options: deny < X-Xss-Protection: 1; mode=block < Date: Mon, 08 Jul 2019 10:54:41 GMT < Content-Length: 40 < * Connection #0 to host localhost left intact {"message":"Failed to import dashboard"} – sivanesan1 Jul 08 '19 at 06:14
  • i am getting the above message – sivanesan1 Jul 08 '19 at 06:15
  • Any server side logs you can check? – Tarun Lalwani Jul 08 '19 at 06:34
  • I am getting doubt on this line "Xss-Protection: 1; mode=block" , do you have any idea how to disable Xss-Protection in curl command itself – sivanesan1 Jul 08 '19 at 07:11
  • Logs are not updated in server side for the curl command – sivanesan1 Jul 08 '19 at 07:14
  • Can you also add the content of the `u04/my_metrics.json` file as well? – Tarun Lalwani Jul 08 '19 at 07:34

3 Answers3

4

You are trying to import dashboard json /u04/my_metrics.json, which was exported in the Grafana UI. You have to use Grafana UI also for import - https://grafana.com/docs/reference/export_import/.

If you want to use Grafana dashboard API (https://grafana.com/docs/http_api/dashboard/), then you need to "export" (correct API name is "get dashboard") from Grafana API.

Grafana UI and Grafana dashboard API produce different dashboards JSON body schema, so you can't interchange them easily. In theory you convert json to correct format, but you need to be familiar with used dashboard JSON body schema.

Jan Garaj
  • 25,598
  • 3
  • 38
  • 59
  • Thanks for the information , As a beginner i am facing difficulties to get the dashboard from api , In the documentation , it mentioned like i need to send request like 'GET /api/dashboards/uid/cIBgcSjkk HTTP/1.1' how to form curl request for this and what is 'cIBgcSjkk' in above command, if i am right i think i need to form the request something like 'curl -X GET --insecure -H "Authorization: Bearer eyJrIjoiOWwwUmpQYUFmczhRVG1CZ1VmdTZ1VlZLTlc2ba0xUb0QiLCJuIjoia2xhIi'' – sivanesan1 Jul 12 '19 at 07:05
  • Can you please assist how to form curl command to get the dashboard api – sivanesan1 Jul 12 '19 at 07:12
  • Finally i found the solution , i have used API exporter from this link https://github.com/hagen1778/grafana-import-export and used curl command to import the dashboard – sivanesan1 Jul 15 '19 at 07:39
  • curl -XPOST -i http://localhost:3000/api/dashboards/db -u username:password --data-binary @/jsonfilepath/my.json -H "Content-Type: application/json" – sivanesan1 Jul 15 '19 at 07:41
0

Please remove the "id": 17 from json. It will start working

0

I need to automate the startup of the docker container with grafana configured with the necessary dashboards, so I follow this steps:

  • First, I configure the dashboard, using in my case metrics stored in Prometheus, once the dashboard design is finished I use the export option to display the UID of the dashboard (it is also present in URL).

enter image description here

  • Next, I use the Postman tool and call the Grafana REST API to get the detail in JSON format of a dasboard through its UID. You can get more information in the following link.

In my case I use the default username and password (admin / admin) and the port 3000 mapped when configuring the Docker container.

The URL is as follows:

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

As you can see, it returns the metadata of the dashboard with a compatible format for import through the Grafana REST API.

enter image description here

As a last step, it will be necessary to delete the "id" property of the JSON to avoid receiving a 404 when importing and proceed to create a new dashboard with that configuration, assigning it a new id.

enter image description here

enter image description here

As you can see, deleting that property will create a new dashboard and assign it the id 4.

enter image description here

It would be more appropriate to use a CURL command similar to the following to automate the creation of the dashboard through a script:

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

I share with you the script that I use to configure the dashboard and datasource during the creation of the docker container:

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

Hope this answer can help someone.

Sergio Sánchez Sánchez
  • 1,694
  • 3
  • 28
  • 48