Viewing File: /opt/cloudlinux/venv/lib/python3.11/site-packages/lvestats/lib/__pycache__/snapshot.cpython-311.pyc

�

5J�i����ddlZddlZddlZddlZddlZddlZddlZddlmZddl	m
Z
ddlmZdZ
dZee��ZGd�d��Ze
d	efd
���ZdS)�N)�Iterator)�contextmanager)�BinaryIOz/var/lve/snapshotsz	.snapshotc��eZdZdZd dededdfd�Zdedefd	�Ze	d
���Z
deddfd�Zdeefd�Z
d
edzdedzdeefd�Zd
edzdedzdeefd�Zdedefd�Zdefd�Zdefd�Zdedefd�Zdedefd�Zdeddfd�Ze	dededzfd���Ze			d!deed
edzdedzdeefd���Ze	dedefd���ZdS)"�Snapshotaj
    Class responsible for loading and saving snapshot files for the interval.

    The files will be saved in a format of: `/var/lve/snapshots/[uid]/dump_time.snapshot`

    dump_time is the timestamp/integer.

    The directories `/var/lve/snapshots/[uid]` and the dump files themselves will be owned by user.
    They will not be readable by other users.
    ��incident�
compresslevel�returnNc�T�||_||_tjd��|_dS)Nzlib-snapshot)r
r	�logging�	getLogger�log)�selfr	r
s   ��/builddir/build/BUILDROOT/lve-stats-4.2.14-2.el8.x86_64/opt/cloudlinux/venv/lib/python3.11/site-packages/lvestats/lib/snapshot.py�__init__zSnapshot.__init__#s'��*��� ��
��$�^�4�4������datac��|d}|�J�|�|��tjtj|�����|j��}|�|��5}|�|��|j	�
d|j��ddd��n#1swxYwY|jS)N�	dump_timezSnapshot dumped to file %s)�_replace_unicode_data�zlib�compress�json�dumps�encoder
�create_file�writer�debug�name)rr�	dump_date�json_compressed�fs     r�savez
Snapshot.save(s����%�	��$�$�$�	
�"�"�4�(�(�(��-��
�4�(8�(8�(?�(?�(A�(A�4�CU�V�V��
�
�
�i�
(�
(�	A�A�
�G�G�O�$�$�$��H�N�N�7���@�@�@�	A�	A�	A�	A�	A�	A�	A�	A�	A�	A�	A����	A�	A�	A�	A��v�
s�56B7�7B;�>B;c�\�t|t��r|�dd��S|S)Nzutf-8�replace)�
isinstance�bytes�decode)�objs r�_to_unicodezSnapshot._to_unicode4s-���c�5�!�!�	2��:�:�g�y�1�1�1��
rc	�Z�g}|�dg��D]7}|�tt|j|�������8||d<g}|�dg��D]7}|�tt|j|�������8||d<dS)N�snap_sql�	snap_http)�get�append�list�mapr+)rr�	u_queries�query�u_urls�https      rrzSnapshot._replace_unicode_data:s����	��X�X�j�"�-�-�	A�	A�E����T�#�d�&6��">�">�?�?�@�@�@�@�$��Z�����H�H�[�"�-�-�	=�	=�D��M�M�$�s�4�#3�T�:�:�;�;�<�<�<�<�"��[���rc��|���}tj�|��rtj|��SgS�N)�get_dir�os�path�isdir�listdir)r�dir_s  r�
get_file_listzSnapshot.get_file_listEs:���|�|�~�~��
�7�=�=����	$��:�d�#�#�#��	r�from_ts�to_tsc�T�|�|���||��S)a�
        Return ordered list of timestamps when snapshots for this user were created.

        :param from_ts: Start timestamp for filtering snapshots (inclusive).
        If None, starts from the earliest snapshot.
        :param to_ts: End timestamp for filtering snapshots (inclusive).
        If None, includes all snapshots up to the latest.
        :return: List of timestamps ordered for the given period.
        )�snapshot_filterr?)rr@rAs   r�get_ts_listzSnapshot.get_ts_listKs(���#�#�D�$6�$6�$8�$8�'�5�I�I�Irc
#�|K�|�||��D�]!}	|�|�|����}tj��sHt|jd��5|�|��}ddd��n#1swxYwYn|�|��}tj	|��}|V���#ttf$rZ}|j�
d|�|�|����t|����Yd}~��d}~wwxYwdS)a�
        Get snapshots for a given period.

        Yields one snapshot at a time for memory efficiency.

        :param from_ts: Start timestamp for filtering snapshots (inclusive).
        If None, starts from the earliest snapshot.
        :param to_ts: End timestamp for filtering snapshots (inclusive).
        If None, includes all snapshots up to the latest.
        :return: Iterator of snapshot dictionaries.
        �uidNzCorrupted file: %s (%s))rD�
