ok

Mini Shell

Direktori : /opt/cloudlinux/venv/lib/python3.11/site-packages/xray/__pycache__/
Upload File :
Current File : //opt/cloudlinux/venv/lib/python3.11/site-packages/xray/__pycache__/agent.cpython-311.pyc

�

��me}D��>�dZddlZddlZddlZddlZddlZddlZddlZddlm	Z	m
Z
mZddlm
Z
mZmZmZddlmZddlmZddlmZmZdd	lmZmZdd
lmZddlmZmZm Z ddl!m"Z"dd
l#m$Z$ddl%m&Z&m'Z'm(Z(m)Z)m*Z*eGd�d����Z+Gd�d��Z,dS)zJ
This module contains contains classes implementing X-Ray Agent behaviour
�N)�Thread�current_thread�Lock)�Any�Optional�Dict�Tuple)�	dataclass�)�
get_client)�
agent_sock�local_tasks_storage)�	XRayError�XRayAPIError)�
FaultDetector)�open_local_storage�flush_memory_storage�get_task_ids)�Task)�
extract_creds)�read_sys_id�
create_socket�write_sys_id�dbm_storage�get_current_cpu_throttling_timec�$�eZdZUded<eed<dS)�APIDataContainer�	APIClient�client�taskN)�__name__�
__module__�__qualname__�__annotations__r���A/opt/cloudlinux/venv/lib64/python3.11/site-packages/xray/agent.pyrr*s%�����������
�J�J�J�J�Jr&rc	�j�eZdZdZdZdZdZdZd/d�Zd�Z	d	�Z
d0d�Zd�Zd
�Z
d1d�Zd�Zd2d�Zdddefd�Zdedd
fd�Zdeddfd�Zededefd���Zdedefd�Zdeded effd!�Zd1d"�Zd#edd
fd$�Zdededefd%�Z 	d0d&dd'e!d(e"e!dd
fd)�Z#d*edd
fd+�Z$d,�Z%ed-edefd.���Z&d
S)3�Agentz
    X-Ray Agent class
    z/var/run/xray-agent.pid��<i��Fc��tjtjtjh��t	jd��|_t��|_t|j��td��}||j���|_td��}|��|_t��|_
t��|_t!��|_t%��|_|rft)|j���|_|j���t)|j���|_|j���|r|���|���dS)N�agent�api_req)�	system_id�adviser)�target)�signal�pthread_sigmask�	SIG_BLOCK�SIGUSR2�logging�	getLogger�loggerr�sys_idrr�send_client�adviser_client�task_client_objectr�api_data_cache_lock�dict�api_data_cacher�fault_detectorr�_setup_signal_handler�signal_handler_thread�start�_flusher�flusher_thread�	_save_pid�listen)�self�save_pid�background_routine�clickhouse_client_object�adviser_client_objects     r'�__init__zAgent.__init__;sI����v�/�&�.�1A�B�B�B��'��0�0���!�m�m����T�[�!�!�!�#-�i�#8�#8� �3�3�d�k�J�J�J��� *�9� 5� 5��3�3�5�5���",�,�,���#'�6�6�� �;?�6�6���+�o�o����	(�)/�t�7Q�)R�)R�)R�D�&��&�,�,�.�.�.�#)��
�">�">�">�D����%�%�'�'�'��	��N�N����	
���
�
�
�
�
r&c�x�tjtjh��}tjd|j��|���t��tjd��	tj	|jtj��dS#t$rtjd��YdSwxYw)Nz?Received SIGUSR2 from pid=%s, flushing database storage on diskz;Sending signal back to process that requested storage flushz5Process that requested storage flush no longer exists)r2�sigwaitinfor5r6�info�si_pid�_flush_mongodb_countersr�os�kill�OSError�warning)rH�siginfos  r'�_wait_for_sigusr2zAgent._wait_for_sigusr2_s����$�f�n�%5�6�6����9�:A�.�	J�	J�	J�	
�$�$�&�&�&�������R�S�S�S�	U��G�G�N�F�N�3�3�3�3�3���	U�	U�	U��O�S�T�T�T�T�T�T�	U���s�1$B�B9�8B9c���tjtjtjh��		|���n$#t
$rt
jd��YnwxYw�;)z�
        Setup SIGUSR2 handler that starts in-memory
        storage flush when received.

        When flushed, send SIGUSR2 back to the process that send signal.
        Tz4Unable to process signal, see traceback for details.)r2r3r4r5rX�	Exceptionr6�	exception)rHs r'rAzAgent._setup_signal_handlerms}��	��v�/�&�.�1A�B�B�B�	Z�
