U
    -_g;                     @   sn   d dl mZ d dlmZ d dlmZmZmZmZm	Z	 G dd deZ
G dd de
eZG dd	 d	e
eeZd
S )    )match)quote)PluginRedHatPluginDebianPluginUbuntuPlugin	PluginOptc                   @   s   e Zd ZdZdZdZdZdZdZdZ	dZ
d	diZed
dddeddddeddddgZdZdd Zdd Zdd ZdddZdd ZdS ) Foremanz$Foreman/Satellite systems managementforemani  )Zsysmgmt)r
   NZ	localhost 
PGPASSWORDdays   z!number of days for dynflow output)defaultZdescproxyfeaturesFz!collect features of smart proxiespuma-gczcollect Puma GC statsz/pumactl %s -S /usr/share/foreman/tmp/puma.statec           	      C   s  d}zd}t |ddd}|  }W 5 Q R X |D ]l}|r4|d dkrJq4|drZd	}q4|rvtd
|rv| d | _|rtd|r| d | _|ds4d}q4W n tk
r   Y nX | jdr| j	ds| jdr| j	dr| jdd | _d| ji| _
| ddddd | ddg | dd }| }| dd }| }| jdd| j dgdd | d d!d"d#d$d%d&d'd(d)d*d+d,d-d| j d.d| j d/d| j d0d1| j d2d1| j d3d| j d4g | d5d6d7d8d9d:| d:| d;g | jd<d=gd	d> | jd?d@dA | jdBdCdDdE | jdFdGdA | jdHdIdA | jdJdK | dLr|| j| jdM dNdA | j| jdO dPdA | dQ | dR}| j|dS| j
dT |   |   d S )UNFz/etc/foreman/database.ymlrzUTF-8)encodingr   #zproduction:Tz\s+host:\s+\S+   z\s+password:\s+\S+ "'r   Zforeman_production_logZforeman_tasks_configZforeman_ssl_access_ssl_log)z!/var/log/foreman/production.log.*z/etc/sysconfig/foreman-tasks/etc/sysconfig/dynflowdz)/var/log/httpd/foreman-ssl_access_ssl.logz/etc/foreman/*key.pemz/etc/foreman/encryption_key.rbZhostnameoutputzhostname -fz/var/log/foreman/production.logz	/var/log/z*/foreman-ssl_*_ssl.logi  )	sizelimitz/etc/foreman/z/etc/sysconfig/foremanr   z/etc/default/foremanz&/var/log/foreman/dynflow_executor*log*z*/var/log/foreman/dynflow_executor*.output*z#/var/log/foreman/apipie_cache*.log*z/var/log/foreman/cron*.log*z /var/log/foreman/db_migrate*log*z/var/log/foreman/db_seed*log*z$/var/log/foreman/production.log[.-]*z$/var/log/foreman-selinux-install.logz&/var/log/foreman-proxy-certs-generate*z/usr/share/foreman/Gemfile*z
*/foreman*z&*/katello-reverse-proxy_error_ssl.log*z*/error_log*z/etc/z*/conf/z	*/conf.d/z'*/katello-reverse-proxy_access_ssl.log*zforeman-selinux-relabel -nvzpassenger-status --show poolz passenger-status --show requestsz"passenger-status --show backtraceszpassenger-memory-statszping -c1 -W1 zping -c1 -W1 localhostz/root/ssl-buildz/usr/share/foreman/config/hooks)	recursivezqpid-stat -b amqps://localhost:5671 -q                     --ssl-certificate=/etc/pki/katello/qpid_router_client.crt                     --ssl-key=/etc/pki/katello/qpid_router_client.key                     --sasl-mechanism=ANONYMOUSzqpid-stat_-q)suggest_filenamezhammer pingZhammer_pingx   )Ztagstimeoutzsystemctl list-units dynflow*Zdynflow_unitsz!"system-dynflow\x2dsidekiq.slice"Zdynflow_sidekiq_statuszdynflow-sidekiq@*)Zunitsr   zgc-statszpumactl_gc-statsZstatsZpumactl_statsz/usr/sbin/foreman-puma-statusa[  SELECT table_name, pg_size_pretty(total_bytes) AS total, pg_size_pretty(index_bytes) AS INDEX , pg_size_pretty(toast_bytes) AS toast, pg_size_pretty(table_bytes) AS TABLE FROM ( SELECT *, total_bytes-index_bytes-COALESCE(toast_bytes,0) AS table_bytes FROM (SELECT c.oid,nspname AS table_schema, relname AS TABLE_NAME, c.reltuples AS row_estimate, pg_total_relation_size(c.oid) AS total_bytes, pg_indexes_size(c.oid) AS index_bytes, pg_total_relation_size(reltoastrelid) AS toast_bytes FROM pg_class c LEFT JOIN pg_namespace n ON n.oid = c.relnamespace WHERE relkind = 'r') a) a order by total_bytes DESCZforeman_db_tables_sizes)r   env)openread
splitlines
startswithr   splitdbhostdbpasswdIOErrorendswithr!   add_file_tagsZadd_forbidden_pathexec_cmdstripZadd_copy_spec	apachepkgadd_cmd_outputZadd_dir_listingZadd_service_statusZadd_journal
get_optionpumactlbuild_query_cmdcollect_foreman_dbcollect_proxies)	selfZproduction_scopeZ
foreman_dbdfileZforeman_lineslineZ	_hostnameZ_host_f_cmd r9   </usr/lib/python3/dist-packages/sos/report/plugins/foreman.pysetup(   s    





zForeman.setupc              	   C   s   |  d d}t|}d}d| d}d| d}d| d}d	| d}d
}|ddddd|dd}	||||d}
|	 D ](\}}| |}| j||dd| jd q|| d}|
 D ]B\}}d}|dkr|rd}| j|d|d}| j||dd| jd qdS )z% Collect foreman db and dynflow data r   z dayszRselect id,name,value from settings where name not similar to '%(pass|key|secret)%'zFselect * from foreman_tasks_tasks where started_at > NOW() - interval z order by started_at asczselect dynflow_execution_plans.* from foreman_tasks_tasks join dynflow_execution_plans on (foreman_tasks_tasks.external_id = dynflow_execution_plans.uuid::varchar) where foreman_tasks_tasks.started_at > NOW() - interval z, order by foreman_tasks_tasks.started_at asczselect dynflow_actions.* from foreman_tasks_tasks join dynflow_actions on (foreman_tasks_tasks.external_id = dynflow_actions.execution_plan_uuid::varchar) where foreman_tasks_tasks.started_at > NOW() - interval zselect dynflow_steps.* from foreman_tasks_tasks join dynflow_steps on (foreman_tasks_tasks.external_id = dynflow_steps.execution_plan_uuid::varchar) where foreman_tasks_tasks.started_at > NOW() - interval zWITH prefix_counts AS (SELECT split_part(name,'::',1) FROM fact_names) SELECT COUNT(*), split_part AS "fact_name_prefix" FROM prefix_counts GROUP BY split_part ORDER BY count DESC LIMIT 100zselect * from schema_migrationsz`select id,type,name,host,port,account,base_dn,attr_login,onthefly_register,tls from auth_sourcesz!select * from dynflow_schema_infozselect count(*) from auditszselect count(*) from logsz2select name,url,download_policy from smart_proxies)Zforeman_settings_tableZforeman_schema_migrationsZforeman_auth_tableZdynflow_schema_infoZaudits_table_countZlogs_table_countZfact_names_prefixesZsmart_proxies)foreman_tasks_tasksZdynflow_execution_plansZdynflow_actionsZdynflow_stepsiX  d   )r   r    r   r!   zdynflow-utilspsqlr<   z /usr/libexec/psql-msgpack-decodeT)csvbinaryN)r0   r   itemsr2   r/   r!   Zis_installed)r5   r   ZintervalZscmdZdtaskcmdZdyncmdZ
dactioncmdZ	dstepscmdZfactnamescmdZ	foremandbZ
foremancsvtablevalr8   ZdynutilsZdynr@   r9   r9   r:   r3      sV    





 

 zForeman.collect_foreman_dbc                 C   s   |  dr~| jddd}| j|| jd}|d dkr~|d  d	d
 D ]4}|d}d|d	  d}| j||d ddd qH| ddddg d
S )z Collect foreman proxies r   z"select name,url from smart_proxiesT)r?   )r!   Zstatusr   r   r   N,zNcurl -s --key /etc/foreman/client_key.pem --cert /etc/foreman/client_cert.pem z/v2/featuresZsmart_proxies_features
   )r   Zsubdirr    Z
HTTP_PROXYZHTTPS_PROXYZNO_PROXYZ	ALL_PROXY)r0   r2   r,   r!   r$   r&   r/   Zadd_env_var)r5   r8   Zproxiesproxyr9   r9   r:   r4     s&    

zForeman.collect_proxiesr>   c                 C   s(   |rd| d}d}||| j t|f S )aI  
        Builds the command needed to invoke the pgsql query as the postgres
        user.
        The query requires significant quoting work to satisfy both the
        shell and postgres parsing requirements. Note that this will generate
        a large amount of quoting in sos logs referencing the command being run
        zCOPY (z6) TO STDOUT WITH (FORMAT 'csv', DELIMITER ',', HEADER)z:%s --no-password -h %s -p 5432 -U foreman -d foreman -c %s)r'   r   )r5   Zqueryr?   r@   Z_dbcmdr9   r9   r:   r2   2  s    zForeman.build_query_cmdc                 C   s:   |  ddd |  ddd | dg | ddd	 d S )
Nz/etc/foreman/(.*)((conf)(.*)?)z5((\:|\s*)(passw|cred|token|secret|key).*(\:\s|=))(.*)z
\1********z"/etc/foreman/(.*)((yaml|yml)(.*)?)z\1"********"z /var/log/foreman/production.log*zfrom settings wherez(http(s)?://)\S+:\S+(@.*)z\1******:******\3)Zdo_path_regex_subZdo_paths_http_subZdo_cmd_output_subr5   r9   r9   r:   postproc@  s$    zForeman.postproc)Fr>   )__name__
__module____qualname__Z
short_descZplugin_nameZplugin_timeoutZprofilesZpackagesr.   r'   r(   r!   r   Zoption_listr1   r;   r3   r4   r2   rH   r9   r9   r9   r:   r	      s4    [
r	   c                       s    e Zd ZdZ fddZ  ZS )RedHatForemanZhttpdc                    s&   |  ddi t   | d d S )Nz"/usr/share/foreman/.ssh/ssh_configZssh_foreman_configzgem list)r+   superr;   r/   rG   	__class__r9   r:   r;   \  s     
zRedHatForeman.setup)rI   rJ   rK   r.   r;   __classcell__r9   r9   rN   r:   rL   X  s   rL   c                   @   s   e Zd ZdZdS )DebianForemanZapache2N)rI   rJ   rK   r.   r9   r9   r9   r:   rQ   f  s   rQ   N)rer   Zshlexr   Zsos.report.pluginsr   r   r   r   r   r	   rL   rQ   r9   r9   r9   r:   <module>   s     H