Grafana: How to export graphs as images and email them

Grafana is one of the most used dashboarding and alerting tools, it has many features but recently i noticed that lacked the capability to…

Grafana: How to export graphs as images and email them
Photo by Markus Winkler on Unsplash

Grafana is one of the most used dashboarding and alerting tools, it has many features but recently i noticed that lacked the capability to send graphs as emails in a scheduled manner.

Fortunately enough Grafana allows exporting graphs as PNG images using its API, But first you need to create an API key, to get one go to

  • Configuration > API keys

Then click the “Add API Key” button

Enter a name for the key, a long time to live value and as role select “Admin”, Then you should see something like this confirming that you created the key

The generic syntax to authenticate vs a Grafana server is the followingcurl -H <key> http://<grafana_server>/<url>

Now that we have the authorization key the next step is to go to a graph that we want to have it in a scheduled fashion and go to its “share” menu

The following menu will open, from there click on the “Direct link rendered image”

A new browser tab will open with the graph as a PNG image, the url of this tab will be used to automatically generate exports of our graphhttp://<grafana_server>/render/d-solo/uzieuOY7z/servers-statistics?orgId=1&from=1648451215600&to=1648472815600&panelId=5&width=1000&height=500&tz=Europe%2FAthens

From this url the important parameters are

  • from: a value in epoch milliseconds that define the start of the graph
  • to: a value in epoch milliseconds that define the end of the graph

Now we need to glue some bash scripting magic and all the Grafana things we did so far, create a file grafana_export.sh and enter the following.

Of course you need to adjust the following to your environment / needs

The script works in a very simple manner

  • START_REPORT_DATE and END_REPORT_DATE get the current date and we append the fixed hours of 07:00:00 for start time and 10:10:00 for end time.
  • The START_REPORT_DATE_EPOCH and END_REPORT_DATE_EPOCH variables have the same timestamps in an epoch milliseconds format that Grafana understands.
  • the url replaces the from and to values with the previous generated values
  • the out variable holds the filename that we want to save the graph
  • curl does the request to Grafana server and saves the graph

Next step is to send the graph attached in an email, for such jobs i prefer tha mailx toolecho "The email body $(date)" | mailx -r <from> -s "A subject $(date)" -a $out <to> && sleep 10

  • echo : Writes the body of the email along with the current timestamp
  • mailx: The tool
  • -r : who sends the email
  • -s: A subject for the email and a timestamp
  • -a $out : -a is an option to add an attachment, $out holds the graph we generated
  • <to> : recipients list
  • && sleep 10 : Well this is not mandatory, but in my case was needed because mailx exited without having time to sent the email with the attachment

I hope you found this article useful and make your life easier :)

Join Medium with my referral link - Konstantinos Patronas
As a Medium member, a portion of your membership fee goes to writers you read, and you get full access to every story…