Skip to content

Commit 1b5c8e2

Browse files
authored
Merge pull request #205 from mt3593/only-save-alert-rules-if-grafana-supports-it
Only support alert rules if grafana is above version 9.4.0
2 parents f7dfd70 + b4fce12 commit 1b5c8e2

File tree

5 files changed

+33
-9
lines changed

5 files changed

+33
-9
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ The aim of this tool is to:
1616
* Dashboard (contains Alert)
1717
* Datasource
1818
* Alert Channel
19+
* Alert Rules (Supported in version 9.4.0 of grafana and up.)
1920
* Teams
2021
* Team Members (Needs Basic Authentication (username and password, see [grafana doc](https://grafana.com/docs/grafana/latest/http_api/org/#admin-organizations-api))
2122
* You need to set `Admin's account and password` in `grafanaSettings.json`, or set the base64 encoded `admin account and password` in ENV `GRAFANA_BASIC_AUTH`. E.g `export GRAFANA_BASIC_AUTH=YWRtaW46YWRtaW4=`

grafana_backup/create_alert_rule.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import json
2-
from grafana_backup.dashboardApi import create_alert_rule
2+
from grafana_backup.dashboardApi import create_alert_rule, get_grafana_version
3+
from packaging import version
34

45

56
def main(args, settings, file_path):
@@ -12,6 +13,12 @@ def main(args, settings, file_path):
1213
with open(file_path, 'r') as f:
1314
data = f.read()
1415

15-
alert_rule = json.loads(data)
16-
result = create_alert_rule(json.dumps(alert_rule), grafana_url, http_post_headers, verify_ssl, client_cert, debug)
17-
print("create alert rule: {0}, status: {1}, msg: {2}".format(alert_rule['title'], result[0], result[1]))
16+
grafana_version = get_grafana_version(grafana_url)
17+
minimum_version = version.parse('9.4.0')
18+
19+
if minimum_version <= grafana_version:
20+
alert_rule = json.loads(data)
21+
result = create_alert_rule(json.dumps(alert_rule), grafana_url, http_post_headers, verify_ssl, client_cert, debug)
22+
print("create alert rule: {0}, status: {1}, msg: {2}".format(alert_rule['title'], result[0], result[1]))
23+
else:
24+
print("Unable to create alert rules, requires Grafana version {0} or above. Current version is {1}".format(minimum_version, grafana_version))

grafana_backup/dashboardApi.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import requests
44
import sys
55
from grafana_backup.commons import log_response, to_python2_and_3_compatible_string
6+
from packaging import version
67

78

89
def health_check(grafana_url, http_get_headers, verify_ssl, client_cert, debug):
@@ -433,6 +434,12 @@ def add_user_to_org(org_id, payload, grafana_url, http_post_headers, verify_ssl,
433434
return send_grafana_post('{0}/api/orgs/{1}/users'.format(grafana_url, org_id), payload, http_post_headers, verify_ssl, client_cert,
434435
debug)
435436

437+
def get_grafana_version(grafana_url):
438+
r = requests.get('{0}/api/health'.format(grafana_url))
439+
if r.status_code == 200:
440+
return version.parse(r.json()['version'])
441+
else:
442+
raise Exception("Unable to get version, returned response: {0}".format(r.status_code))
436443

437444
def send_grafana_get(url, http_get_headers, verify_ssl, client_cert, debug):
438445
r = requests.get(url, headers=http_get_headers, verify=verify_ssl, cert=client_cert)

grafana_backup/save_alert_rules.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import os
2-
from grafana_backup.dashboardApi import search_alert_rules, get_alert_rule
2+
from grafana_backup.dashboardApi import search_alert_rules, get_alert_rule, get_grafana_version
33
from grafana_backup.commons import to_python2_and_3_compatible_string, print_horizontal_line, save_json
4+
from packaging import version
45

56

67
def main(args, settings):
@@ -15,10 +16,17 @@ def main(args, settings):
1516
folder_path = '{0}/alert_rules/{1}'.format(backup_dir, timestamp)
1617
log_file = 'alert_rules_{0}.txt'.format(timestamp)
1718

18-
if not os.path.exists(folder_path):
19-
os.makedirs(folder_path)
19+
grafana_version = get_grafana_version(grafana_url)
20+
minimum_version = version.parse('9.4.0')
2021

21-
save_alert_rules(folder_path, log_file, grafana_url, http_get_headers, verify_ssl, client_cert, debug, pretty_print)
22+
if minimum_version <= grafana_version:
23+
24+
if not os.path.exists(folder_path):
25+
os.makedirs(folder_path)
26+
27+
save_alert_rules(folder_path, log_file, grafana_url, http_get_headers, verify_ssl, client_cert, debug, pretty_print)
28+
else:
29+
print("Unable to save alert rules, requires Grafana version {0} or above. Current version is {1}".format(minimum_version, grafana_version))
2230

2331

2432
def get_all_alert_rules_in_grafana(grafana_url, http_get_headers, verify_ssl, client_cert, debug):

setup.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
'boto3',
1111
'azure-storage-blob',
1212
'google-cloud-storage',
13-
'influxdb'
13+
'influxdb',
14+
'packaging'
1415
]
1516

1617
setup(

0 commit comments

Comments
 (0)