-
Notifications
You must be signed in to change notification settings - Fork 277
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
contact points and notifcation policy backup
- Loading branch information
Showing
12 changed files
with
238 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import os | ||
from grafana_backup.dashboardApi import search_contact_points, get_grafana_version | ||
from grafana_backup.commons import to_python2_and_3_compatible_string, print_horizontal_line, save_json | ||
from packaging import version | ||
|
||
def main(args, settings): | ||
backup_dir = settings.get('BACKUP_DIR') | ||
timestamp = settings.get('TIMESTAMP') | ||
grafana_url = settings.get('GRAFANA_URL') | ||
http_get_headers = settings.get('HTTP_GET_HEADERS') | ||
verify_ssl = settings.get('VERIFY_SSL') | ||
client_cert = settings.get('CLIENT_CERT') | ||
debug = settings.get('DEBUG') | ||
pretty_print = settings.get('PRETTY_PRINT') | ||
folder_path = '{0}/contact_points/{1}'.format(backup_dir, timestamp) | ||
log_file = 'contact_points_{0}.txt'.format(timestamp) | ||
grafana_version_string = settings.get('GRAFANA_VERSION') | ||
|
||
if grafana_version_string: | ||
grafana_version = version.parse(grafana_version_string) | ||
|
||
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.0.0') | ||
if minimum_version <= grafana_version: | ||
if not os.path.exists(folder_path): | ||
os.makedirs(folder_path) | ||
|
||
contact_points = get_all_contact_points_in_grafana(grafana_url, http_get_headers, verify_ssl, client_cert, debug) | ||
save_contact_points('contact_points', contact_points, folder_path, pretty_print) | ||
else: | ||
print("Unable to save contact points, requires Grafana version {0} or above. Current version is {1}".format(minimum_version, grafana_version)) | ||
|
||
|
||
if not os.path.exists(folder_path): | ||
os.makedirs(folder_path) | ||
|
||
|
||
def get_all_contact_points_in_grafana(grafana_url, http_get_headers, verify_ssl, client_cert, debug): | ||
(status, content) = search_contact_points(grafana_url, http_get_headers, verify_ssl, client_cert, debug) | ||
if status == 200: | ||
contact_points = content | ||
print("There are {0} contact points: ".format(len(contact_points))) | ||
for contact_point in contact_points: | ||
print("name: {0}, type: {1}".format(to_python2_and_3_compatible_string(contact_point['name']), to_python2_and_3_compatible_string(contact_point['type']))) | ||
return contact_points | ||
else: | ||
print("query contact points failed, status: {0}, msg: {1}".format(status, content)) | ||
return [] | ||
|
||
|
||
def save_contact_points(file_name, contact_points, folder_path, pretty_print): | ||
file_path = save_json(file_name, contact_points, folder_path, 'contact_point', pretty_print) | ||
print_horizontal_line() | ||
print("contact points are saved to {0}".format(file_path)) | ||
print_horizontal_line() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import os | ||
from grafana_backup.dashboardApi import search_notification_policies, get_grafana_version | ||
from grafana_backup.commons import to_python2_and_3_compatible_string, print_horizontal_line, save_json | ||
from packaging import version | ||
|
||
def main(args, settings): | ||
backup_dir = settings.get('BACKUP_DIR') | ||
timestamp = settings.get('TIMESTAMP') | ||
grafana_url = settings.get('GRAFANA_URL') | ||
http_get_headers = settings.get('HTTP_GET_HEADERS') | ||
verify_ssl = settings.get('VERIFY_SSL') | ||
client_cert = settings.get('CLIENT_CERT') | ||
debug = settings.get('DEBUG') | ||
pretty_print = settings.get('PRETTY_PRINT') | ||
folder_path = '{0}/notification_policies/{1}'.format(backup_dir, timestamp) | ||
log_file = 'notification_policies_{0}.txt'.format(timestamp) | ||
grafana_version_string = settings.get('GRAFANA_VERSION') | ||
|
||
if grafana_version_string: | ||
grafana_version = version.parse(grafana_version_string) | ||
|
||
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.0.0') | ||
if minimum_version <= grafana_version: | ||
if not os.path.exists(folder_path): | ||
os.makedirs(folder_path) | ||
|
||
notification_policies = get_all_notification_policies_in_grafana(grafana_url, http_get_headers, verify_ssl, client_cert, debug) | ||
save_notification_policies('notificatioin_policies', notification_policies, folder_path, pretty_print) | ||
else: | ||
print("Unable to save notification policies, requires Grafana version {0} or above. Current version is {1}".format(minimum_version, grafana_version)) | ||
|
||
|
||
if not os.path.exists(folder_path): | ||
os.makedirs(folder_path) | ||
|
||
|
||
def get_all_notification_policies_in_grafana(grafana_url, http_get_headers, verify_ssl, client_cert, debug): | ||
(status, content) = search_notification_policies(grafana_url, http_get_headers, verify_ssl, client_cert, debug) | ||
if status == 200: | ||
notification_policies = content | ||
print("Notification policies found") | ||
return notification_policies | ||
else: | ||
print("query notification policies failed, status: {0}, msg: {1}".format(status, content)) | ||
return [] | ||
|
||
|
||
def save_notification_policies(file_name, notification_policies, folder_path, pretty_print): | ||
file_path = save_json(file_name, notification_policies, folder_path, 'notification_policys', pretty_print) | ||
print_horizontal_line() | ||
print("notification policies are saved to {0}".format(file_path)) | ||
print_horizontal_line() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import json | ||
from grafana_backup.dashboardApi import update_notification_policy, 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() | ||
|
||
notification_policies = json.loads(data) | ||
result = update_notification_policy(json.dumps(notification_policies), grafana_url, http_post_headers, verify_ssl, client_cert, debug) | ||
print("update notification_policy, status: {0}, msg: {1}".format(result[0], result[1])) | ||
else: | ||
print("Unable to update notification policy, requires Grafana version {0} or above. Current version is {1}".format(minimum_version, grafana_version)) | ||
|
||
|
||
|
||
|