
­­­­­­­­­­­­­­­­­­
<!DOCTYPE html>
<html>
3
hj*                 @   sB   d dl Z d dlZd dlmZ d dlmZ ej ZG dd dZdS )    N)SubparsersAction)Optionc               @   s   e Zd ZdZdZg ZeddededdegZedd	e	ed
deeddeeddeg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!d" ZdS )#RequestAnalyzerz
    A request analyzer module, handles request tracking logic
    and analysis. Parses input generated from a source Reader.
    Nz	--verbosezVerbose outputz-vz--pamzFilter only PAM requestscidzTrack request with this IDz--childzInclude child process logsz--mergez'Merge logs together sorted by timestampzTrack only PAM requestsc             C   s   | j j  dS )z|
        Print the module parser help output

        Args:
            args (Namespace): argparse parsed arguments
        N)module_parserZ
print_help)selfargs r	   /usr/lib/python3.6/request.pyprint_module_help   s    z!RequestAnalyzer.print_module_helpc             C   sv   d}|j d|dd| _| jjddtdd}|jd	}|j|d
d| j| j |j|dd| j| j	 | jj
| jd | jS )z
        Setup module parser, subcommands, and options

        Args:
            parser_grp (argparse.Action): Parser group to nest
               module and subcommands under
        zAnalyze request tracking moduleZrequestzRequest tracking)descriptionhelpN	subparserZCOMMANDS)titledestactionmetavarzOperation ModeslistzList recent requestsZshowzTrack individual request ID)func)Z
add_parserr   Zadd_subparsersr   Zadd_parser_groupZadd_subcommandlist_requests	list_optstrack_request	show_optsZset_defaultsr   )r   Z
parser_grpZcliZdescr   Z
subcmd_grpr	   r	   r
   
setup_args&   s    



zRequestAnalyzer.setup_argsc             C   s8   |j dkrddlm} | }nddlm} ||j}|S )z
        Load the appropriate source reader.

        Args:
            args (Namespace): argparse parsed arguments

        Returns:
            Instantiated source object
        Zjournaldr   )Journald)Files)sourceZsssd.source_journaldr   Zsssd.source_filesr   Zlogdir)r   r   r   r   r   r	   r	   r
   loadB   s    


zRequestAnalyzer.loadc             c   sF   x@|D ]8}x2|D ]*}t j|}|j|r|jdr4q|V  qW qW dS )z
        Yield lines which match any number of patterns (OR) in
        provided patterns list.

        Args:
            source (Reader): source Reader object
        Yields:
            lines matching the provided pattern(s)
        z   *  N)recompilesearch
startswith)r   r   patternslinepatternZre_objr	   r	   r
   matched_lineT   s    





zRequestAnalyzer.matched_linec             C   sJ   g }x@| j ||D ]0}tj|}|j|}|r|jd}|j| qW |S )a  
        Retrieve list of associated REQ_TRACE ids. Filter
        only source lines by pattern, then parse out the
        linked id with the provided regex.

        Args:
            source (Reader): source Reader object
            pattern (list of str): regex pattern(s) used for finding
                 linked ids
            regex (str): regular expression used to extract linked id

        Returns:
            List of linked ids discovered
        r   )r%   r   r   r    groupappend)r   r   r$   ZregexZ
linked_idsmatchZid_refoundr	   r	   r
   get_linked_idsf   s    


zRequestAnalyzer.get_linked_idsc             C   sH   d}|r | j j|j|d  n$t|jdkr<t|dd nt| |S )a  
        Print or consume a line, if merge cli option is provided then consume
        boolean is set to True

        Args:
            line (str): line to process
            source (Reader): source Reader object
            consume (bool): If True, line is added to consume_logs
               list, otherwise print line

        Returns:
            True if line was processed, otherwise False
        T   r    )end)consumed_logsr'   rstriptype__name__print)r   r#   r   ZconsumeZfound_resultsr	   r	   r
   consume_line~   s    zRequestAnalyzer.consume_linec             C   s$  i }x||D ]t}d|krq
