As currently described, this approach does not work. The reason for that is that creating a snapshot via the API does not actually populate any data, as described in e.g. https://community.grafana.com/t/snapshot-using-http-api-does-nothing/. An alternative approach which does work described in https://gist.github.com/svet-b/1ad0656cd3ce0e1a633e16eb20f66425.
Packages:
- The
jq
JSON processing command line tool, which is available for most distributions (https://stedolan.github.io/jq/,sudo apt install jq
) - NodeJS, and the
puppeteer
package (npm install puppeteer
), which is used to run headless Chrome
Scripts:
- The
pdf.js
file attached here, which carries out the PDF conversion
Also, create Grafana API key from web UI (http://docs.grafana.org/http_api/auth/ has some outdated instructions but the idea holds). Save it in an environment variable, along with your Grafana instance URL
export APIKEY=eyJrIjoiMFdmMnA3YlRZTFF5bktqUmhRRk9nUzBISEo0dkZMNW0iLCJuIjoicGRmIiwiaWQiOjF9
export GURL=http://localhost:3000
First we need to take a snapshot of the dashboard to be exported. We can do this via the API, per http://docs.grafana.org/http_api/snapshot/, but in order to request this we actually need the full dashboard JSON. This can be obtained via another API call, based on the dashboard ID - here saved as dashboard.json
:
export DASHID=x3g4Wx5ik
curl -X GET -H "Authorization: Bearer $APIKEY" $GURL/api/dashboards/uid/$DASHID -o dashboard.json
Now create the snapshot via a call to the respective API endpoint. Note that here we directly submit the dashboard JSON of which we'd like a snapshot.
curl -X POST -H "Authorization: Bearer $APIKEY" -H "Content-Type: application/json" -d @dashboard.json $GURL/api/snapshots -o snapshot.json
The result is saved in snapshot.json
. From this we can extract the URL of the generated snapshot.
export SNAPURL=`jq .url snapshot.json`
Now export to PDF with
node pdf.js $SNAPURL output.pdf
Hi, Does this pdf export includes all the panels within the dashboard or the list of dashboards that is parsed?