NGalert / Grafana 8 alert feature: How to export / import alerts as yml/json?

Yes this sounds like a good solution. But I’ll keep you updated if I find out anything…
And for me this is a must… Hope they will add this in a few versions.

I have the same issue. Any update…?

2 Likes

Provisioning alerts is also a must for our team.

Bumping this issue :slight_smile:

2 Likes

bumping too cos export reimport is a must

+1 export/import of all alerts/dashboards would be much appreciated.

I wrote a small javascript function to export all the alert info I need by utilizing the Grafana API. One should be able to write a similar API-based import facility for a full backup and restore cycle.
https://community.grafana.com/t/grafana-8-how-to-import-export-alerts-in-unified-alert-section/50431/11?u=fedorg

3 Likes

This feature is a must to deploy over multiple stages or production instances. In my case we have multiple kubernetes cluster for production use. And the import feature using a configmap is great. I wish to have this for unified alerts as well.

2 Likes

Hi an export of the alerts is definitely possible, using the API:
curl -X GET -H “Authorization: Bearer TOKEN” -H “Content-type: application/json” ‘http://IP:3000/api/ruler/grafana/api/v1/rules/{Namespace}’ | jq > alerts.json
This outputs a json file with all alerts from the given namespace.
The Problem is importing this data to another grafana instance,

curl -X POST -H “Authorization: Bearer TOKEN” -H “Content-type: application/json” -d “$(cat alerts.json)” ‘http://IP2/api/ruler/grafana/api/v1/rules/{namespace}

outputs the error {“message”:“rule group name is not valid”}.

Does anyone know a solution?

3 Likes

+1 definitely need import/export of alerting rules to be able to deploy them across various stages

+1 on this and also wondering if there is an update. Since the alerts are still linked and stored in the dashboard jsons (at least before v8, as far as I am aware) but don’t show up in the new alert rules, it seems related to this GitHub issue when upgrading from v8.2 to v8.3. I’m hoping the fix for this will also involve a separate export possibility for alert rules.

This is much needed, there must be a way to import/export alerts to make it easy to provision alerts.

2 Likes

+1 export/import of all alerts/dashboards would be much appreciated.

+1 export/import feature would be very helpful

Any alternative of export alert rules to grafana ?

+1. Incredibly necessary feature.

+1, we are stuck on upgrading our OSS Grafana instance without this feature

Hi @andi20002000 , this is due to the output from GET includes the folder name:
{ "<folder name>": [ { <alert rule object> } ] }

As soon as I removed the folder part I managed to create the alert rule using curl POST:
{ <alert rule object> }

I haven’t managed to post more than one alert rule at a time yet tho.

Inspired by @andi20002000 we came out with a bit more automated solution.

Makefile

check-grafana-token:
ifndef GRAFANA_TOKEN
	$(error GRAFANA_TOKEN is required)
endif

download-grafana-alerts: check-grafana-token
	curl -X GET \
		  -H "Authorization: Bearer ${GRAFANA_TOKEN}" \
	     'https://example.com/api/ruler/grafana/api/v1/rules' \
	     | jq > './grafana/alerts/alerts.json'

upload-grafana-alerts: check-grafana-token
	./upload-grafana-alerts.sh

upload-grafana-alerts.sh

#!/bin/sh

ALERTS_JSON_PATH=./grafana/alerts/alerts.json
NUMBER_OF_ALERTS=$(jq -c '.["folder-name"] | length' ${ALERTS_JSON_PATH})

for ((i=0; i<NUMBER_OF_ALERTS; i++)); do
  ALERT_OBJECT=$(jq -c --arg i "$i" '.["folder-name"][($i | tonumber)] | del(.rules[0].grafana_alert.uid)' ${ALERTS_JSON_PATH})
  ALERT_NAME=$(jq -c --arg i "$i" '.["folder-name"][($i | tonumber)].name' ${ALERTS_JSON_PATH})
  echo "Creating ${ALERT_NAME}...\n"
	curl -X POST \
		-H "Authorization: Bearer ${GRAFANA_TOKEN}" \
		-H "Content-type: application/json" \
		'https://example.com/api/ruler/grafana/api/v1/rules/folder-name' \
		-d "${ALERT_OBJECT}"
	echo "\n"
done
3 Likes

+1 , Must have feature! Stuck to 8.2.1 release until we can manage alarms as code in a json file, as we did before with 8.2.1

hi guyz, make a little script for export alerts from dashboard, try it

1 Like