HEX
Server: Apache
System: Linux 185.122.168.184.host.secureserver.net 5.14.0-570.60.1.el9_6.x86_64 #1 SMP PREEMPT_DYNAMIC Wed Nov 5 05:00:59 EST 2025 x86_64
User: barbeatleanalyti (1024)
PHP: 8.1.33
Disabled: NONE
Upload Files
File: //proc/self/root/lib/fm-agent/library/__pycache__/log_matcher.cpython-39.pyc
a

��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}z|��Wn6tyJddl}|��\}}}t�|�gYS0�fdd�}tt||��}|�t	�
�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
        rNcst��|�}|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_linesrrr	rs	

zLogMatcher.matchcs�|sg}z|��Wn6tyJddl}|��\}}}t�|�gYS0dd�|D�}��fdd�}	tt|	|��}
|�t	�
�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|]}|���qSr)�split)�.0rrrr	�
<listcomp>C�z.LogMatcher.match_in_column.<locals>.<listcomp>cs |�}t��|�}|r|pdSrr)rZ
data_pointr��columnrrr	rEsz.LogMatcher.match_in_column.<locals>.find_matchr)rr rr*r!rr"r#Zsplitted_linesrr$rr)r	�match_in_column/s


zLogMatcher.match_in_columncCs|jdurtd��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}|D]8\}}t��|}|t|d�kr||7}|�||f�q|jsTdgfS||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	Csng}d}d}t|d�}|dur6t|�D]\}}q&|}n(t|�D]\}}||kr>|�|�q>|}|��||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_filerrrr	�get_file_linesms
zLogMatcher.get_file_linescCst�|�jS)z1
        Grab the file created timstamp.
        )�os�stat�st_ino)r6rrr	�get_file_inode�szLogMatcher.get_file_inode)N)N)�__name__�
__module__�__qualname__�__doc__r
rr+rr1�staticmethodr:r>rrrr	rs

	
r)r
rrrr;�objectrrrrr	�<module>s