def execute(self, connections, **kwrds): """Execute the search for processes, queries, or connections This method searches for processes, queriers, or connections to either kill or display the matches for one or more servers. connections[in] list of connection parameters kwrds[in] dictionary of options output file stream to display information default = sys.stdout connector connector to use default = mysql.connector format format for display default = GRID """ output = kwrds.get('output', sys.stdout) connector = kwrds.get('connector', mysql.connector) fmt = kwrds.get('format', "grid") charset = kwrds.get('charset', None) headers = ("Connection", "Id", "User", "Host", "Db", "Command", "Time", "State", "Info") entries = [] # Build SQL statement for info in connections: conn = parse_connection(info) if not conn: msg = "'%s' is not a valid connection specifier" % (info, ) raise FormatError(msg) if charset: conn['charset'] = charset info = conn connection = connector.connect(**info) if not charset: # If no charset provided, get it from the # "character_set_client" server variable. cursor = connection.cursor() cursor.execute("SHOW VARIABLES LIKE 'character_set_client'") res = cursor.fetchall() connection.set_charset_collation(charset=str(res[0][1])) cursor.close() cursor = connection.cursor() cursor.execute(self.__select) for row in cursor: if KILL_QUERY in self.__actions: cursor.execute("KILL {0}".format(row[0])) if KILL_CONNECTION in self.__actions: cursor.execute("KILL {0}".format(row[0])) if PRINT_PROCESS in self.__actions: entries.append(tuple([_spec(info)] + list(row))) # If output is None, nothing is printed if len(entries) > 0 and output: entries.sort(key=lambda fifth: fifth[5]) print_list(output, fmt, headers, entries) elif PRINT_PROCESS in self.__actions: raise EmptyResultError("No matches found")
def _match(pattern, search_str): """Returns the groups from string search or raise FormatError if it does not match with the pattern. """ grp = pattern.match(search_str) if not grp: raise FormatError(_BAD_CONN_FORMAT.format(connection_values)) return grp.groups()
def execute(self, connections, output=sys.stdout, connector=mysql.connector, **kwrds): """Execute the search for objects This method searches for objects that match a search criteria for one or more servers. connections[in] list of connection parameters output[in] file stream to display information default = sys.stdout connector[in] connector to use default = mysql.connector kwrds[in] dictionary of options format format for display default = GRID """ fmt = kwrds.get('format', "grid") charset = kwrds.get('charset', None) entries = [] for info in connections: conn = parse_connection(info) if not conn: msg = "'%s' is not a valid connection specifier" % (info, ) raise FormatError(msg) if charset: conn['charset'] = charset info = conn conn['host'] = conn['host'].replace("[", "") conn['host'] = conn['host'].replace("]", "") connection = connector.connect(**info) if not charset: # If no charset provided, get it from the # "character_set_client" server variable. cursor = connection.cursor() cursor.execute("SHOW VARIABLES LIKE 'character_set_client'") res = cursor.fetchall() connection.set_charset_collation(charset=str(res[0][1])) cursor.close() cursor = connection.cursor() cursor.execute(self.__sql) entries.extend( [tuple([_spec(info)] + list(row)) for row in cursor]) headers = ["Connection"] headers.extend(col[0].title() for col in cursor.description) if len(entries) > 0 and output: print_list(output, fmt, headers, entries) else: msg = "Nothing matches '%s' in any %s" % \ (self.__pattern, _join_words(self.__types, conjunction="or")) raise EmptyResultError(msg)
def _match(pattern, connection_str, trow_error=True): """Tries to match a pattern with the connection string and returns the groups. """ grp = pattern.match(connection_str) if not grp: if trow_error: raise FormatError(_BAD_CONN_FORMAT.format(connection_str)) return False return grp.groups()
def parse_server_address(connection_str): """Parses host, port and socket from the given connection string. Returns a tuple of (host, port, socket, add_type) where add_type is the name of the parser that successfully parsed the hostname from the connection string. """ # Default values to return. host = None port = None socket = None address_type = None unparsed = None # From the matchers look the one that match a host. # pylint: disable=R0101 for IP_matcher in IP_matchers_list: try: group = _match(IP_matchers[IP_matcher], connection_str) if group: host = group[0] if IP_matcher == ipv6: host = "[%s]" % host if group[1]: part2_port_socket = _match(_CONN_port_ONLY, group[1], trow_error=False) if not part2_port_socket: unparsed = group[1] else: port = part2_port_socket[0] if part2_port_socket[1]: part4 = _match(_CONN_socket_ONLY, part2_port_socket[1], trow_error=False) if not part4: unparsed = part2_port_socket[1] else: socket = part4[0] unparsed = part4[1] # If host is match we stop looking as is the most significant. if host: address_type = IP_matcher break # ignore the error trying to match. except FormatError: pass # we must alert, that the connection could not be parsed. if host is None: raise FormatError(_BAD_CONN_FORMAT.format(connection_str)) _verify_parsing(connection_str, host, port, socket, address_type, unparsed) return host, port, socket, address_type
def _make_age_cond(age): """Make age condition Accept an age description return a timedelta representing the age. We allow the forms: hh:mm:ss, mm:ss, 4h3m, with suffixes d (days), w (weeks), h (hours), m (minutes), and s(seconds) age[in] Age (time) Returns string - time delta """ from mysql.utilities.exception import FormatError mobj = re.match(r"([+-])?(?:(?:(\d?\d):)?(\d?\d):)?(\d?\d)\Z", age) if mobj: sign, hrs, mins, secs = mobj.groups() if not hrs: hrs = 0 if not mins: mins = 0 seconds = int(secs) + 60 * (int(mins) + 60 * int(hrs)) oper = "<=" if sign and sign == "-" else ">=" return ' {0} {1} {2}'.format(TIME, oper, seconds) mobj = re.match(r"([+-])?(\d+[dwhms])+", age) if mobj: sign = None if mobj.group(1): sign = age[0] age = age[1:] seconds = 0 periods = [x for x in re.split("(\d+[dwhms])", age)] if len(''.join(x[0::2])) > 0: raise FormatError(_INCORRECT_FORMAT_MSG.format(age)) args = {} for period in periods[1::2]: seconds += int(period[0:-1]) * _SECS[period[-1:]] oper = "<=" if sign and sign == "-" else ">=" return ' {0} {1} {2}'.format(TIME, oper, seconds) raise FormatError(_INCORRECT_FORMAT_MSG.format(age))
def execute(self, connections, output=sys.stdout, connector=mysql.connector, **kwrds): """Execute the search for objects This method searches for objects that match a search criteria for one or more servers. connections[in] list of connection parameters output[in] file stream to display information default = sys.stdout connector[in] connector to use default = mysql.connector kwrds[in] dictionary of options format format for display default = GRID """ from mysql.utilities.exception import FormatError, EmptyResultError format = kwrds.get('format', "grid") entries = [] for info in connections: conn = parse_connection(info) if not conn: msg = "'%s' is not a valid connection specifier" % (info, ) raise FormatError(msg) info = conn connection = connector.connect(**info) cursor = connection.cursor() cursor.execute(self.__sql) entries.extend( [tuple([_spec(info)] + list(row)) for row in cursor]) headers = ["Connection"] headers.extend(col[0].title() for col in cursor.description) if len(entries) > 0 and output: print_list(output, format, headers, entries) else: msg = "Nothing matches '%s' in any %s" % ( self.__pattern, _join_words(self.__types, conjunction="or")) raise EmptyResultError(msg)
def _verify_parsing(connection_str, host, port, socket, address_type, unparsed): """Verify that the connection string was totally parsed and not parts of it where not matched, otherwise raise an error. """ exp_connection_str = connection_str log.debug("exp_connection_str {0}".format(exp_connection_str)) parsed_connection_list = [] if host: log.debug("host {0}".format(host)) if address_type == ipv6 and "[" not in connection_str: host = host.replace("[", "") host = host.replace("]", "") parsed_connection_list.append(host) if port: log.debug("port {0}".format(port)) parsed_connection_list.append(port) if socket: log.debug("socket {0}".format(socket)) parsed_connection_list.append(socket) parsed_connection = ":".join(parsed_connection_list) log.debug('parsed_connection {0}'.format(parsed_connection)) diff = None if not unparsed: log.debug('not unparsed found, creating diff') diff = connection_str.replace(host, "") if port: diff = diff.replace(port, "") if socket: diff = diff.replace(socket, "") log.debug("diff {0}".format(diff)) log.debug("unparsed {0}".format(unparsed)) if unparsed or (exp_connection_str != parsed_connection and (diff and diff != ":")): log.debug("raising exception") parsed_args = "host:%s, port:%s, socket:%s" % (host, port, socket) log.debug( _UNPARSED_CONN_FORMAT.format(connection_str, parsed_args, unparsed)) raise FormatError( _UNPARSED_CONN_FORMAT.format(connection_str, parsed_args, unparsed))
def parse_connection(connection_values, my_defaults_reader=None, options=None): """Parse connection values. The function parses a connection specification of one of the forms:: - user[:password]@host[:port][:socket] - login-path[:port][:socket] A dictionary is returned containing the connection parameters. The function is designed so that it shall be possible to use it with a ``connect`` call in the following manner:: options = parse_connection(spec) conn = mysql.connector.connect(**options) conn_values[in] Connection values in the form: user:password@host:port:socket or login-path:port:socket my_defaults_reader[in] Instance of MyDefaultsReader to read the information of the login-path from configuration files. By default, the value is None. options[in] Dictionary of options (e.g. basedir), from the used utility. By default, it set with an empty dictionary. Note: also supports options values from optparse. Notes: This method validates IPv4 addresses and standard IPv6 addresses. This method accepts quoted host portion strings. If the host is marked with quotes, the code extracts this without validation and assigns it to the host variable in the returned tuple. This allows users to specify host names and IP addresses that are outside of the supported validation. Returns dictionary (user, passwd, host, port, socket) or raise an exception if parsing error """ if options is None: options = {} def _match(pattern, search_str): """Returns the groups from string search or raise FormatError if it does not match with the pattern. """ grp = pattern.match(search_str) if not grp: raise FormatError(_BAD_CONN_FORMAT.format(connection_values)) return grp.groups() # SSL options, must not be overwritten with those from options. ssl_ca = None ssl_cert = None ssl_key = None ssl = None # Split on the '@' to determine the connection string format. # The user/password may have the '@' character, split by last occurrence. conn_format = connection_values.rsplit('@', 1) if len(conn_format) == 1: # No '@' so try config-path and login-path # The config_path and login-path collide on their first element and # only differs on their secondary optional values. # 1. Try match config_path and his optional value group. If both # matches and the connection data can be retrieved, return the data. # If errors occurs in this step raise them immediately. # 2. If config_path matches but group does not, and connection data # can not be retrieved, do not raise errors and try to math # login_path on step 4. # 3. If connection data is retrieved on step 2, then try login_path on # next step to overwrite values from the new configuration. # 4. If login_path matches, check is .mylogin.cnf exists, if it doesn't # and data configuration was found verify it for missing values and # continue if they are not any missing. # 5. If .mylogin.cnf exists and data configuration is found, overwrite # any previews value from config_path if there is any. # 6. If login_path matches a secondary value but the configuration data # could not be retrieved, do not continue and raise any error. # 7. In case errors have occurred trying to get data from config_path, # and group did not matched, and in addition no secondary value, # matched from login_path (port of socket) mention that config_path # and login_path were not able to retrieve the connection data. # try login_path and overwrite the values. # Handle the format: config-path[[group]] config_path, group = _match(_CONN_CONFIGPATH, conn_format[0]) port = None socket = None config_path_data = None login_path_data = None config_path_err_msg = None login_path = None if config_path: try: # If errors occurs, and group matched: raise any errors as the # group is exclusive of config_path. config_path_data = handle_config_path(config_path, group) except UtilError as err: if group: raise else: # Convert first letter to lowercase to include in error # message with the correct case. config_path_err_msg = \ err.errmsg[0].lower() + err.errmsg[1:] if group is None: # the conn_format can still be a login_path so continue # No '@' then handle has in the format: login-path[:port][:socket] login_path, port, socket = _match(_CONN_LOGINPATH, conn_format[0]) # Check if the login configuration file (.mylogin.cnf) exists if login_path and not my_login_config_exists(): if not config_path_data: util_err_msg = (".mylogin.cnf was not found at is default " "location: {0}. Please configure your " "login-path data before using it (use the " "mysql_config_editor tool)." "".format(my_login_config_path())) if config_path_err_msg and not (port or socket): util_err_msg = ("{0} In addition, {1}" "").format(util_err_msg, config_path_err_msg) raise UtilError(util_err_msg) else: # If needed, create a MyDefaultsReader and search for # my_print_defaults tool. if not my_defaults_reader: try: my_defaults_reader = MyDefaultsReader(options) except UtilError as err: if config_path_err_msg and not (port or socket): util_err_msg = ("{0} In addition, {1}" "").format(err.errmsg, config_path_err_msg) raise UtilError(util_err_msg) else: raise elif not my_defaults_reader.tool_path: my_defaults_reader.search_my_print_defaults_tool() # Check if the my_print_default tool is able to read a # login-path from the mylogin configuration file if not my_defaults_reader.check_login_path_support(): util_err_msg = ("the used my_print_defaults tool does not " "support login-path options: {0}. " "Please confirm that the location to a " "tool with login-path support is included " "in the PATH (at the beginning)." "".format(my_defaults_reader.tool_path)) if config_path_err_msg and not (port or socket): util_err_msg = ("{0} In addition, {1}" "").format(util_err_msg, config_path_err_msg) raise UtilError(util_err_msg) # Read and parse the login-path data (i.e., user, password and # host) login_path_data = my_defaults_reader.get_group_data(login_path) if config_path_data or login_path_data: if config_path_data: if not login_path_data: login_path_data = config_path_data else: # Overwrite values from login_path_data config_path_data.update(login_path_data) login_path_data = config_path_data user = login_path_data.get('user', None) passwd = login_path_data.get('password', None) host = login_path_data.get('host', None) if not port: port = login_path_data.get('port', None) if not socket: socket = login_path_data.get('socket', None) if os.name == "posix" and socket is not None: # if we are on unix systems and used a socket, hostname can be # safely assumed as being localhost so it is not required required_options = ('user', 'socket') host = 'localhost' if host is None else host else: required_options = ('user', 'host', 'port') missing_options = [ opt for opt in required_options if locals()[opt] is None ] # If we are on unix and port is missing, user might have specified # a socket instead if os.name == "posix" and "port" in missing_options: i = missing_options.index("port") if socket: # If we have a socket, port is not needed missing_options.pop(i) else: # if we don't have neither a port nor a socket, we need # either a port or a socket missing_options[i] = "port or socket" if missing_options: message = ",".join(missing_options) if len(missing_options) > 1: comma_idx = message.rfind(",") message = "{0} and {1}".format(message[:comma_idx], message[comma_idx + 1:]) pluralize = "s" if len(missing_options) > 1 else "" raise UtilError("Missing connection value{0} for " "{1} option{0}".format(pluralize, message)) # optional options, available only on config_path_data if config_path_data: ssl_ca = config_path_data.get('ssl-ca', None) ssl_cert = config_path_data.get('ssl-cert', None) ssl_key = config_path_data.get('ssl-key', None) ssl = config_path_data.get('ssl', None) else: if login_path and not config_path: raise UtilError("No login credentials found for login-path: " "{0}. Please review the used connection string" ": {1}".format(login_path, connection_values)) elif not login_path and config_path: raise UtilError("No login credentials found for config-path: " "{0}. Please review the used connection string" ": {1}".format(login_path, connection_values)) elif login_path and config_path: raise UtilError("No login credentials found for either " "login-path: '{0}' nor config-path: '{1}'. " "Please review the used connection string: {2}" "".format(login_path, config_path, connection_values)) elif len(conn_format) == 2: # Check to see if the user attempted to pass a list of connections. # This is true if there is at least one comma and multiple @ symbols. if ((connection_values.find(',') > 0) and (connection_values.find('@') > 1)): raise FormatError(_MULTIPLE_CONNECTIONS.format(connection_values)) # Handle as in the format: user[:password]@host[:port][:socket] userpass, hostportsock = conn_format # Get user, password match = _CONN_USERPASS.match(userpass) if not match: raise FormatError(_BAD_CONN_FORMAT.format(connection_values)) user = match.group('user') if user is None: # No password provided user = match.group('suser').rstrip(':') passwd = match.group('passwd') # Handle host, port and socket if len(hostportsock) <= 0: raise FormatError(_BAD_CONN_FORMAT.format(connection_values)) if hostportsock[0] in ['"', "'"]: # need to strip the quotes host, port, socket = _match(_CONN_QUOTEDHOST, hostportsock) if host[0] == '"': host = host.strip('"') if host[0] == "'": host = host.strip("'") else: host, port, socket, _ = parse_server_address(hostportsock) else: # Unrecognized format raise FormatError(_BAD_CONN_FORMAT.format(connection_values)) # Get character-set from options if isinstance(options, dict): charset = options.get("charset", None) # If one SSL option was found before, not mix with those in options. if not ssl_cert and not ssl_ca and not ssl_key and not ssl: ssl_cert = options.get("ssl_cert", None) ssl_ca = options.get("ssl_ca", None) ssl_key = options.get("ssl_key", None) ssl = options.get("ssl", None) else: # options is an instance of optparse.Values try: charset = options.charset # pylint: disable=E1103 except AttributeError: charset = None # If one SSL option was found before, not mix with those in options. if not ssl_cert and not ssl_ca and not ssl_key and not ssl: try: ssl_cert = options.ssl_cert # pylint: disable=E1103 except AttributeError: ssl_cert = None try: ssl_ca = options.ssl_ca # pylint: disable=E1103 except AttributeError: ssl_ca = None try: ssl_key = options.ssl_key # pylint: disable=E1103 except AttributeError: ssl_key = None try: ssl = options.ssl # pylint: disable=E1103 except AttributeError: ssl = None # Set parsed connection values connection = { "user": user, "host": host, "port": int(port) if port else 3306, "passwd": passwd if passwd else '' } if charset: connection["charset"] = charset if ssl_cert: connection["ssl_cert"] = ssl_cert if ssl_ca: connection["ssl_ca"] = ssl_ca if ssl_key: connection["ssl_key"] = ssl_key if ssl: connection["ssl"] = ssl # Handle optional parameters. They are only stored in the dict if # they were provided in the specifier. if socket is not None and os.name == "posix": connection['unix_socket'] = socket return connection
def parse_user_password(userpass_values, my_defaults_reader=None, options={}): """ This function parses a string with the user/password credentials. This function parses the login string, determines the used format, i.e. user[:password] or login-path. If the ':' (colon) is not in the login string, the it can refer to a login-path or to a username (without a password). In this case, first it is assumed that the specified value is a login-path and the function attempts to retrieve the associated username and password, in a quiet way (i.e., without raising exceptions). If it fails to retrieve the login-path data, then the value is assumed to be a username. userpass_values[in] String indicating the user/password credentials. It must be in the form: user[:password] or login-path. my_defaults_reader[in] Instance of MyDefaultsReader to read the information of the login-path from configuration files. By default, the value is None. options[in] Dictionary of options (e.g. basedir), from the used utility. By default, it set with an empty dictionary. Note: also supports options values from optparse. Returns a tuple with the username and password. """ # Split on the ':' to determine if a login-path is used. login_values = userpass_values.split(':') if len(login_values) == 1: # Format is login-path or user (without a password): First, assume it # is a login-path and quietly try to retrieve the user and password. # If it fails, assume a user name is being specified. #Check if the login configuration file (.mylogin.cnf) exists if login_values[0] and not my_login_config_exists(): return login_values[0], None if not my_defaults_reader: # Attempt to create the MyDefaultsReader try: my_defaults_reader = MyDefaultsReader(options) except UtilError: # Raise an UtilError when my_print_defaults tool is not found. return login_values[0], None elif not my_defaults_reader.tool_path: # Try to find the my_print_defaults tool try: my_defaults_reader.search_my_print_defaults_tool() except UtilError: # Raise an UtilError when my_print_defaults tool is not found. return login_values[0], None # Check if the my_print_default tool is able to read a login-path from # the mylogin configuration file if not my_defaults_reader.check_login_path_support(): return login_values[0], None # Read and parse the login-path data (i.e., user and password) try: loginpath_data = my_defaults_reader.get_group_data(login_values[0]) if loginpath_data: user = loginpath_data.get('user', None) passwd = loginpath_data.get('password', None) return user, passwd else: return login_values[0], None except UtilError: # Raise an UtilError if unable to get the login-path group data return login_values[0], None elif len(login_values) == 2: # Format is user:password; return a tuple with the user and password return login_values[0], login_values[1] else: # Invalid user credentials format return FormatError("Unable to parse the specified user credentials " "(accepted formats: <user>[:<password> or " "<login-path>): %s" % userpass_values)
def _match(pattern, search_str): grp = pattern.match(search_str) if not grp: raise FormatError(_BAD_CONN_FORMAT.format(connection_values)) return grp.groups()
def parse_connection(connection_values, my_defaults_reader=None, options={}): """Parse connection values. The function parses a connection specification of one of the forms:: - user[:password]@host[:port][:socket] - login-path[:port][:socket] A dictionary is returned containing the connection parameters. The function is designed so that it shall be possible to use it with a ``connect`` call in the following manner:: options = parse_connection(spec) conn = mysql.connector.connect(**options) conn_values[in] Connection values in the form: user:password@host:port:socket or login-path:port:socket my_defaults_reader[in] Instance of MyDefaultsReader to read the information of the login-path from configuration files. By default, the value is None. options[in] Dictionary of options (e.g. basedir), from the used utility. By default, it set with an empty dictionary. Note: also supports options values from optparse. Notes: This method validates IPv4 addresses and standard IPv6 addresses. This method accepts quoted host portion strings. If the host is marked with quotes, the code extracts this without validation and assigns it to the host variable in the returned tuple. This allows users to specify host names and IP addresses that are outside of the supported validation. Returns dictionary (user, passwd, host, port, socket) or raise an exception if parsing error """ def _match(pattern, search_str): grp = pattern.match(search_str) if not grp: raise FormatError(_BAD_CONN_FORMAT.format(connection_values)) return grp.groups() # Split on the '@' to determine the connection string format. conn_format = connection_values.split('@') if len(conn_format) == 1: # No '@' then handle has in the format: login-path[:port][:socket] login_path, port, socket = _match(_CONN_LOGINPATH, conn_format[0]) #Check if the login configuration file (.mylogin.cnf) exists if login_path and not my_login_config_exists(): raise UtilError(".mylogin.cnf was not found at is default " "location: %s." "Please configure your login-path data before " "using it (use the mysql_config_editor tool)." % my_login_config_path()) # If needed, create a MyDefaultsReader and search for my_print_defaults # tool. if not my_defaults_reader: my_defaults_reader = MyDefaultsReader(options) elif not my_defaults_reader.tool_path: my_defaults_reader.search_my_print_defaults_tool() # Check if the my_print_default tool is able to read a login-path from # the mylogin configuration file if not my_defaults_reader.check_login_path_support(): raise UtilError("the used my_print_defaults tool does not " "support login-path options: %s. " "Please confirm that the location to a tool with " "login-path support is included in the PATH " "(at the beginning)." % my_defaults_reader.tool_path) # Read and parse the login-path data (i.e., user, password and host) login_path_data = my_defaults_reader.get_group_data(login_path) if login_path_data: user = login_path_data.get('user', None) passwd = login_path_data.get('password', None) host = login_path_data.get('host', None) if not port: port = login_path_data.get('port', 3306) if not socket: socket = login_path_data.get('socket', None) else: raise UtilError("No login credentials found for login-path: %s. " "Please review the used connection string: %s" % (login_path, connection_values)) elif len(conn_format) == 2: # Handle as in the format: user[:password]@host[:port][:socket] userpass, hostportsock = conn_format # Get user, password user, passwd = _match(_CONN_USERPASS, userpass) # Handle host, port and socket if len(hostportsock) <= 0: raise FormatError(_BAD_CONN_FORMAT.format(connection_values)) if hostportsock[0] in ['"', "'"]: # need to strip the quotes host, port, socket = _match(_CONN_QUOTEDHOST, hostportsock) if host[0] == '"': host = host.strip('"') if host[0] == "'": host = host.strip("'") elif len( hostportsock.split(":")) <= 3: # if fewer colons, must be IPv4 host, port, socket = _match(_CONN_IPv4, hostportsock) else: host, port, socket = _match(_CONN_IPv6, hostportsock) else: # Unrecognized format raise FormatError(_BAD_CONN_FORMAT.format(connection_values)) # Set parsed connection values connection = { "user": user, "host": host, "port": int(port) if port else 3306, "passwd": passwd if passwd else '' } # Handle optional parameters. They are only stored in the dict if # they were provided in the specifier. if socket is not None and os.name == "posix": connection['unix_socket'] = socket return connection
def execute(self, connections, **kwrds): """Execute the search for processes, queries, or connections This method searches for processes, queriers, or connections to either kill or display the matches for one or more servers. connections[in] list of connection parameters kwrds[in] dictionary of options output file stream to display information default = sys.stdout connector connector to use default = mysql.connector format format for display default = GRID """ output = kwrds.get('output', sys.stdout) connector = kwrds.get('connector', mysql.connector) fmt = kwrds.get('format', "grid") charset = kwrds.get('charset', None) ssl_opts = kwrds.get('ssl_opts', {}) headers = ("Connection", "Id", "User", "Host", "Db", "Command", "Time", "State", "Info") entries = [] # Build SQL statement for info in connections: conn = parse_connection(info) if not conn: msg = "'%s' is not a valid connection specifier" % (info,) raise FormatError(msg) if charset: conn['charset'] = charset info = conn if connector == mysql.connector: # Add SSL parameters ONLY if they are not None add_ssl_flag = False if ssl_opts.get('ssl_ca') is not None: info['ssl_ca'] = ssl_opts.get('ssl_ca') add_ssl_flag = True if ssl_opts.get('ssl_cert') is not None: info['ssl_cert'] = ssl_opts.get('ssl_cert') add_ssl_flag = True if ssl_opts.get('ssl_key') is not None: info['ssl_key'] = ssl_opts.get('ssl_key') add_ssl_flag = True if add_ssl_flag: cpy_flags = [ClientFlag.SSL, ClientFlag.SSL_VERIFY_SERVER_CERT] info['client_flags'] = cpy_flags connection = connector.connect(**info) if not charset: # If no charset provided, get it from the # "character_set_client" server variable. cursor = connection.cursor() cursor.execute("SHOW VARIABLES LIKE 'character_set_client'") res = cursor.fetchall() connection.set_charset_collation(charset=str(res[0][1])) cursor.close() cursor = connection.cursor() cursor.execute(self.__select) print_rows = [] cols = ["Id", "User", "Host", "db", "Command", "Time", "State", "Info"] for row in cursor: if (KILL_QUERY in self.__actions) or \ (KILL_CONNECTION in self.__actions): print_rows.append(row) cursor.execute("KILL {0}".format(row[0])) if PRINT_PROCESS in self.__actions: entries.append(tuple([_spec(info)] + list(row))) if print_rows: print "# The following KILL commands were executed:" print_list(output, fmt, cols, print_rows) # If output is None, nothing is printed if len(entries) > 0 and output: entries.sort(key=lambda fifth: fifth[5]) print_list(output, fmt, headers, entries) elif PRINT_PROCESS in self.__actions: raise EmptyResultError("No matches found")