
    G%@i&                     L    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ee      Zy)    N)Plugin	PluginOptUbuntuPlugin)is_executablec            	           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       edded       edded       edded      gZd Zd Zd Zd  Zd! Zd" Zd# Zd$ Zd% Zy&)'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                    | j                  d      }| j                  d      }dt        j                  v r4|s2| j                  j	                  d       t        j                  d   }||fS |s| j                  j                  d       y||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_passs      B/usr/lib/python3/dist-packages/sos/report/plugins/charmed_mysql.py_get_db_credentialsz CharmedMySQL._get_db_credentials[   s    //(+//(+"**$WKK1 jj-G  KK#     c                     | j                  | j                   d| j                   d      }|d   dk(  r3t        t	        |d   j                         j                                     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"   
namespacess     r%   _determine_namespacesz"CharmedMySQL._determine_namespacesn   si    ]]}}o-dmm_ =; ;

 h1$Jx0668>>@ABB	r'   c                     | j                  | j                   d| d| j                   d      }|d   dk(  r!|d   j                         j	                         S g S )N -n z get pods -l z( -o jsonpath='{.items[*].metadata.name}'r)   r   r*   )r+   r,   r-   r0   r1   )r"   	namespacepodss      r%   _get_pod_nameszCharmedMySQL._get_pod_namesw   sc    }}}}oT)M$-- I6 6
 >Q>'')//11	r'   c                    | j                    d| }| j                  | d| j                   d| d| j                   g       | j                  | d| j                   d| d       d}| j                  |      }| j	                         }| j
                  j                         D ]R  \  }}|D ]H  }	t        j                  | d	|	 d	| d
       | d| d|	 d| d| d	|	 d	| }
| j                  |
       J T | j                  | d| dg       | j                  d      r| j                         \  }}|r|sy d| }d| d}dj                  d | j                  D              }t        j                          5 }d}|d	z   |z   }t#        |dd      5 }|j%                  |       d d d        |D ]m  }	| d| d|	 d| }| j                  |       | d| d| d|	 d| 	}
| j                  |
       | d| d|	 d| d| d| d| }| j                  ||	 d !       o 	 d d d        y y # 1 sw Y   xY w# 1 sw Y   y xY w)"Nr5   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              3   @   K   | ]  }|j                  d d        yw)"z\"N)replace).0querys     r%   	<genexpr>z6CharmedMySQL._collect_per_namespace.<locals>.<genexpr>   s#       dF+s   	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-   r8   get_cmd_output_path
conf_pathsitemsr   makedirsr+   add_forbidden_pathr   r&   joinmysql_queriestempfileTemporaryDirectoryopenwrite)r"   r6   r   r,   
mysql_contr7   dump_files_pathnamepathpodcopy_cmdr#   r$   optssql_cmdqueriestmpdirpwd_filepwd_pathf	mkdir_cmdqueries_cmds                         r%   _collect_per_namespacez#CharmedMySQL._collect_per_namespace   s   mm_D4 	jT]]O8<j*4==/:
 	 	j	$--	* F8 8	

 
""9- 224////1 	(JD$ (/qQtf=Mj
|1SE4&&'qQtf6  h'(	( 	2323!
 	 ??9%#779GW'$WI.DtfC(Ghh !// G
 ,,. &&!C<(2(C&9 %QGGG$%   C#*Ij\3% @$$*8-  MM), $*GJ<q#*AcU!H:7  MM(+ $*Ij\3% @33;*B")3wi 0%%+H.   ''#,/5)9 ( '  & % %	 s%   >H:H.)A:H:.H7	3H::Ic                     |D cg c]+  }|j                  t        j                  j                        - }}t        j                  j                  |g| S c c}w N)lstripr   r[   seprR   )r"   basepartspstripped_partss        r%   _join_conf_pathzCharmedMySQL._join_conf_path   sD    9>?A!((277;;/??ww||D2>22 @s   0Ac                 x    | j                  d      }| j                         }|D ]  }| j                  ||        y )Nr   )r   r3   rg   )r"   r   r2   r6   s       r%   _process_k8szCharmedMySQL._process_k8s   s=    __\2
//1
# 	?I''	:>	?r'   c                    | j                  | j                  | j                  | j                  d   d      | j                  | j                  | j                  d   d      g       | j	                  | j                  | j                  | j                  d         | j                  | j
                  | j                  d         | j                  | j                  | j                  d         | j                  | j
                  | j                  d         | j                  | j
                  | j                  d         g       | j                  d       | j                  d	       | j                  d
      rY| j                         \  }}|r|sy d|i}d| }d| d}| j                  | j                  D cg c]
  }| d| d c}|       y y c c}w )Nr   z*.pemz*.keyr   r   r   r   zsnap.charmed-mysql.*zsnap info charmed-mysqlr   r   r>   r?   r@   z '')env)rQ   rp   snap_path_currentrN   add_copy_specsnap_path_commonadd_journalrL   r   r&   rS   )r"   r#   r$   	mysql_envr^   r_   rE   s          r%   _process_snapzCharmedMySQL._process_snap   s     &&-
   &&-!
 	 	  &&-   %%-   && 34   %% 34   %% 23#
 	0 	/0 	56 ??9%#779GW'$g.I$WI.DtfC(G595G5GHEG9BugQ'H    & Is   0G
c                     | j                  | j                        ryt        | j                  | j                        xr t        | j                               S )NT)is_installedsnap_packager   r,   sysrootboolr3   r"   s    r%   check_enabledzCharmedMySQL.check_enabled  sH    T../ $--6 /++-.	
r'   c                     | j                  | j                        r| j                          t        | j                  | j
                        r| j                          y y ri   )r}   r~   r{   r   r,   r   rr   r   s    r%   setupzCharmedMySQL.setup  sE    T../ 5 6r'   N)__name__
__module____qualname____doc__
short_descplugin_namerS   r~   rx   rv   r,   r-   rN   r   strr   option_listr&   r3   r8   rg   rp   rr   r{   r   r    r'   r%   r   r      s     !J!KM. #L79H1H #&/3.J 	g8	
 	b38	
 	ut,	
 	%#L	
K& &L\3?:x	
 r'   r   )	r   rT   sos.report.pluginsr   r   r   sos.utilitiesr   r   r   r'   r%   <module>r      s&    
  > > 'T 6< T r'   