o
    O6ufj9 ã                   @   s6  d dl Z d dlZd dlZd dlZd dlZd dlZd dlZd dlmZ d dlZd dl	m
Z
 d dlmZmZ d dlZd dlmZmZmZmZmZmZmZ d dlmZ d dlmZ d dlmZ d dlZd d	lmZm Z m!Z!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z( d d
l)m*Z* ej+ej,fZ-ddd„Z.dd„ Z/g d¢Z0G dd„ deƒZ1dS )é    N)Údatetime)Úrmtree)ÚThreadPoolExecutorÚTimeoutError)ÚImporterHelperÚSoSTimeoutErrorÚboldÚsos_get_command_outputÚTIMEOUT_DEFAULTÚlistdirÚis_executable)Ú_sos)Ú__version__)ÚSoSComponent)
ÚReportÚSectionÚCommandÚ
CopiedFileÚCreatedFileÚAlertÚNoteÚPlainTextReportÚ
JSONReportÚ
HTMLReport)Ú
SoSCleanerFú, c                 C   s”   g }| }|rt | ƒd }nd}|D ]}t |ƒt |ƒ t |ƒ dkr(| |¡ |}|| | }q|t |ƒ d … |krC|d t |ƒ … }| |¡ |S )Nú Ú éH   )ÚlenÚappend)Ú
first_lineÚitemsÚindentÚsepÚlinesÚlineÚnewlineÚitem© r)   ú5/usr/lib/python3/dist-packages/sos/report/__init__.pyÚ_format_list,   s   

r+   c                 C   s   t  | d›d¡S )zâ This function will format --since arg to append 0s if enduser
    didn't. It's used in the _get_parser.
    This will also be a good place to add human readable and relative
    date parsing (like '2 days ago') in the future z<014sz%Y%m%d%H%M%S)r   Ústrptime)Údater)   r)   r*   Ú_format_since>   s   r.   )ÚautoÚalwaysÚneverc                       s„  e Zd ZdZdZdZi dd“dd“dd“dd	“d