Z��&�&�(�(�(�(���
Z�
Z�
Z��!�"X�Y�Y�Y�Y�Y�
Z����	Zs�A�A#�"A#Nc��|�|gn
t��}|D]�}|j�d|��	|�|��\}}n%#t$rtjd|��Y�XwxYwt|��5}|�|j	���ddd��n#1swxYwY|j
dkr|�||j����|�||j|j
����dS)NzFlushing task to mongo %sz Unable to get client and task %s��next_request_id�time)rr8rP�get_cached_or_loadrr6rVr�update_with_local_datar^�
tracing_by�
update_counts�
request_count�
tracing_count)rH�task_id�tasks_to_flush�	apiclientr �storages      r'rRzAgent._flush_mongodb_counters{s~��&-�&9�'���|�~�~��%�	7�	7�G��K���8�'�B�B�B�
�"&�"9�"9�'�"B�"B��	�4�4���
�
�
��� B�G�L�L�L���
����
$�G�,�,�
U���+�+�G�<S�+�T�T�T�
U�
U�
U�
U�
U�
U�
U�
U�
U�
U�
U����
U�
U�
U�
U���&�(�(��"�"�9�d�.@�A�A�A�A��"�"�9�d�.@�#'�#5�7�7�7�7�+	7�	7s#�A
�
A/�.A/�B)�)B-	�0B-	c���t|jd��5}|�tt	j������ddd��dS#1swxYwYdS)zJ
        Save agent process pid to file that other tools can use.
        �wN)�open�PIDFILE�write�strrS�getpid)rH�fs  r'rFzAgent._save_pid�s����$�,��
$�
$�	&��
�G�G�C��	���$�$�%�%�%�	&�	&�	&�	&�	&�	&�	&�	&�	&�	&�	&�	&����	&�	&�	&�	&�	&�	&s�4A�A�Ac��d}d}	tj|j��tj��|z
|jkr||���t��tj��}tj��|z
|jkr'|���tj��}ntd�����)z]
        This method flushes data from memory to
        local storage periodically.
        rTF)�remove)r_�sleep�COUNTERS_FLUSH_INTERVAL�MONGO_FLUSH_INTERVALrRr�CLEANUP_INTERVAL�cleanup_api_data_cache)rH�last_mongo_flush_time�last_api_data_cache_cleanups   r'rDzAgent._flusher�s���
!"��&'�#�	3��J�t�3�4�4�4��y�{�{�2�2�T�5N�N�N��,�,�.�.�.�$�&�&�&�(,�	���%��9�;�;�!<�<�t�?T�T�T��/�/�1�1�1�26�)�+�+�/��$�E�2�2�2�2�	3r&�returnc���tt��}	|���\}}t|j|f���}|���|j�d|j���c)z(
        Start listening socket
        T)r1�argsz[%s] Started)	rr
�acceptr�handlerCr8�debug�name)rH�_socket�
connection�address�ts     r'rGzAgent.listen�sl�� �
�+�+��	6�")�.�.�"2�"2��J���d�k��
�>�>�>�A�
�G�G�I�I�I��K���n�a�f�5�5�5�		6r&c�D�|�||��\|d<|d<dS)zj
        Calculate throttling values and update given data
        with resulting throttling stat
        �hitting_limits�throttled_timeN)r@)rH�data�t_key�	cpu_values    r'�add_limit_faultszAgent.add_limit_faults�s5��
:>�9L�9L��9�:�:�6��
���%5� 6� 6� 6r&r��
socket objectc��t|��\}}}t|��}|�d���}	|�|��}nn#tj$r\}|j�dt|��dt��j
i���|���Yd}~dSd}~wwxYw|�J|j�
||��|j���|���dS	|�d���|�|||��|�|��n#t$$rYnwxYw|���dS#|���wxYw)z�
        Handle incoming connection
        :param connection: socket object usable to
        send and receive data on the connection
        �ignore)�errorszJSON decode failed: %s�t_name��extraNr�)rr�makefile�
read_input�json�JSONDecodeErrorr8�errorrorr��closer@�save�flush�getr��__call__r)	rHr��_pid�_uid�_gid�current_cpu�fileobj�
input_data�es	         r'rzAgent.handle�s���)��4�4���d�D�5�d�;�;���%�%�X�%�6�6��	�����1�1�J�J���#�	�	�	��K���6�!�!�f�f�X�~�7G�7G�7L�,M�
�
O�
O�
O��������F�F�F�F�F�����		������
��$�$�T�;�7�7�7���%�%�'�'�'��������F�	��~�~�.�/�/�7��%�%�j�$��D�D�D��M�M�*�%�%�%�%���	�	�	��D�	����
���������J���������sC�A�B;�AB6�6B;�AE
�E3�
E�E3�E�E3�3F	�fileiozfile objectc��|���}|j�dt��jt|�������|j�dt��j|��t|�����dkrdStj
|���d���S)zy
        Read input data and return decoded json
        :param fileio: a file-like object providing read method
        z[%s] I received %i bytes tracez[%s] I received data: %srNF)�strict)�readr8rPrr��len�encoder��stripr��loads)rHr�r�s   r'r�zAgent.read_input�s���
�{�{�}�}������9�'�)�)�.��D�K�K�M�M�0B�0B�	D�	D�	D�����4�(�*�*�/��	7�	7�	7��t�z�z�|�|����!�!��F��z�$�*�*�,�,�u�5�5�5�5r&�decoded_datac���|j�dt��j|�d��|�d����|�|��dS)z'
        Process received data
        z&[%s] Processing trace for task %s (%s)�tracing_task_id�urlN)r8rPrr�r��process_request_data)rHr�s  r'r�zAgent.__call__�sl��	
����A�'�)�)�.�%�)�)�*;�<�<�%�)�)�%�0�0�	2�	2�	2�	
�!�!�,�/�/�/�/�/r&�fake_task_idzAPIClient instancec��	tt��5}	||���}n4#t$r't	d||���d�����wxYw	ddd��n#1swxYwYn1#t$r$}t	dt|�������d}~wwxYw|�|j	|���S)z<
        Initialize MongoDB client for current task
        z9Cannot resolve tracing_task_id: no match found in storage)�id�all_idsr�Nz Cannot resolve tracing_task_id: )r/r�)
rr�decode�KeyErrorr�keys�RuntimeErrorror<r9)rHr��task_storage�real_idr�s     r'�instantiate_mongo_clientzAgent.instantiate_mongo_clientsa��

	I��0�1�1�
@�\�@�*�<�8�?�?�A�A�G�G���@�@�@�#�T�%1�*6�*;�*;�*=�*=�?�?�@�@�@�@�@�����
@�
@�
@�
@�
@�
@�
@�
@�
@�
@�
@����
@�
@�
@�
@����	I�	I�	I��G�s�1�v�v�G�G�H�H�H�����	I�����&�&���7>�'�@�@�	@sN�B�A4�3�A4�1A$�$A4�(B�4A8�8B�;A8�<B�
B.�
B)�)B.r�c��|dS)zj
        Retrieve tracing task ID from the input data
        :return: tracing task ID (fake one)
        r�r%)r�s r'�get_task_idzAgent.get_task_ids���%�&�&r&r c�r�|jr/|j�dt|j����dSdS)z-
        Check if task is continuous
        z'Task with id: %s detected as continuousTF)�
is_continuousr8rProrf)rHr s  r'�is_continuous_taskzAgent.is_continuous_tasksC����	��K���F� ���.�.�
0�
0�
0��4��ur&rc�\�|j�d|j��|j�|��}|�|j|jfS|�|��}|���}|j�d|||��t||���|j|<||fS)z�
        Returns a client and task from cache of API data or
        initialize client and GET task from MongoDB and add to cache
        zCached API data: %sNz,Adding new container in cache: %s --> %s, %s)rr )	r8r�r?r�rr r��get_taskr)rHr��cached_datarh�_ts     r'r`zAgent.get_cached_or_load(s���
	
