o
    Û1jî&  ã                   @   sF   d dl Z d dlZd dlmZmZmZ d dlmZ G dd„ deeƒZdS )é    N)ÚPluginÚ	PluginOptÚUbuntuPlugin)Úis_executablec                	   @   sÈ   e Zd ZdZdZdZg d¢ZdZdZdZ	dZ
d	Zd
dddddœZeddedd�eddedd�eddedd�eddedd�gZdd„ Zdd „ Zd!d"„ Zd#d$„ Zd%d&„ Zd'd(„ Zd)d*„ Zd+d,„ Zd-d.„ Zd/S )0Ú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 MySQLÚcharmed_mysql)zshow 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;zshow ENGINE InnoDB STATUS;zcharmed-mysqlz/var/snap/charmed-mysql/commonz/var/snap/charmed-mysql/currentÚkubectlz 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)ÚdefaultÚval_typeÚdescÚdbpassÚ z%Password for database dump collectionÚdumpdbsFzGet name of all databasesÚ
logs_sinceÚ48hz9How far back to fetch logs with kubectl --since, K8s onlyc                 C   s\   |   d¡}|   d¡}dtjv r |s | j d¡ tjd }||fS |s*| j d¡ dS ||fS )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ÚenvironÚsoslogÚinfoÚwarning)ÚselfÚdb_userÚdb_pass© r"   úB/usr/lib/python3/dist-packages/sos/report/plugins/charmed_mysql.pyÚ_get_db_credentials[   s   

ÿ
ùÿz CharmedMySQL._get_db_credentialsc                 C   sB   |   | j› d| j› d�¡}|d dkrtt|d  ¡  ¡ ƒƒS g S )Nz get pods -A -l z- -o jsonpath='{.items[*].metadata.namespace}'Ústatusr   Úoutput)Úexec_cmdÚkube_cmdÚselectorÚlistÚsetÚstripÚsplit)r   Ú
namespacesr"   r"   r#   Ú_determine_namespacesn   s   ÿz"CharmedMySQL._determine_namespacesc                 C   s@   |   | j› d|› d| j› d�¡}|d dkr|d  ¡  ¡ S g S )Nú -n z get pods -l z( -o jsonpath='{.items[*].metadata.name}'r%   r   r&   )r'   r(   r)   r,   r-   )r   Ú	namespaceÚpodsr"   r"   r#   Ú_get_pod_namesw   s   ÿzCharmedMySQL._get_pod_namesc                 C   s\  | j › d|› �}|  |› d| j› d�|› d| j› �g¡ |  |› d| j› d|› d�¡ d}|  |¡}|  ¡ }| j ¡ D ]3\}}|D ],}	tj|› d	|	› d	|› �d
d� |› d|› d|	› d|› d|› d	|	› d	|› �}
|  	|
¡ qAq;|  
|› d�|› d�g¡ |  d¡�r,|  ¡ \}}|rŒ|sŽd S d|› �}d|› d�}d dd„ | jD ƒ¡}t ¡ �y}d}|d	 | }t|ddd��}| |¡ W d   ƒ n1 sÈw   Y  |D ]I}	|› d|› d|	› d|› �}|  	|¡ |› d|› d|› d|	› d|› �	}
|  	|
¡ |› d|› d|	› d|› d|› d|› d |› �}| j||	› d!�d"� qÏW d   ƒ d S 1 �s%w   Y  d S d S )#Nr0   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/*.keyr   ú-h 127.0.0.1 -uúmysql ú -ec                 s   s   � | ]	}|  d d¡V  qdS )ú"z\"N)Úreplace©Ú.0Úqueryr"   r"   r#   Ú	<genexpr>ª   s
   € 