get_file_name�
ts_to_namer:�geteuid�drop_privilegesr	�read_file_contentr�loads�
ValueError�OSErrorr�warning�str)rr@rA�ts�filename�content�
snapshot_data�ves        r�
get_snapshotszSnapshot.get_snapshots[s����� �"�"�7�E�2�2�	�	�B�
��-�-�d�o�o�b�.A�.A�B�B���z�|�|�?�(���u�)=�>�>�C�C�"&�"8�"8��"B�"B��C�C�C�C�C�C�C�C�C�C�C����C�C�C�C��#�4�4�X�>�>�G� $�
�7� 3� 3�
�#�#�#�#�#����(�
�
�
��� � �-��&�&�t���r�':�':�;�;���G�G�������������
����	�	s=�AC�2B�C�B	�C�B	�1C�D9�AD4�4D9rRc��t|d��5}|���}ddd��n#1swxYwY	tj|��}nl#tj$rZtj||j��}t|d��5}|�|��ddd��n#1swxYwYYnwxYw|���S)N�rb�wb)	�open�readr�
decompress�errorrr
rr))rrRr#rS�compressed_contents     rrKzSnapshot.read_file_content~sB��
�(�D�
!�
!�	�Q��f�f�h�h�G�	�	�	�	�	�	�	�	�	�	�	����	�	�	�	�	,��o�g�.�.�G�G���z�	,�	,�	,�!%��w��8J�!K�!K���h��%�%�
,�����*�+�+�+�
,�
,�
,�
,�
,�
,�
,�
,�
,�
,�
,����
,�
,�
,�
,���	,�����~�~���sA�2�6�6�A�9B<�B.�"B<�.B2	�2B<�5B2	�6B<�;B<c�t�t|�|jd|jd����S)zZ
        Load all snapshots for given incident
        :return: list of snapshots
        �incident_start_time�incident_end_time)r1rVr	�rs r�get_incident_snapshotszSnapshot.get_incident_snapshots�s2��
�D�&�&�t�}�5J�'K�T�]�[n�Mo�p�p�q�q�qrc�|�tj�tt	|jd����S)NrF)r:r;�join�
SNAPSHOT_PATHrPr	rbs rr9zSnapshot.get_dir�s'���w�|�|�M�3�t�}�U�/C�+D�+D�E�E�Err c�f�tj�|���|��Sr8)r:r;rer9)rr s  rrGzSnapshot.get_file_name�s ���w�|�|�D�L�L�N�N�D�1�1�1rr!c	�*�|���}tj�|��s�	tj|��tj|d��tj||jdd��n@#t$r3}|j	�
d|t|����Yd}~nd}~wwxYw|�|�
|����}t|jd��5t|d��}	tj|���d��n@#t$r3}|j	�
d|t|����Yd}~nd}~wwxYwddd��n#1swxYwY|S)	z�
        create file, change its ownership & permissions if needed. Create directories if needed as well
        :param dump_date: int timestamp used as file name
        :return: open File object
        i�rFrzUnable to create dir %s (%s)NrY�z&Unable to set file permissions %s (%s))r9r:r;�exists�makedirs�chmod�chownr	rNrr]rPrGrHrJrZ�fchmod�fileno)rr!r>�e�	file_name�file_s      rrzSnapshot.create_file�s����|�|�~�~���w�~�~�d�#�#�	M�
M���D�!�!�!����u�%�%�%����t�}�U�3�Q�7�7�7�7���
M�
M�
M�����=�t�S��V�V�L�L�L�L�L�L�L�L�����
M�����&�&�t���y�'A�'A�B�B�	�
�T�]�5�1�
2�
2�	\�	\���D�)�)�E�
\��	�%�,�,�.�.�%�0�0�0�0���
\�
\�
\�����G��TW�XY�TZ�TZ�[�[�[�[�[�[�[�[�����
\����		\�	\�	\�	\�	\�	\�	\�	\�	\�	\�	\����	\�	\�	\�	\��sT�A
B�
B=�
)B8�8B=�F�'D<�;F�<
E9�)E4�/F�4E9�9F�F�Fc�h�|���}tj|��}|�|��}|�||���}||krt	j|d���dS|D]<}tj|�|�|�������=dS)z�
        Delete old snapshots. If there are no more
        :param to_ts: up to which timestamp to remove snapshots
        :return: None
        )rAT)�
ignore_errorsN)	r9r:r=rC�shutil�rmtree�removerGrH)rrA�_dir�files�
all_snapshots�ts_to_removerQs       r�
delete_oldzSnapshot.delete_old�s����|�|�~�~���
�4� � ���,�,�U�3�3�
��+�+�E��+�?�?���L�(�(��M�$�d�3�3�3�3�3�3�"�
C�
C���	�$�,�,�T�_�_�R�-@�-@�A�A�B�B�B�B�
C�
Crrrc���|�t��rD|dt�}|���r 	t	|��S#t
$rYnwxYwdS�Nr)�endswith�SNAPSHOT_EXT�SNAPSHOT_EXT_LEN�isdigit�intrM)rrrQs  r�get_tszSnapshot.get_ts�sn���>�>�,�'�'�	��q�*�*�*�+�B��z�z�|�|�
���r�7�7�N��!�����D������ts�A�
A�Aryc���|�d}|�tj}g}|D]C}t�|��}|�%||cxkr|krn�.|�|���Dt|��Sr~)�sys�maxsizerr�r0�sorted)ryr@rA�resultrRrQs      rrCzSnapshot.snapshot_filter�s����?��G��=��K�E����	"�	"�H�����*�*�B��~�'�R�"8�"8�"8�"8�5�"8�"8�"8�"8�"8��
�
�b�!�!�!���f�~�~�rrQc�0�t|��tzSr8)rPr�)rQs rrHzSnapshot.ts_to_name�s���2�w�w��%�%r)r)NN)�__name__�
__module__�__qualname__�__doc__�dictr�rrPr$�staticmethodr+rr1r?rDrrVrKrcr9rGrrr|r�rCrH�rrrrs�������	�	�5�5��5�c�5�$�5�5�5�5�

��
�#�
�
�
�
�����\��
	#�$�	#�4�	#�	#�	#�	#��t�C�y�����J��t��J��T�z�J�
�c��	J�J�J�J� !��t��!��T�z�!�
�$��	!�!�!�!�F	 �#�	 �#�	 �	 �	 �	 �r��r�r�r�r�F��F�F�F�F�2�#�2�#�2�2�2�2��S��X�����.C��C��C�C�C�C� ��c��c�D�j�����\���#� ����C�y���t����T�z��
�c��	����\�� �&�s�&�s�&�&�&��\�&�&�&rrrFc#�,K�tj��tj��tj��}}}t	jd��d}tjg��tj|��tj|��	dV�tj|��tj|��tj|��dS#tj|��tj|��tj|��wxYw)N�nobody�)	r:�getuid�getgid�	getgroups�pwd�getpwnam�	setgroups�setegid�seteuid)rF�old_uid�old_gid�
old_groups�gids     rrJrJ�s�����#%�9�;�;��	���R�\�^�^�j�W�G�

�,�x�
 �
 ��
#�C��L������J�s�O�O�O��J�s�O�O�O�!�
����
�
�7����
�
�7����
��Z� � � � � ��	�
�7����
�
�7����
��Z� � � � ���s�C�>D)rr
r:r�rur�r�collections.abcr�
contextlibr�typingrrfr��lenr�rr�rJr�rr�<module>r�s�����������	�	�	�	�
�
�
�
�
�
�
�
�
�
�
�
�����$�$�$�$�$�$�%�%�%�%�%�%�������$�
����3�|�$�$��D&�D&�D&�D&�D&�D&�D&�D&�N�!��!�!�!���!�!�!r
Back to Directory File Manager