d“dd“dd“dd“dd	“dg “dg “dg “dd“dd“dd“dg “dd“i dg “dd“dg “dd	“dd“dd“d d“d!d"“d#d“d$d%“d&g “d'g “d(g “d)d“d*d“d+d“d,d“¥i d-d“d.d	“d/g “d0d“d1e“d2e“d3g “d4d“d5d“d6d“d7g “d8d“d9d“d:d“d;d“d<d“d=d“¥ddddddddd	d	d>œ
¥Z‡ fd?d@„Ze	dAdB„ ƒZ
e	dCdD„ ƒZdEdF„ ZdGdH„ ZdIdJ„ ZdKdL„ ZdMdN„ ZdOdP„ ZdQdR„ ZdSdT„ ZdUdV„ Zd¾dWdX„ZdYdZ„ Zd[d\„ Zd]d^„ Zd_d`„ Zdadb„ Zdcdd„ Zdedf„ Zdgdh„ Zedidj„ ƒZd¿dkdl„Z dmdn„ Z!dodp„ Z"dqdr„ Z#dsdt„ Z$dudv„ Z%dwdx„ Z&dÀdzd{„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�„ Z0dŽd�„ Z1d�d‘„ Z2dÁd’d“„Z3d”d•„ Z4d–d—„ Z5d˜d™„ Z6dšd›„ Z7dœd�„ Z8dždŸ„ Z9d d¡„ Z:d¢d£„ Z;d¤d¥„ Z<d¦d§„ Z=d¨d©„ Z>dªd«„ Z?d¬d­„ Z@d®d¯„ ZAd°d±„ ZBd²d³„ ZCd´dµ„ ZDd¶d·„ ZEd¸d¹„ ZFdºd»„ ZGd¼d½„ ZH‡  ZIS )ÂÚ	SoSReportzaRun a set of commands and file collections and save them to a report for
    future analysis
    z.Collect files and command output in an archiveTÚ
alloptionsFÚall_logsÚbuildÚcase_idr   Úchrootr/   ÚcleanÚcontainer_runtimeÚkeep_binary_filesÚdescÚdomainsÚdisable_parsersÚskip_cleaning_filesÚdry_runÚestimate_onlyÚexperimentalÚenable_pluginsÚjournal_sizeéd   ÚkeywordsÚkeyword_fileNÚplugoptsÚlabelÚlist_pluginsÚlist_presetsÚlist_profilesÚlog_sizeé   Úlow_priorityÚmap_fileú /etc/sos/cleaner/default_mappingÚskip_commandsÚ
skip_filesÚskip_pluginsÚ
namespacesÚ	no_reportÚno_env_varsÚno_postprocÚ	no_updateÚnoteÚonly_pluginsÚpresetÚplugin_timeoutÚcmd_timeoutÚprofilesÚsinceÚverifyÚallow_system_changesÚ	usernamesÚuploadÚ
upload_urlÚupload_directoryÚupload_userÚupload_passÚupload_method)
Úupload_no_ssl_verifyÚupload_protocolÚupload_s3_endpointÚupload_s3_regionÚupload_s3_bucketÚupload_s3_access_keyÚupload_s3_secret_keyÚupload_s3_object_prefixÚ
add_presetÚ
del_presetc                    s  t ƒ  |||¡ g | _g | _g | _tƒ | _|| _d| _i | _	|  
¡  |  ¡  | j ¡ | _| jj d¡| _|  ¡  d}| jj| _| jjrFd}n| j ¡ rS| jtjkrSd}| j d| j› d|› d�¡ | jjtvr€| j d	| jj› �¡ t ¡  | j  ¡  |  !d
¡ |  "¡  |  #¡  |  $¡  d S )Nú/ÚreportÚdefaultÚcmdlineÚpolicyzset sysroot to 'z' (ú)zinvalid chroot mode: é   )%ÚsuperÚ__init__Úloaded_pluginsÚskipped_pluginsÚall_optionsÚsetÚenv_varsÚ_argsÚsysrootÚestimated_plugsizesÚprint_headerÚ
_set_debugrw   Úis_rootÚ_is_rootÚmanifestÚ
componentsÚadd_sectionÚ	report_mdÚ_set_directoriesÚoptsÚin_containerÚosr$   ÚsoslogÚdebugr7   Úchroot_modesÚerrorÚloggingÚshutdownÚtempfile_utilr8   Ú_exitÚ_check_container_runtimeÚ_get_namespacesÚ_get_hardware_devices)ÚselfÚparserÚargsrv   Úmsg©Ú	__class__r)   r*   r{   ’   s8   


zSoSReport.__init__c              
   C   sÂ  |  dd¡}|jddddddd	� |jd
ddddd	� |jdddd tdd� |jdddddd	� |jddddd� |jddddddd	� |jdddd � |jd!d"tdd#d$d%� |jd&dd'd(� |jd)dd*d(� |jd+dd,dd-d	� |jd.d/d0d1td2g d3� |jd4td5d6d7d8� |jd9d:d;d0d<td=g d3� |jd>d?dd@dAd� |jdBdCddDddEd	� |jdFddGd(� |jdHddIddJd	� |jdKddLtdMdNdO� |jdPdddQdR� |jdSd dTd � |jdUdVd0dWtdXg d3� |jdYddZdd[d	� |jd\dd]dd^d	� |jd_dd`ddadb� |jdctdd#ddd%� |jdedfd0dgtdhg d3� |jdidtdjddk� |jdld dmd � |jdnd dod � |jdpdqdrd0dstg dtdO� |jdug d0dvdwdx� |jdyg d0dzd{dx� |jd|dd}dd~d	� |jddd€dd�d	� |jd‚dddƒdR� |jd„d d…d � |jd†d d‡d � |jdˆd d‰d � |jdŠd d‹d � |jdŒdg d�¢dŽd�� |jd�ddd‘d’� |jd“d d”d � |jd•d d–d � |jd—d d˜d � |jd™d dšd � |jd›d dœd � |jd�d džd � |jdŸdg d ¢d¡d�� | ¡ }|jd¢tdd£d¤� |jd¥tdd¦d¤� |  d§d¨¡}|jd©dªd«d¬ddd­d®� |jd¯d°g d0d±d®� |jd²d0g d³d´dµ� |jd¶d·d0g d¸d¹dµ� |jdºd0g d»d¼dµ� |jd½d d¾d¿dÀ� |jdÁdddÂdÃdµ� |jdÄdÅdÆdÇdÈ� |jdÉdddÊdËdx� |jdÌdÍg d0dÎd®� d S )ÏNzReport Optionsz.These options control how report collects dataz-az--alloptionsÚ
store_truer3   Fz%enable all options for loaded plugins)ÚactionÚdestru   Úhelpz
--all-logsr4   z-collect all available logs regardless of sizez--sinceÚstorer_   zbEscapes archived files older than date. This will also affect --all-logs. Format: YYYYMMDD[HHMMSS])r¢   r£   ru   Útyper¤   z--buildr5   z;preserve the temporary directory and do not package resultsz	--case-idr6   zspecify case identifier)r¢   r£   r¤   z-cz--chrootr7   r/   zHchroot executed commands to SYSROOT [auto, always, never] (default=auto)z--container-runtimezLDefault container runtime to use for collections. 'auto' for policy control.)ru   r¤   z--descz--descriptionr   zDescription for a new preset)r¦   r¢   ru   r¤   z	--dry-runz#Run plugins but do not collect data)r¢   r¤   z--estimate-onlyzzApproximate disk space requirements for a real sos run; disables --clean and --collect, sets --threads=1 and --no-postprocz--experimentalrA   zenable experimental pluginsz-ez--enable-pluginsÚextendrB   zenable these plugins)r¢   r£   r¦   r¤   ru   z--journal-sizerD   rC   z+limit the size of collected journals in MiB)r¦   ru   r£   r¤   z-kz--plugin-optionz
--plugoptsrG   z7plugin options in plugname.option=value format (see -l)z--labelz--namerH   z"specify an additional report labelz-lz--list-pluginsrI   z)list plugins and available plugin optionsz--list-presetsz#display a list of available presetsz--list-profilesrK   zBdisplay a list of available profiles and plugins that they includez
--log-sizerL   rM   z6limit the size of collected logs (not journals) in MiB)r¢   r£   r¦   ru   r¤   z--low-priorityz(generate report with low system priority)r¢   ru   r¤   z--namespaceszDlimit number of namespaces to collect output for - 0 means unlimitedz-nz--skip-pluginsrS   zdisable these pluginsz--no-reportrU   z disable plaintext/HTML reportingz--no-env-varsrV   z$Do not collect environment variablesz--no-postprocrW   zDisable all post-processing)ru   r£   r¢   r¤   z--notezBehaviour notes for new presetz-oz--only-pluginsrZ   zenable these plugins onlyz--presetzA preset identifier)r¢   r¦   r¤   ru   z--plugin-timeoutzset a timeout for all pluginsz--cmd-timeoutz%set a command timeout for all pluginsz-pz	--profilez
--profilesr^   z)enable plugins used by the given profilesz--skip-commandsrQ   zdo not execute these commands)ru   r¢   r£   r¤   z--skip-filesrR   zdo not collect these filesz--verifyr`   z+perform data verification during collectionz--allow-system-changesra   zJRun commands even if they can change the system (e.g. load kernel modules)z--uploadz+Upload archive to a policy-default locationz--upload-urlz&Upload the archive to specified serverz--upload-directoryz$Specify upload directory for archivez--upload-userz'Username to authenticate to server withz--upload-passz'Password to authenticate to server withz--upload-method)r/   ÚputÚpostz HTTP method to use for uploading)ru   Úchoicesr¤   z--upload-no-ssl-verifyz'Disable SSL verification for upload url)ru   r¢   r¤   z--upload-s3-endpointz#Endpoint to upload to for S3 bucketz--upload-s3-regionz!Region to upload to for S3 bucketz--upload-s3-bucketz"Name of the S3 bucket to upload toz--upload-s3-access-keyzAccess key for the S3 bucketz--upload-s3-secret-keyzSecret key for the S3 bucketz--upload-s3-object-prefixzPrefix for the S3 object/keyz--upload-protocol)r/   ÚhttpsÚftpÚsftpÚs3z$Manually specify the upload protocolú--add-presetz#Add a new named command line preset)r¦   r¢   r¤   z--del-presetz$Delete the named command line presetzCleaner/Masking Optionsz7These options control how data obfuscation is performedz--cleanz	--cleanerz--maskr8   zObfuscate sensitive information)r£   ru   r¢   r¤   z	--domainsr<   z$Additional domain names to obfuscatez--disable-parsersr=   zCDisable specific parsers, so that those elements are not obfuscated)r¢   ru   r£   r¤   z--skip-cleaning-filesz--skip-masking-filesr>   zBList of files to skip/ignore during cleaning. Globs are supported.z
--keywordsrE   zList of keywords to obfuscatez--keyword-filerF   z&Provide a file a keywords to obfuscate)ru   r£   r¤   z--no-updaterX   z%Do not update the default cleaner mapz
--map-filerO   rP   z;Provide a previously generated mapping file for obfuscation)r£   ru   r¤   z--keep-binary-filesr:   zGKeep unprocessable binary files in the archive instead of removing themz--usernamesrb   zList of usernames to obfuscate)Úadd_argument_groupÚadd_argumentr.   ÚstrÚintÚadd_mutually_exclusive_group)Úclsrœ   Ú
report_grpÚ
preset_grpÚcleaner_grpr)   r)   r*   Úadd_parser_options¹   sØ  þ
þþþþ
ÿ
þÿ
þÿÿþ
þ
þ
ü
þ
þÿþ
þþÿ
þþþþÿ
þ
ÿÿÿ
ý
þ
þþþ
ÿÿÿÿÿþþÿÿÿÿÿÿý
ÿ
ÿþ
ý
þþý
þþþþý

þzSoSReport.add_parser_optionsc                 C   sÎ   |  d¡ | d¡ | d¡ | d¡ |jdd�}| d¡ | d¡ |jd	d�}| d
tdƒ› dtdƒ› �¡ | dtdƒ› d�¡ ddddœ}d}|D ]}|jd|d›|| › �dd� qN| |¡ d S )NzSoS Report Detailed HelpzâThe report command is the most common use case for SoS, and aims to collect relevant diagnostic and troubleshooting data to assist with issue analysis without actively performing that analysis on the system while it is in use.z‘Additionally, sos report archives can be used for ongoing inspection for pre-emptive issue monitoring, such as that done by the Insights project.zëThe typical result of an execution of 'sos report' is a tarball that contains troubleshooting command output, copies of config files, and copies of relevant sections of the host filesystem. Root privileges are required for collections.zHow Collections Are Determined)ÚtitlezÂSoS report performs it's collections by way of 'plugins' that individually specify what files to copy and what commands to run. Plugins typically map to specific components or software packages.aE  Plugins may specify different collections on different distributions, and some plugins may only be for specific distributions. Distributions are represented within SoS by 'policies' and may influence how other SoS commands or options function. For examplepolicies can alter where the --upload option defaults to or functions.zSee Alsoz5For information on available options for report, see zsos report --helpz and zman sos-reportzThe following zsos helpz sections may be of interest:
z'Information on the plugin design of sosz!Information on a specific $pluginz+How sos operates on different distributions)zreport.pluginszreport.plugins.$pluginÚpoliciesr   ú	z<36F)r'   )Ú	set_titleÚadd_textrŠ   r   )rµ   ÚsectionÚpsecÚssecÚ
help_linesÚhelplnÚlnr)   r)   r*   Údisplay_help‡  sD   
ÿÿÿÿÿ	ÿÿÿý zSoSReport.display_helpc                 C   s    t dtdt› d�ƒ› d�ƒ d S )NÚ
zsosreport (version rx   )ÚprintÚ_r   ©r›   r)   r)   r*   r„   ¾  s    zSoSReport.print_headerc                 C   s.   |   ¡ |  ¡ dœ|  ¡ |  ¡ |  ¡ dœ| _d S )N)ÚblockÚfibre)ÚstorageÚnetworkÚnamespaced_networkÚfstype)Ú_get_block_devsÚ_get_fibre_devsÚ_get_network_devsÚ_get_network_namespace_devicesÚ_get_devices_by_fstypeÚdevicesrÉ   r)   r)   r*   rš   Á  s   þùzSoSReport._get_hardware_devicesc                 C   sÖ   | j jdkri| j j ¡ }|dv ri | j_| j d¡ dS | jjs.d|› d�}| j |¡ dS || jj ¡ vrNd 	dd„ | jj ¡ D ƒ¡}t
d	|› d
|› �ƒ‚| jj| | jjd< | j d| jjd j› d�¡ dS dS )a[  Check the loaded container runtimes, and the policy default runtime
        (if set), against any requested --container-runtime value. This can be
        useful for systems that have multiple runtimes, such as RHCOS, but do
        not have a clearly defined 'default' (or one that is determined based
        entirely on configuration).
        r/   )ÚnoneÚoffÚdiabledz0Disabled all container runtimes per user option.zVWARNING: No container runtimes are active, ignoring option to set default runtime to 'ú'
r   c                 s   s   � | ]	}|d kr|V  qdS )ru   Nr)   ©Ú.0Úpr)   r)   r*   Ú	<genexpr>ß  s   € ÿÿz5SoSReport._check_container_runtime.<locals>.<genexpr>zCannot use container runtime 'z1': no such runtime detected. Available runtimes: ru   z"Set default container runtime to 'ú'N)r�   r9   Úlowerrw   Úruntimesr�   ÚinfoÚwarningÚkeysÚjoinÚ	ExceptionÚname)r›   Úcrunrž   Úvalidr)   r)   r*   r˜   Ì  s0   ÿÿ
þÿÿíz"SoSReport._check_container_runtimec              
   C   s‚   z#g }g d¢}|D ]}t j d|› �¡r | t d|› d�¡¡ q	|W S  ty@ } z| j d|› �¡ g W  Y d}~S d}~ww )z·Enumerate a list of fibrechannel devices on this system so that
        plugins can iterate over them

        These devices are used by add_device_cmd() in the Plugin class.
        )Úfc_hostÚfc_transportÚfc_remote_portsÚ	fc_vportsz/sys/class/z/*z!Could not get fibre device list: N)r�   ÚpathÚisdirr§   Úglobrå   r�   r“   )r›   ÚdevsÚdevdirsÚdevdirÚerrr)   r)   r*   rÑ   ë  s   €€þzSoSReport._get_fibre_devsc              
      sÄ   zDdd„ t  d¡D ƒ}tdƒ}g ‰ |d dkr/|d  ¡ D ]}| ¡ d  dd	¡}ˆ  |¡ q‡ fd
d„|D ƒ}tt|ƒt|ƒ ƒ}|W S  t	ya } z| j
 d|› �¡ g W  Y d}~S d}~ww )z°Enumerate a list of block devices on this system so that plugins
        can iterate over them

        These devices are used by add_device_cmd() in the Plugin class.
        c                 S   s   g | ]}d |› �‘qS )z/dev/r)   )rÛ   Údr)   r)   r*   Ú
<listcomp>  ó    z-SoSReport._get_block_devs.<locals>.<listcomp>z
/sys/blockzlosetup --all --noheadingsÚstatusr   Úoutputú:r   c                    s"   g | ]}|  d ¡r|ˆ vr|‘qS )Úloop©Ú
startswith©rÛ   Údev©Úreal_loop_devicesr)   r*   rõ     s    ÿþz!Could not get block device list: N)r�   r   r	   Ú
splitlinesÚsplitÚreplacer    Úlistr   rå   r�   r“   )r›   Údevice_listÚloop_devicesÚloop_devÚloop_deviceÚghost_loop_devsÚdev_listró   r)   rÿ   r*   rÐ     s    €þzSoSReport._get_block_devsc                 C   s   d|   ¡ i| _d S )NrÍ   )Ú_get_network_namespacesrT   rÉ   r)   r)   r*   r™     s   
ÿzSoSReport._get_namespacesc              
   C   s    g g g g dœ}z,t d| jjd�r| |  ¡ ¡ |d s1| j d¡ | |  ¡ ¡ |  ¡ |d< W |S W |S  t	yO } z| j 
d|› �¡ W Y d}~|S d}~ww )	a  Helper to encapsulate network devices probed by sos. Rather than
        probing lists of distinct device types like we do for storage, we can
        do some introspection of device enumeration where a single interface
        may have multiple device types. E.G an 'ethernet' device could also be
        a bond, and that is pertinent information for device iteration.

        :returns:   A collection of enumerated devices sorted by device type
        :rtype:     ``dict`` with keys being device types
        )ÚethernetÚbridgeÚteamÚbondÚnmcli)r‚   r  zZNetwork devices not enumerated by nmcli. Will attempt to manually compile list of devices.r  z%Could not enumerate network devices: N)r   r�   r‚   ÚupdateÚ_get_nmcli_devsr�   r‘   Ú_get_eth_devsÚ_get_bridge_devsrå   râ   )r›   Ú_devsró   r)   r)   r*   rÒ     s,   üÿ÷	þ€þzSoSReport._get_network_devsc                 C   s*   i }| j d D ]}d|  |¡i||< q|S )zqEnumerate the network devices that exist within each network
        namespace that exists on the system
        rÍ   r  )rT   r  )r›   Ú_nmdevsÚnmspr)   r)   r*   rÓ   <  s
   

ÿz(SoSReport._get_network_namespace_devicesc              
   C   sÂ   i }zBt dƒ}|d dkr8|d  ¡ dd… D ] }| ¡ \}}||vr)|g||< n||  |¡ |d  |¡ qtt|d ƒƒ|d< W |S  ty` } z| j d|› �¡ W Y d}~|S d}~ww )	z‚Use nmcli, if available, to enumerate network devices. From this
        output, manually grok together lists of devices.
        znmcli --fields DEVICE,TYPE devr÷   r   rø   ry   Nr  zCould not parse nmcli devices: )	r	   r  r  r    r  r   rå   r�   r‘   )r›   r  Ú_ndevsrþ   ÚdnameÚdtyperó   r)   r)   r*   r  G  s"   þ€þzSoSReport._get_nmcli_devsc              
      s  dg‰ g }|s1z‡ fdd„t ddƒD ƒ}W n^ ty0 } z| j d|› �¡ W Y d}~nHd}~ww z$d|› d�}t|ƒ}|d	 d
