hagen1778 / grafana-import-export

shell scripts for importing and exporting Grafana's dashboards and datasources
158 stars 62 forks source link

Dashboard not found error on import #10

Open DerfOh opened 5 years ago

DerfOh commented 5 years ago

Error received

curl -XPOST -i http://localhost:3000/api/dashboards/db -u xxxx:xxxxx --data-binary @./desktop.json -H "Content-Type: application/json"
HTTP/1.1 100 Continue

HTTP/1.1 404 Not Found
Content-Type: application/json
Date: Mon, 15 Apr 2019 15:57:45 GMT
Content-Length: 54''
{“message”:“Dashboard not found”,“status”:“not-found”}

Root Cause:

File export includes the 'id' of the dashboard. This needs to be set to 'null' in order to be imported.

image

Also see: https://community.grafana.com/t/unable-to-create-grafana-dashboard-through-api-but-working-through-ui-import-option-with-same-json-file/2470

Workaround:

Execute the following in the dashboards directory (works with GNU sed only)

sed -i '0,/"id": .*/{s/"id": .*/"id": null,/}' *.json
180254 commented 3 years ago

In my experience, top-level "id" property may also not exist at all. To change only the top-level id better use jq: (depending on the file structure)

jq '.id = null' some.json
jq '.dashboard.id = null' some.json

To wrap a dashboard, exported using a Grafana UI, in a structure that allows you to create/update it via the Grafana API, I am using this script:

for GRAFANA_DASHBOARD_FILE in *.json; do

  jq '{dashboard: ., folderId:0, overwrite:true} | .dashboard.id = null' "${GRAFANA_DASHBOARD_FILE}" >"update-dashboards.json.tmp

  curl "https://${GRAFANA_HOSTNAME}/api/dashboards/db" \
    -u "${GRAFANA_CURL_CREDENTIALS}" \
    -H "Content-Type: application/json" \
    --data-binary "@update-dashboards.json.tmp"

  echo ""
done
bijwaard commented 2 years ago

Looks like uid changed a bit over time. Currently with grafana 8.3.6, I had to make the following change in exporter.sh:

    for dash in $(fetch_fields $KEY 'search?query=&' 'url'); do
        #DB=$(echo ${dash}|sed 's,db/,,g').json
        DB=${dash##*/}.json
        base=${dash%/*}
        uid=${base##*/}
        echo $DB
        curl -f -k -H "Authorization: Bearer ${KEY}" "${HOST}/api/dashboards/uid/${uid}" | jq 'del(.overwrite,.dashboard.version,.meta.created,.meta.createdBy,.meta.updated,.meta.updatedBy,.meta.expires,.meta.version)' > "$DIR/dashboards/$DB"
    done
max420max commented 8 months ago

I tried "id": null but does not work for me , kindly help

bijwaard commented 8 months ago

Haven't tried this in a long while, what error message do you get, if any?

max420max commented 8 months ago

Haven't tried this in a long while, what error message do you get, if any?

Dashboard is created but empty data..

max420max commented 8 months ago

POST : https://XXXXXXXXXx/api/dashboards/import

{ "dashboard": { "id": null, "uid": "Polaris_123xx", "title": "pol_testt", "tags": [ "templated" ], "timezone": "browser", "refresh": "25s" },

... .. }, "time": { "from": "now-5m", "to": "now" }, "timepicker": {}, "timezone": "", "title": "Polaris_123", "uid": "Polaris_123xx", "version": 1, "weekStart": "", "overwrite": true

}

bijwaard commented 8 months ago

could have to do with the recent changes to metric names in grafana > 9.x, developers tried to anonymize them, had to edit panel JSON to fix back to readable metric names.

max420max commented 8 months ago

Hi,

I am using Grafana v10.1.2

I can change in matrix name ,

this is my updated template , i exported template from Grafana GUI , now i want exported template load through HTTP API([api/dashboards/import) but when i load this template and login into grafana GUI i can see just dashbaord name created and rest is empty.

bijwaard commented 8 months ago

quite hard to diagnose for me, sorry, hope someone else can help you.