Skip to content
Snippets Groups Projects
Commit 6d8e4632 authored by arthur.yueh's avatar arthur.yueh
Browse files

refactor code and add support of grafana v6.2+ page parameter

parent fcb9ee21
No related branches found
No related tags found
No related merge requests found
import sys import sys
def left_ver_newer_than_right_ver(current_version, specefic_version):
def convertVersion(ver):
return int(''.join(ver.split(".")))
return convertVersion(current_version) > convertVersion(specefic_version)
def print_horizontal_line(): def print_horizontal_line():
print('') print('')
print("########################################") print("########################################")
...@@ -7,12 +12,12 @@ def print_horizontal_line(): ...@@ -7,12 +12,12 @@ def print_horizontal_line():
def log_response(resp): def log_response(resp):
status_code = resp.status_code status_code = resp.status_code
print("[debug] status: {0}".format(status_code)) print("[DEBUG] status: {0}".format(status_code))
print("[debug] body: {0}".format(resp.json())) print("[DEBUG] body: {0}".format(resp.json()))
return resp return resp
def toPython2And3CompatibleString(someString): def to_python2_and_3_compatible_string(some_string):
if sys.version_info[0] > 2: if sys.version_info[0] > 2:
return someString return some_string
else: else:
return someString.encode('utf8') return some_string.encode('utf8')
...@@ -2,36 +2,42 @@ import requests, json, re ...@@ -2,36 +2,42 @@ import requests, json, re
from grafanaSettings import * from grafanaSettings import *
from commons import * from commons import *
def search_dashboard(): def health_check():
print("search dashboard in grafana:") url = GRAFANA_URL + '/api/health'
return send_grafana_get(grafana_url + '/api/search/?type=dash-db&limit={0}'.format(search_api_limit)) print("grafana health: {0}".format(url))
return send_grafana_get(url)
def search_dashboard(page, limit):
url = GRAFANA_URL + '/api/search/?type=dash-db&limit={0}&page={1}'.format(limit, page)
print("search dashboard in grafana: {0}".format(url))
return send_grafana_get(url)
def get_dashboard(board_uri): def get_dashboard(board_uri):
(status_code, content) = send_grafana_get(grafana_url + "/api/dashboards/{0}".format(board_uri)) url = GRAFANA_URL + "/api/dashboards/{0}".format(board_uri)
print("query dashboard uri: {0}, status: {1}".format(board_uri, status_code)) print("query dashboard uri: {0}".format(url))
(status_code, content) = send_grafana_get(url)
return (status_code, content) return (status_code, content)
def delete_dashboard(board_uri): def delete_dashboard(board_uri):
r = requests.delete(grafana_url + "/api/dashboards/db/{0}".format(board_uri), headers=http_post_headers) r = requests.delete(GRAFANA_URL + "/api/dashboards/db/{0}".format(board_uri), headers=HTTP_POST_HEADERS)
return int(status_code) return int(status_code)
def create_dashboard(payload): def create_dashboard(payload):
return send_grafana_post(grafana_url + '/api/dashboards/db', payload) return send_grafana_post(GRAFANA_URL + '/api/dashboards/db', payload)
def search_datasource(): def search_datasource():
print("search datasources in grafana:") print("search datasources in grafana:")
return send_grafana_get(grafana_url + '/api/datasources') return send_grafana_get(GRAFANA_URL + '/api/datasources')
def create_datasource(payload): def create_datasource(payload):
return send_grafana_post(grafana_url + '/api/datasources', payload) return send_grafana_post(GRAFANA_URL + '/api/datasources', payload)
def search_folders(): def search_folders():
print("search folder in grafana:") print("search folder in grafana:")
return send_grafana_get(grafana_url + '/api/search/?type=dash-folder') return send_grafana_get(GRAFANA_URL + '/api/search/?type=dash-folder')
def get_folder(uid): def get_folder(uid):
(status_code, content) = send_grafana_get(grafana_url + "/api/folders/{0}".format(uid)) (status_code, content) = send_grafana_get(GRAFANA_URL + "/api/folders/{0}".format(uid))
print("query folder:{0}, status:{1}".format(uid, status_code)) print("query folder:{0}, status:{1}".format(uid, status_code))
return (status_code, content) return (status_code, content)
...@@ -46,22 +52,20 @@ def get_folder_id_from_old_folder_url(folder_url): ...@@ -46,22 +52,20 @@ def get_folder_id_from_old_folder_url(folder_url):
folder_data = response[1] folder_data = response[1]
else: else:
folder_data = json.loads(response[1]) folder_data = json.loads(response[1])
return folder_data['id'] return folder_data['id']
return 0 return 0
def create_folder(payload): def create_folder(payload):
return send_grafana_post(grafana_url + '/api/folders', payload) return send_grafana_post(GRAFANA_URL + '/api/folders', payload)
def send_grafana_get(url): def send_grafana_get(url):
r = requests.get(url, headers=http_get_headers, verify=verifySSL) r = requests.get(url, headers=HTTP_GET_HEADERS, verify=VERIFY_SSL)
if debug: if DEBUG:
log_response(r) log_response(r)
return (r.status_code, r.json()) return (r.status_code, r.json())
def send_grafana_post(url, json_payload): def send_grafana_post(url, json_payload):
r = requests.post(url, headers=http_post_headers, data=json_payload, verify=verifySSL) r = requests.post(url, headers=HTTP_POST_HEADERS, data=json_payload, verify=VERIFY_SSL)
if debug: if DEBUG:
log_response(r) log_response(r)
return (r.status_code, r.json()) return (r.status_code, r.json())
import os import os
grafana_url = os.getenv('GRAFANA_URL', 'http://localhost:3000') GRAFANA_URL = os.getenv('GRAFANA_URL', 'http://localhost:3000')
token = os.getenv('GRAFANA_TOKEN', 'eyJrIjoidUhaU2ZQQndrWFN3RTVkUnVOQkRka1JoaG9KWFFObEgiLCJuIjoiYWRtaW4iLCJpZCI6MX0=') TOKEN = os.getenv('GRAFANA_TOKEN', 'eyJrIjoiSkQ5NkdvWllHdnVNdlVhWUV3Tm5LSGc4NG53UFdSTjQiLCJuIjoiYWRtaW4iLCJpZCI6MX0=')
http_get_headers = {'Authorization': 'Bearer ' + token} HTTP_GET_HEADERS = {'Authorization': 'Bearer ' + TOKEN}
http_post_headers = {'Authorization': 'Bearer ' + token, 'Content-Type': 'application/json'} HTTP_POST_HEADERS = {'Authorization': 'Bearer ' + TOKEN, 'Content-Type': 'application/json'}
search_api_limit = 100000 SEARCH_API_LIMIT = 10000
debug = True DEBUG = True
verifySSL = False VERIFY_SSL = False
...@@ -11,15 +11,13 @@ folder_path = args.path ...@@ -11,15 +11,13 @@ folder_path = args.path
log_file = 'dashboards_{0}.txt'.format(datetime.today().strftime('%Y%m%d%H%M')) log_file = 'dashboards_{0}.txt'.format(datetime.today().strftime('%Y%m%d%H%M'))
def get_all_dashboards_in_grafana(): def get_all_dashboards_in_grafana(page, limit=SEARCH_API_LIMIT):
status_and_content_of_all_dashboards = search_dashboard() (status, content) = search_dashboard(page, limit)
status = status_and_content_of_all_dashboards[0]
content = status_and_content_of_all_dashboards[1]
if status == 200: if status == 200:
dashboards = content dashboards = content
print("There are {0} dashboards:".format(len(dashboards))) print("There are {0} dashboards:".format(len(dashboards)))
for board in dashboards: for board in dashboards:
print('name: {}'.format(toPython2And3CompatibleString(board['title']))) print('name: {}'.format(to_python2_and_3_compatible_string(board['title'])))
return dashboards return dashboards
else: else:
print("get dashboards failed, status: {0}, msg: {1}".format(status, content)) print("get dashboards failed, status: {0}, msg: {1}".format(status, content))
...@@ -36,19 +34,42 @@ def save_dashboard_setting(dashboard_name, file_name, dashboard_settings): ...@@ -36,19 +34,42 @@ def save_dashboard_setting(dashboard_name, file_name, dashboard_settings):
def get_indivisual_dashboard_setting_and_save(dashboards): def get_indivisual_dashboard_setting_and_save(dashboards):
for board in dashboards: for board in dashboards:
status_code_and_content = get_dashboard(board['uri']) (status, content) = get_dashboard(board['uri'])
if status_code_and_content[0] == 200: if status == 200:
save_dashboard_setting( save_dashboard_setting(
toPython2And3CompatibleString(board['title']), to_python2_and_3_compatible_string(board['title']),
board['uid'], board['uid'],
status_code_and_content[1] content
) )
file_path = folder_path + '/' + log_file file_path = folder_path + '/' + log_file
with open(u"{0}".format(file_path) , 'w+') as f: with open(u"{0}".format(file_path) , 'w+') as f:
f.write('{}\t{}'.format(board['uid'], toPython2And3CompatibleString(board['title']))) f.write('{}\t{}'.format(board['uid'], to_python2_and_3_compatible_string(board['title'])))
dashboards = get_all_dashboards_in_grafana() def save_dashboards_above_Ver6_2():
print_horizontal_line() limit = 5000 # limit is 5000 above V6.2+
get_indivisual_dashboard_setting_and_save(dashboards) current_page = 1
print_horizontal_line() while True:
dashboards = get_all_dashboards_in_grafana(current_page, limit)
print_horizontal_line()
if len(dashboards) == 0:
break
else:
current_page += 1
get_indivisual_dashboard_setting_and_save(dashboards)
print_horizontal_line()
def save_dashboards():
dashboards = get_all_dashboards_in_grafana(1)
print_horizontal_line()
get_indivisual_dashboard_setting_and_save(dashboards)
print_horizontal_line()
(status, resp) = health_check()
if status == 200:
is_api_support_page_param = left_ver_newer_than_right_ver(resp['version'], "6.2.0")
if is_api_support_page_param:
save_dashboards_above_Ver6_2()
else:
save_dashboards()
else:
print("server status is not ok: {0}".format(resp))
...@@ -18,7 +18,7 @@ def get_all_folders_in_grafana(): ...@@ -18,7 +18,7 @@ def get_all_folders_in_grafana():
folders = content folders = content
print("There are {0} folders:".format(len(content))) print("There are {0} folders:".format(len(content)))
for folder in folders: for folder in folders:
print("name: {0}".format(toPython2And3CompatibleString(folder['title']))) print("name: {0}".format(to_python2_and_3_compatible_string(folder['title'])))
return folders return folders
else: else:
print("get folders failed, status: {0}, msg: {1}".format(status, content)) print("get folders failed, status: {0}, msg: {1}".format(status, content))
...@@ -37,13 +37,13 @@ def get_indivisual_folder_setting_and_save(folders): ...@@ -37,13 +37,13 @@ def get_indivisual_folder_setting_and_save(folders):
status_code_and_content = get_folder(folder['uid']) status_code_and_content = get_folder(folder['uid'])
if status_code_and_content[0] == 200: if status_code_and_content[0] == 200:
save_folder_setting( save_folder_setting(
toPython2And3CompatibleString(folder['title']), to_python2_and_3_compatible_string(folder['title']),
folder['uid'], folder['uid'],
status_code_and_content[1] status_code_and_content[1]
) )
file_path = folder_path + '/' + log_file file_path = folder_path + '/' + log_file
with open(u"{0}".format(file_path) , 'w+') as f: with open(u"{0}".format(file_path) , 'w+') as f:
f.write('{}\t{}'.format(folder['uid'], toPython2And3CompatibleString(folder['title']))) f.write('{}\t{}'.format(folder['uid'], to_python2_and_3_compatible_string(folder['title'])))
folders = get_all_folders_in_grafana() folders = get_all_folders_in_grafana()
print_horizontal_line() print_horizontal_line()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment