Skip to content

Commit 48e96d7

Browse files
authored
Merge pull request #238 from ysde/feature/add_support_for_contact_point_and_notification
add contact points and notifcation policy backup functionalities
2 parents 5e12194 + 3d29210 commit 48e96d7

14 files changed

+351
-56
lines changed

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,17 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7+
# [1.4.0] - 2023-09-20
8+
9+
### Added
10+
- add contact points and notifcation policy backup functionalities by @ysde in #238
11+
- added http headers to get_grafana_version request by @Mar8x in #239
12+
13+
### Changed
14+
- added py3-packaging to slim docker image, reported by @tasiotas in #241 :tada:
15+
16+
### Removed
17+
718
# [1.3.3] - 2023-07-27
819

920
### Added

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ There are three ways to setup the configuration:
4747
3. Use `~/.grafana-backup.json` to define variables in json format.
4848

4949
### Example Config
50-
* Check out the [examples](examples) folder for more configuration details
50+
* Copy [grafanaSettings.example.json](examples/grafanaSettings.example.json) and modify it for you to use, remove `azure`, `aws`, `gcp`, `influxdb` blocks (but keep the ones you used).
51+
* Check out the [examples](examples) folder for more configuration details.
5152

5253
**NOTE** If you use `environment variables`, you need to add the following to your `.bashrc` or execute once before using the tool (please change variables according to your setup):
5354

grafana_backup/api_checks.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from grafana_backup.commons import print_horizontal_line
2-
from grafana_backup.dashboardApi import health_check, auth_check, uid_feature_check, paging_feature_check
2+
from grafana_backup.dashboardApi import health_check, auth_check, uid_feature_check, paging_feature_check, contact_point_check
33

44

55
def main(settings):
@@ -12,30 +12,37 @@ def main(settings):
1212
api_auth_check = settings.get('API_AUTH_CHECK')
1313

1414
if api_health_check:
15-
(status, json_resp) = health_check(grafana_url, http_get_headers, verify_ssl, client_cert, debug)
15+
(status, json_resp) = health_check(grafana_url,
16+
http_get_headers, verify_ssl, client_cert, debug)
1617
if not status == 200:
1718
return (status, json_resp, None, None, None)
1819

1920
if api_auth_check:
20-
(status, json_resp) = auth_check(grafana_url, http_get_headers, verify_ssl, client_cert, debug)
21+
(status, json_resp) = auth_check(grafana_url,
22+
http_get_headers, verify_ssl, client_cert, debug)
2123
if not status == 200:
2224
return (status, json_resp, None, None, None)
2325

24-
dashboard_uid_support, datasource_uid_support = uid_feature_check(grafana_url, http_get_headers, verify_ssl, client_cert, debug)
26+
dashboard_uid_support, datasource_uid_support = uid_feature_check(
27+
grafana_url, http_get_headers, verify_ssl, client_cert, debug)
2528
if isinstance(dashboard_uid_support, str):
2629
raise Exception(dashboard_uid_support)
2730
if isinstance(datasource_uid_support, str):
2831
raise Exception(datasource_uid_support)
2932

30-
paging_support = paging_feature_check(grafana_url, http_get_headers, verify_ssl, client_cert, debug)
33+
paging_support = paging_feature_check(
34+
grafana_url, http_get_headers, verify_ssl, client_cert, debug)
3135
if isinstance(paging_support, str):
3236
raise Exception(paging_support)
3337

38+
is_contact_point_available = contact_point_check(
39+
grafana_url, http_get_headers, verify_ssl, client_cert, debug)
40+
3441
print_horizontal_line()
3542
if status == 200:
3643
print("[Pre-Check] Server status is 'OK' !!")
3744
else:
3845
print("[Pre-Check] Server status is NOT OK !!: {0}".format(json_resp))
3946
print_horizontal_line()
4047

41-
return (status, json_resp, dashboard_uid_support, datasource_uid_support, paging_support)
48+
return (status, json_resp, dashboard_uid_support, datasource_uid_support, paging_support, is_contact_point_available)

grafana_backup/archive.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from glob import glob
2-
import os, tarfile, shutil
2+
import os
3+
import tarfile
4+
import shutil
35

46

57
def main(args, settings):
@@ -10,7 +12,7 @@ def main(args, settings):
1012
backup_files = list()
1113

1214
for folder_name in ['folders', 'datasources', 'dashboards', 'alert_channels', 'organizations', 'users', 'snapshots',
13-
'dashboard_versions', 'annotations', 'library-elements', 'teams', 'team_members', 'alert_rules']:
15+
'dashboard_versions', 'annotations', 'library-elements', 'teams', 'team_members', 'alert_rules', 'contact_points', 'notification_policies']:
1416
backup_path = '{0}/{1}/{2}'.format(backup_dir, folder_name, timestamp)
1517

1618
for file_path in glob(backup_path):

grafana_backup/constants.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@
77
homedir = os.environ["HOME"]
88

99
PKG_NAME = "grafana-backup"
10-
PKG_VERSION = "1.3.3"
10+
PKG_VERSION = "1.4.0"
1111
JSON_CONFIG_PATH = "{0}/.grafana-backup.json".format(homedir)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import json
2+
from grafana_backup.dashboardApi import create_contact_point, get_grafana_version
3+
from packaging import version
4+
5+
6+
def main(args, settings, file_path):
7+
grafana_url = settings.get('GRAFANA_URL')
8+
http_post_headers = settings.get('HTTP_POST_HEADERS')
9+
verify_ssl = settings.get('VERIFY_SSL')
10+
client_cert = settings.get('CLIENT_CERT')
11+
debug = settings.get('DEBUG')
12+
13+
try:
14+
grafana_version = get_grafana_version(grafana_url, verify_ssl)
15+
except KeyError as error:
16+
if not grafana_version:
17+
raise Exception("Grafana version is not set.") from error
18+
19+
minimum_version = version.parse('9.4.0')
20+
21+
if minimum_version <= grafana_version:
22+
with open(file_path, 'r') as f:
23+
data = f.read()
24+
25+
contact_points = json.loads(data)
26+
for cp in contact_points:
27+
result = create_contact_point(json.dumps(
28+
cp), grafana_url, http_post_headers, verify_ssl, client_cert, debug)
29+
print("create contact_point: {0}, status: {1}, msg: {2}".format(
30+
cp['name'], result[0], result[1]))
31+
else:
32+
print("Unable to create contact points, requires Grafana version {0} or above. Current version is {1}".format(
33+
minimum_version, grafana_version))

0 commit comments

Comments
 (0)