o
    O6uf7   ã                   @   sX   d dl Z d dlmZ d dlmZ dZG dd„ deƒZG dd„ deƒZG d	d
„ d
eƒZdS )é    N)Úquote)ÚClusterz(/etc/pki/ovirt-engine/keys/engine_id_rsac                   @   sl   e Zd ZdZdZdZdZg 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d„ ZdS )Úovirtaî  
    This cluster profile is for the oVirt/RHV project which provides for a
    virtualization cluster built ontop of KVM.

    Nodes enumerated will be hypervisors within the envrionment, not virtual
    machines running on those hypervisors. By default, ALL hypervisors within
    the environment are returned. This may be influenced by the 'cluster' and
    'datacenter' cluster options, which will limit enumeration to hypervisors
    within the specific cluster and/or datacenter. The spm-only cluster option
    may also be used to only collect from hypervisors currently holding the
    SPM role.

    Optionally, to only collect an archive from manager and the postgresql
    database, use the no-hypervisors cluster option.

    By default, a second archive from the manager will be collected that is
    just the postgresql plugin configured in such a way that a dump of the
    manager's database that can be explored and restored to other systems will
    be collected.

    The ovirt profile focuses on the upstream, community ovirt project.

    The rhv profile is for Red Hat customers running RHV (formerly RHEV).

    The rhhi_virt profile is for Red Hat customers running RHV in a
    hyper-converged setup and enables gluster collections.
    zCommunity oVirt)zovirt-enginez3/usr/share/ovirt-engine/dbscripts/engine-psql.sh -c))úno-databaseFzDo not collect a database dump)ÚclusterÚ z'Only collect from hosts in this cluster)Ú
datacenterr   z*Only collect from hosts in this datacenter)úno-hypervisorsFzDo not collect from hypervisors)úspm-onlyFzOnly collect from SPM host(s)c                 C   s"   | j › dt|ƒ› �}| j|dd�S )z�
        Wrapper for running DB queries on the manager. Any scrubbing of the
        query should be done _before_ passing the query to this method.
        ú T©Ú	need_root)Údb_execr   Úexec_primary_cmd)ÚselfÚqueryÚcmd© r   ú>/usr/lib/python3/dist-packages/sos/collector/clusters/ovirt.pyÚ_run_db_query<   s   zovirt._run_db_queryc                    s@   |sdS g d¢‰ t ‡ fdd„|D ƒƒr|  d|› d�¡ dS |S )z�
        Manually sanitize SQL queries since we can't leave this up to the
        driver since we do not have an actual DB connection
        ú%)ú ú\Ú
úúú"ú'c                 3   s   � | ]}|ˆ v V  qd S )Nr   )Ú.0Úx©Úinvalid_charsr   r   Ú	<genexpr>M   s   € z#ovirt._sql_scrub.<locals>.<genexpr>zWARNING: Cluster option 'z2' contains invalid characters. Using '%%' instead.)ÚanyÚlog_warn)r   Úvalr   r    r   Ú
_sql_scrubD   s   zovirt._sql_scrubc                 C   sR   | j jr#t| jj| jj| jjgƒs%| j  t¡r'|  	t¡ |  
d¡ dS dS dS dS )a  
        Checks for the presence of the VDSM ssh keys the manager uses for
        communication with hypervisors.

        This only runs if we're locally on the RHV-M, *and* if no ssh-keys are
        called out on the command line, *and* no --password option is given.
        z]Found engine SSH key. User command line does not specify a key or password, using engine key.N)ÚprimaryÚlocalr#   ÚoptsÚssh_keyÚpasswordÚpassword_per_nodeÚfile_existsÚ
ENGINE_KEYÚadd_default_ssh_keyÚ	log_debug©r   r   r   r   Ú_check_for_engine_keysT   s   ÿ
ûzovirt._check_for_engine_keysc                 C   s.   d| _ |  d¡s|  ¡ | _|  ¡  |  ¡  d S )NFr   )Úpg_passÚ
get_optionÚparse_db_confÚconfÚformat_db_cmdr2   r1   r   r   r   Úsetupe   s
   

zovirt.setupc                 C   sb   |   |  d¡¡}|   |  d¡¡}d|› d|› d�| _|  d¡r&|  jd7  _|  d| j› �¡ d S )	Nr   r   z_SELECT host_name from vds where cluster_id in (select cluster_id FROM cluster WHERE name like 'zG' and storage_pool_id in (SELECT id FROM storage_pool WHERE name like 'z'))r
   z AND spm_status = 2z#Query command for ovirt DB set to: )r&   r4   Údbqueryr0   )r   r   r   r   r   r   r7   l   s   þ