ÿ
ÿz6CharmedMySQL._collect_per_namespace.<locals>.<genexpr>Ú	mysql_pwdÚwÚutf8)Úencodingz	 exec -c z -- mkdir -p z -- sh -lc 'MYSQL_PWD=$(cat z) z "z"' && rm -rf z_dbs.txt)Úsuggest_filename)r(   Úadd_cmd_outputr)   r3   Úget_cmd_output_pathÚ
conf_pathsÚitemsr   Úmakedirsr'   Úadd_forbidden_pathr   r$   ÚjoinÚmysql_queriesÚtempfileÚTemporaryDirectoryÚopenÚwrite)r   r1   r   r(   Ú
mysql_contr2   Údump_files_pathÚnameÚpathÚpodÚcopy_cmdr    r!   ÚoptsÚsql_cmdÚqueriesÚtmpdirÚpwd_fileÚpwd_pathÚfÚ	mkdir_cmdÚqueries_cmdr"   r"   r#   Ú_collect_per_namespace€   s–   þÿ
ÿÿÿÿúþ

þ
ÿÿÿ
ÿÿÿÿ
ÿþþýÿþí$ùôz#CharmedMySQL._collect_per_namespacec                 G   s"   dd„ |D ƒ}t jj|g|¢R Ž S )Nc                 S   s   g | ]	}|  tjj¡‘qS r"   )Úlstripr   rU   Úsep)r>   Úpr"   r"   r#   Ú
<listcomp>Ï   s    z0CharmedMySQL._join_conf_path.<locals>.<listcomp>)r   rU   rL   )r   ÚbaseÚpartsÚstripped_partsr"   r"   r#   Ú_join_conf_pathÎ   s   zCharmedMySQL._join_conf_pathc                 C   s,   |   d¡}|  ¡ }|D ]}|  ||¡ qd S )Nr   )r   r/   ra   )r   r   r.   r1   r"   r"   r#   Ú_process_k8sÒ   s
   
ÿzCharmedMySQL._process_k8sc                    s  |   |  | j| jd d¡|  | j| jd d¡g¡ |  |  | j| jd ¡|  | j| jd ¡|  | j| jd ¡|  | j| jd ¡|  | j| jd ¡g¡ |  d¡ |  d	¡ |  d
¡r†|  	¡ \}}|rd|sfd S d|i}d|› �}d|› d�‰ | j‡ fdd„| j
D ƒ|d� d S d S )Nr	   z*.pemz*.keyr
   r   r   r   zsnap.charmed-mysql.*zsnap info charmed-mysqlr   r   r8   r9   r:   c                    s   g | ]
}ˆ › d |› d�‘qS )z 'ú'r"   r=   ©rY   r"   r#   re     s    z.CharmedMySQL._process_snap.<locals>.<listcomp>)Úenv)rK   ri   Úsnap_path_currentrH   Úadd_copy_specÚsnap_path_commonÚadd_journalrF   r   r$   rM   )r   r    r!   Ú	mysql_envrX   r"   rl   r#   Ú_process_snapØ   s`   ýýúþþþþþï




þ÷zCharmedMySQL._process_snapc                 C   s*   |   | j¡rdS t| j| jƒot|  ¡ ƒS )NT)Úis_installedÚsnap_packager   r(   ÚsysrootÚboolr/   ©r   r"   r"   r#   Úcheck_enabled  s
   
þzCharmedMySQL.check_enabledc                 C   s2   |   | j¡r
|  ¡  t| j| jƒr|  ¡  d S d S )N)rt   ru   rs   r   r(   rv   rj   rx   r"   r"   r#   Úsetup  s
   ÿzCharmedMySQL.setupN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú
short_descÚplugin_namerM   ru   rp   rn   r(   r)   rH   r   Ústrrw   Úoption_listr$   r/   r3   ra   ri   rj   rs   ry   rz   r"   r"   r"   r#   r      sT    û	þþþþó		N<r   )	r   rN   Úsos.report.pluginsr   r   r   Úsos.utilitiesr   r   r"   r"   r"   r#   Ú<module>   s
   