Programmatically saving the resulting JSON from scripted dashboard execution

I have been experimenting with Grafana scripted dashboards using the example scripted.js and have managed to create a script that takes a device name from a URL argument, queries graphite for the list of available metrics, and generates a dashboard for that device. It works well. Now I want to automate calls to my modified scripted.js so I can programmatically build many dashboards and store the resulting JSON for each one in individual files in /opt/grafana/public/dashboards/static which will the show up in the search list because I configured the following in my grafana ini file:

[dashboards.json]
enabled = true
path = /opt/grafana/public/dashboards/static

I have managed to do this manually using the Gear Menu -> View JSON -> Save As, copy the file to the static dir, etc., and it works well.

The problem that I have run into is that I cannot figure out how to automate calls to the scripted.js program. If I do something like this…

curl GET "http://grafana/dashboard/script/myscrtiped.js?device=m2g.myrouter.*"

The response is not the JSON dashboard for the device named m2g.myrouter, but is instead my Grafana server’s main home page. I suspect that this is happening because curl does not execute the javascript, it just makes one GET against that page and prints whatever it gets back.

How can I make a call to myscripted.js and get the “Gear Menu -> View JSON” output?
Do I need to do use node.js to talk to http://grafana/dashboard/script/myscrtiped.js?device=m2g.myrouter.* and scrape the resulting dashboard JSON?

It feels like I’m making this much more difficult than it should be.

I found this old issue post…
https://github.com/grafana/grafana/issues/991
…where Torkelo talks about some dasboardSrv and datasourceSrv services and also references an example called scripted_gen_and_save.js. I was able to locate the scripted_gen_and_save.js example but executing it under the current version of Grafana generates the following error:
TypeError: services.datasourceSrv.getGrafanaDB is not a function

I’m not sure if scripted_gen_and_save.js would have done what I wanted, I suspect it would have saved the JSON dashboard into the Grafana sqlite database whereas what I want to do is just take the dashboard JSON, save it in a file, and put it in the directory specified by the [dashboards.json] config section I mentioned above.

Thank you for your time and awesome software.

Grafana does not support this any more.

If you want to build dashboards programmatically we recommend doing that in a tool outside Grafana. Then you can use a tool like https://github.com/utkarshcmu/wizzy to import them (or do that manully using the HTTP api)

Wondering if Grafana should facilitate something like this. I’m using Grafana along with Prometheus in a microservices oriented company.

We do Prometheus hierarchical federation where each team has its own prometheus + grafana. The top prometheus only fetches a subset of metrics for each service and I’d like the grafana instance to automatically create a standar dashbord based on the “available” jobs. This is very similar to templating in a way, and cannot see why should be done adding another tool i.e. another moving part.

The end goal is to have a behavior similar to New Relic… where you just push your data thru and a Templated Dashboard renders the data.

Any chance this could be re-integrated?

1 Like

Hello ggargiulo,

I am looking for solution for the same configuration (Prometheus in a microservices oriented company) , could you share your experience and ,maybe, also share your javascript ?

Thanks in advance.

I wrote a quick and dirty dashboard generator for node.js. It builds off of scripted.js and is not very pretty because I’m not particularly proficient at JS. But it works and may provide you with a decent starting point. I put the script and some instructions for getting going with it here:

I should also mention the new jsonnet library for Grafana that is being worked at the moment:

Yes, it is possible. https://github.com/raselashraf21/Grafana-Dashboard