|j d}|d dd }tjd|d }|jd}||f|j krr|||f  |7  < q
||||f< q
W x|j D ]\}}	g }
g }xx|	j D ]j}d}d}d}|j d}|d d	d }|j d
d dd }tjd|r| j| tjd|r\|j dd }|j  |j d}|d d	d }|j d
d dd }n8tjd|rz|j	dd }ntjd|r|j	 d }|rt
d|  |r||
krt
d|dd   |
j| |rd|ksd|kr||krttjd|rt
d|  |j| qW qW dS )z
        Parse log file and print formatted verbose list_requests output

        Args:
            source (Reader): source Reader object
        zCID#[r   N   z
CID#[0-9]*   r,      :   z\[cmdZcache_req_send'r+   Zcache_req_process_inputZcache_req_search_sendz   - z	       - ZUIDZGIDz\dr.   r.   r.   )splitr   r    r&   keysitems
splitlinesprint_formattedclearrsplitr3   r'   bool)r   r   datar#   fieldstsresultr   kvZcr_doneZid_doneZcidlineZpluginnameidZcr_fieldZcrr	   r	   r
   print_formatted_verbose   sV    







z'RequestAnalyzer.print_formatted_verbosec             C   s   |j drdS d|krdS |jdd }|dd }|jd}|d d	d }|d	 d	d }|d d	d }|j s|d d	d }t| d| d| d|  |S )z
        Parse line and print formatted list_requests output

        Args:
            line (str): line to parse
        Returns:
            Client ID from printed line, 0 otherwise
        z   *  r   Z	refreshed)r+   Nr5   r7   r:   	         z: [uid z] CID #z: ir.   r.   r.   )r!   r<   	isnumericr3   )r   r#   rF   rE   r   cmdZuidr	   r	   r
   r@      s    


zRequestAnalyzer.print_formattedc             C   s   | j |}|jj}d}dg}|jr.|jj}d}tjd| d |j|d |jr^| j	| n8x6| j
||D ]&}t|jdkrt| ql| j| qlW dS )	z
        List component (default: NSS) responder requests

        Args:
            args (Namespace):  populated argparse namespace
        nssz\[cmdpamz******** Listing z client requests ********Fr   N)r   	ComponentNSSrT   PAMloggerinfoset_componentverboserL   r%   r1   r2   r3   r@   )r   r   r   	componentrespr"   r#   r	   r	   r
   r      s    

zRequestAnalyzer.list_requestsc                st  | j |}|j}d}d}|jj}d}d| dg |jrD|jj}d}tjd| d| d |j||j	 x$| j
| D ]}| j|||j}qxW tjd	| d
 d| d| dg |j|jj|j	 d}	| j| |	}
 j   fdd|
D  x&| j
| D ]}| j|||j}qW |jrNt| jdd d}x|D ]}t| q<W | rp| rptjd| d dS )z
        Print Logs pertaining to individual SSSD client request

        Args:
            args (Namespace):  populated argparse namespace
        FrS   z\[CID#z\]rT   z******** Checking z responder for Client ID z *******z)********* Checking Backend for Client ID z	 ********zREQ_TRACE.*\[sssd.z CID #z\[RID#[0-9]+\]c                s   g | ]} j d | qS )\)r'   ).0rK   )r$   r	   r
   
<listcomp>4  s    z1RequestAnalyzer.track_request.<locals>.<listcomp>c             S   s   | j dd S )NrM   r   )r<   )sr	   r	   r
   <lambda><  s    z/RequestAnalyzer.track_request.<locals>.<lambda>)keyzID z not found in logs!N)r   r   rU   rV   rT   rW   rX   rY   rZ   Zchildr%   r4   mergeZBEr*   rA   sortedr/   r3   warn)r   r   r   r   Zresp_resultsZ
be_resultsr\   r]   r(   Zbe_id_regexZbe_idsZsorted_listentryr	   )r$   r
   r     s<    

zRequestAnalyzer.track_request)r2   
__module____qualname____doc__r   r/   r   rC   r   intr   r   r   r   r%   r*   r4   rL   r@   r   r   r	   r	   r	   r
   r   
   s&   


	Fr   )r   ZloggingZsssd.parserr   r   Z	getLoggerrX   r   r	   r	   r	   r
   <module>   s
   