HEX
Server: Apache
System: Linux www3.pit.tblive.com 5.14.0-687.38.1.el9_8.x86_64 #1 SMP PREEMPT_DYNAMIC Wed Aug 12 17:19:12 EDT 2026 x86_64
User: awaldron (1020)
PHP: 8.1.34
Disabled: exec,passthru,shell_exec,system
Upload Files
File: //opt/cloudlinux/venv/lib64/python3.11/site-packages/xray/manager/__pycache__/base.cpython-311.pyc
�

�Pcj����dZddlZddlZddlZddlZddlZddlZddlZddlZddl	Z	ddl
m
Z
mZddlm
ZmZddl	mZmZmZddlZddlmZddlmZmZddlmZmZdd	lmZdd
lmZddlm Z ddl!m"Z#d
dl$m%Z%d
dl&m'Z'd
dl(m)Z)d
dl*m+Z+m,Z,m-Z-m.Z.m/Z/d
dl0m1Z1d
dl2m3Z3d
dl4m5Z5d
dl6m7Z7m8Z8m9Z9d
dl:m;Z;d
dl<m=Z=d
dl>m?Z?m@Z@mAZAd
dlBmCZCmDZDmEZEmFZFmGZGmHZHd
dlImJZJmKZKmLZLmMZMmNZNe	jOrd
dlPmQZQejRd��ZSGd�d��ZTdS)zX
This module contains classes implementing X-Ray Manager behaviour
and helper functions
�N)�datetime�	timedelta)�escape�glob)�Iterator�Optional�Tuple)�Feature)�is_panel_feature_supported�get_installed_php_versions)�php_get_vhost_versions�is_wpos_supported)�is_litespeed_running)�drop_privileges)�
disable_quota)�gettext�)�ClWposGetter)�
get_client)�ContinuousManager)�XRayAPIError�	XRayError�XRayMissingDomain�XRayManagerExit�XRayManagerExitPHPUnsupported)�FPMReloadController)�open_local_storage)�NginxUserCache)�
DomainInfo�Task�	url_split)�ManageUserAgent)�AdvancedMetrics)�get_xray_exec_user�user_mode_restricted�nginx_user_cache)�no_active_tasks�switch_schedstats�ssa_disabled�_cagefsctl_remount�set_privileges�is_file_recently_modified)�create_ini_files�remove_ini_files�is_global_ini_mode�remove_global_ini_mode_marker�create_global_ini_mode_marker)�	APIClientz%^[A-Za-z0-9_][A-Za-z0-9@:._-]{0,127}$c�
�eZdZdZdZddddddd	d
ddd
dddd�Zdfdedefd�Zde	e
fd�Zdefd�Z
dedefd�Zdedefd�Zdedefd�Zdedefd�Zdedefd�Zdedd fd!�Zd"�Zejd#��Zd$Zd%ejdefd&�Zd'edefd(�Zed)ede ejfd*���Z!d+edd fd,�Z"d-edefd.�Z#d/edefd0�Z$dedefd1�Z%d2edefd3�Z&d2edd fd4�Z'ed'ede(ee
ffd5���Z)d6edd fd7�Z*edefd8���Z+defd9�Z,e-ddd:�d-ed;ed<ed=e
d>ed?edefd@���Z.d-edefdA�Z/dBdC�d-ed=e
defdD�Z0dEedefdF�Z1dG�Z2e-dEedefdH���Z3dEedefdI�Z4edJedefdK���Z5defdL�Z6edMe7dNdOfdP���Z8dEedefdQ�Z9d-edRedefdS�Z:d-edefdT�Z;d-edefdU�Z<d-edefdV�Z=defdW�Z>defdX�Z?dgdYedZe
d[e
defd\�Z@dYedefd]�ZAdYed^e
defd_�ZBdefd`�ZCdefda�ZDdefdb�ZEdefdc�ZFdefdd�ZGdefde�ZHd S)h�BaseManagerz�
    Basic manager abstract class.
    Implements methods common for all managers.
    Requires managers to implement their custom methods.
    z/var/run/xray-agent.pidz/opt/alt/php54/link/confz/opt/alt/php55/link/confz/opt/alt/php56/link/confz/opt/alt/php70/link/confz/opt/alt/php71/link/confz/opt/alt/php72/link/confz/opt/alt/php73/link/confz/opt/alt/php74/link/confz/opt/alt/php80/link/confz/opt/alt/php81/link/confz/opt/alt/php82/link/confz/opt/alt/php83/link/confz/opt/alt/php84/link/confz/opt/alt/php85/link/conf)z	alt-php54z	alt-php55z	alt-php56z	alt-php70z	alt-php71z	alt-php72z	alt-php73z	alt-php74z	alt-php80z	alt-php81z	alt-php82z	alt-php83z	alt-php84z	alt-php85F�	system_id�phpinfo_modec��t��|_||_tjd��|_t
��|_t��|_	||_
td��|j���|_dS)N�manager�lists�r5)r�api_client_class�sys_id�logging�	getLogger�loggerr�continuous_monitoringr"�manage_user_agentr6�
ui_api_client)�selfr5r6s   �H/opt/cloudlinux/venv/lib64/python3.11/site-packages/xray/manager/base.py�__init__zBaseManager.__init__ssr��2<���������'�	�2�2���%6�%8�%8��"�!0�!2�!2���(���0�Z��0�0�4�;�G�G�G������returnc��	t|j��5}t|�����cddd��S#1swxYwYdS#tt
f$rt
jd��YdSwxYw)Nz'Unable to read daemon pid from pidfile.)�open�DAEMON_PIDFILE�int�read�OSError�IOErrorr=�warning)rC�fs  rD�_get_daemon_pidzBaseManager._get_daemon_pid~s���	G��d�)�*�*�
%�a��1�6�6�8�8�}�}�
%�
%�
%�
%�
%�
%�
%�
%�
%�
%�
%�
%����
%�
%�
%�
%�
%�
%����!�	G�	G�	G��O�E�F�F�F�F�F�F�	G���s3�A�!A�A�A�A�A�A�%A:�9A:c��|jS)z\
        Get supported PHP versions
        :return: a dict with supported versions
        )�VERSIONS�rCs rD�supported_versionszBaseManager.supported_versions�s��
�}�rF�php_versionc�.�||���vS)z�
        Check if given PHP version is supported
        :param php_version: PHP version, e.g. 'alt-phpXY'
        :return: True if version is supported, False otherwise
        )rU)rCrVs  rD�is_version_supportedz BaseManager.is_version_supported�s���d�5�5�7�7�7�7rF�domain_infoc�Z�|����|j��S)zh
        Get a path to directory for additional ini file for based on
        panel-set version
        )rU�get�panel_php_version�rCrYs  rD�	_ini_pathzBaseManager._ini_path�s'��
�&�&�(�(�,�,�[�-J�K�K�KrFc���|�|��}	|jr|j}n&|�|��r|j}n	|jp|}n#t
$rYnwxYw|j�d|��|S)z�
        Resolve a path to directory for additional ini file
        It depends on version set for domain and on selector
        :param domain_info: a DomainInfo object
        :return: path to directory for ini files
        zIni path resolved as %s)r^�php_ini_scan_dir�is_selector_enabled�selector_ini_path�phpd_location_ini_path�
ValueErrorr?�info)rCrY�ini_paths   rD�get_ini_pathzBaseManager.get_ini_path�s����>�>�+�.�.��		��+�
J�&�7����)�)�+�6�6�
J�&�8���&�=�I������	�	�	��D�	����	
����2�H�=�=�=��s�5A
�
A�Ac���|js=|�|��r(|j�d|j��|j}n|j}|j�d|��|S)a
        Get PHP version which serves given domain
        :param domain_info: a DomainInfo object, including user of domain
                            and PHP version set in control panel environment
        :return: real php version of domain (selector or panel one)
        zSelector is enabled for user %szPHP version detected as %s)�is_selector_appliedrar?re�user�selector_php_versionr\)rCrY�current_versions   rD�get_php_versionzBaseManager.get_php_version�sq���.�	<�4�3K�3K�K�3X�3X�	<��K���>��@P�Q�Q�Q�)�>�O�O�)�;�O�����5��G�G�G��rFc�t�ttj��sdS|j�dS|�|��S)a

        Is selector enabled for given domain
        :param domain_info: a DomainInfo object, including user of domain
                            and PHP version set in control panel
        :return: True if selector is enabled for domain, False otherwise
        F)rr
�PHP_SELECTORrk�panel_specific_selector_enabledr]s  rDrazBaseManager.is_selector_enabled�s>��*�'�*>�?�?�	��5��+�3��5��3�3�K�@�@�@rFNc��|�|j��|jr|�|��dSt	��r|�|j��dS|�|j��dS)z�
        Reload FPM service or kill all *php* processes of user
        :param domain_info: a ready-to-use DomainInfo object
        N)�reset_criu_imgs�name�	panel_fpm�restart_fpm_servicer�gracefully_restart_litespeedrj�
kill_user_phpr]s  rD�php_procs_reloadzBaseManager.php_procs_reload�s���
	
���[�-�.�.�.�� �	1��$�$�[�1�1�1�1�1�
!�
#�
#�	1��-�-�k�.>�?�?�?�?�?����{�/�0�0�0�0�0rFc��t|��5t��5tj|��}	t	t
j�|jd��d���	��n3#ttf$r|j�
dd���YnwxYwddd��n#1swxYwYddd��dS#1swxYwYdS)z�
        Litespeed's graceful restart.
        https://www.litespeedtech.com/support/wiki/doku.php/litespeed_wiki:php:detached_mode#for_a_user
        z.lsphp_restart.txt�wz$Unable to restart lsws after changesT)�exc_infoN)rr�pwd�getpwnamrI�os�path�join�pw_dir�close�FileNotFoundError�PermissionErrorr?rO)rC�username�pws   rDrvz(BaseManager.gracefully_restart_litespeed�s���
�X�
&�
&�	[�	[�
���	[�	[���h�'�'�B�
[��R�W�\�\�"�)�-A�B�B�C�H�H�N�N�P�P�P�P��%��7�
[�
[�
[���#�#�$J�UY�#�Z�Z�Z�Z�Z�
[����	[�	[�	[�	[�	[�	[�	[�	[�	[�	[�	[����	[�	[�	[�	[�	[�	[�	[�	[�	[�	[�	[�	[�	[�	[�	[�	[����	[�	[�	[�	[�	[�	[sX�C�B:�AA;�:B:�;-B+�(B:�*B+�+B:�.C�:B>	�>C�B>	�C�C�Cz^php(?:[0-9]+(?:\.[0-9]+)?)?$)�httpd�apache2�	litespeed�lshttpd�lsphpzphp-fpm�procc��|j�|jd��sdS	|���}t	d��D]L}|�dS|���}|�|j��rdS|���}�Mn&#tj	$rYdStj
$rYnwxYwdS)a�
        Whether the process is a web PHP backend that must be signalled
        to pick up xray.ini changes. CLI PHP processes -- bare `php` comm
        with no web-server ancestor (wp-cli, composer, user cron scripts)
        -- do not persist across web requests, so a SIGHUP would only
        break the user's workload without helping tracing (ZD288595).
        A process that cannot be classified is treated as a web backend,
        keeping the historical kill behavior.
        rsT�NF)�
_CLI_PHP_COMM�matchre�parent�rangers�
startswith�_WEB_ANCESTOR_COMMS�psutil�
NoSuchProcess�Error)rCr�r��_�parent_names     rD�_needs_php_reloadzBaseManager._needs_php_reload�s����!�'�'��	�&�(9�:�:�	��4�
	��[�[�]�]�F��2�Y�Y�
)�
)���>� �5�5�$�k�k�m�m���)�)�$�*B�C�C� ��4�4��������

)���#�	�	�	��5�5��|�	�	�	��D�	�����ts#�(B�/B�B�B=�,B=�<B=r�c���t��}t��}|�|��D]�}d|jdvr�|�|��s!|�|jd���H	|�tj��n#tj	$rY�zwxYw|�|jd����|r|j
�d|��|S)a9
        Send SIGHUP to web PHP backends, which belong to given username,
        so they are respawned with up-to-date xray configuration.
        CLI PHP processes of the user are left alone (ZD288595)
        :param username: name of user
        :return: list of pids of successfully killed processes
        �phprs�pidz6Skipped CLI php processes not serving web requests: %s)�list�
user_procsrer��append�send_signal�signal�SIGHUPr�r�r?)rCr��killed_procs�
skipped_procsr�s     rDrwzBaseManager.kill_user_php
s���v�v�����
��O�O�H�-�-�
	2�
	2�D��D�I�f�-�-�-���)�)�$�/�/�
��$�$�T�Y�u�%5�6�6�6��
�� � ���/�/�/�/���'�
�
�
���
��������	�%� 0�1�1�1�1��	f��K���U�Wd�e�e�e��s�;B�B-�,B-�	user_namec#��K�tj|��j}gd�}tj|��D],}||jdj|jdjfvr|V��-dS)z�
        Generator yields processes, which belong to given user_name.
        Processes are checked using UID, not user_name
        :param user_name: user name
        :return: a generator object
        )rsr��uidsr�N)r|r}�pw_uidr��process_iterre�real�	effective)r��uid_by_name�attrs�ps    rDr�zBaseManager.user_procs&su�����l�9�-�-�4��'�'�'���$�U�+�+�	�	�A��q�v�f�~�2�A�F�6�N�4L�M�M�M������	�	rF�domainc���|sdStdt|���d���}|r=tj|d��|j�d|d��dSdS)zt
        Reset criu images if any found for given domain in order to
        enable/disable X Ray correctly
        Nz/var/run/mod_lsapi/*�
_criu_imgsrzcriu images in %s dropped)r�glob_escape�shutil�rmtreer?re)rCr��
criu_imgs_dirs   rDrrzBaseManager.reset_criu_imgs4s���
�	��F��S�K��4G�4G�S�S�S�T�T�
��	L��M�-��*�+�+�+��K���8�-��:J�K�K�K�K�K�	L�	LrF�urlc�P�t|��\}}|�|��S)z4
        DomainInfo retrieving based on url
        )r!�get_domain_info�rCr��domain_namer�s    rD�_domain_info_by_urlzBaseManager._domain_info_by_url@s'��#�3�����Q��#�#�K�0�0�0rFr�c�:�ttd�����)z�
        Retrieve information about given domain from control panel environment
        Required to be implemented by child classes
        :param domain_name: name of domain
        :return: a DomainInfo object
        z0Manager should implement retrieving domain info!��NotImplementedErrorr�)rCr�s  rDr�zBaseManager.get_domain_infoHs��"�!�$V�"W�"W�X�X�XrFc�:�ttd�����)z�
        Check if selector is enabled specifically for panel
        Required to be implemented by child classes
        :param domain_info: a DomainInfo object
        :return: True if yes, False otherwise
        zCManager should implement specific panel check for selector enabled!r�r]s  rDrpz+BaseManager.panel_specific_selector_enabledQs��"�!�$i�"j�"j�k�k�krF�dom_infoc�:�ttd�����)z�
        Get FPM service name for particular panel
        :param dom_info: DomainInfo object
        :return: name of FPM service
        z5Manager should implement FPM service name retrieving!r�)rCr�s  rD�fpm_service_namezBaseManager.fpm_service_nameZs��"�!�$[�"\�"\�]�]�]rFc�F�|�|��}t|t��rt�|��s.|j�dt|��|d����dS	tj	d|dgddd���|j�
d	|��t|�����dS#tttjf$r7}|j�d
t|��|d����Yd}~dSd}~wwxYw)zg
        Restart FPM service for particular version
        :param dom_info: DomainInfo object
        z6Refusing to reload FPM service: name failed validation)�fpm_servicere��extraNz
/sbin/service�reloadT)�capture_output�text�checkzService %s reloadedzFailed to reload FPM service)�errre)r��
isinstance�str�_FPM_SERVICE_NAME_REr�r?�error�repr�
subprocess�runrer�save_latest_reloadrMrd�SubprocessError)rCr��fpm_serv�es    rDruzBaseManager.restart_fpm_servicebsK��
�(�(��2�2���(�C�(�(�	�0D�0J�0J�8�0T�0T�	�
�K���H�&*�8�n�n�h�G�G�
�
�
�
�
�F�	?��N�O�X�x�@�QU�\`�hl�m�m�m�m��K���2�H�=�=�=�
 ��)�)�<�<�>�>�>�>�>����Z�%?�@�	g�	g�	g��K���<�C�PQ�F�F�\d�De�De��f�f�f�f�f�f�f�f�f�����	g���s�46C
�
D �),D�D c�J�tj|��}d|j�d�}tj�tj�|����s@tjtj�|����t|��||j	fS)z5Resolve path for user and prepare directory if neededz/var/clwpos/uids/z
/info.json)
r|r}r�r~r�isdir�dirname�makedirsr*�pw_gid)r��_upwd�_paths   rD�prepare_wpos_info_pathz"BaseManager.prepare_wpos_info_pathxs�����X�&�&��<�E�L�<�<�<���w�}�}�R�W�_�_�U�3�3�4�4�	)��K������.�.�/�/�/��x�(�(�(��e�l�"�"rFrjc���t��sdS|�|��\}}t|��r|j�d|��dS|dz}tt
|��t�����}|j�d||��td|d���5	t|d	��5}tj||��ddd��n#1swxYwYtj||��|j�d
��n?#t$r2}|j�dt#|����Yd}~nd}~wwxYwddd��dS#1swxYwYdS)z9Prepare and write info.json file for WPOS (AWP) utilitiesNz*File %s modified recently, skip generatingz.tmp)�vhost_versions�installed_versionszGoing to write file %s with %sr�_)�
target_uid�
target_gid�maskrzzWPOS data written successfullyz'Unable to write info for WPOS due to %s)rr�r,r?re�dictr
r�debugr+rI�json�dumpr��moverMr�r�)rCrj�dest�user_gid�working_dest�to_write�	info_jsonr�s        rD�write_wpos_infozBaseManager.write_wpos_info�s%�� �"�"�	��F��4�4�T�:�:���h�$�T�*�*�	��K���I�4�P�P�P��F��f�}���'=�d�'C�'C�Xr�Xt�Xt�u�u�u������:�D�(�K�K�K�
�q�X�E�
J�
J�
J�	U�	U�
U��,��,�,�3�	��I�h�	�2�2�2�3�3�3�3�3�3�3�3�3�3�3����3�3�3�3���L�$�/�/�/��� � �!A�B�B�B�B���
U�
U�
U���!�!�"K�S�QR�V�V�T�T�T�T�T�T�T�T�����
U����
	U�	U�	U�	U�	U�	U�	U�	U�	U�	U�	U�	U����	U�	U�	U�	U�	U�	Us`�2E-�4D!�C&�D!�&C*	�*D!�-C*	�.2D!� E-�!
