HEX
Server: LiteSpeed
System: Linux node612.namehero.net 4.18.0-553.121.1.lve.el8.x86_64 #1 SMP Thu Apr 30 16:40:41 UTC 2026 x86_64
User: dfwparty (1186)
PHP: 8.3.31
Disabled: NONE
Upload Files
File: //lib/python3.6/site-packages/sos/report/plugins/__pycache__/charmed_mysql.cpython-36.pyc
3

ܓ�i�&�@sFddlZddlZddlmZmZmZddlmZGdd�dee�ZdS)�N)�Plugin�	PluginOpt�UbuntuPlugin)�
is_executablec	@s�eZdZdZdZdZdddddd	gZd
ZdZdZ	d
Z
dZdddddd�Ze
ddedd�e
ddedd�e
ddedd�e
dd ed!d�gZd"d#�Zd$d%�Zd&d'�Zd(d)�Zd*d+�Zd,d-�Zd.d/�Zd0d1�Zd2d3�Zd4S)5�CharmedMySQLa�
    The Charmed MySQL plugin is used to collect MySQL configuration and logs
    from the Charmed MySQL snap package or K8s deployment.
    It also collects MySQL Router and MySQL Shell configuration and logs
    where available, journal logs for the snap, and snap info.

    If the `dumpdbs` option is set to `True`, the plugin will also try and
    collect the names of the databases that the user has access to. The
    `mysql` user is used by default, but that can be set using the `dbuser`
    option. When using the `dumpdbs` option, you must then provide the
    password for the user using the `dbpass` option or the `MYSQL_PWD`
    environment variable.
    z
Charmed MySQLZ
charmed_mysqlzshow databases;z*select * from mysql.juju_units_operations;zPselect * from performance_schema.replication_group_members order by MEMBER_HOST;z"show global status like "%conne%";z,select * from performance_schema.host_cache;zshow ENGINE InnoDB STATUS;z
charmed-mysqlz/var/snap/charmed-mysql/commonz/var/snap/charmed-mysql/currentZkubectlz app.kubernetes.io/name=mysql-k8sz
/etc/mysqlz/var/log/mysqlz/etc/mysqlrouterz/var/log/mysqlrouterz/var/log/mysqlsh)�
MYSQL_CONF�
MYSQL_LOGS�MYSQL_ROUTER_CONF�MYSQL_ROUTER_LOGS�MYSQL_SHELL_LOGS�dbuser�mysqlz%Username for database dump collection)�defaultZval_typeZdesc�dbpass�z%Password for database dump collection�dumpdbsFzGet name of all databases�
logs_sinceZ48hz9How far back to fetch logs with kubectl --since, K8s onlycCsX|jd�}|jd�}dtjkr<|r<|jjd�tjd}n|sP|jjd�dS||fS)Nrr�	MYSQL_PWDzWMYSQL_PWD present: Using MYSQL_PWD environment variable, user did not provide password.zHdumpdbs_error: option is set, but username and password are not provided)NN)�
get_option�os�environZsoslog�infoZwarning)�self�db_user�db_pass�r�#/usr/lib/python3.6/charmed_mysql.py�_get_db_credentials[s

z CharmedMySQL._get_db_credentialscCsB|j|j�d|j�d��}|ddkr>tt|dj�j���SgS)Nz get pods -A -l z- -o jsonpath='{.items[*].metadata.namespace}'�statusr�output)�exec_cmd�kube_cmd�selector�list�set�strip�split)r�
namespacesrrr�_determine_namespacesns
z"CharmedMySQL._determine_namespacescCs@|j|j�d|�d|j�d��}|ddkr<|dj�j�SgS)Nz -n z
 get pods -l z( -o jsonpath='{.items[*].metadata.name}'rrr)r r!r"r%r&)r�	namespace�podsrrr�_get_pod_namesws
zCharmedMySQL._get_pod_namescCsB|j�d|��}|j|�d|j�d�|�d|j��g�|j|�d|j�d|�d��d}|j|�}|j�}xx|jj�D]j\}}x`|D]X}	tj|�d	|	�d	|��d
d�|�d|�d
|	�d|�d
|�d	|	�d	|��
}
|j	|
�q�WqxW|j
|�d�|�d�g�|jd��r>|j�\}}|�s(|�r,dSd|��}
d|
�d�}d
j
dd�|jD��}tj���}d}|d	|}t|ddd��}|j|�WdQRXx�|D]�}	|�d|�d
|	�d|��}|j	|�|�d|�d
|�d
|	�d|��	}
|j	|
�|�d|�d
|	�d|�d|�d|�d |��
}|j||	�d!�d"��q�WWdQRXdS)#Nz -n z get all -l z -o widez describe pods -l z	 logs -l z	 --since=z* --all-containers=true --prefix --all-podsr
�/T)�exist_okz cp -c � �:z/*/MYSQL_CONF/*.pemz/*/MYSQL_CONF/*.keyrz-h 127.0.0.1 -uzmysql z -ecss|]}|jdd�VqdS)�"z\"N)�replace)�.0�queryrrr�	<genexpr>�sz6CharmedMySQL._collect_per_namespace.<locals>.<genexpr>Z	mysql_pwd�w�utf8)�encodingz	 exec -c z
 -- mkdir -p z -- sh -lc 'MYSQL_PWD=$(cat z) z "z
"' && rm -rf z_dbs.txt)Zsuggest_filename)r!�add_cmd_outputr"r+Zget_cmd_output_path�
conf_paths�itemsr�makedirsr �add_forbidden_pathrr�join�
mysql_queries�tempfileZTemporaryDirectory�open�write)rr)rr!Z
mysql_contr*Zdump_files_path�name�pathZpodZcopy_cmdrr�opts�sql_cmdZqueriesZtmpdirZpwd_fileZpwd_path�fZ	mkdir_cmdZqueries_cmdrrr�_collect_per_namespace�sP

,



 
,z#CharmedMySQL._collect_per_namespacecGs dd�|D�}tjj|f|��S)NcSsg|]}|jtjj��qSr)�lstriprrC�sep)r2�prrr�
<listcomp>�sz0CharmedMySQL._join_conf_path.<locals>.<listcomp>)rrCr=)r�base�partsZstripped_partsrrr�_join_conf_path�szCharmedMySQL._join_conf_pathcCs0|jd�}|j�}x|D]}|j||�qWdS)Nr)rr(rG)rrr'r)rrr�_process_k8s�s

zCharmedMySQL._process_k8sc	s|j|j|j|jdd�|j|j|jdd�g�|j|j|j|jd�|j|j|jd�|j|j|jd�|j|j|jd�|j|j|jd�g�|jd�|jd	�|jd
��r|j	�\}}|s�|r�dSd|i}d|��}d
|�d��|j�fdd�|j
D�|d�dS)Nrz*.pemz*.keyrr	r
rzsnap.charmed-mysql.*zsnap info charmed-mysqlrrz-h 127.0.0.1 -uzmysql z -ecsg|]}��d|�d��qS)z '�'r)r2r3)rErrrKsz.CharmedMySQL._process_snap.<locals>.<listcomp>)�env)r<rN�snap_path_currentr9Z
add_copy_spec�snap_path_commonZadd_journalr8rrr>)rrrZ	mysql_envrDr)rEr�
_process_snap�sJ







zCharmedMySQL._process_snapcCs*|j|j�rdSt|j|j�o(t|j��S)NT)�is_installed�snap_packagerr!�sysroot�boolr()rrrr�
check_enabledszCharmedMySQL.check_enabledcCs.|j|j�r|j�t|j|j�r*|j�dS)N)rUrVrTrr!rWrO)rrrr�setupszCharmedMySQL.setupN)�__name__�
__module__�__qualname__�__doc__Z
short_descZplugin_namer>rVrSrRr!r"r9r�strrXZoption_listrr(r+rGrNrOrTrYrZrrrrrsP

		N<r)	rr?Zsos.report.pluginsrrrZ
sos.utilitiesrrrrrr�<module>	s