Matomo2GrafanaReport is a Python script that generates a Grafana dashboard with a specified time range and variables, exports it to a PDF, and sends it via email with an attached PNG image. It automates the process of creating and sharing Grafana dashboards for different websites.
The script uses the pyppeteer
library to interact with Grafana, requests
to handle API requests, and smtplib
to send emails. The main steps are:
- Parse input arguments and load configurations from
config.ini
andsites.ini
. - Load the template JSON file and modify it with new variables and time range.
- Get or create a folder ID in Grafana.
- Delete the existing dashboard if it exists in the specified folder.
- Create a new dashboard with the modified template.
- Print the dashboard to PDF and convert it to PNG.
- Upload the PNG image to Jirafeau and get a public link.
- Send an HTML email with the image link and the PDF file attached.
- Python 3.x
pyppeteer
libraryrequests
librarysmtplib
librarypdf2image
library (for converting PDF to PNG)- Access to a Grafana instance with a Matomo datasource
- A Grafana API key with necessary permissions
- An SMTP server for sending emails
- A Jirafeau instance for uploading PNG images (optional)
- Clone the repository:
git clone https://github.com/your_username/Matomo2GrafanaReport.git
- Install required packages:
pip install -r requirements.txt
- Update the
config.ini
andsites.ini
files with your specific configurations. - Run the script with the desired month and year:
python3 report_generator.py --month MM-YYYY
The script uses two configuration files:
[Grafana]
url =
api_key =
user =
password =
[Paths]
template_json = template.json
[Settings]
timezone = Europe/Paris
[Jirafeau]
url =
[SMTP]
server = mail.infomaniak.com
port = 587
login =
password =
from =
[Email]
subject_template =
[Site1]
name = example1.com
email = example1@example.com
[Site2]
name = example2.com
email = example2@example.com
- Ensure the provided API key has necessary permissions.
- Check Grafana URL and version for compatibility.
- Add Matomo datasource with name
MATOMO
with this configuration :- Datasource type :
JSON API
- URL :
https://<your_matomo_url>/index.php
- Query string :
token_auth=<your_matomo_api_key>&module=API&format=json
- Datasource type :
- Ensure
pdf2image
library is installed and updated. - Adjust viewport size and waiting time in
print_to_pdf
function.
- Verify SMTP server, port, login, and password.
- Check email format and attachment for validity.
- Ensure the provided Jirafeau URL is correct and accessible.
- Adjust the upload function to handle different file formats or sizes.