E�+(E�E-�E�E-�-E1�4E1c��ddi}|r|�|��tjtt	|���������S)z�
        Create JSON response message with result field == success
        and given keyword arguments in other fields
        :return: json packed string
        �result�success)�updater��dumpsr��sorted�items)�kwargs�initials  rD�responsezBaseManager.response�sP���Y�'���	#��N�N�6�"�"�"��z�$�v�g�m�m�o�o�6�6�7�7�8�8�8rFc�b�|�d��r|d�d�|d<|jdi|��S)z�
        Create JSON response message for continuous actions.
        Extends action definition with 'continuous' addition
        :return: json packed string
        �actionz continuous�)r[r�)rCr�s  rD�response_continuouszBaseManager.response_continuous�sI���:�:�h���	@�"(��"2�?�?�?�F�8���t�}�&�&�v�&�&�&rF)�	auto_task�autotracing�	client_ip�
tracing_by�
tracing_countrrc
�\�|�|��}|�|j��|�|��}|�|��r|�|��}	n.|j�dd|i���t||���|�	|j
���}
|rdn
t��}t|||||	|||j���}|
�
|��|_|�|���|�|j
���|
�|j��|�|��t)d�	��	t+���|j|j��n1#t0$r$}
t3jd
|
j��Yd}
~
nd}
~
wwxYwt7|j�����t;��rt=��|�d|j�
��S)a

        Start monitoring of given URL.
        Arguments are only allowed by keyword
        :param url: URL to monitor
        :param client_ip: a client IP address
        :param tracing_by: time or qty
        :param tracing_count: a number of minutes|requests to monitor
        :param auto_task: if a task is an auto created one (x-ray 2.0)
        :param autotracing: task is an auto created with
                            user marked as *autotracing*
        :return: JSON encoded result of start action
        zPHP version is unsupported�versionr�r:z
