def get_first_dashboard_by_page(page):
     (status, content) = search_dashboard(page, 1, grafana_url,
                                          http_get_headers, verify_ssl,
                                          client_cert, debug)
     if status == 200 and len(content):
         if sys.version_info[0] > 2:
             content[0] = {
                 k: to_python2_and_3_compatible_string(v)
                 for k, v in content[0].items()
             }
             dashboard_values = sorted(content[0].items(),
                                       key=lambda kv: str(kv[1]))
         else:
             content[0] = {
                 k: to_python2_and_3_compatible_string(unicode(v))
                 for k, v in content[0].iteritems()
             }
             dashboard_values = sorted(content[0].iteritems(),
                                       key=lambda kv: str(kv[1]))
         return True, dashboard_values
     else:
         if len(content):
             return False, "get dashboards failed, status: {0}, msg: {1}".format(
                 status, content)
         else:
             # No dashboards exist, disable paging feature
             return False, False
def get_individual_user_and_save(users, folder_path, log_file, grafana_url,
                                 http_get_headers, verify_ssl, client_cert,
                                 debug, pretty_print):
    file_path = folder_path + '/' + log_file
    if users:
        with open(u"{0}".format(file_path), 'w') as f:
            for user in users:
                (status, content) = get_user(user['id'], grafana_url,
                                             http_get_headers, verify_ssl,
                                             client_cert, debug)
                if status == 200:
                    user.update(content)

                (status, content) = get_user_org(user['id'], grafana_url,
                                                 http_get_headers, verify_ssl,
                                                 client_cert, debug)
                if status == 200:
                    user.update({'orgs': content})

                save_user_info(
                    to_python2_and_3_compatible_string(user['name']),
                    str(user['id']), user, folder_path, pretty_print)
                f.write('{0}\t{1}\n'.format(
                    user['id'],
                    to_python2_and_3_compatible_string(user['name'])))
def get_individual_folder_setting_and_save(folders, folder_path, log_file, grafana_url, http_get_headers, verify_ssl, client_cert, debug, pretty_print):
    file_path = folder_path + '/' + log_file
    with open(u"{0}".format(file_path), 'w+') as f:
        for folder in folders:
            (status, content) = get_folder(folder['uid'], grafana_url, http_get_headers, verify_ssl, client_cert, debug)
            if status == 200:
                save_folder_setting(
                    to_python2_and_3_compatible_string(folder['title']), 
                    folder['uid'],
                    content,
                    folder_path,
                    pretty_print
                )
                f.write('{0}\t{1}\n'.format(folder['uid'], to_python2_and_3_compatible_string(folder['title'])))
def get_individual_org_info_and_save(orgs, folder_path, log_file, grafana_url, http_get_headers,
                                     verify_ssl, client_cert, debug):
    file_path = folder_path + '/' + log_file
    if orgs:
        with open(u"{0}".format(file_path), 'w') as log_file:
            for org in orgs:
                (status, content) = get_org(org['id'], grafana_url, http_get_headers, verify_ssl, client_cert, debug)
                if status == 200:
                    save_org_info(
                        to_python2_and_3_compatible_string(org['name']),
                        str(org['id']),
                        content,
                        folder_path
                    )
                    log_file.write('{0}\t{1}\n'.format(org['id'], to_python2_and_3_compatible_string(org['name'])))
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')

    with open(file_path, 'r') as f:
        data = f.read()

    content = json.loads(data)
    content['dashboard']['id'] = None

    payload = {
        'dashboard':
        content['dashboard'],
        'folderId':
        get_folder_id(content, grafana_url, http_post_headers, verify_ssl,
                      client_cert, debug),
        'overwrite':
        True
    }

    result = create_dashboard(json.dumps(payload), grafana_url,
                              http_post_headers, verify_ssl, client_cert,
                              debug)
    dashboard_title = to_python2_and_3_compatible_string(
        content['dashboard'].get('title', ''))
    print("create dashboard {0} response status: {1}, msg: {2} \n".format(
        dashboard_title, result[0], result[1]))
def get_individual_folder_setting_and_save(folders, folder_path, log_file,
                                           grafana_url, http_get_headers,
                                           verify_ssl, debug):
    for folder in folders:
        status_code_and_content = get_folder(folder['uid'], grafana_url,
                                             http_get_headers, verify_ssl,
                                             debug)
        if status_code_and_content[0] == 200:
            save_folder_setting(
                to_python2_and_3_compatible_string(folder['title']),
                folder['uid'], status_code_and_content[1], folder_path)
            file_path = folder_path + '/' + log_file
            with open(u"{0}".format(file_path), 'w+') as f:
                f.write('{}\t{}'.format(
                    folder['uid'],
                    to_python2_and_3_compatible_string(folder['title'])))
def get_individual_alert_channel_and_save(channels, folder_path, log_file):
    file_path = folder_path + '/' + log_file
    if channels:
        with open(u"{0}".format(file_path), 'w') as f:
            for channel in channels:
                if 'uid' in channel:
                    channel_identifier = channel['uid']
                else:
                    channel_identifier = channel['id']

                save_alert_channel(
                    to_python2_and_3_compatible_string(channel['name']),
                    to_python2_and_3_compatible_string(
                        str(channel_identifier)), channel, folder_path)
                f.write('{0}\t{1}\n'.format(
                    to_python2_and_3_compatible_string(
                        str(channel_identifier)),
                    to_python2_and_3_compatible_string(channel['name'])))
