Hi, first of all I should mention that I’m not very knowledgeable about programming, graphana, or APIs. I have searched this and other forums about this specific topic and I have come across unclear or incomplete answers, sorry in advance if this is already overexplained.
I want to automate and program in Python a way to get images from grafana panels in a systematic way. For this I would like to be able to, within the code, specify different time gaps of the data in the graphs, different aggregates applied on my data and different choices of variables to plot in each panel.
From what I read it seems that through the API it is not possible to download an image of the graph itself, but it is necessary to use the “Grafana image renderer” plugin. Is this so?
Is all this possible? I understand that it is necessary for the admin of my dashboards to create an API key, in addition to having the URLs of the specific panels at hand.
TL;DR you probably can but wouldn’t something like matplotlib be better in your case? I guess most datasources would provide some kind of query API without the need of Grafana in between.
from what I’ve read (and understood) you need a couple of things:
1. Dashboard creation
You’ve said that you’d like:
For this I would like to be able to, within the code, specify different time gaps of the data in the graphs, different aggregates applied on my data
If you want them to be static the whole time (e.g. today and in next week), you can create just one dashboard and use it all the time but I get that you want the data to be dynamic? If so, you can do that in two ways:
Create the dashboard as part of your script - that’s a bit hard considering you have to get to know Grafana’s dashboard json (it’s not hard but can be pain in your derriere at the beginning). Advantage - you can use image renderer.
Use Explore view - Grafana offers an Explore view, where you can just type in a query (it’s limited in visualization options though). Advantage - no JSON manipulation.
2. Variables in dashboard
and different choices of variables to plot in each panel.
This is the easiest part, as variables can be passed in URL (Grafana does it itself), if you mean of course Grafana’s built-in variables.
3. Screenshots
Grafana image renderer is a plugin that can work either inside Grafana or as a standalone service. I’m not sure it would fit your case though - you could of course make a screenshot and save it somewhere and then download it by script but I’m 90% sure you cannot get the screenshot directly from image renderer. Wouldn’t something like selenium have screenshot feature?
So… basically it is possible. But there are some obstacles you might get into, so I would think about something like matplotlib that would be only pulling the data from the datasource and transform it accordingly. UNLESS of course everything is already ready in Grafana. Then, I’d go with screenshotting panels in selenium, wouldn’t get into image renderer.
Hey, thanks for the detailed answer, I will try to answer it in parts.
you probably can but wouldn’t something like matplotlib be better in your case? I guess most datasources would provide some kind of query API without the need of Grafana in between.
I did all this first as you say here. With matplotlib taking data directly from the datasource. I was told that it was preferable to get graphs as seen in the Grafana dashboard, unfortunately.
Dashboard creation
and
Variables in dashboard
The dashboards are already done. They are dynamic (if I understand well what this means), the data received in these are refreshed from time to time. There are three of them, and in each of them there is a panel that interests me. These panels are line graphs, which have different equipment as variables (again, if I understand what you mean by variables: different sets of data to be plotted that mean the same thing but come from different sensors). In this sense, each panel has two variables to select, is that understood?
It is a bit difficult for me to express myself in English.
Screenshots
Too bad, I put my faith in that plugin to solve my problem.
I tried Selenium and felt that I was overcomplicating the process (thinking, again, that the solution was the API or Plugin). It seems to be the only solution I have left.
From what I understand then, with the API there is zero chance of getting a jpg/png file from a specific panel?