Hacked By AnonymousFox

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

�

)�fF?���ddlmZddlZddlZddlZddlZddlZddlZddlZddl	Z	ddl
mZmZ
ddlmZddlmZddlmZmZddlmZddlmZdd	lmZdd
lmZddlmZddlmZm Z m!Z!dd
l"m#Z#m$Z$ddl%m&Z'ddl(m)Z*ddl+m,Z,ddl-m.Z.m/Z/da0dZ1dZ2dZ3d�Z4d�Z5d�Z6dDd�Z7Gd�de8��Z9e1fd�Z:dEd�Z;d�Z<d�Z=Gd �d!e��Z>d"�Z?d#�Z@dFd%�ZAdFd&�ZBd'�ZCd(�ZDdGd*�ZEdFd+�ZFd,�ZGdEd-�ZHdEd.�ZIdEd/�ZJdEd0�ZKd1�ZLd2�ZMd3�ZNGd4�d5eO��ZPd6�ZQd7�ZRd8�ZSd9�ZTdFd:�ZUd;�ZVd<eWd=eXfd>�ZY			dHd?eZeZed@eZeWdzdAe[dBe[d=eWf
dC�Z\dS)I�)�absolute_importN)�gethostname�error)�Any)�urlparse)�minidom�DOMException)�
ExpatError)�PrettyTable)�FormattedException)�cpapi)�LIMIT_LVP_ID)�CPAPIExternalProgramFailed�NotSupported�ParsingError)�
MySQLGovernor�MySQLGovException)�engine)�mkdir��uid_max)�get_max_lve_id�USER_NOTIFICATIONS_OFF_MARKERz/proc/lve/listz"/var/run/mysql-governor-config.xmlz!/etc/container/mysql-governor.xmlc�:�t�t��atS)z'
    Get current maximum of LVE ID
    )�
MAX_LVE_IDr����/builddir/build/BUILDROOT/lve-stats-4.2.8-1.el8.x86_64/opt/cloudlinux/venv/lib/python3.11/site-packages/lvestats/lib/commons/func.py�get_current_max_lve_idr)s��
��#�%�%�
��rc��|r	|||z}}|�	|S�Nr)�a�bs  r�gcdr$3s&��
���!�a�%�1�����Hrc�V�t��}|D]}|�|���|S)z�
    >>> merge_dicts({1: 2}, {3: 4})
    {1: 2, 3: 4}
    >>> merge_dicts({1: '1', 2: '2'}, {2: 'two', 3: 'three'})
    {1: '1', 2: 'two', 3: 'three'}
    )�dict�update)�dicts�
dic_merged�dic_s   r�merge_dictsr+:s9�����J�� � �����$������r��c#�jK�tdt|��|��D]}||||z�V��dS)zS
    :type chunk_length: int
    :type _list: list
    :rtype: Generator[list]
    rN)�range�len)�_list�chunk_length�is   r�
get_chunksr3GsQ�����1�c�%�j�j�,�
/�
/�(�(���A�a�,�&�&�'�'�'�'�'�(�(rc��eZdZdd�ZdS)�LVEVersionError�c�b�t�|d�|����dS)NzCan't detect LVE version; {})�	Exception�__init__�format)�self�messages  rr9zLVEVersionError.__init__Rs-�����4�!?�!F�!F�w�!O�!O�P�P�P�P�PrN)r6)�__name__�
__module__�__qualname__r9rrrr5r5Qs.������Q�Q�Q�Q�Q�Qrr5c��	t|d��5}|�d��}	t|�d��d��cddd��S#tt
tf$r$td�||�����wxYw#1swxYwYdS#ttf$r}t|���d}~wwxYw)z0
    :param str _lve_file:
    :return int:
    �r��:rNz$error parsing line '{}' from file {})�open�read�int�split�	TypeError�
IndexError�
ValueErrorr5r:�OSError�IOError)�	_lve_file�lve_file_stream�lve_ver_str�es    r�get_lve_versionrQVs?��
!�
�)�S�
!�
!�	m�_�)�.�.�q�1�1�K�
m��;�,�,�S�1�1�!�4�5�5�	m�	m�	m�	m�	m�	m�	m�	m���z�:�6�
m�
m�
m�%�&L�&S�&S�T_�aj�&k�&k�l�l�l�
m����		m�	m�	m�	m����	m�	m�	m�	m�	m�	m��
�W��!�!�!��a� � � �����!���sK�B(�B�'A�B(�;B�B�B�B(�"B�#B(�(C
�9C�C
c�z�|p
t��}	|���}n#t$rd}YnwxYw|pdS)a
    Parse governor config file and return governor mode
    :type governor: MySQLGovernor
    :rtype: str
    :return:
        if file exists and correct: "on", "single", "off", "abusers" or "all"
        if governor is not installed or error occurs: "none"
    N�none)r�get_governor_moder)�governor�
