There exists a python package for this exact usage grafana_alerts
. You want to get Grafana alerts onto your python script using some sort of API.
Installation: pip install grafana_alerts
You'll have to create a config file
/etc/grafana_alerts/grafana_alerts.cfg
#
# Grafana alerts configuration file.
#
# The URL where grafana server is listening. It must finish with the character '/' (default value: http://localhost:3130) grafana_url =
http://yourgrafanaserver.com/grafana/
# Grafana token with viewer access (default value: empty string) grafana_token =
qwertysDssdsfsfsdfSFsfsfEWrwrwERwrewrwrWeRwRwerWRwERwerWRwerweRwrEWrWErwerWeRwRwrewerr==
# email to use as alert sender (default value: grafana-alert@localhost) email_from = alert@example.com
# smtp server to use (default value: localhost) smtp_server = localhost
# smtp server host to use (default value: 25)
# if port is not 25, starts a tls session. smtp_port = 25
# smtp server username to use if it is needed. Optional. Leave it commented if not used. (default value: no username)
#smtp_username = my_smtp_username
# smtp server password to use if it is needed. Optional. Leave it commented if not used. (default value: no password)
#smtp_password = my_smtp_password
Add the task to execute it for example each 3 minutes:
*/3 * * * * grafana_alerts
Read more about the package and repository.