krT|d  ¡ D ]}|ˆ vrS| |¡ qHW n tys } z| j d|› d|› �¡ W Y d}~nd}~ww |dd„ |D ƒdd„ |D ƒdœS )a8  Enumerate a list of ethernet network devices so that plugins can
        reliably iterate over the same set of devices without doing piecemeal
        discovery.

        These devices are used by `add_device_cmd()` when `devices` includes
        "ethernet" or "network".

        :param namespace:   Inspect this existing network namespace, if
                            provided
        :type namespace:    ``str``

        :returns:   All valid ethernet devices found, potentially within a
                    given namespace
        :rtype:     ``list``
        Úbonding_mastersc                    s   g | ]}|ˆ vr|‘qS r)   r)   rý   ©Ú	filt_devsr)   r*   rõ   r  s
    ÿz+SoSReport._get_eth_devs.<locals>.<listcomp>z/sys/class/netNz.Failed to manually determine network devices: zip netns exec z ls /sys/class/netr÷   r   rø   z'Could not determine network namespace 'z' devices: c                 S   ó   g | ]	}|  d ¡r|‘qS )r  rû   )rÛ   Úbdr)   r)   r*   rõ   ‰  ó    c                 S   r  )Útunrû   )rÛ   Útdr)   r)   r*   rõ   Š  r   )r  r  r!  )r   rå   r�   râ   r	   r  r    )r›   Ú	namespaceÚ	_eth_devsró   Ú_nscmdÚ_nsoutÚ_nsethr)   r  r*   r  [  sB   

ÿÿ€ÿ
€ÿÿ€ÿýzSoSReport._get_eth_devsc                 C   sÈ   g }zt ddd�}W n ty% } z| j d|› �¡ W Y d}~nd}~ww |d dkrb|d  ¡ d	d… D ]+}z| | ¡ d ¡ W q6 tya } z| j d
|› d|› �¡ W Y d}~q6d}~ww |S )zãEnumerate a list of bridge devices so that plugins can reliably
        iterate over the same set of bridges.

        These devices are used by `add_device_cmd()` when `devices` includes
        "bridge" or "network".
        z