ý
zovirt.format_db_cmdc                 C   sZ   |   d¡rg S |  | j¡}|d dkr$|d  ¡ dd… }dd„ |D ƒS td	|d › �ƒ‚)
Nr	   Ústatusr   Úoutputé   éÿÿÿÿc                 S   s   g | ]}|  d ¡d  ¡ ‘qS )ú(r   )ÚsplitÚstrip)r   Únr   r   r   Ú
<listcomp>�   s    z#ovirt.get_nodes.<locals>.<listcomp>z$database query failed, return code: )r4   r   r9   Ú
splitlinesÚ	Exception)r   ÚresÚnodesr   r   r   Ú	get_nodes{   s   
ÿzovirt.get_nodesc                 C   s   |   d¡s| jr|  ¡ S dS )Nr   F)r4   r6   Úcollect_databaser1   r   r   r   Úrun_extra_cmd†   s   zovirt.run_extra_cmdc              	   C   s�   i }d}| j d|› �dd�}|d dkrF|d  ¡ }|D ]'}zt| d¡d ƒ}t| d¡d	  d
d¡ƒ}|||< W q tyC   Y qw |S dS )Nz6/etc/ovirt-engine/engine.conf.d/10-setup-database.confzcat Tr   r:   r   r;   ú=é   r   r   F)r   rC   Ústrr?   ÚreplaceÚ
IndexError)r   r6   ÚengconfrE   ÚconfigÚlineÚkÚvr   r   r   r5   ‹   s   þzovirt.parse_db_confc                 C   sÊ   d}d|› d| j d › d|› d| j d › d|› d| j d	 › d|› d
| j d › �}d| j d › d|› �}| j|dd�}|d  ¡ D ]}t |d¡r]| ¡ }| jj d| d¡d ¡ |  S q@|  	d¡ dS )NÚ
postgresqlz-k z.dbname=ÚENGINE_DB_DATABASEz -k z.dbhost=ÚENGINE_DB_HOSTz.dbport=ÚENGINE_DB_PORTz.dbuser=ÚENGINE_DB_USERzPGPASSWORD=ÚENGINE_DB_PASSWORDz> /usr/sbin/sos report --name=postgresql --batch -o postgresql Tr   r;   z*sosreport-*tar*Úpostgresql_dumpú/r=   zFailed to gather database dumpF)
r6   r   rC   Úfnmatchr@   r'   ÚmanifestÚ	add_fieldr?   Ú	log_error)r   ÚpluginÚsos_optr   Údb_sosrQ   Ú_pg_dumpr   r   r   rH   œ   s<   ÿÿþþýýÿÿÿ
ÿü
zovirt.collect_databaseN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Úcluster_nameÚpackagesr   Úoption_listr   r&   r2   r8   r7   rG   rI   r5   rH   r   r   r   r   r      s    r   c                   @   s    e Zd ZdZdZd Zdd„ ZdS )ÚrhvzRed Hat Virtualization)ÚrhevmÚrhvmc                 C   s$   |j | jj kr	dS | d¡rdS dS )NÚmanagerzovirt-node-ng-nodectlÚrhvhÚrhelh)Úaddressr'   Úis_installed)r   Únoder   r   r   Úset_node_labelº   s
   
zrhv.set_node_labelN)rd   re   rf   rh   ri   Ú
sos_presetrt   r   r   r   r   rk   ´   s
    rk   c                   @   s0   e Zd ZdZdZddiZdZdd„ Zdd	„ Zd
S )Ú	rhhi_virtz6Red Hat Hyperconverged Infrastructure - Virtualization)Úglusterzgluster.dumpÚonrk   c                 C   s   | j  d¡o	|  ¡ S )Nrm   )r'   rr   Ú_check_for_rhhivr1   r   r   r   Úcheck_enabledÊ   s   zrhhi_virt.check_enabledc                 C   s2   |   d¡}|d dkr|d  ¡ d  ¡ dkS dS )Nz+SELECT count(server_id) FROM gluster_serverr:   r   r;   r<   Ú0F)r   rC   r@   )r   Úretr   r   r   ry   Í   s   
zrhhi_virt._check_for_rhhivN)	rd   re   rf   rh   Úsos_pluginsÚsos_plugin_optionsru   rz   ry   r   r   r   r   rv   Ã   s    rv   )	r\   Úshlexr   Úsos.collector.clustersr   r.   r   rk   rv   r   r   r   r   Ú<module>   s   
 "