Skip to content

Commit

Permalink
Merge pull request #238 from ysde/feature/add_support_for_contact_poi…
Browse files Browse the repository at this point in the history
…nt_and_notification

add contact points and notifcation policy backup functionalities
  • Loading branch information
acjohnson authored Sep 20, 2023
2 parents 5e12194 + 3d29210 commit 48e96d7
Show file tree
Hide file tree
Showing 14 changed files with 351 additions and 56 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,17 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

# [1.4.0] - 2023-09-20

### Added
- add contact points and notifcation policy backup functionalities by @ysde in #238
- added http headers to get_grafana_version request by @Mar8x in #239

### Changed
- added py3-packaging to slim docker image, reported by @tasiotas in #241 :tada:

### Removed

# [1.3.3] - 2023-07-27

### Added
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ There are three ways to setup the configuration:
3. Use `~/.grafana-backup.json` to define variables in json format.

### Example Config
* Check out the [examples](examples) folder for more configuration details
* 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).
* Check out the [examples](examples) folder for more configuration details.

**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):

Expand Down
File renamed without changes.
19 changes: 13 additions & 6 deletions grafana_backup/api_checks.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from grafana_backup.commons import print_horizontal_line
from grafana_backup.dashboardApi import health_check, auth_check, uid_feature_check, paging_feature_check
from grafana_backup.dashboardApi import health_check, auth_check, uid_feature_check, paging_feature_check, contact_point_check


def main(settings):
Expand All @@ -12,30 +12,37 @@ def main(settings):
api_auth_check = settings.get('API_AUTH_CHECK')

if api_health_check:
(status, json_resp) = health_check(grafana_url, http_get_headers, verify_ssl, client_cert, debug)
(status, json_resp) = health_check(grafana_url,
http_get_headers, verify_ssl, client_cert, debug)
if not status == 200:
return (status, json_resp, None, None, None)

if api_auth_check:
(status, json_resp) = auth_check(grafana_url, http_get_headers, verify_ssl, client_cert, debug)
(status, json_resp) = auth_check(grafana_url,
http_get_headers, verify_ssl, client_cert, debug)
if not status == 200:
return (status, json_resp, None, None, None)

dashboard_uid_support, datasource_uid_support = uid_feature_check(grafana_url, http_get_headers, verify_ssl, client_cert, debug)
dashboard_uid_support, datasource_uid_support = uid_feature_check(
grafana_url, http_get_headers, verify_ssl, client_cert, debug)
if isinstance(dashboard_uid_support, str):
raise Exception(dashboard_uid_support)
if isinstance(datasource_uid_support, str):
raise Exception(datasource_uid_support)

paging_support = paging_feature_check(grafana_url, http_get_headers, verify_ssl, client_cert, debug)
paging_support = paging_feature_check(
grafana_url, http_get_headers, verify_ssl, client_cert, debug)
if isinstance(paging_support, str):
raise Exception(paging_support)

is_contact_point_available = contact_point_check(
grafana_url, http_get_headers, verify_ssl, client_cert, debug)

print_horizontal_line()
if status == 200:
print("[Pre-Check] Server status is 'OK' !!")
else:
print("[Pre-Check] Server status is NOT OK !!: {0}".format(json_resp))
print_horizontal_line()

return (status, json_resp, dashboard_uid_support, datasource_uid_support, paging_support)
return (status, json_resp, dashboard_uid_support, datasource_uid_support, paging_support, is_contact_point_available)
6 changes: 4 additions & 2 deletions grafana_backup/archive.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from glob import glob
import os, tarfile, shutil
import os
import tarfile
import shutil


def main(args, settings):
Expand All @@ -10,7 +12,7 @@ def main(args, settings):
backup_files = list()

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

for file_path in glob(backup_path):
Expand Down
2 changes: 1 addition & 1 deletion grafana_backup/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
homedir = os.environ["HOME"]

PKG_NAME = "grafana-backup"
PKG_VERSION = "1.3.3"
PKG_VERSION = "1.4.0"
JSON_CONFIG_PATH = "{0}/.grafana-backup.json".format(homedir)
33 changes: 33 additions & 0 deletions grafana_backup/create_contact_point.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import json
from grafana_backup.dashboardApi import create_contact_point, get_grafana_version
from packaging import version


def main(args, settings, file_path):
grafana_url = settings.get('GRAFANA_URL')
http_post_headers = settings.get('HTTP_POST_HEADERS')
verify_ssl = settings.get('VERIFY_SSL')
client_cert = settings.get('CLIENT_CERT')
debug = settings.get('DEBUG')

try:
grafana_version = get_grafana_version(grafana_url, verify_ssl)
except KeyError as error:
if not grafana_version:
raise Exception("Grafana version is not set.") from error

minimum_version = version.parse('9.4.0')

if minimum_version <= grafana_version:
with open(file_path, 'r') as f:
data = f.read()

contact_points = json.loads(data)
for cp in contact_points:
result = create_contact_point(json.dumps(
cp), grafana_url, http_post_headers, verify_ssl, client_cert, debug)
print("create contact_point: {0}, status: {1}, msg: {2}".format(
cp['name'], result[0], result[1]))
else:
print("Unable to create contact points, requires Grafana version {0} or above. Current version is {1}".format(
minimum_version, grafana_version))
Loading

0 comments on commit 48e96d7

Please sign in to comment.