brctl showé   ©Útimeoutz$Unable to enumerate bridge devices: Nr÷   r   rø   ry   z"Could not parse device from line 'z': )r	   rå   r�   râ   r  r    r  rá   )r›   Ú_bridgesÚ_boutró   Ú_bliner)   r)   r*   r  �  s$   €ÿÿ€ÿzSoSReport._get_bridge_devsc                 C   s\   g }t dƒ}|d dkr,|d  ¡ D ]}| ¡ s |dd…  ¡ r!q| | d¡d ¡ q|S )z·Enumerate a list of network namespaces on this system so that
        plugins can iterate over them

        Note that stderr is not collected, so no handling of error lines.
        zip netnsr÷   r   rø   Nry   r   )r	   r  Úisspacer    Ú	partition)r›   Úout_nsÚip_netnsr&   r)   r)   r*   r  £  s   z!SoSReport._get_network_namespacesc                 C   s�   i }t dƒ}|d dkr|S |d  ¡ D ]1}| ¡  ¡ }t|ƒdkr(| dd¡ d|d v r2d|d< | |d g ¡ ||d   |d ¡ q|S )	Nzlsblk -snrpo FSTYPE,NAMEr÷   r   rø   ry   ÚunknownÚextÚext4)r	   r  Ústripr  r   ÚinsertÚ
setdefaultr    )r›   Ú_dev_fstypesr  r&   Úhelperr)   r)   r*   rÔ   ³  s   z SoSReport._get_devices_by_fstypec                 C   s4   | j | j| j| j| j| j| j| jj| j| j	| j
dœS )N)ÚcmddirÚlogdirÚrptdirÚtmpdirr�   rw   r‚   Ú	verbosityÚcmdlineoptsrÕ   rT   )r:  r;  r<  r=  r�   rw   r‚   r�   r>  rÕ   rT   rÉ   r)   r)   r*   Úget_commonsÂ  s   õzSoSReport.get_commonsc                 C   s
   | j  ¡ S ©N)r–   ÚnewrÉ   r)   r)   r*   Úget_temp_fileÑ  ó   
zSoSReport.get_temp_filec                 C   s4   | j  | jd¡ | j  | jd¡ | j  | jd¡ d S )Nií  )ÚarchiveÚmakedirsr:  r;  r<  rÉ   r)   r)   r*   Ú_make_archive_pathsÔ  s   zSoSReport._make_archive_pathsc                 C   s   d| _ d| _d| _d S )NÚsos_commandsÚsos_logsÚsos_reports)r:  r;  r<  rÉ   r)   r)   r*   rŒ   Ù  s   
zSoSReport._set_directoriesc                 C   s$   | j jr| jt_d| _d S d| _d S )NTF)r�   r‘   Ú
_exceptionÚsysÚ
excepthookÚraise_pluginsrÉ   r)   r)   r*   r…   Þ  s   

zSoSReport._set_debugc                 C   sN   t tdƒs
tj ¡ st | ||¡ dS tj| ||dtjd� tƒ  t	 
¡  dS )z* Wrap exception in debugger if not in tty Úps1é   )ÚlimitÚfileN)ÚhasattrrL  ÚstderrÚisattyÚ__excepthook__Ú	tracebackÚprint_exceptionÚstdoutrÇ   ÚpdbÚpm)ÚetypeÚeval_Úetracer)   r)   r*   rK  å  s   ÿzSoSReport._exceptionc                 C   s^   | j s| jrt ¡ \}}}tj|||tjd� tƒ  t 	|¡ |r+|r-|  
||¡ d S d S d S )N)rR  )rN  Úexit_processrL  Úexc_inforW  rX  rY  rÇ   rZ  Úpost_mortemÚ_log_plugin_exception)r›   ÚplugnameÚfuncr\  ÚvalÚtbr)   r)   r*   Úhandle_exceptionô  s   
ÿzSoSReport.handle_exceptionc                 C   sX   t | dd ƒr| jj| jtj dd¡d� t | dd ƒr*| jj| jtj dd¡d� d S d S )NÚsos_log_filerI  úsos.log©r£   Úsos_ui_log_fileúui.log)ÚgetattrrE  Úadd_filerh  r�   rí   rä   rk  rÉ   r)   r)   r*   Ú_add_sos_logs   s   
ÿ

ÿÿzSoSReport._add_sos_logsc                    sT   ˆ j j}tˆ j jƒsdS t|dƒsdS |rˆ  | ¡ ¡sdS t‡ fdd„|jD ƒƒS )NTr^   Fc                    s   g | ]}|ˆ j jv ‘qS r)   )r�   r^   rÚ   rÉ   r)   r*   rõ     s    z,SoSReport._is_in_profile.<locals>.<listcomp>)r�   rZ   r   r^   rS  Ú_is_not_specifiedræ   Úany)r›   Úplugin_classrZ   r)   rÉ   r*   Ú_is_in_profile  s   
zSoSReport._is_in_profilec                 C   s   || j jv S rA  )r�   rS   ©r›   Úplugin_namer)   r)   r*   Ú_is_skipped  s   zSoSReport._is_skippedc                 C   ó*   ||   ¡ ƒ ¡  o|| jjvo|| jjvS rA  )r@  Úcheck_enabledr�   rB   rZ   ©r›   ru  ÚpluginClassr)   r)   r*   Ú_is_inactive  ó
   
ÿ
þzSoSReport._is_inactivec                 C   rw  rA  )r@  Údefault_enabledr�   rB   rZ   ry  r)   r)   r*   Ú_is_not_default  r|  zSoSReport._is_not_defaultc                 C   s   | j jo	|| j jvS rA  )r�   rZ   rt  r)   r)   r*   rp  #  s   
ÿzSoSReport._is_not_specifiedr2  c                 C   s"   | j  | ¡ ||  ¡ ƒ|f¡ d S rA  )r}   r    ræ   r@  )r›   rr  Úreasonr)   r)   r*   Ú_skip'  s
   

ýzSoSReport._skipc                 C   s    | j  | ¡ ||  ¡ ƒf¡ d S rA  )r|   r    ræ   r@  )r›   rr  r)   r)   r*   Ú_load.  s   

þzSoSReport._loadc                 C   s€  t jjj}tt jjƒ}| ¡ }g | _tƒ | _t	| j
jƒ}| jj}g }| j
jr.| t jjj¡ t|| ƒ}| jj}t| j
jƒ}	|D ]Þ}
tj |
¡\}}z±|||ƒ}t	|ƒsVW q@| j |¡}||| j
jd�s~| j tdƒ|
 ¡ | j
jdkr~|  |tdƒ¡ W q@| j |¡ |  |¡}|s•|  |tdƒ¡ W q@|  |¡r¤|  |tdƒ¡ W q@|  ||¡r´|  |tdƒ¡ W q@|  ||¡rÄ|  |tdƒ¡ W q@t |d	ƒrÐ| j !|j¡ | oÔ|}|  "|¡ræ|ræ|  |td
ƒ¡ W q@|jD ]}||	v rô|	 #|¡ qé|  $|¡ W q@ t%�y } z| j tdƒ|
|f ¡ |  &¡  W Y d }~q@d }~ww t	|	ƒdk�r>| j 'tdƒd (|	¡ ¡ |  )¡  |  *d¡ d S d S )N)rA   z%plugin %s does not validate, skippingr   zdoes not validateÚexcludedÚskippedÚinactiveÚoptionalr^   znot specifiedz(plugin %s does not install, skipping: %sz+Unknown or inactive profile(s) provided: %sr   ry   )+Úsosrt   ÚpluginsÚimport_pluginr   Úget_modulesÚplugin_namesr   r^   r   r�   rw   Úvalid_subclassesrA   r    ÚExperimentalPluginÚtupleÚvalidate_pluginr  r�   rí   ÚsplitextÚmatch_pluginr�   râ   rÈ   r>  r€  rs  rv  r{  r~  rS  r  rp  Úremover�  rå   rg  r“   rä   rK   r—   )r›   rˆ  r9  r‡  Úusing_profilesÚpolicy_classesÚextra_classesÚvalid_plugin_classesrŽ  Úremaining_profilesÚplugÚplugbaser3  Úplugin_classesrr  Ú
in_profileÚdefault_profileÚiÚer)   r)   r*   Úload_plugins4  sˆ   

ÿ
ÿ