governor_modes  rrTrTfsY���*�=�?�?�H�� �2�2�4�4�
�
�������
�
�
������"�F�"s�'�6�6c��tj�d��sdStj�t��rt}nt}|S)z
    :rtype: str|None
    z/usr/sbin/db_governorN)�os�path�exists�GOVERNOR_CONFIG�GOVERNOR_CONFIG_OLD)�governor_config_paths r�get_governor_config_pathr^wsK��
�7�>�>�1�2�2���t�	�w�~�~�o�&�&�3�.���2���rc�d�t��}|�dS	tj|��}|�d��D]S}	|jdj|kr*|jdj���dkcS�D#t$rY�PwxYwn#tttf$rYdSwxYwdS)z3
    :rtype: bool|None
    :type username: str
    N�user�name�mode�ignoreF)r^r�parse�getElementsByTagName�
attributes�value�lower�KeyErrorrLr
r	)�usernamer]�governor_configr`s    r�get_governor_ignore_for_userrl�s���
4�5�5���#��t�
�!�-�(<�=�=��#�8�8��@�@�	�	�D�
��?�6�*�0�H�<�<��?�6�2�8�>�>�@�@�H�L�L�L�L�=���
�
�
���
����	��
�Z��.�����t�t������5s;�+B�=B�=B�B�
B�B�
B�B�B-�,B-c��eZdZdS)�DomainExceptionN)r=r>r?rrrrnrn�s�������Drrnc�F�t��}|���S)z/
    :rtype: (str, MySQLGovException|None)
    )r�get_governor_status)�mysql_governors rrprp�s��#�_�_�N��-�-�/�/�/rc��	tj|��S#tttf$rt��cYSwxYw)z�
    Get dict[user, domain], empty if cannot obtain.
    :param reseller_name: reseller's name
    :rtype: dict[str, str|None]
    )r
�reseller_domainsrr�AttributeErrorr&)�
reseller_names r�get_reseller_domainsrv�sG����%�m�4�4�4��&��n�E�����v�v�
�
�
����s��%>�>Tc��	tj||��}|S#ttttjjtjjtf$r*}|rtt|�����gcYd}~Sd}~wwxYw)z�
    Get list of aliases for specified user's domain, or empty list.
    :type username: str
    :type domain: str
    :type raise_exception: bool
    :raises DomainException: if cannot obtain domains
    :rtype: list[str]|None
    N)r
�useraliasesrirLrH�cpapiexceptionsr�NoPanelUserrrn�str)rj�domain�raise_exception�user_aliasesrPs     r�get_aliasesr�s�����(��6�:�:�����

�g�y�%�*?�*L��!�-�|�=�����	*�!�#�a�&�&�)�)�)��	�	�	�	�	�	�����	���s��=B�A;�5B�;Bc��	tj|��}d�|D��S#ttttjjtjjtf$r*}|rtt|�����gcYd}~Sd}~wwxYw)z�
    Get list of domains for specified user, or None.
    :type username: str
    :type raise_exception: bool
    :raises DomainException: if cannot obtain domains
    :rtype: list[str]|None
    c��g|]
}|d��S)rr)�.0r|s  r�
<listcomp>zget_domains.<locals>.<listcomp>�s��5�5�5�f��q�	�5�5�5rN)r
�userdomainsrirLrHryrrzrrnr{)rjr}�user_domainsrPs    r�get_domainsr��s���6��(��2�2��6�5��5�5�5�5��

�g�y�%�*?�*L��!�-�|�=�����	*�!�#�a�&�&�)�)�)��	�	�	�	�	�	�����	���s�"�=B	�B�>B	�B	c�Z�t|��}t|��dkrdS|dS)z�
    :param username: user name string
    :return:
        if not supported in CP, or domain name is not set: None
        if we can find it: domain name string
    :rtype: str|None
    :type username: str
    rN)r�r/)rjr�s  r�
get_domainr��s3���x�(�(�L�
�<���A����t���?�rc��|���}|�d��|�d|z��|���dS)zP
    Create database
    :param db_engine:
    :param db_name:
    :return:
    �COMMITzCREATE DATABASE %sN)�connect�execute�close)�	db_engine�db_name�conns   r�
init_databaser��sP�������D��L�L������L�L�%��/�0�0�0��J�J�L�L�L�L�Lr�
tmp_unittest_c�|�|ttj����dd�z}t||��|S)z�
    Create database with random prefix in name
    Using for unittests
    :param db_engine:
    :param str prefix:
    :return str:
    r�)r{�uuid�uuid4r�)r��prefixr�s   r�init_database_rndr��s:���s�4�:�<�<�(�(��1��-�-�G��)�W�%�%�%��Nrc�"�t|��}|jdd�}|�r�|jdkrtj|��dStj|jdz|jz��}|���}|r�d|j	vr�|�
d|z��|�
d�����d	d	}|����
d
��d	}ttt |�
d������dd
gkr|�
d|z��nz|�
d|z��na|r_d|j	vrV|�
d��}|���D],}|d|kr|�
d|d	z���-|�
d��|�
d|z��|���dSdS)z�
    Drop database (support MySQL and PostgresSQL)
    :param uri:
    :param force: kill all connection and locked session before drop database
    :return:
    �N�sqliteTz://�
postgresqlzBUPDATE pg_database SET datallowconn = 'false' where datname = '%s'zSHOW server_versionr� �.�	�zOSELECT pg_terminate_backend(procpid) FROM pg_stat_activity WHERE datname = '%s'zKSELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname = '%s'�mysqlzSHOW PROCESSLIST�zKILL %sr�zDROP DATABASE %s)rrY�schemerX�unlink�sqlalchemy_engine�
create_engine�netlocr�rar��fetchall�striprG�list�maprFr�)	�uri�force�uri_r�r�r��pg_ver�cursor�fetcheds	         r�
drop_databaser�s���C�=�=�D��i����m�G����;�(�"�"��I�g�����4�%�3�D�K�%�4G�$�+�4U�V�V�	�� � �"�"���	9�\�Y�^�3�3��L�L�]�`g�g�h�h�h��\�\�"7�8�8�A�A�C�C�A�F�q�I�F��\�\�^�^�)�)�#�.�.�q�1�F��C��V�\�\�#�.�.�/�/�0�0�A�q�6�9�9����n�qx�x�y�y�y�y����j�mt�t�u�u�u�u�
�	9�w�)�.�0�0��\�\�"4�5�5�F�!�?�?�,�,�
9�
9���1�:��(�(��L�L��W�Q�Z�!7�8�8�8�����X�������'�'�1�2�2�2��
�
�����t�9�rc���d}tj�|��sdSt|��5}|���cddd��S#1swxYwYdS)Nz/etc/redhat-releaser6)rXrYrZrD�readline)rY�release_files  r�
_release_liner�,s��� �D�
�7�>�>�$�����r�	
�d���'�|��$�$�&�&�'�'�'�'�'�'�'�'�'�'�'�'����'�'�'�'�'�'s�A�A�Ac�f�|�t��}|���}d|vr|�d��dz}nt��S||}t��}|�d��D]5}	|�t|�����&#t$rY�2wxYw|S)a�
    Return CloudLinux version as list [major, minor]
    or [] if can't obtain version
    :param str release_line:
    :return list: splited version
    >>> cl_ver('CloudLinux Server release 5.11 (Vladislav Volkov)')
    [5, 11]
    >>> cl_ver('CloudLinux Server release 6.8 (Oleg Makarov)')
    [6, 8]
    >>> cl_ver('CloudLinux release 7.2 (Valeri Kubasov)')
    [7, 2]
    >>> cl_ver('Incorrect release line')
    []
    N�releaser�r�)r�rG�indexr��appendrFrJ)�release_line�release_line_splited�ver_indx�ver_str�ver�ss      r�cl_verr�4s�����$����'�-�-�/�/���(�(�(�'�-�-�i�8�8�1�<����v�v�
�"�8�,�G�
�&�&�C�
�]�]�3�
�
����	��J�J�s�1�v�v�������	�	�	��D�	�����Js�>"B!�!
B.�-B.c�J��tjd���}�fd�|D��S)N)�cplogin�reseller)�keylsc�&��g|]
\}}�|k�|��Srr)r�r��	reseller_r�s   �rr�z#_reseller_users.<locals>.<listcomp>Vs(���Y�Y�Y�*���8�y�CX�CX�G�CX�CX�CXr)r
�cpinfo)r��cplogin_reseller_s` r�_reseller_usersr�Ts2�����+B�C�C�C��Y�Y�Y�Y�.?�Y�Y�Y�Yrc��ttdt��}	||��S#tjj$rt��cYSwxYw)N�reseller_users)�getattrr
r�ry�CPAPIExceptionr�)r�r�s  r�get_users_for_resellerr�YsW���U�$4�o�F�F�N���~�h�'�'�'��� �/�����v�v�
�
�
����s�
(�"A
�A
c��t|d��5tj||��ddd��n#1swxYwYtj|d��dS)z�
    The equivalent console touch command
    :param str fname: path to file
    :param None| tuple times: (atime, mtime) if None atime and mtime are default
    r"N�)rDrX�utime�chmod)�fname�timess  r�touchr�as���
�e�S�	�	���
��������������������������H�U�E�����s�3�7�7c�2���fd�}t||��dS)z2
    :type data: list
    :type filename: str
    c�Z��tj|��}|����dS)z:type csv_file: fileN)�csv�writer�	writerows)�csv_filer��datas  �r�	csv_writez#atomic_write_csv.<locals>.csv_writeqs,�����H�%�%����������rN��
_atomic_write)�filenamer�r�s ` r�atomic_write_csvr�ls4���
������(�I�&�&�&�&�&rc�2���fd�}t||��dS)z1
    :type data: str
    :type filename: str
    c�2��|����dS)z:type str_file: fileN)�write)�str_filer�s �r�	str_writez#atomic_write_str.<locals>.str_write}s������t�����rNr�)r�r�r�s ` r�atomic_write_strr�xs4���
������(�I�&�&�&�&�&rc� �d\}}d}tjddtj�|�����5}|j}||��|���t|��D]W}	tj|�	����n/#t$r }|}tj|��Yd}~�Pd}~wwxYw|�	ddd��n#1swxYwYtj
|d��tj||��dS)zF
    :type write_function: (file) -> None
    :type filename: str
    )r�g333333�?N�wF)�delete�dirr�)�tempfile�NamedTemporaryFilerXrY�dirnamera�flushr.�fsync�filenorK�time�sleepr��rename)	r��write_function�retry_count�retry_interval�exc�f�tmp_filename�_rPs	         rr�r��so��
#)��K��
�C�	�	$�S�,1�)+�����)B�)B�
D�
D�
D��GH��v����q����	���	�	�	��{�#�#�	�	�A�
+��������$�$�$�����
+�
+�
+����
�>�*�*�*�*�*�*�*�*�����
+�����I�������������������� �H�\�5�!�!�!��I�l�H�%�%�%�%�%s;�8C�5&B�C�
C�'C�=C�C�C�C� Cc�8�eZdZdd�Zed���Zd�Zd�ZdS)�reboot_lockNc���||_|�t���|_n||_t	j|jtjtjzd��|_dS)Ni�)	�timeoutr��_build_lock_filenamer�rXrD�O_WRONLY�O_CREAT�lock)r;r�rs   rr9zreboot_lock.__init__�sN������=�$�9�9�;�;�D�J�J��D�J��G�D�J���b�j�(@�%�H�H��	�	�	rc��d}tj�|��st|��tj�|d��S)Nz/var/lvez
lvestats.lock)rXrYrZ�mkdir_p�join)�
lock_file_dirs rrz reboot_lock._build_lock_filename�sA��"�
��w�~�~�m�,�,�	#��M�"�"�"��w�|�|�M�?�;�;�;rc�n�tj��}		tj|jtjtjz��dS#t$r`}|jtjkr�|j	�%tj��|z
|j	krYd}~dStj
d��Yd}~nd}~wwxYw��)NTg�������?)r��fcntl�flockr�LOCK_EX�LOCK_NBrL�errno�EAGAINrr�)r;�
start_timerPs   r�	__enter__zreboot_lock.__enter__�s����Y�[�[�
�	$�

$���D�I�u�}�u�}�'D�E�E�E�����
$�
$�
$��7�e�l�*�*���|�/�D�I�K�K�*�4L�t�|�4[�4[��������J�s�O�O�O�O�O�O�O�O�����
$����		$s�1A	�	
B3�<B.�B.�.B3c���tj|jtj��t	j|j��	t	j|j��dS#YdSxYwr!)r
rr�LOCK_UNrXr�r�r�)r;�typerg�	tracebacks    r�__exit__zreboot_lock.__exit__�sW��
��D�I�u�}�-�-�-�
�������	��I�d�j�!�!�!�!�!��	��D�D���s�A�A)NN)r=r>r?r9�staticmethodrrrrrrr�r��se������I�I�I�I��<�<��\�<�
$�
$�
$�����rr�c�j�|t��kr|tkr|t��z
dfS|dfS)z�
    Extract container_id and bool(is_reseller) from serialized value;
    :type serialized_value: int
    :rtype: tuple[int, bool]
    TF)rr)�serialized_values r�deserialize_lve_idr�sC���0�2�2�2�2�7G�<�7W�7W��"8�":�":�:�D�@�@��U�"�"rc�J�|tkr|dkrt��|zS|S)zm
    Get serialized int by lvp_id and lve_id
    :type lvp_id: int
    :type lve_id: int
    :rtype: int
    r)rr)�lve_id�lvp_ids  r�serialize_lve_idr�s-������&�A�+�+�%�'�'�&�0�0��Mrc�J�	t��}n#t$rd}YnwxYw|S)ze
    Get hostname using socket.gethostname;
    Return 'N/A' if some error happens;
    :return:
    zN/A)r�SocketError)�hostnames r�get_hostnamer"�s;����=�=�����������������Os�� � c�&�|t��kS)zZ
    Returns is user should be skipped due to set max uid
    :param user_id: user id
    r)�user_ids r�skip_user_by_maxuidr%�s��
�W�Y�Y��rc��t|d���}g}|r|D]}t||d���}|r||z
}�tt||z����S)z4
    Returns ALL user domains: main/sub/aliases
    F)r})r�rr��set)rj�include_aliases�domains�aliasesr|�aliass      r�get_all_user_domainsr,�st���(�E�:�:�:�G��G��!��	!�	!�F���&�%�H�H�H�E��
!��5� �����G�g�%�&�&�'�'�'rc�D�|�d��r
|dd�}|S)z+
    Cuts extra prefix www from domain
    zwww.�N)�
startswith)r|s r�normalize_domainr0s,������ � ���������Mrr`�returnc��tj�tj�t	j|��jdt����S)zL
    Checks if notifications are turned off by user: marker file exists
    z	.lvestats)rXrYrZr�pwd�getpwnam�pw_dirr)r`s r�user_should_be_notifiedr6
sM���w�~�~�b�g�l�l�3�<��+=�+=�+D�k�+H�J�J�K�K�K�Kr�rows�fields�left_padding�
padding_widthc��|pg}t|||���}|D]}d|j|<�
|�|��|���S)z<
    Prepare rows and columns names to print in console
    )r9r:�l)r�align�add_rows�
get_string)r7r8r9r:�table�fields      r�get_ascii_tablerBsm���\�r�F���%1�&3�
5�
5�
5�E��!�!�� ���E���	�N�N�4���������r)r,r!)T)r�)Nrr)]�
__future__rr�rXr3r�r�r�r
r�_socketrrr �typingr�urllib.parser�xml.domrr	�xml.parsers.expatr
�prettytabler�clcommonrr
�clcommon.clprocr�clcommon.cpapi.cpapiexceptionsrrr�clcommon.librr�
sqlalchemyrr��clcommon.clcaptainrr�clcommon.clfuncr�lvestats.lib.configrrr�LVE_FILEr[r\rr$r+r3r8r5rQrTr^rlrnrprvrr�r�r�r�r�r�r�r�r�r�r�r�r��objectr�rrr"r%r,r0r{�boolr6r�rFrBrrr�<module>rUs���'�&�&�&�&�&�
�
�
�
�	�	�	�	�
�
�
�
���������������������5�5�5�5�5�5�5�5�������!�!�!�!�!�!�)�)�)�)�)�)�)�)�(�(�(�(�(�(�#�#�#�#�#�#�'�'�'�'�'�'�������(�(�(�(�(�(�a�a�a�a�a�a�a�a�a�a�9�9�9�9�9�9�9�9�2�2�2�2�2�2�/�/�/�/�/�/�#�#�#�#�#�#�M�M�M�M�M�M�M�M�
�
���6��9�����
�
�
�
�
�
�(�(�(�(�Q�Q�Q�Q�Q�i�Q�Q�Q�
'�
!�
!�
!�
!� #�#�#�#�" � � ����,	�	�	�	�	�(�	�	�	�0�0�0�	�	�	�����(6�6�6�6�&���
�
�
�
�
�
�
�%�%�%�%�P'�'�'�����@Z�Z�Z�Z�
��������	'�	'�	'�'�'�'�&�&�&�8&�&�&�&�&�&�&�&�&�R	#�	#�	#�
�
�
�
�
�
����(�(�(�(����K�#�K�$�K�K�K�K�04�()�)*���$�t�C�y�/�� ��I��,��"%��$'��03������r

Hacked By AnonymousFox1.0, Coded By AnonymousFox