Your IP : 216.73.216.86


Current Path : /lib/fm-agent/library/__pycache__/
Upload File :
Current File : //lib/fm-agent/library/__pycache__/log_matcher.cpython-36.pyc

3

q��i�@s<ddlZddlmZmZddlZddlZGdd�de�ZdS)�N)�datetime�	timedeltac@sTeZdZdZdd�Zddd�Zddd�Zd	d
�Zdd�Ze	d
d��Z
e	dd��ZdS)�
LogMatcherz5
    Handles the matching of filter in the logs.
    cCs
||_dS)zV
        @param file_creation: last known creation timestamp of the log file.
        N)�_inode)�self�inode�r�(/usr/lib/fm-agent/library/log_matcher.py�__init__szLogMatcher.__init__Nc	s�|sg}y|j�Wn4tk
rHddl}|j�\}}}tj|�gSX�fdd�}tt||��}|jt	j
�t|�f�|SdS)z�
        Match the ampount of times expression is present in lines,
        return the results with the new entry appended to it.

        @param expression: String,
        @param lines: Iterable
        @param results: List of past results
        rNcstj�|�}|r|pdS)NF)�re�search)�line�match)�
expressionrr	�
find_match'sz$LogMatcher.match.<locals>.find_match)�_valid_inode�
ValueError�sys�exc_info�logging�info�list�filter�appendr�now�len)	r�linesr�resultsr�_�errorr�
matched_linesr)rr	rs	
zLogMatcher.matchcs�|sg}y|j�Wn4tk
rHddl}|j�\}}}tj|�gSXdd�|D�}��fdd�}	tt|	|��}
|jt	j
�t|
�f�|S)a�
        Return the number of lines that the where the column is
        equal to the expression by splitting the lines.

        @param lines: Array of lines to split and search.
        @param expression: Regular expression to match agains the specified column
        @param column: Column number to separate from the regular line.
        @param results: List of past results
        rNcSsg|]}|j��qSr)�split)�.0r
rrr	�
<listcomp>Csz.LogMatcher.match_in_column.<locals>.<listcomp>cs |�}tj�|�}|r|pdS)NF)rr)r
Z
data_pointr)�columnrrr	rEsz.LogMatcher.match_in_column.<locals>.find_match)rrrrrrrrrrrr)rrrr$rrrrZsplitted_linesrr r)r$rr	�match_in_column/s

zLogMatcher.match_in_columncCs|jdkrtd��dS)z�
        Validate that we have an inode. If we dont
        that means we are running the check for the first time,
        and don't have enought information to calculate the matchs.
        NzInode is None. Returning None)rr)rrrr	rNs
zLogMatcher._valid_inodecCs`d}g}x@|D]8\}}tj�|}|t|d�kr||7}|j||f�qW|jsXdgfS||fS)a�
        Check the results and the timescale to determine if a metric
        should be given. The delta between now and the last results time
        must be greater than the timescale to properly calculate the metric.

        @param results: List of past results.
        @param timescale: Integer of the buffer size to take into
        consideration.
        @param last_results_time: Datetime
        r)�minutesN)rrrrr)rr�	timescaleZ	total_sumZ
valid_results�	timestamp�result�deltarrr	�calculate_metricWszLogMatcher.calculate_metricc	Csvg}d}d}t|d�}|dkr:xt|�D]\}}q(W|}n,x&t|�D]\}}||krD|j|�qDW|}|j�||fS)z`
        Grab the lines from the last known line number to the
        end of the file.
        r�rN)�open�	enumerater�close)	Zlast_known_line_number�sourceZ
current_inode�stored_inodeZexpected_lines�index�total_linesZopened_filer
rrr	�get_file_linesms
zLogMatcher.get_file_linescCstj|�jS)z1
        Grab the file created timstamp.
        )�os�stat�st_ino)r0rrr	�get_file_inode�szLogMatcher.get_file_inode)N)N)�__name__�
__module__�__qualname__�__doc__r
rr%rr+�staticmethodr4r8rrrr	rs

	r)rrrrr5�objectrrrrr	�<module>s