€ÿ€ýÿüzSoSReport.load_pluginsc                 C   s@   | j jr| jD ]\}}|j ¡ D ]
}t|jv rd|_qqd S d S )NT)r�   r3   r|   ÚoptionsÚvaluesÚboolÚval_typeÚvalue)r›   rc  r—  Úoptr)   r)   r*   Ú_set_all_optionsƒ  s   
€þþzSoSReport._set_all_optionsc                 C   s  | j jrÿi }| j jD ]o}z	| d¡\}}W n ty    d}Y nw t|tƒrH| ¡ }|dv r1d}n|dv r8d}nzt|ƒ}W n	 tyG   Y nw z	| d¡\}}W n ty^   |}d}Y nw z||  W n tyr   i ||< Y nw ||| |< q
| j	D ]c\}}||v rà|| D ]S}||j
vr¢| j d|› d|› d	�¡ |  d
¡ z|j
|  || | ¡ | j d|› d|j
| › �¡ W q‰ tyÜ } z| j |¡ |  d
¡ W Y d }~q‰d }~ww ||= q}| ¡ D ]}| j d|› d�¡ qå| ¡ �r| j d¡ d S d S d S )Nú=T)ÚonÚenableÚenabledÚtrueÚyes)r×   ÚdisableÚdisabledÚfalseÚnoFÚ.zno such option "z" for plugin (rx   ry   zSet z plugin option to zCWARNING: unable to set option for disabled or non-existing plugin (z).r   )r�   rG   r  Ú
ValueErrorÚ
isinstancer²   rß   r³   ÚKeyErrorr|   rŸ  r�   r“   r—   Ú	set_valuer‘   rå   rã   )r›   r�   r¤  re  r—  rc  ró   r)   r)   r*   Ú_set_tunablesŠ  sz   ÿ
þþÿ

ÿ
ÿÿ€þ€ÿ
È7zSoSReport._set_tunablesc                 C   sˆ   dd l }| | jj| jj¡D ]}| d¡d }|| jvr+| j d|› d�¡ |  	d¡ q| jj
D ]}|| jvrA| j d|› d�¡ q0d S )Nr   r°  za non-existing plugin (z$) was specified in the command line.ry   z'Requested to skip non-existing plugin 'z'.)Ú	itertoolsÚchainr�   rZ   rB   r  rŠ  r�   Úfatalr—   rS   râ   )r›   r¶  Úpluginru  r)   r)   r*   Ú_check_for_unknown_pluginsÅ  s    
ÿ

€

ÿ€þz$SoSReport._check_for_unknown_pluginsc                 C   s2   | j D ]\}}|jD ]}| j |j| ¡ q
qd S rA  )r|   rŸ  r~   r    )r›   ru  r¹  r¤  r)   r)   r*   Ú_set_plugin_optionsÔ  s
   
ÿÿzSoSReport._set_plugin_optionsc                 C   sÆ   d}g }| j jdkr|d| j j› d�g7 }d| j _| j js&|dg7 }d| j _| j js3|dg7 }d| j _| j jr@|dg7 }d	| j _| j jrM|d
g7 }d	| j _|rY|dd |¡ 7 }n|d7 }|d7 }|S )Nz
Estimate-only mode enabledry   z
--threads=z overriden to 1z--build enabledTz--no-postproc enabledz--clean disabledFz--upload* options disabledz", which overrides some options:
  z
  r°  z

)r�   Úthreadsr5   rW   r8   rc   rä   )r›   rž   Úext_msgr)   r)   r*   Ú_set_estimate_onlyÙ  s,   



zSoSReport._set_estimate_onlyc                 C   sh   | j  d¡ t| jƒr| j  dt| jƒ› dt| jƒ› d�¡ n| j  dt| jƒ› d�¡ | j  d¡ d S )Nr   r   z profiles, z pluginsz	 profiles)Úui_lográ   r   r|   r^   rÉ   r)   r)   r*   Ú_report_profiles_and_pluginsô  s   
ÿz&SoSReport._report_profiles_and_pluginsc              	   C   s¸  | j s| js| j tdƒ¡ d S | j r8| j tdƒ¡ | j d¡ | j D ]\}}| j |d›d| ¡ › �¡ q$n| j tdƒ¡ | j d¡ | jrr| j tdƒ¡ | j d¡ | jD ]\}}}| j |d›d|d›d| ¡ › �¡ qZ| j d¡ | j�r)| j td	ƒ¡ | j d
 d  	¡ }|D ]B}|| }|j
}|j
dkrµ|dkr¨| jjp¦t}n|dkr³| jjp±t}nt}|jdkr¿| jj }| j |jd›d|d›d|j› �¡ q�| j d¡ | j tdƒ¡ | jD ]D}|jdv rñ|j
|jkrñqãt|j
tƒ�r|j
du �rd}	nd}	n|j
}	|	d u �rd
}	| j d|j› d|j› �d›d|	d›d|j› �¡ qãn| j tdƒ¡ | j d¡ t| jƒ}
|
 ¡  td|
dd�}|D ]}| j d|› �¡ �qI|  ¡  d S )Nzno valid plugins foundz,The following plugins are currently enabled:r   z<20r   zNo plugin enabled.z-The following plugins are currently disabled:z<14z4The following options are available for ALL plugins:r   ry   éÿÿÿÿr*  úcmd-timeoutÚpostprocz<25ú<15z+The following plugin options are available:)r*  rÃ  rÂ  Tr§  r×   r°  zNo plugin options available.z
Profiles: ©r#   )r|   r}   r�   r¸  rÈ   r¿  rá   Úget_descriptionr~   Úget_default_plugin_optsr£  r�   r\   r
   r]   ræ   rW   r;   ru   r²  r¡  r¹  r  r^   Úsortr+   rÀ  )r›   rc  r—  Ú	plugclassr  Ú	_defaultsÚ_optr¤  re  Útmpoptr^   r%   r&   r)   r)   r*   rI   þ  sz   ÿ
ÿ


&


ÿ
ÿð
zSoSReport.list_pluginsc           	      C   sÄ   | j s| j tdƒ¡ d S | j tdƒ¡ | j d¡ dd„ }t| j ƒ}| ¡  |D ]1}g }| jD ]\}}||ƒrC||j v rC| 	|¡ q1t
|d›|dd�}|D ]}| j d	|› �¡ qOq*|  ¡  d S )
Nzno valid profiles foundz%The following profiles are available:r   c                 S   s
   t | dƒS )Nr^   )rS  )Úcr)   r)   r*   Ú	_has_profN  rD  z*SoSReport.list_profiles.<locals>._has_profrÄ  TrÅ  r   )r^   r�   r¸  rÈ   r¿  rá   r  rÈ  r|   r    r+   rÀ  )	r›   rÎ  r^   Úprofiler‡  ræ   r¹  r%   r&   r)   r)   r*   rK   G  s&   

€ÿzSoSReport.list_profilesc                 C   sø   | j js| j tdƒ¡ d S | j tdƒ¡ | j d¡ | j j ¡ D ]W}|s'q"| j  |¡}| j d|j	d›�¡ | j d|j
d›�¡ |jrQ| j d|jd›�¡ | jjdkrs|j ¡ }d	d›}t||d
dd�}|D ]}| j |¡ qj| j d¡ q"d S )Nzno valid presets foundz$The following presets are available:r   zname: z>14zdescription: znote: r   zoptions:Tr   )r#   r$   )rw   Úpresetsr�   r¸  rÈ   r¿  rá   rã   Úfind_presetræ   r;   rY   r�   r>  Úto_argsr+   )r›   r[   r�   Úoptions_strr%   r&   r)   r)   r*   rJ   ]  s*   
ñzSoSReport.list_presetsc              
   C   sâ   | j }| |¡r| j d|› d�¡ dS |p| jj}|p| jj}z|j|||| jd� W n tyH } z| j d|› �¡ W Y d}~dS d}~ww | j	 
d¡}| j	d|… | j	|d	 d…  }| j d
|› dd |¡› d�¡ dS )zÑAdd a new command line preset for the current options with the
            specified name.

            :param name: the name of the new preset
            :returns: True on success or False otherwise
        zA preset named 'z' already existsF)ræ   r;   rY   r�   zCould not add preset: Nr¯   r   rP  zAdded preset 'z' with options r   rÆ   T)rw   rÑ  r¿  r“   r�   r;   rY   rq   rå   rv   Úindexrá   rä   )r›   ræ   r;   rY   rw   r�  Ú	arg_indexr�   r)   r)   r*   rq   u  s&   
