Hacked By AnonymousFox

Current Path : /opt/cloudlinux/venv/lib64/python3.11/site-packages/lvestats/lib/__pycache__/
Upload File :
Current File : //opt/cloudlinux/venv/lib64/python3.11/site-packages/lvestats/lib/__pycache__/snapshot.cpython-311.pyc

�

)�f�����ddlmZddlZddlZddlZddlZddlZddlZddlm	Z	m
Z
ddlmZddl
Z
ddlmZdZdZee��ZGd�d	e��Zed
���ZdS)�)�absolute_importN)�Optional�List)�contextmanager)�mapz/var/lve/snapshotsz	.snapshotc��eZdZdZdd�Zd�Zed���Zd�Zd�Z	d�Z
d	�Zd
�Zd�Z
d�Zd
�Zd�Zd�Zed���Zedd���Zed���ZdS)�Snapshotad
    This class is 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/snaphosts/[uid] and the dump files itself will be owned by user
    They will not be readable by other users.
    �c�T�||_||_tjd��|_dS)Nzlib-snapshot)�
compresslevel�incident�logging�	getLogger�log)�selfr
rs   ��/builddir/build/BUILDROOT/lve-stats-4.2.8-1.el8.x86_64/opt/cloudlinux/venv/lib/python3.11/site-packages/lvestats/lib/snapshot.py�__init__zSnapshot.__init__!s'��*��� ��
��$�^�4�4�����c��|d}|�J�|�|��tjtj|�����|j��}|�|��5}|�|��|j	�
d|jz��ddd��n#1swxYwY|jS)N�	dump_timezSnapshot dumped to file %s)�_replace_unicode_data�zlib�compress�json�dumps�encoder�create_file�writer�debug�name)r�data�	dump_date�json_compressed�fs     r�savez
Snapshot.save's����%�	��$�$�$�	
�"�"�4�(�(�(��-��
�4�(8�(8�(?�(?�(A�(A�4�CU�V�V��
�
�
�i�
(�
(�	B�A�
�G�G�O�$�$�$��H�N�N�7�!�&�@�A�A�A�	B�	B�	B�	B�	B�	B�	B�	B�	B�	B�	B����	B�	B�	B�	B��v�
s�58B9�9B=�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�listrr,)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_listFs:���|�|�~�~��
�7�=�=����	$��:�d�#�#�#��	rc�T�|�|���||��S)z�
        Return ordered list of timestamps when snapshots for this use were created
        :param from_ts:
        :param to_ts:
        :return: list of timesteamps, ordered for that period
        )�snapshot_filterr?)r�from_ts�to_tss   r�get_ts_listzSnapshot.get_ts_listMs(���#�#�D�$7�$7�$9�$9�7�E�J�J�Jrc
��g}|�||��D�];}	|�|�|����}tj��sHt|jd��5|�|��}ddd��n#1swxYwYn|�|��}|�tj
|������#tttf$r_}|j�d|�|�|�����dt!|���d���Yd}~��5d}~wwxYw|S)z�
        Get all snapshot for a period
        :param from_ts
        :param to_ts
        :return: list of snapshots, ordered by date
        �uidNzCorrupted file: z (�))rD�
get_file_name�
ts_to_namer:�geteuid�drop_privilegesr
�read_file_contentr1r�loads�IOError�
ValueError�OSErrorr�warning�str)rrBrC�result�ts�filename�content�ves        r�
get_snapshotszSnapshot.get_snapshotsWs������"�"�7�E�2�2�
	q�
	q�B�	
q��-�-�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��
�
�d�j��1�1�2�2�2�2���Z��1�
q�
q�
q��� � � �d�>P�>P�QU�Q`�Q`�ac�Qd�Qd�>e�>e�>e�>e�gj�km�gn�gn�gn�gn�!o�p�p�p�p�p�p�p�p�����
q�����
s>�AC�2B�C�B	�C�B	�AC�E�4AE�Ec��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*)rrUr$rV�compressed_contents     rrLzSnapshot.read_file_contentmsB��
�(�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�Z�|�|jd|jd��S)zZ
        Load all snapshots for given incident
        :return: list of snapshots
        �incident_start_time�incident_end_time)rXr
�rs r�get_incident_snapshotszSnapshot.get_incident_snapshotsys*���!�!�$�-�0E�"F��
�Vi�Hj�k�k�krc�|�tj�tt	|jd����S)NrF)r:r;�join�
SNAPSHOT_PATHrRr
rds rr9zSnapshot.get_dir�s'���w�|�|�M�3�t�}�U�/C�+D�+D�E�E�Erc�f�tj�|���|��Sr8)r:r;rgr9)rr s  rrHzSnapshot.get_file_name�s ���w�|�|�D�L�L�N�N�D�1�1�1rc	�F�|���}tj�|��s�	tj|��tj|d��tj||jdd��nG#ttf$r3}|j
�d|t|����Yd}~nd}~wwxYw|�
|�|����}t|jd��5t!|d��}	tj|���d��nG#ttf$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)Nr[�z&Unable to set file permissions %s (%s))r9r:r;�exists�makedirs�chmod�chownr
rNrPrr_rRrHrIrKr\�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���W�%�
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���W�%�
\�
\�
\�����G��TW�XY�TZ�TZ�[�[�[�[�[�[�[�[�����
\����		\�	\�	\�	\�	\�	\�	\�	\�	\�	\�	\����	\�	\�	\�	\��sT�A
B�C�)B?�?C�	F�'E�F�F�)F�=F�F�F�F�Fc�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
        )rCT)�
ignore_errorsN)	r9r:r=rA�shutil�rmtree�removerHrI)rrC�_dir�files�
all_snapshots�ts_to_removerTs       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�
Crc���|�t��rD|dt�}|���r 	t	|��S#t
$rYnwxYwdS�Nr)�endswith�SNAPSHOT_EXT�SNAPSHOT_EXT_LEN�isdigit�intrO)rtrTs  r�get_tszSnapshot.get_ts�sp���>�>�,�'�'�	��q�*�*�*�+�B��z�z�|�|�
���r�7�7�N��!�����D������ts�A�
A�ANc���|�d}|�tj}g}|D]C}t�|��}|�%||cxkr|krn�.|�|���Dt|��Sr�)�sys�maxsizer	r�r1�sorted)r{rBrCrSrUrTs      rrAzSnapshot.snapshot_filter�s����?��G��=��K�E����	"�	"�H�����*�*�B��~�'�R�"8�"8�"8�"8�5�"8�"8�"8�"8�"8��
�
�b�!�!�!���f�~�~�rc�0�t|��tzSr8)rRr�)rTs rrIzSnapshot.ts_to_name�s���2�w�w�|�#�#r)r
)NN)�__name__�
__module__�__qualname__�__doc__rr%�staticmethodr,rr?rDrXrLrer9rHrr~r�rArI�rrr	r	sT��������5�5�5�5��������\��

#�
#�
#����K�K�K����,
 �
 �
 �l�l�l�F�F�F�2�2�2����0C�C�C�"�	�	��\�	������\���$�$��\�$�$�$rr	c#�,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     rrKrK�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)�
__future__rrrr:r�rwr�typingrr�
contextlibrr��builtinsrrhr��lenr��objectr	rKr�rr�<module>r�s��'�&�&�&�&�&���������	�	�	�	�
�
�
�
�
�
�
�
�����!�!�!�!�!�!�!�!�%�%�%�%�%�%�
�
�
�
�������$�
����3�|�$�$��w$�w$�w$�w$�w$�v�w$�w$�w$�t�!�!���!�!�!r

Hacked By AnonymousFox1.0, Coded By AnonymousFox