����/��1D�E�E�E��)�-�-�l�;�;���"��%�{�'7�7�7��1�1�,�?�?�	�
�
�
�
!�
!������H�&��I�	7�	7�	7�,<�I�BD�-F�-F�-F���L�)��"�}�r&c��	tt��5}d�|���D��}ddd��n#1swxYwYn+#t$r|j�d��YdSwxYwt
|j�����D]c}|j5||jvr9||vr5|j�	d|��|j�
|��ddd��n#1swxYwY�ddS)z�
        Cleanup an API data im-memory cache dict in order not store
        inactive (stopped, already completed) tasks there
        c�6�g|]}|�����Sr%)r�)�.0�ks  r'�
<listcomp>z0Agent.cleanup_api_data_cache.<locals>.<listcomp>Bs ��H�H�H�q����
�
�H�H�Hr&Nz,Unable to cleanup cache, storage unavailablez!Cleaning up inactive container %s)rrr�r�r8rV�listr?r=rP�pop)rHr��active_tasks�_tasks    r'rxzAgent.cleanup_api_data_cache;s���
	��0�1�1�
I�\�H�H�L�4E�4E�4G�4G�H�H�H��
I�
I�
I�
I�
I�
I�
I�
I�
I�
I�
I����
I�
I�
I�
I����	�	�	��K���>�
@�
@�
@��F�F�	����
�$�-�2�2�4�4�5�5�	3�	3�E��)�
3�
3��D�/�/�/�E��4M�4M��K�$�$�%H�%�P�P�P��'�+�+�E�2�2�2�
3�
3�
3�
3�
3�
3�
3�
3�
3�
3�
3����
3�
3�
3�
3��	3�	3sF�A
�A�A
�A�A
�A�	A
�
$A5�4A5�'AC6�6C:	�=C:	�request_datac���t��j}|�|�|����\}}t	jd|j��t|�|��|j���5}|�|j	���|j
dkr(|j�d��	ddd��dS|�||��}|j�d|��|�
|��	|j�d|��|�|��n#t$rYnwxYw|xj	d	z
c_	|�|j	���ddd��n#1swxYwY|jr|�|j��|jd
kr"|j
dkr|�|��dSdSdS)z�
        Increment request ID in /usr/share/alt-php-xray/requests/{tracing_task_id} file
        Substitute request_id and tracing_task_id in request_data.
        Send request_data to ClickHouse
        :param request_data: original request data
        z
