Newer
Older
from grafana_backup.dashboardApi import search_dashboard, get_dashboard
from grafana_backup.commons import to_python2_and_3_compatible_string, print_horizontal_line, save_json
def main(args, settings):
backup_dir = settings.get('BACKUP_DIR')
timestamp = settings.get('TIMESTAMP')
limit = settings.get('SEARCH_API_LIMIT')
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')
David Smith
committed
pretty_print = settings.get('PRETTY_PRINT')
uid_support = settings.get('DASHBOARD_UID_SUPPORT')
paging_support = settings.get('PAGING_SUPPORT')
folder_path = '{0}/dashboards/{1}'.format(backup_dir, timestamp)
log_file = 'dashboards_{0}.txt'.format(timestamp)
if not os.path.exists(folder_path):
os.makedirs(folder_path)
if paging_support:
save_dashboards_above_Ver6_2(folder_path, log_file, grafana_url, http_get_headers, verify_ssl, client_cert, debug, pretty_print, uid_support)
save_dashboards(folder_path, log_file, limit, grafana_url, http_get_headers, verify_ssl, client_cert, debug, pretty_print, uid_support)
arthur.yueh
committed
def get_all_dashboards_in_grafana(page, limit, grafana_url, http_get_headers, verify_ssl, client_cert, debug):
(status, content) = search_dashboard(page,
limit,
grafana_url,
http_get_headers,
dashboards = content
print("There are {0} dashboards:".format(len(dashboards)))
for board in dashboards:
print('name: {0}'.format(to_python2_and_3_compatible_string(board['title'])))
return dashboards
else:
print("get dashboards failed, status: {0}, msg: {1}".format(status, content))
return []
David Smith
committed
def save_dashboard_setting(dashboard_name, file_name, dashboard_settings, folder_path, pretty_print):
file_path = save_json(file_name, dashboard_settings, folder_path, 'dashboard', pretty_print)
arthur.yueh
committed
print("dashboard: {0} -> saved to: {1}".format(dashboard_name, file_path))
arthur.yueh
committed
def get_individual_dashboard_setting_and_save(dashboards, folder_path, log_file, grafana_url, http_get_headers, verify_ssl, client_cert, debug, pretty_print, uid_support):
Mikel
committed
with open(u"{0}".format(file_path), 'w') as f:
board_uri = "uid/{0}".format(board['uid'])
else:
board_uri = board['uri']
(status, content) = get_dashboard(board_uri, grafana_url, http_get_headers, verify_ssl, client_cert, debug)
David Smith
committed
folder_path,
pretty_print
f.write('{0}\t{1}\n'.format(board_uri, to_python2_and_3_compatible_string(board['title'])))
def save_dashboards_above_Ver6_2(folder_path, log_file, grafana_url, http_get_headers, verify_ssl, client_cert, debug, pretty_print, uid_support):
current_page = 1
while True:
dashboards = get_all_dashboards_in_grafana(current_page, limit, grafana_url, http_get_headers, verify_ssl, client_cert, debug)
print_horizontal_line()
if len(dashboards) == 0:
break
else:
current_page += 1
get_individual_dashboard_setting_and_save(dashboards, folder_path, log_file, grafana_url, http_get_headers, verify_ssl, client_cert, debug, pretty_print, uid_support)
print_horizontal_line()
def save_dashboards(folder_path, log_file, limit, grafana_url, http_get_headers, verify_ssl, client_cert, debug, pretty_print, uid_support):
dashboards = get_all_dashboards_in_grafana(current_page, limit, grafana_url, http_get_headers, verify_ssl, client_cert, debug)
print_horizontal_line()
get_individual_dashboard_setting_and_save(dashboards, folder_path, log_file, grafana_url, http_get_headers, verify_ssl, client_cert, debug, pretty_print, uid_support)
print_horizontal_line()