€þ ÿzSoSReport.add_presetc              
   C   sŒ   | j }| |¡s| j d|› d�¡ dS z|j|d� W n ty9 } z| j t|ƒd ¡ W Y d}~dS d}~ww | j d|› d�¡ d	S )
z Delete a named command line preset.

            :param name: the name of the preset to delete
            :returns: True on success or False otherwise
        zPreset 'z' not foundF©ræ   rÆ   NzDeleted preset 'rÙ   T)rw   rÑ  r¿  r“   rr   rå   r²   rá   )r›   ræ   rw   r�  r)   r)   r*   rr   ’  s   
€þzSoSReport.del_presetc              
   C   s¬   | j  ¡ }| jjr||  ¡ 7 }| jjr| j |¡ d S |tdƒ7 }zt	|ƒ W d S  t
y=   | j d¡ |  d¡ Y d S  tyU } z|  d|¡ W Y d }~d S d }~ww )Nz,Press ENTER to continue, or CTRL-C to quit.
zExiting on user cancelé‚   ry   )rw   Úget_msgr�   r@   r¾  Úbatchr¿  rá   rÈ   ÚinputÚKeyboardInterruptr“   r—   rå   )r›   rž   r�  r)   r)   r*   rÙ  ¦  s   
€ÿzSoSReport.batchc                 C   sr   t  ¡ }d}|› d�}tj | j|¡}| j |› d|› d|› d�¡ | j d|› �¡ | jj	|› d�|dd	� d S )
Nz!caught exception in plugin methodz-plugin-errors.txtz "r°  z()"zwriting traceback to rÆ   Úa)Úmode)
rW  Ú
format_excr�   rí   rä   r;  r�   r“   rE  Ú
add_string)r›   r¹  ÚmethodÚtracerž   Úplugin_err_logÚlogpathr)   r)   r*   rb  ¶  s   
zSoSReport._log_plugin_exceptionc              
   C   sô   | j  ¡  z| j tdƒ¡ |  ¡  |  ¡  W d S  ttfyK } z%|j	t
v r9tdƒ td|j› d�ƒ tdƒ ntd|› �ƒ ‚ W Y d }~n,d }~w tyr } z| j d¡ | j d¡ t ¡  | j |¡ W Y d }~nd }~ww |  d¡ d S )Nz Setting up archive ...r   r   z while setting up archivezError setting up archive: z) Unexpected exception setting up archive:ry   )rw   Úpre_workr¿  rá   rÈ   Úsetup_archiverG  ÚOSErrorÚIOErrorÚerrnoÚfatal_fs_errorsrÇ   Ústrerrorrå   r“   rW  Ú	print_excr—   ©r›   r�  r)   r)   r*   Úprework¿  s.   