Is Manual: %s)r�r]rz*Tracing count is 0, nothing should be doneNz[%s] Sending to ClickHousez[%s] Sending to SmartAdvicerr_)rr�r`r�r6rP�	is_manualrrar^rer8�update_request_datar:r;rrR�fake_idrb�
complete_task)rHr�r��_r ri�updated_request_datas       r'r�zAgent.process_request_dataNs���
���!���)�)�$�*:�*:�<�*H�*H�I�I���4���_�d�n�5�5�5�
�� 0� 0�� >� >�&*�n�6�6�6�	Q�9@��'�'��8O�'�P�P�P��!�Q�&�&��� � �!M�N�N�N��	Q�	Q�	Q�	Q�	Q�	Q�	Q�	Q�$(�#;�#;�L�<@�$B�$B� �
�K���9�1�=�=�=����1�2�2�2�
��� � �!>��B�B�B��#�#�$8�9�9�9�9���
�
�
���
����

�#�#�q�(�#�#��'�'��8O�'�P�P�P�1	Q�	Q�	Q�	Q�	Q�	Q�	Q�	Q�	Q�	Q�	Q����	Q�	Q�	Q�	Q�4�>�	7��(�(���6�6�6��?�f�$�$��);�q�)@�)@����t�$�$�$�$�$�%�$�)@�)@s>�AF�AF�0E	�F�	
E�F�E�.F�F�Fc���|jdz|d<|j|d<|dD]C}|jdz|d<|j|d<|ddkr|�|d��|d<�D|j�dt��j|�d��|�d����|j�d	t��j|��|S)
z�
        Substitute request_id and tracing_task_id
        :param data: original input
        :param task: a Task instance
        :return: updated input
        r�
request_idr�r��type�mysql_query�queryz[%s] Input updated => %s & %sz[%s] Full updated input %s)	rdrf�hide_symbolsr8rPrr�r�r�)rHr�r �items    r'r�zAgent.update_request_dataws���"�/�!�3��\��"&�,��
����L�	A�	A�D�!%�!3�a�!7�D���&*�l�D�"�#��F�|�}�,�,� $� 1� 1�$�w�-� @� @��W�
������8�'�)�)�.����"3�4�4�d�h�h�|�6L�6L�	N�	N�	N�	
����6�(�*�*�/��	7�	7�	7��r&rrdrec�4�|�||���dS)z:
        Update task counters in mongodb instance
        )rerdN)�update_counts_only)rHrrdres    r'rczAgent.update_counts�s0��	�!�!�
�0=�	"�	?�	?�	?�	?�	?r&r�c���|j�dt��j|j��tjd��|�|j��dS)z_
        Stop and complete request_qty task
        :param _task: tracing task to stop
        z [%s] Task %s should be completedrN)r8rPrr�rfr_rt�_run_complete_task_cmd)rHr�s  r'r�zAgent.complete_task�s]��
	
����;�'�)�)�.���	(�	(�	(�	
�
�1�
�
�
��#�#�E�M�2�2�2�2�2r&c�D�tjddd|jd|g��dS)Nzcloudlinux-xray-manager�stopz--system_idz--tracing_task_id)�
subprocess�check_outputr9)rHrfs  r'r�zAgent._run_complete_task_cmd�s7����%�v��4�;���!
�	�	�	�	�	r&r�c�\�d�}tjd��}tj|||��S)zA
        Sanitize data in single quotes from MySQL query
        c���|�d��}|�d��}d�}tjd||�d����}|�|�|p|��S)zL
            Works with whole string in single or double quotes
            �quote�truncc�b�|�d��rdS|�d��rdSdS)zE
                Works with characters inside quotes
                �digit�0�symbol�xN)�group)�inner_ms r'�
inner_replz8Agent.hide_symbols.<locals>.replacer.<locals>.inner_repl�s@���=�=��)�)���3��]�]�8�,�,���3��r&z.((?P<digit>\d)|(?P<symbol>[^0-9_:;\-/',. \\]))�in_quote)r��re�sub)�m�qr�r��	sanitizeds     r'�replacerz$Agent.hide_symbols.<locals>.replacer�sz������ � �A����� � �A�
�
�
���P�)�1�7�7�:�+>�+>�@�@�I��,��,�A�F��,�,�,r&zH(?P<quote>['"])(?P<in_quote>.*?)((?<![\\|/])(?P=quote)|(?P<trunc>\.{3})))r��compiler�)r�r��patterns   r'r�zAgent.hide_symbols�s7��
	-�	-�	-�2�*�l�m�m���v�g�x��5�5�5r&)FF)N)r{N)r�r�r{N)'r!r"r#�__doc__rmrurvrwrMrXrArRrFrDrGr�rrr�r>r�ror��staticmethodr�r�boolr�r	r`rxr�r��intrrcr�r�r�r%r&r'r)r)0s���������(�G� ������"�"�"�"�HU�U�U�Z�Z�Z�7�7�7�7�4&�&�&�3�3�3�*	6�	6�	6�	6����$�$�$�$�L6��6�3�6�6�6�6�0�T�0�d�0�0�0�0�@�/2�@�7K�@�@�@�@�(�'�$�'�3�'�'�'��\�'��t��������s��u�[�$�=N�7O�����&3�3�3�3�&'%��'%�$�'%�'%�'%�'%�R���D��T�����06:�?�?�$8�?�%(�?�%-�c�]�?�>B�?�?�?�?�
3�4�
3�D�
3�
3�
3�
3�����6�#�6�#�6�6�6��\�6�6�6r&r))-r�r�r6rSr�r2r�r_�	threadingrrr�typingrrrr	�dataclassesr
rhr�internal.constantsr
r�internal.exceptionsrr�internal.fault_detectorr�internal.local_countersrrr�internal.typesr�internal.user_plugin_utilsr�internal.utilsrrrrrrr)r%r&r'�<module>r	s	������������	�	�	�	�	�	�	�	�
�
�
�
���������2�2�2�2�2�2�2�2�2�2�-�-�-�-�-�-�-�-�-�-�-�-�!�!�!�!�!�!�!�!�!�!�!�!�?�?�?�?�?�?�?�?�8�8�8�8�8�8�8�8�2�2�2�2�2�2�����������
!� � � � � �5�5�5�5�5�5��������������������������
X6�X6�X6�X6�X6�X6�X6�X6�X6�X6r&

Zerion Mini Shell 1.0