def get_params(): parser = argparse.ArgumentParser( usage="usage: Pythagoras [options]", description= "Import dashboards and set metadashboard, projectname and config") parser.add_argument('kibiter_url', help='Kibana URL') parser.add_argument('archimedes_root_path', help='Archimedes folder') parser.add_argument('top_menu_path', help='Top menu yaml file') parser.add_argument('--kibiter-index', dest='kibiter_index', help='Kibiter index', default='.kibiter') parser.add_argument('--kibiter-time-from', dest='kibiter_time_from', help='Kibiter time from', default='now-90d') parser.add_argument('--kibiter-index-pattern', dest='kibiter_index_pattern', help='Kibiter index pattern', default='git') parser.add_argument('--kibiter-version', dest='kibiter_version', help='Kibiter version', default='6.8.6') parser.add_argument('--elasticsearch-url', dest='elasticsearch_url', help='ElasticSearch URL', default=None) parser.add_argument('--import-dashboards', dest='import_dashboards', action='store_true', help='Import dashboards') parser.add_argument('--set-top-menu', dest='set_top_menu', action='store_true', help='Set top menu') parser.add_argument('--set-project-name', dest='set_project_name', action='store_true', help='Set project name') parser.add_argument('--set-config', dest='set_config', action='store_true', help='Set the Kibiter conf') parser.add_argument('--overwrite', dest='overwrite', action='store_true', help='Overwrite existing Kibana obj') args = parser.parse_args() if args.set_top_menu and not args.elasticsearch_url: logger.error("ElasticSearch URL not declared") return return args
def save_yaml(data, file_path): """Save the data defined as dict to a YAML file :param data: a dictionary containing the top menu data :param file_path: path of the YAML file """ menu = yaml.dump(data, default_flow_style=False) with open(file_path, 'w') as f: try: f.write(menu) except yaml.YAMLError as ex: logger.error(ex)
def load_yaml(file_path): """Load the YAML file containing the top menu and returns its content :param file_path: path of the YAML file """ with open(file_path, 'r') as f: try: menu = yaml.load(f, Loader=yaml.SafeLoader) except yaml.YAMLError as ex: logger.error(ex) return logger.info("%s loaded" % file_path) return menu
def export_batch(archimedes, dashboards, by='id', force=False): for title in dashboards: dashboard_id = dashboards[title] logger.info("Exporting dashboard %s" % title) try: if by == 'id': archimedes.export_to_disk(obj_type="dashboard", obj_id=dashboard_id, force=force) else: archimedes.export_to_disk(obj_type="dashboard", obj_title=title, force=force) except Exception as ex: logger.error("Impossible to import dashboard %s, skipping it. %s" % (dashboard_id, ex)) continue
def set_kibiter_endpoint(elasticsearch_url, kibiter_index, endpoint, data): """Set a target Kibiter endpoint :param elasticsearch_url: URL of elasticsearch DB :param kibiter_index: name of the target index :param endpoint: the Kibiter-related endpoint (`metadashbord` or `projectname`) :param data: the content to be uploaded """ endpoint_url = urijoin(elasticsearch_url, kibiter_index, 'doc', endpoint) response = requests.put(endpoint_url, data=json.dumps(data), headers=CONTENT_TYPE_HEADER, verify=False) try: response.raise_for_status() logger.info("%s successfully set" % endpoint) except Exception as ex: logger.error("%s not set: %s" % (endpoint, ex))
def get_kibiter_data(elasticsearch_url, kibiter_index, endpoint): """Get the data from a Kibiter endpoint :param elasticsearch_url: URL of elasticsearch DB :param kibiter_index: name of the target index :param endpoint: the top-menu Kibiter-related endpoint (`metadashbord` or `projectname`) :return the content of the endpoint """ endpoint_url = urijoin(elasticsearch_url, kibiter_index, 'doc', endpoint) response = requests.get(endpoint_url, headers=CONTENT_TYPE_HEADER, verify=False) r_json = None try: response.raise_for_status() r_json = response.json()['_source'][endpoint] except Exception as ex: logger.error("No data retrieved from %s, %s" % (endpoint, ex)) return r_json