3 ܓi&@sFddlZddlZddlmZmZmZddlmZGdddeeZdS)N)Plugin PluginOpt UbuntuPlugin) is_executablec @seZdZdZdZdZdddddd gZd Zd Zd Z d Z dZ ddddddZ e ddedde ddedde ddedde dd ed!dgZd"d#Zd$d%Zd&d'Zd(d)Zd*d+Zd,d-Zd.d/Zd0d1Zd2d3Zd4S)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_LOGSMYSQL_ROUTER_CONFMYSQL_ROUTER_LOGSMYSQL_SHELL_LOGSdbusermysqlz%Username for database dump collection)defaultZval_typeZdescdbpassz%Password for database dump collectiondumpdbsFzGet name of all databases logs_sinceZ48hz9How far back to fetch logs with kubectl --since, K8s onlycCsX|jd}|jd}dtjkr<| r<|jjdtjd}n|sP|jjddS||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_optionosenvironZsosloginfoZwarning)selfdb_userdb_passr#/usr/lib/python3.6/charmed_mysql.py_get_db_credentials[s   z CharmedMySQL._get_db_credentialscCsB|j|jd|jd}|ddkr>tt|djjSgS)Nz get pods -A -l z- -o jsonpath='{.items[*].metadata.namespace}'statusroutput)exec_cmdkube_cmdselectorlistsetstripsplit)r namespacesrrr_determine_namespacesns  z"CharmedMySQL._determine_namespacescCs@|j|jd|d|jd}|ddkr<|djjSgS)Nz -n z get pods -l z( -o jsonpath='{.items[*].metadata.name}'rrr)r r!r"r%r&)r namespacepodsrrr_get_pod_namesws  zCharmedMySQL._get_pod_namescCsB|jd|}|j|d|jd|d|jg|j|d|jd|dd}|j|}|j}xx|jjD]j\}}x`|D]X} tj|d | d |d d |d |d | d|d |d | d | } |j | qWqxW|j |d|dg|j dr>|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"qWWdQRXdS)#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|]}|jddVqdS)"z\"N)replace).0queryrrr sz6CharmedMySQL._collect_per_namespace..Z mysql_pwdwutf8)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_pathsitemsrmakedirsr add_forbidden_pathrrjoin mysql_queriestempfileZTemporaryDirectoryopenwrite)rr)rr!Z mysql_contr*Zdump_files_pathnamepathZpodZcopy_cmdrroptssql_cmdZqueriesZtmpdirZpwd_fileZpwd_pathfZ mkdir_cmdZ queries_cmdrrr_collect_per_namespacesP  ,           ,z#CharmedMySQL._collect_per_namespacecGs dd|D}tjj|f|S)NcSsg|]}|jtjjqSr)lstriprrCsep)r2prrr sz0CharmedMySQL._join_conf_path..)rrCr=)rbasepartsZstripped_partsrrr_join_conf_pathszCharmedMySQL._join_conf_pathcCs0|jd}|j}x|D]}|j||qWdS)Nr)rr(rG)rrr'r)rrr _process_k8ss  zCharmedMySQL._process_k8sc s|j|j|j|jdd|j|j|jddg|j|j|j|jd|j|j|jd|j|j|jd|j|j|jd|j|j|jdg|jd|jd |jd r|j \}}| s| rdSd |i}d |}d |d|jfdd|j D|ddS)Nrz*.pemz*.keyrr r r zsnap.charmed-mysql.*zsnap info charmed-mysqlrrz-h 127.0.0.1 -uzmysql z -ecsg|]}d|dqS)z ''r)r2r3)rErrrKsz.CharmedMySQL._process_snap..)env) r<rNsnap_path_currentr9Z add_copy_specsnap_path_commonZ add_journalr8rrr>)rrrZ mysql_envrDr)rEr _process_snapsJ            zCharmedMySQL._process_snapcCs*|j|jrdSt|j|jo(t|jS)NT) is_installed snap_packagerr!sysrootboolr()rrrr check_enableds zCharmedMySQL.check_enabledcCs.|j|jr|jt|j|jr*|jdS)N)rUrVrTrr!rWrO)rrrrsetups zCharmedMySQL.setupN)__name__ __module__ __qualname____doc__Z short_descZ plugin_namer>rVrSrRr!r"r9rstrrXZ option_listrr(r+rGrNrOrTrYrZrrrrrsP    N< r) rr?Zsos.report.pluginsrrrZ sos.utilitiesrrrrrr s