Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Folders are available since Grafana v.5.0.*. Create scripts to migrate folders and dashboards (with folders id)
- Loading branch information
Matheus Figueredo
committed
Aug 16, 2018
1 parent
b15a3d8
commit 74fa101
Showing
8 changed files
with
146 additions
and
39 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
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,14 @@ | ||
import json, sys, re, argparse | ||
from dashboardApi import * | ||
|
||
parser = argparse.ArgumentParser() | ||
parser.add_argument('path', help='file path saved of folder\' setting') | ||
args = parser.parse_args() | ||
|
||
file_path = args.path | ||
|
||
with open(file_path, 'r') as f: | ||
data = f.read() | ||
|
||
folder = json.loads(data) | ||
create_folder(json.dumps(folder['dashboard'])) |
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 |
---|---|---|
@@ -1,56 +1,80 @@ | ||
import requests, json | ||
import requests, json, re | ||
from grafanaSettings import * | ||
|
||
|
||
def search_dashboard(): | ||
print "search dashboard in grafana:" | ||
r = send_grafana_get(grafana_url + '/api/search/') | ||
r = send_grafana_get(grafana_url + '/api/search/?type=dash-db') | ||
return r.content | ||
|
||
|
||
def get_dashboard(board_uri): | ||
r = send_grafana_get(grafana_url + "/api/dashboards/{0}".format(board_uri)) | ||
status_code = r.status_code | ||
content = r.content | ||
print "query dashboard:{0}, status:{1}".format(board_uri, status_code) | ||
return (status_code, content) | ||
|
||
|
||
def delete_dashboard(board_uri): | ||
r = requests.delete(grafana_url + "/api/dashboards/db/{0}".format(board_uri), headers=http_post_headers) | ||
status_code = r.status_code | ||
print "status: {0}".format(status_code) | ||
print "msg: {0}".format(r.content) | ||
return int(status_code) | ||
|
||
|
||
def create_dashboard(payload): | ||
r = send_grafana_post(grafana_url + '/api/dashboards/db', payload) | ||
status_code = r.status_code | ||
print "status: {0}".format(status_code) | ||
print "msg: {0}".format(r.content) | ||
return int(status_code) | ||
|
||
|
||
def search_datasource(): | ||
r = send_grafana_get(grafana_url + '/api/datasources') | ||
print "search datasources in grafana:" | ||
return r.content | ||
|
||
|
||
def create_datasource(payload): | ||
r = send_grafana_post(grafana_url + '/api/datasources', payload) | ||
status_code = r.status_code | ||
print "status: {0}".format(status_code) | ||
print "msg: {0}".format(r.content) | ||
return int(status_code) | ||
|
||
def search_folders(): | ||
print "search folder in grafana:" | ||
r = send_grafana_get(grafana_url + '/api/search/?type=dash-folder') | ||
return r.content | ||
|
||
def get_folder(uid): | ||
r = send_grafana_get(grafana_url + "/api/folders/{0}".format(uid)) | ||
status_code = r.status_code | ||
content = r.content | ||
print "query folder:{0}, status:{1}".format(uid, status_code) | ||
return (status_code, content) | ||
|
||
def get_folder_id_from_old_folder_url(folder_url): | ||
if folder_url != "": | ||
# Get folder uid | ||
matches = re.search('dashboards\/[A-Za-z0-9]{1}\/(.*)\/.*', folder_url) | ||
uid = matches.group(1) | ||
|
||
response = get_folder(uid) | ||
folder_data = json.loads(folder[1]) | ||
|
||
return folder_data['id'] | ||
|
||
return 0 | ||
|
||
def create_folder(payload): | ||
r = send_grafana_post(grafana_url + '/api/folders', payload) | ||
status_code = r.status_code | ||
print "status: {0}".format(status_code) | ||
print "msg: {0}".format(r.content) | ||
return int(status_code) | ||
|
||
def send_grafana_get(url): | ||
r = requests.get(url, headers=http_get_headers) | ||
return r | ||
|
||
|
||
def send_grafana_post(url, json_payload): | ||
r = requests.post(url, headers=http_post_headers, data=json_payload) | ||
return r | ||
return r |
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,9 @@ | ||
#!/bin/bash | ||
|
||
current_path=`pwd` | ||
folders_path="$1" | ||
|
||
find "$folders_path" -mindepth 1 | while read f ; do | ||
echo "$f" | ||
python $current_path/createFolder.py "$f" | ||
done |
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,37 @@ | ||
import argparse | ||
from dashboardApi import * | ||
from commons import * | ||
|
||
parser = argparse.ArgumentParser() | ||
parser.add_argument('path', help='folder path to save folders') | ||
args = parser.parse_args() | ||
|
||
folder_path = args.path | ||
|
||
def get_all_folders_in_grafana(): | ||
content_of_all_folders = search_folders() | ||
folders = json.loads(content_of_all_folders) | ||
print "There are {0} folders:".format(len(folders)) | ||
for board in folders: | ||
print board['title'] | ||
return folders | ||
|
||
|
||
def save_folder_setting(file_name, folder_settings): | ||
file_path = folder_path + '/' + file_name + '.folder' | ||
with open(file_path , 'w') as f: | ||
f.write(folder_settings) | ||
print "folder:{0} are saved to {1}".format(file_name, file_path) | ||
|
||
|
||
def get_indivisual_folder_setting_and_save(folders): | ||
for board in folders: | ||
status_code_and_content = get_folder(board['uri']) | ||
if status_code_and_content[0] == 200: | ||
save_folder_setting(board['title'], status_code_and_content[1]) | ||
|
||
folders = get_all_folders_in_grafana() | ||
print_horizontal_line() | ||
folder_settings = get_indivisual_folder_setting_and_save(folders) | ||
print_horizontal_line() | ||
|