Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

script to export data from Grafana dashboard to csv file

Need to have script to export data from Grafana dashboard to csv file. Input: (dashboard slug/name and time frame like: -1h or -24h) any link to grafana api/doc should be fine.

like image 537
pythonhmmm Avatar asked Nov 17 '16 06:11

pythonhmmm


1 Answers

Well, 3yr old question, but I've actually done a whole bunch of exactly this to yield some reporting on dashboards in our grafana. You can use whatever you want (including bash to pull dashboard data out based on the UID and you can certainly search for the slugs, but the API pulls out all of its information in JSON like below:

DASH:

{
  "dashboard": {
    "id": 1,
    "uid": "cIBgcSjkk",
    "title": "Production Overview",
    "tags": [
      "templated"
    ],
    "timezone": "browser",
    "schemaVersion": 16,
    "version": 0
  },
  "meta": {
    "isStarred": false,
    "url": "/d/cIBgcSjkk/production-overview"
  }
}

This code can then be piped through jq for your reporting. You can pull any variable through simplistic pathing of the dashboard json with option to use loops and lots of other features.

JQ:

$ curl -s https://grafana.local/api/dashboards/uid/cIBgcSjkk \
  | jq -r '.dashboard |[ .uid, .title, .version ]| @csv'
"cIBgcSjkk","Production Overview",0

Refs:

  • https://grafana.com/docs/grafana/latest/http_api/dashboard/
  • https://stedolan.github.io/jq/manual/#Formatstringsandescaping
like image 59
hikerspath Avatar answered Sep 29 '22 19:09

hikerspath