ý€€üzSoSReport.preworkc                 C   s^  | j  tdƒ¡ | jD ]¡\}}zM| jj |¡ | t| jj|ƒ¡ t	 
¡ }|j d|¡ | j|_| ¡  | ¡  | j |j¡ | jjrG| ¡  t	 
¡ }|j d|¡ |j d|| ¡ W q tyd   ‚  ttfyž } z-|jtv rŽ| j  d¡ | j  d|j› d�¡ | j  d¡ |  d¡ |  |d	¡ W Y d }~qd }~w ty¬   |  |d	¡ Y qw d S )
Nz Setting up plugins ...Úsetup_startÚ	setup_endÚ
setup_timer   r   z while setting up pluginsry   Úsetup) r¿  rá   rÈ   r|   r‹   r‡  rŠ   Úset_plugin_manifestrm  r   Únowrˆ   Ú	add_fieldrE  Úadd_default_collectionsrñ  r€   r  Ú	_env_varsr�   r`   Úsetup_verifyrÛ  ræ  rç  rè  ré  r“   rê  r—   rg  rå   )r›   rc  r—  ÚstartÚendr�  r)   r)   r*   rñ  Ø  sD   ÿ
ÿ
€ÿæzSoSReport.setupc                 C   s.   g }|  dt› �¡ | jjd |¡dd� dS )zWFetch version information from all plugins and store in the report
        version filezsosreport: rÆ   zversion.txt)Úcontentr£   N)r    r   rE  rß  rä   )r›   Úversionsr)   r)   r*   Úversion÷  s
   
ÿzSoSReport.versionc                 C   sö   | j  tdƒ¡ | j  d¡ d}g | _g | _| jD ]}|d7 }| j ||d f¡ qz;g }t| jj	ƒ�}| 
| jt| jƒ¡}W d   ƒ n1 sHw   Y  |D ]}|s\| j d|› �¡ qO| j  d¡ W d S  tyz   | j  d¡ t d¡ Y d S w )Nz! Running plugins. Please wait ...r   r   ry   zUnexpected plugin task result: z
Exiting on user cancel
)r¿  rá   rÈ   ÚpluglistÚrunning_plugsr|   r    r   r�   r¼  ÚmapÚ_collect_pluginr  r�   r‘   rÛ  r“   r�   r—   )r›   Úplugruncountrœ  ÚresultsÚexecutorÚresr)   r)   r*   Úcollect  s0   
ÿÿ€ýzSoSReport.collectc              	   C   sÚ  t dƒ�ˆ}z>| j|d d  d }| | j|¡}|jpd}t ¡ }|j d|¡ |j	|d� t ¡ }|j d|¡ |j d|| ¡ W nA t
y„   d|d › d	�}| j d
|› d�¡ | j |¡ | j |d ¡ | j|d d  d  ¡  |jdd� |j ¡  Y nw W d  ƒ n1 s�w   Y  | jjrë| j ¡ }	ztd|	› �ƒ}
t|
d  ¡ d ƒ| j|d < W n tyÅ   d| j|d < Y nw t |	¡D ]}tj  !|	|¡}tj  "|¡råtj  #|¡såt$|ƒ qËt %|¡ qËdS )zaWraps the collect_plugin() method so we can apply a timeout
        against the plugin as a wholery   r   NÚ
start_timer)  Úend_timeÚrun_timezPlugin z
 timed outú
 rÆ   T)Úwaitzdu -sB1 rø   )&r   r|   ÚsubmitÚcollect_pluginr*  r   ró  rˆ   rô  Úresultr   r¿  r“   r�   rá   rþ  r‘  Úset_timeout_hitr•   Ú_threadsÚclearr�   r@   rE  Úget_tmp_dirr	   r³   r  rƒ   rå   r�   r   rí   rä   rî   Úislinkr   Úunlink)r›   r¹  ÚpoolÚ_plugÚtr*  rø  rù  rž   r=  ÚduÚfr)   r)   r*   r     sJ   

ö€ó
ÿÿ
zSoSReport._collect_pluginc           	   
   C   s  z|\}}| j |d  d }| j |¡ W n
 ty   Y dS w t| j ƒ}d|› d|› �d›d|d›dd d	d
„ | jD ƒ¡› d�}|  |¡ zt| ¡  z| j 	|¡ W n t
yf   | j d|› d�¡ Y nw z| j 	|¡ W n t
y‚   | j d|› d�¡ Y nw d}t| jƒt| jjƒkr¦| jr¦ddd›dd dd
„ | jD ƒ¡› d�}| js®| js®d}|r¸|  |¡ W d S W d S  tyÄ   Y d S  ttfyþ } z-|jtv rí| j d|j› d�¡ | j d| j› d�¡ t d¡ |  |d¡ W Y d }~d S d }~w t�y   |  |d¡ Y d S w )Nry   Fz  Starting rs   z<5r   rÄ  z [Running: c                 s   ó   � | ]}|V  qd S rA  r)   rÚ   r)   r)   r*   rÝ   S  ó   € z+SoSReport.collect_plugin.<locals>.<genexpr>ú]zCould not remove z from plugin list, ignoring...z& from running plugin list, ignoring...r   z  Finishing plugins z<12c                 s   r  rA  r)   rÚ   r)   r)   r*   rÝ   k  r  z
  Finished running pluginsr	  z while collecting plugin dataz# Data collected still available at rÆ   r  )r|   rþ  r    rå   r   rä   Úui_progressr  rý  r‘  r±  r�   r‘   r³   r�   r¼  r   ræ  rç  rè  ré  r¿  r“   rê  r=  r�   r—   rg  )	r›   r¹  Úcountrc  r—  ÚnumplugsÚstatus_liner÷   r�  r)   r)   r*   r  J  sz   ÿ
ÿ

ÿÿ
ÿÿÿ
ÿðòö
ÿÿ
€þþzSoSReport.collect_pluginc                 C   sV   | j jdkr| j jsd| d¡› �}n|› d�}| j js)tj |¡ tj ¡  d S d S )Nr   úéZ   rÆ   )	r�   r>  rÙ  ÚljustÚquietrL  rY  ÚwriteÚflush)r›   r  r)   r)   r*   r  €  s   
þzSoSReport.ui_progressc                 C   s@   | j sd S d dd„ dd„ | j D ƒD ƒ¡d }| j |d¡ d S )NrÆ   c                 S   s   g | ]\}}|› d |› �‘qS )r¦  r)   )rÛ   ræ   re  r)   r)   r*   rõ   Œ  s    ÿz.SoSReport.collect_env_vars.<locals>.<listcomp>c                 S   s.   g | ]}t j |¡d ur|t j |¡› f‘qS rA  )r�   ÚenvironÚget)rÛ   ræ   r)   r)   r*   rõ   Ž  s    ÿÚenvironment)r€   rä   rE  rß  )r›   Úenvr)   r)   r*   Úcollect_env_vars‰  s   
þüzSoSReport.collect_env_varsc                 C   sâ  t ƒ }| jD ]p\}}t|d�}|jD ]	}| t|ƒ¡ q|jr'| t|jƒ¡ |jD ]}| t	|d d|d  d�¡ q*|j
D ]}| t|d dtj d|  ¡ d |d	 ¡d
�¡ q?|jD ]\}}}	| t|tj d|¡d�¡ q]| |¡ qtddftddftddff}
|
D ]g\}}}z*|  ¡ }||ƒ ¡ }| dd¡ ¡ }| |¡ | ¡  | jj|tj d|¡d� W q‡ ttfyî } z*|jt v rä| j! "d¡ | j! "d|j#› d|› d�¡ | j! "d¡ |  $d¡ W Y d }~q‡d }~ww d S )NrÖ  Úsrcpathz..Údstpath)ræ   ÚhrefÚcmdr   r:  rR  )ræ   Úreturn_coder-  zsos.txtÚtextzsos.jsonÚJSONzsos.htmlÚHTMLzutf-8r  rJ  rj  r   r   z while writing z reportry   )%r   r|   r   ÚalertsÚaddr   Úcustom_textr   Úcopied_filesr   Úexecuted_commandsr   r�   rí   rä   r@  Úcopy_stringsr   r   r   r   rC  ÚunicodeÚencodeÚdecoder$  r%  rE  rn  ræ  rç  rè  ré  r¿  r“   rê  r—   )r›   rt   rc  r—  r¿   Úalertr  r.  rú  ÚtagsÚ
formatlistÚclass_ÚfilenameÚtype_Úfdrø   r�  r)   r)   r*   Úgenerate_reports“  sb   




ÿ

ý
ÿ
ÿý
ÿ
ÿ
€úözSoSReport.generate_reportsc                 C   sÐ   | j D ]b\}}z| d¡r| ¡  n	| j d|› �¡ W q ttfyW } z-|jtv rG| j	 
d¡ | j	 
d|j› d�¡ | j	 
d¡ |  d¡ |  |d¡ W Y d }~qd }~w tye   |  |d¡ Y qw d S )NrÃ  zSkipping postproc for plugin r   r   z" while post-processing plugin datary   )r|   Ú
get_optionrÃ  r�   rá   ræ  rç  rè  ré  r¿  r“   rê  r—   rg  rå   )r›   rc  r—  r�  r)   r)   r*   rÃ  Ê  s,   

ÿ€
ÿ
€ÿñzSoSReport.postprocc                 C   sr   |sdS z$d}t |dƒ}t |¡}	 | |¡}|sn| |¡ q| ¡  W | ¡ S  ty8   |  ¡  Y | ¡ S w )NFi   Úrb)	ÚopenÚhashlibrB  Úreadr  Úcloserå   rg  Ú	hexdigest)r›   rE  Ú	hash_nameÚ	hash_sizeÚ
archive_fpÚdigestÚhashdatar)   r)   r*   Ú_create_checksumÝ  s$   



ü
þ
þzSoSReport._create_checksumc                 C   s0   t |d | dƒ}|r| |d ¡ | ¡  d S )Nr°  ÚwrÆ   )rF  r$  rI  )r›   rE  rK  ÚchecksumÚfpr)   r)   r*   Ú_write_checksumï  s   zSoSReport._write_checksumc              
      s8  d }d }d }|   ¡  d}| jjrQz%| j| j| j| j| jdœ}td|d�}| | j	 
¡ ¡ | ¡ \}}d}W n tyP } zttd|› �ƒƒ W Y d }~nd }~ww |  ¡  | jd urb| j	 | jj¡ |  ¡  |r™tj | j| j	j¡}	|jtj |	dd¡dd� |jtj |	dd	¡d	d� |jtj |	d
d¡dd� | jjr¡|  ¡  | jj�rddlm‰  ddlm}
 |
| jƒ}tdd„ |  d¡D ƒƒ| j!d< ˆ t| j! "¡ ƒƒ}| j# $d|› �¡ t%| j! &¡ dd„ dd�d d… }d ‡ fdd„|D ƒ¡}| j# $d|› �¡ | j# $d¡ | j# $d¡ | j# $d¡ | j# $d¡ | jj'�sšt (d¡}| jj�s*ttd ƒƒ ziz|�r5| j	 )|¡ | j	 *| jj¡}W nN t+t,f�yv } z)tdƒ ttd!|j-› d"| j	 
¡ › �ƒƒ tdƒ |j.t/v �rl|  0d#¡ W Y d }~nd }~w t�yŒ   | jj1�rƒ‚ Y W t (|¡ dS w W t (|¡ nRt (|¡ w | jj2�s¤| jj3�rª| j# 4d$¡ | j	 
¡ }tj 5|¡}|�r½| 6|¡}ztj | j|¡}t 7||¡ |}W n t+t,f�yå   ttd%|› �ƒƒ Y dS w d }| jj'�s©|�sõtd&ƒ n¾z| j 8¡ }|  9||¡}W n t�y   ttd'ƒƒ Y dS w z	|  :|||¡ W n t+t,f�y2   ttd(|› �ƒƒ Y nw tj 5|¡}|�rE| 6| ;d)d*¡¡}tj | j|¡}t <|¡}|d+ | }|d+ | }z
t 7||¡ |}W n t+t,f�y~   ttd,|› �ƒƒ Y dS w zt 7||¡ W n t+t,f�yœ   ttd-|› �ƒƒ Y nw | jj=|||||d.� n
| jj=||||d.� | jj>�sÂ| jj?�sÂ| jj@�rü| jj'�sôz| j A|¡ | j# $td/ƒ¡ W n$ t�yó } z| j# Bd0|› �¡ W Y d }~nd }~ww d1}| j# B|¡ tC D¡  | jE�r	| jE ¡  | j�rtj F| j¡�rtG| jƒ dS )2NF)rw   r=  Úsys_tmprŸ  rˆ   T)Úin_placeÚhook_commonsz#ERROR: Unable to obfuscate report: rI  ri  )Ú
short_namerl  rJ  zmanifest.jsonr   ©Úget_human_readable)ÚPathc                 S   s   g | ]}|  ¡ j‘qS r)   )ÚlstatÚst_size)rÛ   r  r)   r)   r*   rõ   1  rö   z(SoSReport.final_work.<locals>.<listcomp>z**/*Úsos_logs_reportszNEstimated disk space requirement for whole uncompressed sos report directory: c                 S   s   | d S )Nry   r)   )Úxr)   r)   r*   Ú<lambda>7  s    z&SoSReport.final_work.<locals>.<lambda>)ÚkeyÚreverseé   z,  c                 3   s*   � | ]\}}|› d ˆ |dd�› �V  qdS )z: r   )Ú	precisionNr)   )rÛ   rÜ   ÚvrY  r)   r*   rÝ   8  s   € ÿz'SoSReport.final_work.<locals>.<genexpr>zFive biggest plugins:  r   z>Please note the estimation is relevant to the current options.z€Be aware that the real disk space requirements might be different. A rule of thumb is to reserve at least double the estimation.é?   zCreating compressed archive...r   z while finalizing archive ry   zQ
Unable to encrypt when using --build. Encryption is only available for archives.zError moving directory: z Creating archive tarball failed.z:Error generating archive checksum after archive creation.
z!Error writing checksum for file: z.tarz-obfuscated.tarr°  zError moving archive file: zError moving checksum file: )rO   zUploaded archive successfullyzUpload attempt failed: zEUnable to upload archive when using --build as no archive is created.)HÚgenerate_manifest_tag_summaryr�   r8   rw   r=  rU  rˆ   r   Úset_target_pathrE  Úget_archive_pathÚexecuterå   rÇ   rÈ   ro  Úadd_final_manifest_dataÚcompression_typeÚ_obfuscate_upload_passwordsr�   rí   rä   Ú_nameÚobfuscate_filer#  Úadd_ui_log_to_stdoutr@   Úsos.utilitiesrZ  Úpathlibr[  Úsumrï   rƒ   r   r¿  rá   Úsortedr"   r5   ÚumaskÚrename_archive_rootÚfinalizeræ  rç  rê  rè  ré  r—   r‘   Úencrypt_passÚencrypt_keyrâ   ÚbasenameÚobfuscate_stringÚrenameÚget_preferred_hash_namerP  rT  r  ÚstatÚdisplay_resultsrc   rd   rk   Úupload_archiver“   r”   r•   r–   rî   r   )r›   rE  Ú	directoryrO   Údo_cleanrW  ÚcleanerÚ_pathsró   Ú_dirr[  Útmpdir_pathÚ_sumÚ
bigpluginsÚbp_outÚ	old_umaskr�  Údir_nameÚ	final_dirrR  rK  Úbase_archiveÚ
final_nameÚarchivestatÚarchive_hashÚ
final_hashrž   r)   rY  r*   Ú
final_workö  sL  û€ÿ
ÿÿþ


ÿÿ
ÿÿÿ


ÿ
ÿ
€
ú€

þ


ýÿ
ÿ
þÿÿÿÿ
€ÿ

zSoSReport.final_workc                 C   s   | j s| j tdƒ¡ dS dS )Nzno valid plugins were enabledFT)r|   r�   r“   rÈ   rÉ   r)   r)   r*   Úverify_pluginsÆ  s   zSoSReport.verify_pluginsc                 C   s  | j  d| j¡ | j  d| jr| jjnd¡ | j  d| jj¡ d}tdƒr?t	dt
 ¡ › �ƒ}|d d	kr?|d
  ¡ d	  d¡}| j  d¡ | j j d|¡ | j j dt
 d	¡¡ | j  d¡ | j ¡ D ]\}}| j j ||¡ qc| j  d| jj¡ | j  d| jj¡ | j  d¡ dS )zgAdd 'global' data to the manifest, that is any information that is
        not plugin-specific
        r‚   r[   Úunsetr^   r2  Úionicez
ionice -p r÷   r   rø   rù   ÚpriorityÚio_classÚnicenessrÕ   Úenabled_pluginsÚdisabled_pluginsr‡  N)r‹   rô  r‚   r[   ræ   Úadd_listr�   r^   r   r	   r�   Úgetpidr  r5  rŠ   r–  ÚnicerÕ   r"   rB   rS   )r›   Ú	_io_classÚ_iora  r£  r)   r)   r*   Úadd_manifest_dataÌ  s&   ÿzSoSReport.add_manifest_datac                    s|   d‡ fdd„	}i ‰ | j jD ]!}|jD ]}||ƒ q|jD ]}||dƒ q|jD ]}||ƒ q'q| j  dttˆ  ¡ ƒƒ¡ dS )ac  Add a section to the manifest that contains a dict summarizing the
        tags that were created and assigned during this report's creation.

        This summary dict can be used for easier inspection of tagged items by
        inspection/analyzer projects such as Red Hat Insights. The format of
        this dict is `{tag_name: [file_list]}`.
        Úfilepathc              	      s~   | d D ]8}| | r|sqzˆ | }W n t y   g }Y nw | t| | tƒr,| | n| | g¡ ttt|ƒƒƒˆ |< qd S )Nr=  )r³  r§   r²  r  rt  r   )Úentra  Útagrí   ©Útag_summaryr)   r*   Úcompile_tagsí  s   ÿÿöz=SoSReport.generate_manifest_tag_summary.<locals>.compile_tagsÚfiles_copiedr¥  N)r¡  )	r‹   r‡  ÚcommandsÚfilesÚcollectionsrô  Údictrt  r"   )r›   r¦  r—  r.  Ú_fileÚ
collectionr)   r¤  r*   rg  å  s   




ÿÿz'SoSReport.generate_manifest_tag_summaryc              	   C   s„   d}| j  |tdd | j¡f ¡ | jj ¡ }dt› d| jj› dd |¡› d�}| j  |¡ | j  dt› dd | j ¡ ¡› �¡ d S )	Nz[%s:%s] executing 'sos %s'rñ  r   ú[z:setup] using 'z' preset defaults (rx   z:setup] effective options now: )	r�   rá   Ú__name__rä   rv   r[   r�   rÒ  ræ   )r›   rž   Úpreset_argsr)   r)   r*   Ú_merge_preset_options  s   ÿÿzSoSReport._merge_preset_optionsc              
   C   sì  zš| j  |  ¡ ¡ |  ¡  |  ¡  |  ¡  |  ¡  |  ¡  |  ¡  | j	j
r+|  
¡  t‚| j	jr5|  ¡  t‚| j	jr?|  ¡  t‚| j	jrL|  | j	j¡ t‚| j	jrY|  | j	j¡ t‚|  ¡ s_t‚|  ¡  |  ¡  |  ¡  |  ¡  |  ¡  | j	js{|  ¡  | j	jsƒ|  ¡  | j	jsŒ|  ¡  n| j d¡ |  ¡  |  ¡ W S  t y´   | j	j!r¦‚ t" #dd ¡dks²|  $¡  Y n; t%yË   | j &d¡ |  $¡  |  'd¡ Y n$ tyî } zt" #dd ¡dksÞ|  $¡  t( )|j*¡ W Y d }~nd }~ww |  'd¡ d S )Nz)Skipping postprocessing of collected dataÚSOS_TEST_LOGSÚkeepz
Exiting on user cancelr×  ry   )+rw   Úset_commonsr@  rž  r¥  r±  rµ  rº  r»  r�   rI   Ú
SystemExitrK   rJ   rq   rr   r“  rÙ  rí  r   rñ  r  rV   r*  rU   rC  rW   rÃ  r¿  rá   rü  r’  ræ  r‘   r�   ÚgetenvÚcleanuprÛ  r“   r—   rL  ÚexitÚcoderì  r)   r)   r*   rj    sp   

€€ýzSoSReport.executerA  )NN)r2  )r   r   )Jr¯  Ú
__module__Ú__qualname__Ú__doc__r;   Úroot_requiredr
   Úarg_defaultsr{   Úclassmethodr¹   rÅ   r„   rš   r˜   rÑ   rÐ   r™   rÒ   rÓ   r  r  r  r  rÔ   r@  rC  rG  rŒ   r…   ÚstaticmethodrK  rg  ro  rs  rv  r{  r~  rp  r€  r�  rž  r¥  rµ  rº  r»  r¾  rÀ  rI   rK   rJ   rq   rr   rÙ  rb  rí  rñ  rü  r  r   r  r  r*  rC  rÃ  rP  rT  r’  r“  r   rg  r±  rj  Ú__classcell__r)   r)   rŸ   r*   r2   J   st   ÿþýüûúùø	÷
öõôóòñðïîíìëêéèçæåäãâá à!ß"Þ#Ý$Ü%Û&Ú'Ù(Ø)×*Ö+Õ,Ô-Ó.Ò/Ñ0Ð1Ï2Î3Í4Ã@'
 N
6
2



O;
I
	
16	
7 Q r2   )Fr   )2rL  rW  r�   rè  r”   rG  rZ  r   rï   Úshutilr   Úconcurrent.futuresr   r   Úsos.report.pluginsr†  rq  r   r   r   r	   r
   r   r   r   rÈ   r   Úsos.componentr   Úsos.policiesÚsos.report.reportingr   r   r   r   r   r   r   r   r   r   Úsos.cleanerr   ÚENOSPCÚEROFSré  r+   r.   r’   r2   r)   r)   r)   r*   Ú<module>   s0   $0
	