*autotracing*)r�rrr�ini_locationrrj�domain_owner�rVT��enabledz0Failed to send metadata to Smart Advice with: %sN�start�r��tracing_task_id) r�r�rjrmrXrgr?rOrr;r<r$r �create�task_id�add�set_cronjobr��	starttimerxr(r�
post_metadatarsrr=�reasonr�disabler/r-r�)rCr�rrrrrrY�real_php_version�ini_files_location�clientrj�tracing_taskr�s              rDrzBaseManager.start�sI��0�.�.�s�3�3�����[�-�.�.�.��/�/��<�<���$�$�%5�6�6�	G�!%�!2�!2�;�!?�!?����K��� <�Y�P`�Da��b�b�b�/��5E�F�F�F��&�&���&�=�=��"-�G���3E�3G�3G�����!�'�+���$�)�	
�	
�	
�� &�}�}�\�:�:������%5��6�6�6�� � �4�;� �7�7�7�	�
�
�l�,�-�-�-����k�*�*�*��$�'�'�'�'�	Z��N�N�(�(��)9�;�;K�L�L�L�L���	Z�	Z�	Z��O�N�PQ�PX�Y�Y�Y�Y�Y�Y�Y�Y�����	Z�����{�'�(�(�0�0�2�2�2����	������}�}�G�\�=Q�}�R�R�Rs�2,F�
G
�)G�G
c�6�|�|dddd���S)z�
        Start continuous monitoring of given URL (auto task).
        Arguments are only allowed by keyword
        :param url: URL to monitor
        :return: JSON encoded result of start action
        �*�timei�T)r�rrrr�r)rCr�s  rD�
start_autozBaseManager.start_auto�s"���z�z�c�S�V�SW�cg�z�h�h�hrF�)rc�8�|�|dd|dd���S)a6
        Start monitoring of given URL (autotracing task).
        Note, user is overridden as *autotracing*
        Arguments are only allowed by keyword
        :param url: URL to monitor
        :param tracing_count: count of requests to capture
        :return: JSON encoded result of start action
        r�request_qtyT)r�rrrrrr)rCr�rs   rD�start_autotracingzBaseManager.start_autotracing�s1���z�z���$�'���
�
�
�	
rFrc���|�|j|���}|���}gd�}|j|vr1t	td��t
|j��z���|jdk}|r[|���t|j	��5}|�
|j���ddd��n#1swxYwY	|�|j
��}|�|j��nm#t $r|j�d��d}YnEt&$r9}t)|dd	��r|j�d��d}n�Yd}~nd}~wwxYw|���}	|���|���}
|
d
kr|�|
��n|�||��|�?t5|j�����|s|	r|�|��t;��rt=��rt?d	���|� d|j!�
��S)z�
        Stop monitoring of given task ID
        :param tracing_task_id: an ID of task to stop
        :return: JSON encoded result of stop action
        �r5r)�running�stopped�	completed�holdz!Cannot stop task with status '%s'r&)�next_request_idNz+Stopping task for an already deleted domain�_internal_missing_domainFrr
�stopr
)"r;r<�get_task�statusrr�r��_request_daemon_storage_flushr�fake_id�update_with_local_datar*r�r��set_domain_ownerrjrr?rer�getattr�remove�drop_cronjob�recalculate_countsr,�	_completer�restorerxr'r)r(r�r)rCrrr�try_to_complete_statuses�was_running�storagerYr��ini_drained�remaining_counts           rDr,zBaseManager.stops����&�&���o�&�^�^�����(�(��#N�#N�#N� ���&>�>�>�!�!�$G�"H�"H�3�|�Ob�Kc�Kc�"c�d�d�d�"�)�Y�6���	]�
�.�.�0�0�0�#�L�$8�9�9�
]�W��3�3�G�D[�3�\�\�\�
]�
]�
]�
]�
]�
]�
]�
]�
]�
]�
]����
]�
]�
]�
]�	��2�2�<�3C�D�D�K��)�)�+�*:�;�;�;�;�� �	�	�	��K���J�K�K�K��K�K�K��	�	�	��q�4�e�<�<�
��� � �!N�O�O�O�"�������������	����$#�)�)�+�+���!�!�#�#�#�'�9�9�;�;���Q����K�K��(�(�(�(��N�N�<��0�0�0��"��;�+�,�,�4�4�6�6�6��
3�k�
3��%�%�k�2�2�2����	-����	-��e�,�,�,�,��}�}�F�L�<P�}�Q�Q�Qs0�$C�C�C�4D
�
&E7�5	E7�>/E2�2E7c��tjtjtjh��|���}|rK	tj|tj��n*#t$r|j�	d��YnwxYw|j�
d��tjtjhd��}|�tj	d��tjtj
tjh��dS)z�
        Request daemon to flush it's in-memory storage on disk using SIGUSR2
        and wait for getting SIGUSR2 back that daemon sends when it successfully
        flushed data on disk and we are ready to proceed.
        zVUnable to send daemon signal. Some stats will not be flushed. Maybe daemon is stopped?z2Waiting for daemon to signal back about flush end.g.@NzUDaemon did not signal back in given timeout. Some stats may not be flushed. Continue.)r��pthread_sigmask�	SIG_BLOCK�SIGUSR2rQr~�killrMr?rOre�sigtimedwaitr=�SIG_DFL)rC�
daemon_pid�siginfos   rDr/z)BaseManager._request_daemon_storage_flush_s���	��v�/�&�.�1A�B�B�B��)�)�+�+�
��	�
���
�F�N�3�3�3�3���
�
�
���#�#�l������
����
	
����M�N�N�N��%�v�~�&6��=�=���?��O�s�t�t�t���v�~���/?�@�@�@�@�@s�A"�"$B	�B	c��|�|j|���}|���}|jdvr1t	td��t
|j��z���|�|j��}|�	|j
��d|���vr|�|��}nd}|�
|���|�|j���|�|j��t#|j
�����|�|��t)d�	��|�d
|j���S)z�
        Continue monitoring of given task ID
        :param tracing_task_id: an ID of task to continue
        :return: JSON encoded result of continue action
        r%�r&r(z%Cannot continue task with status '%s'�54Nr	r:Tr
�continuer
)r;r<r-r.rr�r�r�r�r2rjrUrmrrr�rrrrxr(r�r)rCrrrrYrs      rD�	continue_zBaseManager.continue_xsl���&�&���o�&�^�^�����(�(����":�:�:�!�!�$K�"L�"L�s�S_�Sf�Og�Og�"g�h�h�h��.�.�|�/?�@�@���%�%�k�&6�7�7�7��4�*�*�,�,�,�,�#�3�3�K�@�@��� $�����%5��6�6�6�� � �4�;� �7�7�7�	�
�
�l�,�-�-�-��{�'�(�(�0�0�2�2�2����k�*�*�*��$�'�'�'�'��}�}�J��@T�}�U�U�UrFc�:�|�|j|���}|���}|jdvr1t	td��t
|j��z���|�||��|�d|j	���S)z�
        Complete given tak ID
        :param tracing_task_id: an ID of task to complete
        :return: JSON encoded result of complete action
        r%rHz%Cannot complete task with status '%s'�completer
)
r;r<r-r.rr�r�r7r�r�rCrrrs    rDrMzBaseManager.complete�s����&�&���o�&�^�^�����(�(����":�:�:�!�!�$K�"L�"L�s�S_�Sf�Og�Og�"g�h�h�h����|�V�,�,�,��}�}�J��@T�}�U�U�UrF�taskc�$�|ddvrdStj��}|d}|dp|d}tj|��}|dkr|td�	��z|krd
S|dkr|t|d�
��z|krd
SdS)zT
        Returns True if task should be completed.
        False otherwise.
        r.)r'r(Frr�
createtimer"r)�daysTrr)�minutes)r�now�
fromtimestampr)rOrTr�	inception�task_start_times     rD�_is_to_completezBaseManager._is_to_complete�s�����>�5�5�5��5��l�n�n���,�'�
���%�;��l�);�	�"�0��;�;����&�&�?�Y�A�=N�=N�=N�+N�QT�+T�+T��4�����O�i��_�H]�6^�6^�6^�$^�ad�$d�$d��4��urFc��|j����dg��}|j�dt|����|D][}|�|��s�tjdt|d����|�|d���\|�	d���S)z�
        Gets all server tasks and complete those which match following criteria:
         - traced_by requests_qty: if task started 2 days ago - complete
         - traced_by time: if task runs longer than start time + tracing time - complete
        r�z#Check tasks for autocompleting...%sz"Going to complete task with id: %srzautocomplete-tasks�r�)
rB�
get_task_listr[r?rer�rXr=r,r�)rC�all_server_tasks�	task_items   rD�autocomplete_taskszBaseManager.autocomplete_tasks�s��� �-�;�;�=�=�A�A�(�B�O�O������>��DT�@U�@U�V�V�V�)�	4�	4�I��'�'�	�2�2�
���L�=�s�9�M^�C_�?`�?`�a�a�a��I�I�i� 1�2�3�3�3�3��}�}�$8�}�9�9�9rF�t_taskrr2c��|���|���|���|jr|���dSdS)z�
        Common complete actions:
        - delete cron job,
        - erase request id file
        - send 'complete' status to mongo
        :param t_task: a tracing task object
        :param client: an APIClient object
        N)r5�erase_request_id_storagerMr�share)r_rs  rDr7zBaseManager._complete�s^��	�������'�'�)�)�)���������	��L�L�N�N�N�N�N�	�	rFc�:�|�|j|���}|���}|jdkr1t	td��t
|j��z���|���|�d|j	���S)z�
        Delete given task ID
        :param tracing_task_id: an ID of task to delete
        :return: JSON encoded result of delete action
        r%r&z#Cannot delete task with status '%s'�deleter
)
r;r<r-r.rr�r�rdr�rrNs    rDrdzBaseManager.delete�s����&�&���o�&�^�^�����(�(����)�+�+�!�!�$I�"J�"J�S�Q]�Qd�Me�Me�"e�f�f�f��
�
�����}�}�H�l�>R�}�S�S�SrF�emailc��|�|��}|j�|j||��|�d|���S)z�
        Enable continuous monitoring for given URL
        :param url: URL to monitor
        :param email: email to send reports to
        :return: JSON encoded result of enable action
        �enable�r�r�)r�r@rgrsr�)rCr�re�d_infos    rD�enable_continuouszBaseManager.enable_continuous�sM���)�)�#�.�.���"�)�)�&�+�s�E�B�B�B��'�'�x�S�'�A�A�ArFc��t|��\}}|j�|��|�d|���S)z�
        Disable continuous monitoring for given URL
        :param url: URL to monitor
        :return: JSON encoded result of disable action
        rrh)r!r@rr�r�s    rD�disable_continuouszBaseManager.disable_continuous�sC��#�3�����Q��"�*�*�;�7�7�7��'�'�y�c�'�B�B�BrFc��t|��\}}|j�|��|�d|���S)z�
        Start continuous monitoring for given URL
        :param url: URL to monitor
        :return: JSON encoded result of start action
        rrh)r!r@rr�r�s    rD�start_continuouszBaseManager.start_continuoussC��#�3�����Q��"�(�(��5�5�5��'�'�w�C�'�@�@�@rFc��t|��\}}|j�|��|�d|���S)z�
        Stop continuous monitoring for given URL
        :param url: URL to monitor
        :return: JSON encoded result of stop action
        r,rh)r!r@r,r�r�s    rD�stop_continuouszBaseManager.stop_continuoussC��#�3�����Q��"�'�'��4�4�4��'�'�v�3�'�?�?�?rFc�b�|j���}|�d|���S)zq
        Get list of continuous monitoring tasks
        :return: JSON encoded result of get list action
        zget continuous list�r��data)r@�get_tracing_listr�)rC�tracing_lists  rD�continuous_tracing_listz#BaseManager.continuous_tracing_lists/��
�1�B�B�D�D���}�}�$9��}�M�M�MrFc�^�|�d|j������S)z#
        Get list of tasks
        z
tasks-listrr)r�rBr[rTs rD�
tasks_listzBaseManager.tasks_lists)���}�}�L�t�7I�7W�7W�7Y�7Y�}�Z�Z�ZrFr�	time_from�time_toc���|�|j|���}|���}|�d|j�|||������S)z@
        Get list of requests for given tracing task id
        r%z
requests-list)ryrzrr)r;r<r-r�rB�get_request_list)rCrryrzrr�s      rD�
requests_listzBaseManager.requests_list$sg��
�&�&���g�&�V�V���O�O�����}�}�"��#�4�4�W�	�[b�4�c�c��
�
�	
rFc��|�|j|���}|���}|�d|j�|�����S)zE
        Get hourly requests stats for given tracing task id
        r%zrequests-stats-by-hourrr)r;r<r-r�rB�get_requests_stats_by_hour)rCrrr�s    rD�requests_stats_by_hourz"BaseManager.requests_stats_by_hour0s_��
�&�&���g�&�V�V���O�O�����}�}�+�$�2D�2_�2_�`g�2h�2h��
�
�	
rF�
request_idc���|�|j|���}|���}|�d|j�||�����S)zO
        Get collected statistics for request ID of given tracing task
        r%zrequest-datarr)r;r<r-r�rB�get_request_data)rCrr�rr�s     rD�request_datazBaseManager.request_data;sX��
�&�&���g�&�V�V���O�O�����}�}�N��9K�9\�9\�]d�fp�9q�9q�}�r�r�rrFc�`�|j���|�d���S)a
        Enable X-Ray User Agent:
            start or restart service if it is accidentally already running
        For systemd systems -- start socket unit only
        For SysV -- start the entire service
        :return: JSON encoded result of enable action
        zenable-user-agentrZ)rArgr�rTs rD�enable_user_agentzBaseManager.enable_user_agentDs.��	
��%�%�'�'�'��}�}�$7�}�8�8�8rFc�`�|j���|�d���S)a7
        Disable X-Ray User Agent:
             stop the entire service
             or do nothing if it is accidentally not running
        For systemd systems -- also check if socket unit is running
                               and stop it too
        :return: JSON encoded result of disable action
        zdisable-user-agentrZ)rArr�rTs rD�disable_user_agentzBaseManager.disable_user_agentOs.��	
��&�&�(�(�(��}�}�$8�}�9�9�9rFc�|�|j���}|�d|t�����S)zn
        Get status of X-Ray User Agent service
        :return: JSON encoded result of status action
        zuser-agent-status)r�r.�user_nginx_cache)rAr.r�r&)rC�agent_statuss  rD�user_agent_statuszBaseManager.user_agent_status[s8��
�-�4�4�6�6���}�}�$7��_o�_q�_q�}�r�r�rrFc�*�t��}|ddur|���nS|ddur|���n4|ddur*|���}|�|���S|���S)�]
        Advanced metrics tool
        :return: JSON encoded result of status action
        rgTrr.)r.)r#rgrr.r�)rC�args�amr.s    rD�advanced_metricszBaseManager.advanced_metricscs���
