File manager - Edit - /opt/cloudlinux/venv/lib/python3.11/site-packages/lvestats/snapshots/__pycache__/reader.cpython-311.pyc
Back
� X�9i�K � � � d dl Z d dlZd dlZd dlZd dlZd dlmZmZ d dl m Z d dlmZ d dl mZ d dlmZ d dlmZ d dlmZ d d lmZ d ZdZdZd � Zej j j j dd�� � Zd� Z G d� d� � Z!dS )� N)�dbengine�uidconverter)�dateutil)�prepare_data_json)�lve_read_snapshot_parser)�Snapshot)�incident)�or_)�sessionmakerz?Snapshots collected starting from %s to %s for lve id %d @ %s: zDone.. � localhostc �J � | j r| j \ }}n�| j r | j }|dz }nz t j d� | j � � � � }t j d� | j � � � � }n # t $ r t d� � Y dS w xY w||fS )Ng�!��?� z:please use [YY]YY-MM-DD[ HH:MM] format for --from and --to)NN) �period� timestampr � parse_date�join�ffrom�to� ValueError�print)�opts�start�ends ��/builddir/build/BUILDROOT/lve-stats-4.2.14-1.el8.x86_64/opt/cloudlinux/venv/lib/python3.11/site-packages/lvestats/snapshots/reader.py�_calculate_periodr s� � ��{� ��[� ��s�s� �� �����h���� ��'������(<�(<�=�=�E��%�c�h�h�t�w�&7�&7�8�8�C�C��� � � ��N�O�O�O��:�:� ���� �#�:�s �AB �B�Bc �z � t � � }|� |� � }|j s,|j s%|� � � t d� � dS |j �|j r|j st d� � dS t j | � � }n,# t j $ r}t |� � Y d }~dS d }~ww xY w| � dt � � }|j r<t j |j |||� � }|�t d|j � d|� d�� � dS n|j }t! |� � \ }} |�| �dS t# ||| |||j |j |j |j � � } |j r| � � � d S |j r| � |j � � d S | � � � d S )Nz/One of -u --user or -i --id should be specified� zD--snap-sql-item can only be used with --timestamp and --json options� server_idzUser �@z not found)r � parse_args�id�user� print_helpr � snap_sql_itemr �jsonr �make_db_engine�MakeDbException�get�DEFAULT_SERVER_IDr �username_to_uidr �LVEReadSnaphot�output�compact�list�stats�unit�run)�config�argv_�parserr �engine�er �uidr r �lve_read_snapshots r �snapshot_reader_mainr9 - s� � � %� '� '�F����U�#�#�D��7� �4�9� ������� �?�@�@�@��q���%�t�~�%�T�Y�%� �T�U�U�U��q���(��0�0�����#� � � � �a�����q�q�q�q�q��������� � � �;�(9�:�:�I��y� ��*�4�9�i��F�S�S���;��;�$�)�;�;�i�;�;�;�<�<�<��1� � �g��"�4�(�(�J�E�3��}����q�&�� ������� ����� � �� �y� ���� � � � � � �� ����� �*�*�*�*�*��������s �>B �B<�"B7�7B<c � � t | t j � � r&t j t j | � � � � S | S )z� Convert local datetime to unix timestamp, or just passes unix timestamp as output if specified. :param o: :return: )� isinstance�datetimer �gm_datetime_to_unixtimestamp�local_to_gm)�os r �_try_convert_to_timestampr@ ` s; � � �!�X�&�'�'� N��4�X�5I�!�5L�5L�M�M�M��H� c � � e Zd Z dd�Zdd�Zd� Zd� Zd� Zed e j fd �� � Zed� � � Zd� Z d � Zd� Zd� Zd� Zd� Zd� Zd� Zed� � � Zd� ZdS )r+ FNc �� � || _ || _ || _ || _ t |� � | _ t |� � | _ || _ || _ | | _ dddddddd�| _ d S ) a� :param start: datetime.datetime | int (unix timestamp) :param end: datetime.datetime | int (unix timestamp) :param uid: :param server_id: :param output_file: filename :param do_json: boolean :param compact_mode: boolean - truncate SQL queries for memory efficiency :param snap_sql_item: int - index of specific SQL query to return (only with --timestamp) �CPUzVirtual memory�EPzPhysical memory�NPROC�IO�IOPS)� cpu_fault� mem_fault� mep_fault�memphy_fault�nproc_fault�io_fault� iops_faultN)r5 �do_json�output_filer7 r@ r r r �compact_moder$ �fault_names) �selfr5 r r r7 r rQ rP rR r$ s r �__init__zLVEReadSnaphot.__init__n s� � �. ������&������.�u�5�5�� �,�S�1�1���"���(���*��� �)��-�"�� � � ����rA �� c � � |s|S g }|D ]{}t |� � dk rQ|d |d |d }}}t |� � |k r |d|� dz }|� |||g� � �f|� |� � �||S )a Truncate SQL queries for compact mode to reduce memory usage. :param snap_sql: List of SQL query arrays [cmd, time, sql_query] :param max_length: Maximum length for SQL query strings :return: List of truncated SQL query arrays � r r � Nz...)�len�append)rT �snap_sql� max_length� truncated�query_entry�cmd�time�sqls r �truncate_sql_queriesz#LVEReadSnaphot.truncate_sql_queries� s� � � � ��O�� �#� .� .�K��;���1�$�$�!,�Q���Q���Q��3�T���s�8�8�j�(�(��k�z�k�*�U�2�C�� � �#�t�S�!1�2�2�2�2� � � ��-�-�-�-��rA c �� � t |� � }|s+|� d� � |� � � dS t |� � dk r+|� d� � |� � � dS |d }|� dg � � }| j dk s| j t |� � k rLd| j � dt |� � dz � �}|� d |� d �� � |� � � dS || j }d|d�}|� t j |d� � � � � |� d� � |� � � dS )z� Handle --snap-sql-item request: return specific SQL query by index. :param snapshots_generator: Generator of snapshot data :param out: Output stream :return: None (writes JSON directly to output) zI{"success": 0, "error": "No snapshot found for the specified timestamp"} Nr z\{"success": 0, "error": "--snap-sql-item only works with --timestamp for single snapshots"} r r\ zSQL item index z$ out of range. Available indices: 0-z{"success": 0, "error": "z"} )�success�datarY )�indent� )r. �write�flushrZ r( r$ r% �dumps) rT �snapshots_generator�out�snapshots_list� snapshot_datar\ � error_msg�sql_item�results r �_handle_sql_item_requestz'LVEReadSnaphot._handle_sql_item_request� sj � � �1�2�2��� ��I�I�b�c�c�c��I�I�K�K�K��F��~����"�"��I�I�u�v�v�v��I�I�K�K�K��F�&�q�)� � �$�$�Z��4�4�����!�!�T�%7�3�x�=�=�%H�%H�u�$�*<�u�u�be�fn�bo�bo�rs�bs�u�u�I��I�I�C�9�C�C�C�D�D�D��I�I�K�K�K��F��D�.�/����1�1��� � �$�*�V�A�.�.�.�/�/�/�� � �$����� � �����rA c �� � |� t � � � t j | j k t j | j k t t j � | j | j � � t j � | j | j � � � � � � � t j � � � � � S �N) �queryr �filterr7 r r �incident_start_time�betweenr r �incident_end_time�order_by�all)rT �sessions r � get_incidentszLVEReadSnaphot.get_incidents� s� � ��M�M�(�#�#� �V�����(��"�d�n�4���0�8�8���T�X�N�N��.�6�6�t�z�4�8�L�L�� �� � �X�h�2� 3� 3� �S�U�U� rA c �� � g }| � |� � D ]T}|� |j t |j |j pd� � d|j | � |� � d�� � �U|S )Nr r ��fromr � incidents� snapshots�duration)r~ r[ rx �max� dump_timerz �snapshot_count�get_duration)rT r} rr �is r �stats_by_incidentz LVEReadSnaphot.stats_by_incident� s� � ����#�#�G�,�,� � �A��M�M��1��a�k�1�+>�+C�!�D�D�!"�!"�!1� $� 1� 1�!� 4� 4�� � � � � � � rA r c � � t | j |� � }t t | j | j pd� � |� � }||z S )Nr )r� rx �minr� rz )r� �from_�to_s r r� zLVEReadSnaphot.get_duration� sA � ��A�)�5�1�1���#�a�k�1�#6�#;�!�<�<�c�B�B���U�{�rA c � � d}d}|t | � � k ra| | }|j |k r|dz }�,|j |k rn<|dz }|dz }|t � |||� � z }|t | � � k �a|dk rdd|fS |||dz fS )Nr r )rZ r� rx r+ r� )r� �pos�from_ts�to_ts�countr� r� s r �get_incident_countz!LVEReadSnaphot.get_incident_count� s� � ������C� �N�N�"�"��#��A��{�W�$�$��q�����$�u�,�,���Q�J�E��1�H�C���3�3�A�w��F�F�F�H� �C� �N�N�"�"� �A�:�:��a��9���(�C�!�G�+�+rA c � � | � |� � }t d| j i� � � � � }g }| j }d}|| j k r�t ||z | j � � }| � ||||� � \ } } }| dk r|}�Ht j |||� � }t |� � dk r|� d� � |� ||| t |� � | d�� � |}|| j k ��|S )Nr7 r r� )r~ r r7 � get_file_listr r r� r� �snapshot_filterrZ r[ )rT r} � time_unitr� �snapshot_filesrr r� r� r� �incident_countr� r� s r �stats_by_time_unitz!LVEReadSnaphot.stats_by_time_unit s) � ��&�&�w�/�/� �!�5�$�(�"3�4�4�B�B�D�D�����*������� � ���)�+�T�X�6�6�E�,0�,C�,C�I�s�T[�]b�,c�,c�)�N�H�c���"�"���� �0���%�P�P�I��9�~�~��"�"�� � ��#�#�#��M�M�#��!/�!$�Y��� (�� � � � � �G�% ��� � �&