def get_graylog_api(cfg, environment, host, password, port, proxy, no_tls, username, keyring): gl_api = None scheme = "https" if no_tls: scheme = "http" if scheme == "https" and port is None: port = 443 port = port or 80 proxies = {scheme: proxy} if proxy else None if environment is not None: gl_api = GraylogAPIFactory.api_from_config(cfg, environment, port, proxies, no_tls, username) else: if host is not None: if username is None: username = CliInterface.prompt_username(scheme, host, port) gl_api = GraylogAPIFactory.api_from_host(host=host, port=port, username=username, password=password, scheme=scheme, proxies=proxies, tls=no_tls) else: if cfg.has_section("environment:default"): gl_api = GraylogAPIFactory.api_from_config( cfg, "default", port, proxies, no_tls, username) else: cli_error( "Error: No host or environment configuration specified and no default found." ) if not password and keyring: password = get_password_from_keyring(gl_api.host, gl_api.username) if not password: password = CliInterface.prompt_password(scheme, gl_api.host, gl_api.port, gl_api.username, gl_api.api_path) gl_api.password = password if keyring: store_password_in_keyring(gl_api.host, gl_api.username, password) gl_api.update_host_timezone(gl_api.user_info().get('timezone')) return gl_api
def replace_log_level(query): log_level_regex = 'level\:\s*[a-zA-Z]+\s*' match = re.search(log_level_regex, query) if match: log_level_name = match.group(0).split(':')[1].strip().upper() log_level_code = LogLevel.find_by_level_name(log_level_name) if not log_level_code: message = "The given log level({}) is invalid. Use one of the following: {}" cli_error( message.format(log_level_name, LogLevel.list_levels())) exit() return re.sub(log_level_regex, 'level:%s ' % log_level_code, query) else: return query
def select_saved_query(graylog_api): searches = graylog_api.get_saved_queries()["searches"] if not searches: cli_error("You have no saved queries to display.") for i, search in enumerate(searches): search_title = search["title"].encode(utils.UTF8) query = search["query"]["query"].encode(utils.UTF8) or "*" message = "{}: Query '{}' (query: {})".format( i, search_title, query) click.echo(message) search_index = click.prompt("Enter query number:", type=int, default=0) search = searches[search_index] query = search['query']['query'].encode(utils.UTF8) or '*' fields = search['query']['fields'].encode(utils.UTF8).split(',') return query, fields
def api_from_config(cfg, env_name, port, proxies, no_tls, username): section_name = "environment:" + env_name host = None if cfg.has_option(section_name, utils.HOST): host = cfg.get(section_name, utils.HOST) else: cli_error( "'host' option is not available in section [%(section_name)s]" % {'section_name': section_name}) if not port and cfg.has_option(section_name, utils.PORT): port = cfg.get(section_name, utils.PORT) if cfg.has_option(section_name, utils.API_PATH): api_path = cfg.get(section_name, utils.API_PATH) else: api_path = utils.DEFAULT_API_PATH scheme = "https" if no_tls: scheme = "http" if not username and cfg.has_option(section_name, utils.USERNAME): username = cfg.get(section_name, utils.USERNAME) elif not username: username = CliInterface.prompt_username(scheme, host, port) if not proxies and cfg.has_option(section_name, utils.PROXY): proxies = {scheme: cfg.get(section_name, utils.PROXY)} default_stream = None if cfg.has_option(section_name, utils.DEFAULT_STREAM): default_stream = cfg.get(section_name, utils.DEFAULT_STREAM) return GraylogAPI(host=host, port=port, api_path=api_path, username=username, default_stream=default_stream, scheme=scheme, proxies=proxies)