�
�
����>�T�!�!��I�I�K�K�K�K�
�)�_��
$�
$��J�J�L�L�L�L�
�(�^�t�
#�
#��Y�Y�[�[�F��=�=��=�/�/�/��}�}���rFc�b�t��t��|���S�r�)r1r-r��rCr�s  rD�enable_serverwide_modez"BaseManager.enable_serverwide_modess*��
	&�'�'�'������}�}���rFc�b�t��t��|���Sr�)r.r0r�r�s  rD�disable_serverwide_modez#BaseManager.disable_serverwide_mode}s*��
	����%�'�'�'��}�}���rF)F)NN)I�__name__�
__module__�__qualname__�__doc__rJrSr��boolrErrKrQr�rUrXrr^rgrmrarxrv�re�compiler�r�r��Processr�r�rw�staticmethodrr�rrr�r�rpr�rur	r�r�r�r�r%rrr#r,r/rKrMrXr^r r7rdrjrlrnrprvrxr}r�r�r�r�r�r�r�r�r�rFrDr4r4Ys��������/�N�0�/�/�/�/�/�/�/�/�/�/�/�/�/���H�"	H�	H�#�	H�T�	H�	H�	H�	H�G��#��G�G�G�G��D�����8��8��8�8�8�8�L�Z�L�C�L�L�L�L��
��s�����*
�:�
�#�
�
�
�
�A�z�A�d�A�A�A�A�1�J�1�4�1�1�1�1�[�[�[�"�B�J�?�@�@�M�[���f�n�������8�c��d�����2��c��h�v�~�&>�����\��
L�c�
L�d�
L�
L�
L�
L�1�s�1�z�1�1�1�1�Y�3�Y�:�Y�Y�Y�Y�l�:�l�$�l�l�l�l�^��^��^�^�^�^�?�J�?�4�?�?�?�?�,�#��#��s�C�x��#�#�#��\�#�U�C�U�D�U�U�U�U�,�	9�c�	9�	9�	9��\�	9�'�s�'�'�'�'�� �!�CS�CS�CS��CS��	CS�
�CS��
CS��CS��CS�

�CS�CS�CS���CS�Ji��i��i�i�i�i�CE�
�
�
��
�C�
��
�
�
�
�$NR�C�NR�C�NR�NR�NR�NR�`A�A�A�2� V�� V�� V� V� V��� V�DV��V��V�V�V�V���d��t�����\��,
:�C�
:�
:�
:�
:��
�$�
��
�
�
��\�
�T�c�T�c�T�T�T�T�B�S�B��B��B�B�B�B�C�c�C�c�C�C�C�C�A�C�A�C�A�A�A�A�@�3�@�3�@�@�@�@�N��N�N�N�N�[�C�[�[�[�[�

�

�S�

�S�

�#�

�Y\�

�

�

�

�	
�c�	
�c�	
�	
�	
�	
�s�C�s�S�s�S�s�s�s�s�	9�3�	9�	9�	9�	9�
:�C�
:�
:�
:�
:�s�3�s�s�s�s������� �c������s������rFr4)Ur�r�r=r~r|r�r�r�r��typingrrrrr�rrr	r��clcommon.constr
�clcommon.cpapirr�!xray.internal.clwpos_safe_importsr
r�clcommon.utilsr�clcommon.clpwdr�secureior�xrayrr��adviser.clwpos_getr�	apiclientr�continuous.managerr�internal.exceptionsrrrrr�internal.fpm_utilsr�internal.local_countersr�internal.nginx_utilsr�internal.typesrr r!�internal.user_managerr"�internal.advanced_metricsr#�internal.user_plugin_utilsr$r%r&�internal.utilsr'r(r)r*r+r,�reconfiguration.global_inir-r.r/r0r1�
TYPE_CHECKING�apiclient.api_clientr2r�r�r4r�rFrD�<module>r�s`����
��������	�	�	�	�
�
�
�
�	�	�	�	�
�
�
�
�
�
�
�
�����
�
�
�
�(�(�(�(�(�(�(�(�,�,�,�,�,�,�,�,�,�,�,�,�,�,�,�,�,�,�
�
�
�
�"�"�"�"�"�"�Q�Q�Q�Q�Q�Q�Q�Q�W�W�W�W�W�W�W�W�/�/�/�/�/�/�*�*�*�*�*�*�"�"�"�"�"�"�������-�-�-�-�-�-�"�"�"�"�"�"�2�2�2�2�2�2���������������5�4�4�4�4�4�8�8�8�8�8�8�1�1�1�1�1�1�8�8�8�8�8�8�8�8�8�8�3�3�3�3�3�3�7�7�7�7�7�7�c�c�c�c�c�c�c�c�c�c�������������������������������
��1�0�0�0�0�0�0�""�r�z�"J�K�K��l�l�l�l�l�l�l�l�l�lrF