示例#8
0
def get_individual_dashboard_setting_and_save(dashboards, folder_path,
                                              log_file, grafana_url,
                                              http_get_headers, verify_ssl,
                                              client_cert, debug):
    file_path = folder_path + '/' + log_file
    if dashboards:
        with open(u"{0}".format(file_path), 'w') as f:
            for board in dashboards:
                (status, content) = get_dashboard(board['uri'], grafana_url,
                                                  http_get_headers, verify_ssl,
                                                  client_cert, debug)
                if status == 200:
                    save_dashboard_setting(
                        to_python2_and_3_compatible_string(board['title']),
                        board['uid'], content, folder_path)
                    f.write('{0}\t{1}\n'.format(
                        board['uid'],
                        to_python2_and_3_compatible_string(board['title'])))
示例#9
0
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_dashboard_setting_and_save(dashboards, folder_path, log_file, grafana_url, http_get_headers, verify_ssl, client_cert, debug, pretty_print, api_version):
    file_path = folder_path + '/' + log_file
    if dashboards:
        with open(u"{0}".format(file_path), 'w') as f:
            for board in dashboards:
                if left_ver_newer_than_right_ver(api_version, "4.6.3"):
                    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)
                if status == 200:
                    save_dashboard_setting(
                        to_python2_and_3_compatible_string(board['title']), 
                        board['uid'], 
                        content,
                        folder_path,
                        pretty_print
                    )
                    f.write('{0}\t{1}\n'.format(board['uid'], to_python2_and_3_compatible_string(board['title'])))
def get_individual_folder_setting_and_save(folders, folder_path, log_file, grafana_url, http_get_headers, verify_ssl, client_cert, debug, pretty_print, uid_support):
    file_path = folder_path + '/' + log_file
    with open(u"{0}".format(file_path), 'w+') as f:
        for folder in folders:
            if uid_support:
                folder_uri = "uid/{0}".format(folder['uid'])
            else:
                folder_uri = folder['uri']

            (status_folder_settings, content_folder_settings) = get_folder(folder['uid'], grafana_url, http_get_headers, verify_ssl, client_cert, debug)
            (status_folder_permissions, content_folder_permissions) = get_folder_permissions(folder['uid'], grafana_url, http_get_headers, verify_ssl, client_cert, debug)

            if status_folder_settings == 200 and status_folder_permissions == 200:
                save_folder_setting(
                    to_python2_and_3_compatible_string(folder['title']),
                    folder_uri,
                    content_folder_settings,
                    content_folder_permissions,
                    folder_path,
                    pretty_print
                )
                f.write('{0}\t{1}\n'.format(folder_uri, to_python2_and_3_compatible_string(folder['title'])))
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 []
示例#13
0
def get_all_orgs_in_grafana(grafana_url, http_get_headers, verify_ssl,
                            client_cert, debug):
    (status, content) = search_orgs(grafana_url, http_get_headers, verify_ssl,
                                    client_cert, debug)
    if status == 200:
        orgs = content
        print("There are {0} orgs:".format(len(orgs)))
        for org in orgs:
            print('name: {0}'.format(
                to_python2_and_3_compatible_string(org['name'])))
        return orgs
    else:
        print("get orgs failed, status: {0}, msg: {1}".format(status, content))
        return []
def get_all_dashboards_in_grafana(page, limit, grafana_url, http_get_headers,
                                  verify_ssl, debug):
    (status, content) = search_dashboard(page, limit, grafana_url,
                                         http_get_headers, verify_ssl, 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_all_users(page, limit, grafana_url, http_get_headers, verify_ssl,
                  client_cert, debug):
    (status, content) = search_users(page, limit, grafana_url,
                                     http_get_headers, verify_ssl, client_cert,
                                     debug)
    if status == 200:
        users = content
        print("There are {0} users:".format(len(users)))
        for user in users:
            print('name: {0}'.format(
                to_python2_and_3_compatible_string(user['name'])))
        return users
    else:
        print("get users failed, status: {0}, msg: {1}".format(
            status, content))
        return []
示例#16
0
def get_versions_and_save(dashboards, folder_path, log_file, grafana_url,
                          http_get_headers, verify_ssl, client_cert, debug,
                          pretty_print, uid_support):
    if dashboards:
        for board in dashboards:
            board_folder_path = os.path.join(folder_path, board['uid'])
            if not os.path.exists(board_folder_path):
                os.makedirs(board_folder_path)

            (status,
             content) = get_dashboard_versions(board['id'], grafana_url,
                                               http_get_headers, verify_ssl,
                                               client_cert, debug)
            if status == 200:
                print("found {0} versions for dashboard {1}".format(
                    len(content),
                    to_python2_and_3_compatible_string(board['title'])))
                get_individual_versions(content, board_folder_path, log_file,
                                        grafana_url, http_get_headers,
                                        verify_ssl, client_cert, debug,
                                        pretty_print)