
Generating reports with Grafana and scheduling their delivery
In this post we will see something very interesting, if we have some very cool Grafana Dashboards, where with personalized graphics we see at all times the situation of our platform, No? Imagine that to this, we add the possibility of generating a Dashboard report that interests us in PDF, and we scheduled your email to see yesterday's summary, from last week…
To the point, Come! We started with a utility that Izak Marais has developed and we have on GitHub (from Microsoft already, Who understands it…), Not bad, We have a service that will generate a PDF file of the Dashboard that interests us through PDFlatex, And then with SendEmail we will send the email totally personalized and with the reports that we want!
Requirements,
First things, on our Grafana machine, in my case it's a Centos 7.3 to which we will install the following requirements:
[SourceCode]Yum install TexLive-pdftex Texlive-latex-bin Texlive-Texconfig* Texlive-Latex* Texlive-Metafont* Texlive-CMAP* Texlive-EC TexLive-FNCYCHAP* TexLive-pdfTex-Def TexLive-FancyHDR* Texlive-TitleSEC* Texlive-Multirow Texlive-framed* Texlive-Wrapfig* Texlive-Parskip* Texlive-Caption Texlive-ifluatex* Texlive-collection-fontsRecommended Texlive-collection-Latex Recommended TexInfo-Tex[/SourceCode]
We download Go and install it:
[SourceCode]wget https://dl.google.com/go/go1.10.2.linux-amd64.tar.gz
tar -C /usr/local -xzf go1.10.2.linux-amd64.tar.gz
export PATH=$PATH:/usr/local/go/bin[/SourceCode]
And finally we download the Reporter that will allow us to do what we said, generate a PDF with Grafana Dashboards:
[SourceCode]Go get github.com/IzakMarais/reporter/…
go install -v github.com/IzakMarais/reporter/cmd/grafana-reporter[/SourceCode]
We end up opening the port in the firewall and reloading the configuration:
[SourceCode]firewall-cmd –zone=public –Permanent –add-port=8686/tcp
firewall-cmd –Reload[/SourceCode]
We will be able to try to run it directly to validate that everything works well, or use '–help’ to see some interesting parameter, such as connecting to a remote Grafana, etc… Run:
[SourceCode]/root/go/bin/grafana-reporter[/SourceCode]
Eye! Before you go any further, Dashboards must be created in Grafana v5 and not inherited from v4, Ok? If we have an old dashboard, so simple it will be to make a “Save As…” and we will have it turned into a version 5.
Generating a report manually,
Right now we could try to generate a report, We have a couple of options, one that would be to create a Report button in the Dashboard itself so that when someone wants to, download a PDF of what you are viewing. It would be as easy as in Grafana:
Within the Dashboard that interests us > We click on the nut or “Settings” > “Link” > “Add Dashboard Link” and we indicate the following data
- Type: Link, We left that guy marked.
- URL: HTTP://DIRECCION_IP_GRAFANA:8686/api/v5/report/{dashboardUID}
- Title: PDF Report
- Tooltip: Generate a PDF of this Dashboard
- Icon: Doc, to be an icon of a document.
- Time range: We mark it to allow you to put periods of time.
- Variable values: We marked it to play with the dates.
- Open in new tab: We mark it if we want the PDF to generate it in a new tab.
And nothing, we would see a button that when pressed will generate the report in PDF!
As we can guess, the URL to generate the PDF would be something like (obviously we can play with the margins of the dates or periods of the PDF):
[SourceCode]HTTP://DIRECCIÓN_IP_GRAFANA:8686/api/v5/report/ID-DASHBOARD?from=now-24h&to=now[/SourceCode]
Generating scheduled reports,
Come to the interesting stuff! First things, We must make the graphana-reporter start with the machine, the fastest, so that it starts on startup, we also add in Cron with 'crontab -e’ The following:
[SourceCode]@reboot /root/go/bin/grafana-reporter[/SourceCode]
If we want to send them by email automatically, We will install SendEmail:
[SourceCode]Yum Install SendEmail[/SourceCode]
We will create a script where we put the URLs of the Reports that we are interested in generating and download them with wget, and finally we sent them by email, I'll call him 'grafana_report_diario.sh' as an example:
[SourceCode]# VMware Servers Dashboard
wget -O diario_VMware.pdf http://DIRECCION_IP_GRAFANA:8686/api/v5/report/NRkUakSik?from=now-24h&to=now
# Citrix Server Dashboard
wget -o diario_Citrix.pdf http://DIRECCION_IP_GRAFANA:8686/api/v5/report/YpIJqYkik?from=now-24h&to=now
Sleep 40s
/usr/bin/sendemail -f of***@*****ra.com-t Pa**@*****ra.com -s mail.bujarra.com -m "Estimados señores,\nThe reports with the consumption of your datacenter are attached.nHave a very good day!" -u "Grafana – Daily Report" -Xu us*****@*****ra.com -xp XXXXXXXX -o tls=auto -a diario_VMware.pdf diario_Citrix.pdf[/SourceCode]
This script 'grafana_report_diario.sh’ It will be executed, for example, every day at 0 a.m. and as we can see, it will take fresh data and send it by email to our bosses, clients, IT department…
Result,
This would be an example of the PDF you are going to receive, of course, each one will have their own personalized Dashboard, Well, let it serve as an example!
Don't tell me no!!! Impressive!!!
I hope you have found it interesting and you can always contribute something new and positive in your work!!