Skip to content
Snippets Groups Projects
Unverified Commit da614397 authored by Felix's avatar Felix Committed by GitHub
Browse files

Merge pull request #1 from idealo/deleting

Deleting
parents f815fed4 2e2d2c7a
No related branches found
No related tags found
No related merge requests found
from grafana_backup.constants import (PKG_NAME, PKG_VERSION, JSON_CONFIG_PATH)
from grafana_backup.delete import main as delete
from grafana_backup.grafanaSettings import main as conf
from docopt import docopt
import os
import sys
docstring = """
{0} {1}
Usage:
grafana-backup delete [--config=<filename>] [--components=<folders,dashboards,datasources,alert-channels,snapshots>]
grafana-backup [--config=<filename>]
grafana-backup -h | --help
grafana-backup --version
Options:
-h --help Show this help message and exit
--version Get version information and exit
--config=<filename> Override default configuration path
--components=<folders,dashboards,datasources,alert-channels,snapshots> Comma separated list of individual components to delete
rather than deleting all components by default
""".format(PKG_NAME, PKG_VERSION)
args = docopt(docstring, version='{0} {1}'.format(PKG_NAME, PKG_VERSION))
def main():
arg_config = args.get('--config', False)
default_config = '{0}/conf/grafanaSettings.json'.format(os.path.dirname(__file__))
if arg_config:
settings = conf(arg_config)
elif os.path.isfile(JSON_CONFIG_PATH):
settings = conf(JSON_CONFIG_PATH)
elif os.path.isfile(default_config):
settings = conf(default_config)
if args.get('delete', None):
delete(args, settings)
sys.exit()
else:
print(docstring)
sys.exit()
if __name__ == '__main__':
main()
......@@ -95,8 +95,30 @@ def create_alert_channel(payload, grafana_url, http_post_headers, verify_ssl, cl
client_cert, debug)
def delete_alert_channel_by_uid(uid, grafana_url, http_post_headers):
print('{0}/api/alert-notifications/uid/{1}'.format(grafana_url, uid))
print(http_post_headers)
r = requests.delete('{0}/api/alert-notifications/uid/{1}'.format(grafana_url, uid), headers=http_post_headers)
return int(r.status_code)
def delete_alert_channel_by_id(id_, grafana_url, http_post_headers):
r = requests.delete('{0}/api/alert-notifications/{1}'.format(grafana_url, id_), headers=http_post_headers)
return int(r.status_code)
def delete_folder(uid, grafana_url, http_post_headers):
r = requests.delete('{0}/api/folders/{1}'.format(grafana_url, uid), headers=http_post_headers)
return int(r.status_code)
def delete_snapshot(key, grafana_url, http_post_headers):
r = requests.delete('{0}/api/snapshots/{1}'.format(grafana_url, key), headers=http_post_headers)
return int(r.status_code)
def delete_dashboard(board_uri, grafana_url, http_post_headers):
r = requests.delete('{0}/api/dashboards/db/{1}'.format(grafana_url, board_uri), headers=http_post_headers)
r = requests.delete('{0}/api/dashboards/uid/{1}'.format(grafana_url, board_uri), headers=http_post_headers)
return int(r.status_code)
......@@ -131,6 +153,11 @@ def create_datasource(payload, grafana_url, http_post_headers, verify_ssl, clien
client_cert, debug)
def delete_datasource(uid, grafana_url, http_post_headers, verify_ssl, client_cert, debug):
r = requests.delete('{0}/api/datasources/uid/{1}'.format(grafana_url, uid), headers=http_post_headers)
return int(r.status_code)
def search_folders(grafana_url, http_get_headers, verify_ssl, client_cert, debug):
print("search folder in grafana:")
return send_grafana_get('{0}/api/search/?type=dash-folder'.format(grafana_url), http_get_headers, verify_ssl,
......
from grafana_backup.api_checks import main as api_checks
from grafana_backup.delete_dashboards import main as delete_dashboards
from grafana_backup.delete_datasources import main as delete_datasources
from grafana_backup.delete_folders import main as delete_folders
from grafana_backup.delete_alert_channels import main as delete_alert_channels
from grafana_backup.delete_snapshots import main as delete_snapshots
import sys
def main(args, settings):
arg_components = args.get('--components', False)
delete_functions = {'dashboards': delete_dashboards,
'datasources': delete_datasources,
'folders': delete_folders,
'alert-channels': delete_alert_channels,
'snapshots': delete_snapshots}
(status, json_resp, uid_support, paging_support) = api_checks(settings)
# Do not continue if API is unavailable or token is not valid
if not status == 200:
print("server status is not ok: {0}".format(json_resp))
sys.exit(1)
settings.update({'UID_SUPPORT': uid_support})
settings.update({'PAGING_SUPPORT': paging_support})
if arg_components:
arg_components_list = arg_components.split(',')
# Delete only the components that provided via an argument
for delete_function in arg_components_list:
delete_functions[delete_function](args, settings)
else:
# delete every component
for delete_function in delete_functions.keys():
delete_functions[delete_function](args, settings)
from grafana_backup.dashboardApi import search_alert_channels
from grafana_backup.dashboardApi import delete_alert_channel_by_uid
from grafana_backup.dashboardApi import delete_alert_channel_by_id
from grafana_backup.commons import to_python2_and_3_compatible_string, print_horizontal_line
def main(args, settings):
grafana_url = settings.get('GRAFANA_URL')
http_get_headers = settings.get('HTTP_POST_HEADERS')
verify_ssl = settings.get('VERIFY_SSL')
client_cert = settings.get('CLIENT_CERT')
debug = settings.get('DEBUG')
pretty_print = settings.get('PRETTY_PRINT')
alert_channels = get_all_alert_channels_in_grafana(grafana_url, http_get_headers, verify_ssl, client_cert, debug)
get_individual_alert_channel_and_delete(alert_channels, pretty_print, grafana_url, http_get_headers)
print_horizontal_line()
def get_all_alert_channels_in_grafana(grafana_url, http_get_headers, verify_ssl, client_cert, debug):
(status, content) = search_alert_channels(grafana_url, http_get_headers, verify_ssl, client_cert, debug)
if status == 200:
channels = content
print("There are {0} channels:".format(len(channels)))
for channel in channels:
print("name: {0}".format(to_python2_and_3_compatible_string(channel['name'])))
return channels
else:
print("query alert channels failed, status: {0}, msg: {1}".format(status, content))
return []
def get_individual_alert_channel_and_delete(channels, pretty_print, grafana_url, http_get_headers):
if channels:
for channel in channels:
status = 0
if 'uid' in channel:
status = delete_alert_channel_by_uid(channel['uid'], grafana_url, http_get_headers)
else:
status = delete_alert_channel_by_id(channel['id'], grafana_url, http_get_headers)
channel_name = to_python2_and_3_compatible_string(channel['name'])
if status == 200:
print("alert_channel:{0} is deleted".format(channel_name))
else:
print("deleting alert_channel {0} failed with {1}".format(channel_name, status))
from grafana_backup.dashboardApi import search_dashboard, delete_dashboard
from grafana_backup.commons import to_python2_and_3_compatible_string, print_horizontal_line
def main(args, settings):
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')
pretty_print = settings.get('PRETTY_PRINT')
uid_support = settings.get('UID_SUPPORT')
paging_support = settings.get('PAGING_SUPPORT')
if paging_support:
delete_dashboards_above_Ver6_2(grafana_url, http_get_headers, verify_ssl, client_cert, debug, pretty_print, uid_support)
else:
delete_dashboards(limit, grafana_url, http_get_headers, verify_ssl, client_cert, debug, pretty_print, uid_support)
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,
verify_ssl, client_cert,
debug)
if status == 200:
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 []
def get_individual_dashboard_and_delete(dashboards, grafana_url, http_get_headers, verify_ssl, client_cert, debug, pretty_print, uid_support):
if dashboards:
for board in dashboards:
status = delete_dashboard(board['uid'], grafana_url, http_get_headers)
if status == 200:
print("deleted dashboard {0}".format(board['title']))
else:
print("got {0} when trying to delete board {1}".format(status, board['title']))
def delete_dashboards_above_Ver6_2(grafana_url, http_get_headers, verify_ssl, client_cert, debug, pretty_print, uid_support):
limit = 5000 # limit is 5000 above V6.2+
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_and_delete(dashboards, grafana_url, http_get_headers, verify_ssl, client_cert, debug, pretty_print, uid_support)
print_horizontal_line()
def delete_dashboards(limit, grafana_url, http_get_headers, verify_ssl, client_cert, debug, pretty_print, uid_support):
current_page = 1
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_and_delete(dashboards, grafana_url, http_get_headers, verify_ssl, client_cert, debug, pretty_print, uid_support)
print_horizontal_line()
from grafana_backup.dashboardApi import search_datasource, delete_datasource
from grafana_backup.commons import print_horizontal_line
def main(args, settings):
grafana_url = settings.get('GRAFANA_URL')
http_get_headers = settings.get('HTTP_POST_HEADERS')
verify_ssl = settings.get('VERIFY_SSL')
client_cert = settings.get('CLIENT_CERT')
debug = settings.get('DEBUG')
pretty_print = settings.get('PRETTY_PRINT')
get_all_datasources_and_delete(grafana_url, http_get_headers, verify_ssl, client_cert, debug, pretty_print)
print_horizontal_line()
def get_all_datasources_and_delete(grafana_url, http_get_headers, verify_ssl, client_cert, debug, pretty_print):
status_code_and_content = search_datasource(grafana_url, http_get_headers, verify_ssl, client_cert, debug)
if status_code_and_content[0] == 200:
datasources = status_code_and_content[1]
print("There are {0} datasources:".format(len(datasources)))
for datasource in datasources:
print(datasource)
status = delete_datasource(datasource['uid'], grafana_url, http_get_headers, verify_ssl, client_cert, debug)
if status == 200:
print("datasource:{0} is deleted".format(datasource['name']))
else:
print("deleting of datasource {0} failed with: {1}".format(datasource['name'], status))
else:
print("query datasource failed, status: {0}, msg: {1}".format(status_code_and_content[0],
status_code_and_content[1]))
from grafana_backup.dashboardApi import search_folders, delete_folder
from grafana_backup.commons import to_python2_and_3_compatible_string, print_horizontal_line
def main(args, settings):
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')
uid_support = settings.get('UID_SUPPORT')
folders = get_all_folders_in_grafana(grafana_url, http_get_headers, verify_ssl, client_cert, debug)
print_horizontal_line()
get_individual_folder_setting_and_save(folders, grafana_url, http_get_headers, verify_ssl, client_cert, debug, pretty_print, uid_support)
print_horizontal_line()
def get_all_folders_in_grafana(grafana_url, http_get_headers, verify_ssl, client_cert, debug):
status_and_content_of_all_folders = search_folders(grafana_url, http_get_headers, verify_ssl, client_cert, debug)
status = status_and_content_of_all_folders[0]
content = status_and_content_of_all_folders[1]
if status == 200:
folders = content
print("There are {0} folders:".format(len(content)))
for folder in folders:
print("name: {0}".format(to_python2_and_3_compatible_string(folder['title'])))
return folders
else:
print("get folders failed, status: {0}, msg: {1}".format(status, content))
return []
def get_individual_folder_setting_and_save(folders, grafana_url, http_get_headers, verify_ssl, client_cert, debug, pretty_print, uid_support):
for folder in folders:
status = delete_folder(folder['uid'], grafana_url, http_get_headers)
if status == 200:
print("deleted folder {0}".format(folder))
else:
print("failed to delete folder {0} with {1}".format(folder, status))
from grafana_backup.dashboardApi import search_snapshot, delete_snapshot
from grafana_backup.commons import print_horizontal_line
def main(args, settings):
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')
get_all_snapshots_and_delete(grafana_url, http_get_headers, verify_ssl, client_cert, debug, pretty_print)
print_horizontal_line()
def get_all_snapshots_and_delete(grafana_url, http_get_headers, verify_ssl, client_cert, debug, pretty_print):
status_code_and_content = search_snapshot(grafana_url, http_get_headers, verify_ssl, client_cert, debug)
if status_code_and_content[0] == 200:
snapshots = status_code_and_content[1]
print("There are {0} snapshots:".format(len(snapshots)))
for snapshot in snapshots:
print(snapshot)
status = delete_snapshot(snapshot['key'], grafana_url, http_get_headers)
if status == 200:
print("deleted snapshot {0}".format(snapshot['name']))
else:
print("failed to delete snapshot {0}, with {1}".format(snapshot['name'], status))
else:
print("query snapshot failed, status: {0}, msg: {1}".format(status_code_and_content[0],
status_code_and_content[1]))
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