%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /opt/alt/python37/lib/python3.7/site-packages/clsummary/__pycache__/
Upload File :
Create Path :
Current File : //opt/alt/python37/lib/python3.7/site-packages/clsummary/__pycache__/summary.cpython-37.pyc

B

�2Lb���@s�ddlmZddlmZddlZddlZddlmZddlZddlZddl	Z	ddl
Z
ddlZddlZddl
Z
ddlZddlZddlZddlZddlmZmZddlmZmZddlmZddlmZdd	lmZdd
lmZddlm Z ddl!m"Z"ddl#Z#ddl$Z%ddl&Z&dd
l'm(Z(ddl)m*Z*ddl+m,Z,ddl-m.Z.m/Z/m0Z0m1Z1m2Z2ddl3m4Z4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z:m;Z;m<Z<ddl=m>Z>m?Z?m@Z@ddlAmBZBddlCmDZDmEZEmFZFddlGmHZHmIZIddlJmKZKmLZLddlMmNZNddlOmPZPddlQmRZRddlSmTZTmUZUmVZVmWZWmXZXmYZYmZZZm[Z[ddl\m]Z]m^Z^m_Z_m`Z`maZambZbmcZcmdZdmeZemfZfmgZgmhZhmiZimjZjmkZkddllmmZmddlnmoZompZpmqZqddlrmsZsdd ltmuZvdd!lwmxZxmyZydd"lzm{Z{dd#l|m}Z}yTdd$l~mZm�Z�m�Z�m�Z�m�Z�m�Z�dd%l�m�Z�dd&l�m�Z�dd l�muZ�dd'l�m�Z�Wn>e�k
�rNeeZ�eeZ�eeZ�eeZ�eeZeeZ�eeZ�dZ�dZ�dZ�YnXd(Z�e	��d)�Z�d*Z�d+Z�d,Z�d-Z�d.Z�d/Z�d0Z�d1Z�Gd2d3�d3e��Z�e�e�d4�d5d6�Z�Gd7d8�d8e��Z�d9d:�Z�dS);�)�absolute_import)�divisionN)�range)�	lru_cache�partial)�Counter�defaultdict)�groupby)�	iteritems)�
attrgetter)�	cpu_count)�getfqdn)�Path)�cpapi)�Feature)�
MySQLGovernor)�is_cl_solo_edition�is_cl_shared_edition�
CLEditions�	is_ubuntu�is_secureboot_enabled)	�get_rhn_systemid_value�run_command�ExternalProgramFailed�is_testing_enabled_repo�is_litespeed_running�get_cl_version�
get_virt_type�grep�is_nginx_running)�
find_wp_paths�get_wp_cache_plugin� get_wp_paths_with_enabled_module)�
SysCtlConf)�cagefs_statistics_config�db_governor_lib�clconfig_utils)�print_dictionary�replace_params)�LimitsDataStorage�DEFAULTS)�EMPTY_LIMITS)�LvpMap�)�parse_cloudlinux_summary_opts)�Dict�Optional�Callable�Union�List�Any�Tuple�AnyStr)�is_statistic_enabled� set_statistic_collection_enabled�$write_statistics_send_status_to_file�
SummaryStatus�is_python_selector_installed�is_ruby_selector_installed�is_nodejs_selector_installed�is_php_selector_installed�dummy_none_function� get_packages_with_lve_extensions�is_sending_process_running�$get_statistics_send_status_from_file�get_client_data_from_jwt_token�get_cl_plus_sender_status�is_active_cloudlinux_license)�get_rpm_packages_info)�get_cpu_metrics�get_memory_metrics�NotSupported)�ALL_MODULES)�PKG_VERSION)�CONFIG_PATH�_read_config_file)�is_email_notification_enabled)�get_hidepid_typing_from_mounts)�get_versions_statistics�get_native_version_safe�get_php_selector_usage�iter_server_applications�get_default_php_version�get_mode_of_php_selector)�CloudlinuxSelectorLib)�get_default_version)�PKG_RELEASEz/var/log/cloudlinux-summary.logzcloudlinux-summary�unknownZ	installedZ
not_installedZnot_initializedZnot_selected�enabledZdisabledz-42c
@sLeZdZdZddddgdgddiddiddigd�	Zddd�Zd
d	d
�ZdS)�StatisticsDictz>
    Special class to store all metrics before sending it
    z-42i���gE�)	�str�int�float�str_list�int_list�int_dictZ
float_dict�str_dict�
rpm_stat_listTc
CsR|j|}y
|�}Wn8tk
rL}z|r8t�d||�|}Wdd}~XYnX|S)a
        Method for catching any exceptions while calling passed function,
        logging them and return default result in case if exception is present or
        return formatted result in case if exception is absent
        :param log_exception: True - exception will been logged, False - exception won't been logged
               This flag must be used if param `func` is function, which processes intermediate value, which can equal
               to -42 (it means that exception was logged in previous moment) and
               doesn't call external functions or doesn't process another values. In other cases
               log_exception should be equal True, because in calling of external function or processing of another
               value can be raised exception, which should be logged.
               Example: functions `lambda: intermediate_value[0].property` or
               [len(value) for value intermediate_value.values()] should be used with log_exception equal False,
               because they only process intermediate value. But function:

               def some_function(intermediate_value):
                    result = external_function()
                    return len(intermediate_value), result

               should be used with log_exception equal True
        z%s. Exception: "%s"N)�DEFAULT_RESULTS�	Exception�
app_logger�	exception)�self�func�type_of_result�log_message�
log_exceptionZdefault_result�result�err�ro�B/opt/alt/python37/lib/python3.7/site-packages/clsummary/summary.py�run_safe_or_log_errors�s

z%StatisticsDict.run_safe_or_log_errorscCs|j||||d�}|||<dS)z�
        Method which call `run_safe_or_log_errors` and save result from it
        :param log_exception: True - exception will been logged, False - exception won't been logged
               See method `run_safe_or_log_errors`
        )rlN)rq)rhrirjZname_of_metricrkrlrmrororp�
format_metric�szStatisticsDict.format_metricN)T)T)�__name__�
__module__�__qualname__�__doc__rdrqrrrorororpr[�s
&r[)�	site_dict�returncCsttdd�|d����S)zB
    Checks if wordpress site has at least one enabled module
    cSs|dS)NrZro)�modulerororp�<lambda>��z*site_has_enabled_modules.<locals>.<lambda>�modules)�any�map�values)rwrororp�site_has_enabled_modules�sr�c@s�eZdZdZdZe�dddge�e	j
�e�e	j�e�e	j�g�Z
dZdZdZd	d
�Zedd��Zed
d��Zedd��Zedd��Zedd��Zedd��Zdd�Zedd��Zdd�Zdd�Zedd ��Zed!d"��Zed#d$��Z d%d&�Z!d'd(�Z"ed)d*��Z#d+d,�Z$ed-d.�d/d0��Z%d1d2�Z&ed3d4��Z'ed5d6��Z(ee)e*e*fe+d7�d8d9��Z,d:d;�Z-d<d=�Z.ee/e0d-d>�d?d@��Z1e)e0e/fe0e+d-dA�dBdC�Z2dDdE�Z3edFdG��Z4dHdI�Z5dJdK�Z6edLdM��Z7dNdO�Z8d-d.�dPdQ�Z9dRdS�Z:edTdU��Z;dVdW�Z<dXdY�Z=dZd[�Z>ed\d]��Z?d^d_�Z@d`da�ZAdbdc�ZBddde�ZCdfdg�ZDdhdi�ZEdjdk�ZFdldm�ZGdndo�ZHeeIeJdp�dqdr��ZKeLeJeMeJeMe)eIe/ffd.�dsdt�ZNeIeIe)eIeIfe)eIeOeJeIffdu�dvdw�ZPeIeIdx�dydz�ZQeeRd-d{�d|d}���ZSd~d�ZTd�d��ZUed�d���ZVed�d���ZWed�d���ZXed�d���ZYed�d���ZZed�d���Z[d�d��Z\ed�d���Z]ed�d���Z^d�d��Z_ed�d���Z`ed�d���Zad�d��Zbed�d���Zcd�d��Zded�d���Zed�d��Zfed�d���Zged�d���Zhed�d���Ziee+d.�d�d���Zjee+d.�d�d���Zkee/d.�d�d���ZleeId.�d�d���Zmed�d���Znd�d��Zod�d��Zpe0eqe/d��d�d��Zrd�d��Zsd�d��Ztd�d��Zuee)d.�d�d„�Zvd�dĄZwd�dƄZxeLe/e/e/fd.�d�dȄZyeeLe/eIfd.�d�dʄ�Zzd�d̄Z{d�d΄Z|d�dЄZ}d�d҄Z~d�dԄZed�dք�Z�d�d؄Z�e�d�dڄ�Z�ed�d܄�Z�ed�dބ�Z�d�d�Z�d�d�Z�d�d�Z�ed�d�d��Z�ed�d�d��Z�ed�d��Z�ed�d��Z�ed�d��Z�ed�d��Z�ed�d���Z�d�d��Z�ed�d���Z�d-S)��CloudlinuxSummaryz/var/lve/dashboard_certificatez/etc/cl_plus/.disabled�python�ruby�nodejsz-https://stat-api.cloudlinux.com/api/clos-statz-https://stat-api.cloudlinux.com/api/rpm-statsz6https://repo.cloudlinux.com/static/cl-settings-v1.jsoncCs>t�|_d|_t�|_d|_d|_|��|_d|_	t
�|_dS)N)�dict�_opts�_security_tokenr[�
statistics�_lvpmap�
_system_id�_is_process_not_limited�is_process_not_limited�packages_by_lenr#�sysctl)rhrororp�__init__�s
zCloudlinuxSummary.__init__cCs,ttj�d��}ttj�d��}|o*|S)zm
        Return true if process is running outside LVE or it's not running by
        utility `nice`
        �RUNNING_BY_NICE�RUNNING_IN_LVE)�bool�os�environ�get)Zis_running_by_niceZis_running_in_lverororpr�sz)CloudlinuxSummary._is_process_not_limitedcCs|jdkrt�|_|jS)z.
        Load lvpmap only when needed
        N)r��_get_lvpmap)rhrororp�lvpmaps
zCloudlinuxSummary.lvpmapcCs|jdkrtd�|_|jS)N�	system_id)r�r)rhrororpr�s

zCloudlinuxSummary.system_idcCst�r
dSdS)NZubuntuZ	rhel_like)rrorororp�
_get_platform#szCloudlinuxSummary._get_platformcCst�r
tStS)N)r�ENABLED�DISABLEDrorororp�_detect_secureboot'sz$CloudlinuxSummary._detect_securebootcs,tjtj�d��fdd�td�D��}|S)N�c3s|]}t���VqdS)N)�randomZchoice)�.0�_)�range_for_random_choicerorp�	<genexpr>.sz=CloudlinuxSummary._generate_security_token.<locals>.<genexpr>�@)�stringZ
ascii_lettersZdigits�joinr)�security_tokenro)r�rp�_generate_security_token+sz*CloudlinuxSummary._generate_security_tokenc
Cs�i}|jdkr*d}t�|�|�d|i�d|j}t�|�|j|jd�}d}ytj|j|dd�}WnDtj	k
r�}z$t
|�}t�|�|�d|i�Wdd}~XYnX|js�d�|j
|j�}t�|�|�d|i�t�d�y2|��d}t|dt��r|d�|���Wn~ttfk
�rLd	}t�|�|�d|i�YnTtk
�r�}z*t�d
t
|�|j�|�ddti�Wdd}~XYnXt�d�|S)
NzSecurity token is emptyrmz!Getting statistics from server %s)r�r��<)�params�timeoutz*Server answer is: HTTP code {}; Reason: {}z!Received response from the serverz Can't parse api response to jsonz=Invalid json response from server, field %s not found in "%s"z1Invalid response from server. See %s for details.z,SUCCESS: received statistics from the server)r�rf�error�_error_and_exit�SUMMARY_URL�infor��requestsr��RequestExceptionr\�ok�format�status_code�reason�json�
isinstancer��update�_get_smart_advice_statistics�	TypeError�
ValueError�KeyError�text�LOG_FILE)rhZ	stat_data�messager��response�erororp�_get_remote_data1sP





 



z"CloudlinuxSummary._get_remote_datacCsL|jdk	r|jStj�|j�r*|��|_n|��}|�|�r@|nd|_|jS)N)r�r��path�isfile�DASHBOARD_CERTIFICATE�_read_token_from_filer��_write_token_to_file)rh�tokenrororpr�hs
z CloudlinuxSummary.security_tokenc
Cspy4t|jd��}|�|�WdQRXt�|jd�dSttfk
rj}zt�dt	|��dSd}~XYnXdS)z�
        Write security token to file and return success/fail status
        :param token: generated security token
        :return: T/F status
        �wNi�Tz,Error while writing secure token to file: %sF)
�openr��writer��chmod�IOError�OSErrorrfr�r\)rhr��fr�rororpr�tsz&CloudlinuxSummary._write_token_to_filec
Csby&t|j��}|����pdSQRXWn6ttfk
r\}zt�dt|��dSd}~XYnXdS)Nz.Error while reading file with secure token: %s)	r�r��read�stripr�r�rfr�r\)rhr�r�rororpr��sz'CloudlinuxSummary._read_token_from_filecCstjtjdddd�S)z�
        Detect old LVE limits integration presence according to
        https://docs.cloudlinux.com/index.html?lve_limits_with_packages.html
        :return: True/False - present/absent
        ZCUSTOM_GETPACKAGE_SCRIPT�=F)�	file_name�
param_nameZ	separatorZdefault_val)�detectZget_boolean_paramZCL_CONFIG_FILErorororp�_detect_old_lve_integration�s
	z-CloudlinuxSummary._detect_old_lve_integrationcCstj�d�S)zO
        Detects presence/absence of lsapi
        :return: True/False
        z/usr/bin/switch_mod_lsapi)r�r��existsrorororp�_is_lsapi_present�sz#CloudlinuxSummary._is_lsapi_presentcCs�|dkrt�sPtSn<|dkr*t�r&tStS|dkr>t�sPtSn|dkrPt�sPtSt|�}|dkrdtS|dkr�y|��dr~tStSt	k
r�tSXn|dkr�|�
�r�tStStd�|���dS)	zP
        Get selector status for nodejs, python, ruby and php selectors
        r�r�r��phpN)r�r�Zselector_enabledzUnknown interpreter: {})
r;�
NOT_INSTALLEDr<r�r=r>rVZget_selector_statusr�r�Zphp_selector_is_disabledr�r�)�interpreter�librororp�_get_status_of_selector�s*z)CloudlinuxSummary._get_status_of_selectorc
Cs�yt�|���}|Stjk
rX}z&t�dt|��|�dt|�i�Wdd}~XYn6tt	fk
r�}zt�dt|��dSd}~XYnXdS)Nz3Request exception while getting remote settings: %srmz'Error while parsing remote settings: %s)
r�r�r�r�rfr�r\r�r�r�)rhZsettings_url�settingsr�rororp�_get_remote_settings�s"z&CloudlinuxSummary._get_remote_settingsc
Cs�|j�d�rdSttj�d��}|r(dS|�|j�}|dkr@dSy"|dd}|ddob|�|�Stk
r�}z&t	�
d	t|��|�d
t|�i�Wdd}~XYnXdS)z-
        Return cl-statistics status
        z--force-collectTZCL_TEST_SYSTEMNFz
cl-statisticsz
rollout-grouprZz4Error occurred while trying to get rollout group: %srm)
r�r�r�r�r�r��SETTINGS_URL�
_match_serverr�rfr�r\r�)rhZis_test_environmentr�Z
rollout_groupr�rororp�_is_statistics_enabled�sz(CloudlinuxSummary._is_statistics_enabledcCs
t|d�S)N�)r])�hash_serverrororp�
_to_number�szCloudlinuxSummary._to_numbercCs@|jdkrdSt�|j�����dd�}|�|�d|dkS)NT��r)r��hashlibZsha256�encodeZ	hexdigestr�)rhZurl_numr�rororpr��s
zCloudlinuxSummary._match_serverN)rxcCs*d}x |r$t�s$|d8}t�d�qWdS)zC
        Wait for running background process of cl-summary
        �2r-g�������?N)rA�time�sleep)Zretriesrororp�_wait_for_background_process�sz.CloudlinuxSummary._wait_for_background_processcCs�|jds|jdr>t|jd�t��dd�}t|d�dS|jdr�t�rRdnd}t��|dd	�}|�d
t�i�t|d�td�|j�d�r�|�	�}|j
|t�d
�|j�d�r�|��|j�d�r�|j
|j|jdd�t�d�nt|jd�dS|���s2tjdt��d�}t|�|jddidd�|j�d��r�|j�d��s�t��s�tjdt��d�}t|�|jddidd�|j�d��r�|jdk�r�d}t�|�tjdtt��d�}t|�|�d|i�|j�d��rLt��r�t��ddd	�}n<ttjt��dd��tjddd d!�|��t��dd�}t|d�td�|jd�r�|j�r�tdd"��r�t��ddd	�}t|d�td�nttjt��dd��dS)#N�enable�disable�success)�	timestamprmT�statusZ
collectingZready)r�r�rmZsending_statusrz
get-remote)�datar7zrpm-packagesz--sendF)�summary�url�save_statuszRPM statistics sentzkStatistics collection is disabled globally. Please, try again later or contact support if it happens again.)rmr�r�rmzYCollecting statistics is disabled globally. Use --force-collect to ignore global settings)�
error_codez--force-collectz_Statistics collection is disabled by admin. Run `cloudlinux-summary enable` and then try again.zXSending statistics is disabled by admin. Use --force-collect to ignore admin`s settings.z8Statistics was not sent, because security token is emptyz�We are not able to collect statistics because we are not able to make a security token. Check %s for details or contact support.z--async)rmr�r�z9/usr/sbin/cloudlinux-summary --send --json &> /dev/null &z	/bin/bash)�shell�
executable)�acquire_lock)r�r8r�r'rAr�rB�exitr�r��_print_result_and_exitr7�_get_rpm_packages_summary� _send_statistics_and_save_statusr��RPM_PACKAGES_URLrfr�r�r:�FAILEDr9r�r�r�r�ZIN_PROGRESS�
subprocess�runr�r�)rhr�r�rm�status_dictr�rororp�&_actions_before_run_process_as_limited�s�











z8CloudlinuxSummary._actions_before_run_process_as_limitedc	Csjtjdddd�}t�|��F}|�dt|�g�tjddt|�dg|ttj	d	t
�tj�d
�d�SQRXdS)
z�
        Run same command in lve and set environ RUNNING_IN_LVE=1
        in order to check it in child process.
        :return:
        �ri@)Zls_cpuZls_cpusZ
ls_memory_phyz--lve-idz/bin/lve_suwrapperz-nz/usr/sbin/cloudlinux-summary�1)r�ZCPU_DATA)�envN)
�	lvectllibZmake_liblve_settingsZ
temporary_lve�extendr\r�callr�r�r�r��dumpsZCPUINFO_DATA)�argsr�Zlve_idrororp�_run_self_in_lveps
z"CloudlinuxSummary._run_self_in_lvecCs$tjddddg|ttjdd�d�S)z�
        Run same command using utility `nice` and set environ RUNNING_BY_NICE=1
        in order to check it in child process.
        :return:
        z
/usr/bin/nicez-nZ19z/usr/sbin/cloudlinux-summaryr)r�)r)rr	r�r�r�)rrororp�_run_self_by_nice�sz#CloudlinuxSummary._run_self_by_nice)�optsrxcs8t�fdd�dD��rdSt�fdd�dD��r4dSdS)zK
        Check that passed command should run as unlimited process
        c3s|]}�|VqdS)Nro)r��option)rrorpr��szECloudlinuxSummary._should_run_as_unlimited_process.<locals>.<genexpr>)zrpm-packagesr�z
get-remoter�r�Tc3s|]}�|VqdS)Nro)r�r)rrorpr��s)z--sendz--asyncz--jsonz--force-collectF)r})rro)rrp� _should_run_as_unlimited_process�s

z2CloudlinuxSummary._should_run_as_unlimited_processc
Cs|�|�|_|jr�|��|�|j�r0td�n�tdd�rN|�|�}t|�nly|�|�}t|�WnTt	j
k
r�}z4d|}t|�t�
t�}|j|dd�td�Wdd}~XYnXnH|�|j�r�d}t��|d�}tj||jd	�t|d�td
�|��dS)NrT)�skip_jwt_checkz$failed to run task in lve, error: %s)�exc_info���z|You shouldn't use env var "RUNNING_IN_LVE" or "RUNNING_BY_NICE" for run of any command except collection of main statistics.)r�rm)Zextrar-)�_parse_argsr�r�rrr�rr
rrZ
PyLveError�print�logging�	getLoggerrsrgr�rfr�r'�run_get_summary)rh�argv�rcr�Z	error_msg�logZerr_msgr�rororpr�s2






zCloudlinuxSummary.runcCs�t��}|��t��|}||jd<|jdrJ|j�t|j|�ddd�|j�d�rv|j|j|j	dd�t
�d	�nt|jd�dS)
NZcl_summary_execution_timez--lve-idr\Zcl_summary_max_mem_usedz,Can't get memory usage by cloudlinux-summaryz--sendT)r�r�r�zMain statistics sent)
r��_get_summaryr�r�rrr�_get_max_memoryr�r�r�rfr�r')rhZ
start_time�running_timerororpr�s"


z!CloudlinuxSummary.run_get_summary)r��summary_resultrxcCs2tj|d�}|dkr&||d<tj|d<t|�dS)z@
        Save status of sending statistics to json file
        )rmr�r�r�rmN)r:�SUCCESSr�r9)r�rrrororp�_save_status�s

zCloudlinuxSummary._save_status)r�r�r�rxcCsJtt���}||d<|j||d�}||d�}t|d�|rF|�||�dS)zH
        Send statistics data to server and save status to file
        r�)r�)rmr�TN)r]r��_send_statisticsr'r!)rhr�r�r�r�Zs_resultrmrororpr��s

z2CloudlinuxSummary._send_statistics_and_save_statusc	
Cs�|d}|dkrdSdddt|jd�ddd	d
�tt�|���g}yt|dd�\}}}Wn.tk
r�}zt�	d
|�dSd}~XYnX|dkr�t
�|�}ytdd�|dD��St
tfk
r�dSXt�d||�dS)Nr�r-z/usr/sbin/lveinfoz--jsonz--idz--lve-idz--show-columns�mPMemz--periodz{}mT)�return_full_outputz Unable to run lveinfo, error: %srcSsg|]}|d�qS)r#ro)r��xrororp�
<listcomp>sz5CloudlinuxSummary._get_max_memory.<locals>.<listcomp>r�z-lveinfo failed with exit code: %i, output: %s)r\r�r�r]�mathZceilrrrf�warningr��loads�maxr�r�r�)	rhrZtime_minutes�cmdr�json_strr�r�Zparsed_datarororprs*
z!CloudlinuxSummary._get_max_memorycCs�d}y�d|}t�|�t�d�}xbtd�D]R}ytj||dd�}Wn6tjk
rz}z|}t�d|�Wdd}~XYq,XPq,W|�|j	d	kr�t�d
�nd�
|j	|j�}t�|�Wn6tjk
r�}zt|�}t�
|�Wdd}~XYnX|S)z�
        Sends statistics to server
        :param data: Statistics data dict
        :return: string - message for JSON 'result' key
        r�zSending statictics to server %sz*Unknown exception while sending statistics�r�)r�r��N��zSending statictics OKz*Server answer is: HTTP code {}; Reason: {})rfr�r�r�rZpost�ConnectionErrorr�r�r�r�r�r\r�)r�r�Zout_messager�Zexpected_err�ir�rnrororpr"&s0
 
z"CloudlinuxSummary._send_statisticscCs&dt��d�}|��|j�|�|S)Nr-)�versionr�)r��_prepare_cl_normal_statisticsr�r�)rhrmrororprNs
zCloudlinuxSummary._get_summarycCs&dt��d�}|��|j�|�|S)Nr-)r2r�)r��'_fill_dict_with_rpm_packages_statisticsr�r�)rhrmrororpr�Vs
z+CloudlinuxSummary._get_rpm_packages_summarycCst��tjS)z/
         Get version of control panel
        )r�ZgetCPZ
CP_VERSIONrorororp�_get_panel_version^sz$CloudlinuxSummary._get_panel_versioncCs`|j�tjdd�}|dk	rR||jd<|j�dd�ddd�|j�d	d�dd
d�n
t|jd
<dS)zN
        Fill dict with statistics by statistics about MySQL governor
        r\zCan't get MySQL governor modeNZmysql_governor_modecSs
t���S)N)rZget_governor_versionrorororprzvr{zCCloudlinuxSummary._fill_mysql_governor_statistics.<locals>.<lambda>Zmysql_governor_versionz Can't get MySQL governor versioncSst���dS)Nr)rZget_governor_statusrorororprz|r{Zmysql_governor_statuszCan't get MySQL governor status)r�rqr%Zget_gov_mode_operationrrr�)rhZmysql_gov_moderororp�_fill_mysql_governor_statisticshs"
z1CloudlinuxSummary._fill_mysql_governor_statisticscCs~|j�tjddd�|j�dd�ddd�|j�|jdd	d
�|j�|jddd�|j�td
dd�|j�dd�d
dd�dS)zM
        Fill dict with statistics by statistics about control panel
        r\�control_panel_namezCan't get control panel namecSsdd�t����D�S)NcSsg|]\}}|r|�qSroro)r��nameZis_supportedrororpr&�szVCloudlinuxSummary._fill_control_panel_statistics.<locals>.<lambda>.<locals>.<listcomp>)rZget_supported_cl_features�itemsrorororprz�r{zBCloudlinuxSummary._fill_control_panel_statistics.<locals>.<lambda>r_Zsupported_cl_featuresz8Can't get list of supported cl features by control panelZcontrol_panel_versionzCan't get control panel versionZcontrol_panel_apachezCan't get control panel apacher]Z
nginx_runningzCan't get control panel nginxcSstt���S)N)�lenr�cpusersrorororprz�r{Zusers_totalzCan't get amount of usersN)r�rrr��	getCPNamer5�_get_control_panel_apacher)rhrororp�_fill_control_panel_statistics�s<z0CloudlinuxSummary._fill_control_panel_statisticscCsPt�r(t��rd}qLt��r"d}qLd}n$|jddkrHt��rBdnd}nd}|S)	z�
        Wrapper to retrieve control panel Apache version:
        EA3 or EA4 for cPanel, native otherwise
        :return: EA3|EA4|native
        Z	litespeedZ
openlitespeedZunknown_litespeedr7ZcPanelZEA4ZEA3�native)rr�Zdetect_enterprise_litespeedZdetect_open_litespeedr�Zis_ea4)rhrmrororpr=�sz+CloudlinuxSummary._get_control_panel_apachecCs8t��}|dkr|Stjttjtdtdti}|�|d�S)z�
        Wrapper to convert internal values from cagefs_statistics_config.get_cagefs_status function to values
         for statistics
        :return:
        NZEnabledZDisabledZUnknown)	r$Zget_cagefs_statusZCAGEFS_STATUS_NOT_INSTALLEDr�ZCAGEFS_STATUS_NOT_INITIALIZED�NOT_INITIALIZEDr�r�r�)�
cagefs_statusZcagefs_status_maprororp�_cagefs_status_wrapper�sz(CloudlinuxSummary._cagefs_status_wrappercCs~|j�|jddd�|jdttgkr2d|jd<n|j�tjddd�|j�ttjd�ddd	�|j�ttjd
�ddd�dS)
zF
        Fill dict with statistics by statistics about CageFS
        r\rAzCan't get CageFS statusNZcagefs_user_modezCan't get CageFS user modeTZcagefs_enabled_quantityz/Can't get quantity of users with enabled CageFSFZcagefs_disabled_quantityz0Can't get quantity of users with disabled CageFS)	r�rrrBr�r@r$Zget_cagefs_user_moderZget_quantity)rhrororp�_fill_cagefs_statistics�s,

z)CloudlinuxSummary._fill_cagefs_statisticscs^ytdd�|j��D��}Wntjk
r2dSXt|jj����t�fdd�|�	�D��S)zJ
        Get amount of end-users which belong to active resellers
        css|]\}}|dkr|VqdS)rNro)r�r��lvprororpr��szLCloudlinuxSummary._get_amount_of_endusers_under_resellers.<locals>.<genexpr>Nc3s|]}|�kVqdS)Nro)r�Zlvp_id)�enabled_lvp_idrorpr�s)
rr��
lve_lvp_pairsrrI�set�name_mapZid_list�sum�elements)rh�	lvp_countro)rErp�'_get_amount_of_endusers_under_resellers�sz9CloudlinuxSummary._get_amount_of_endusers_under_resellerscCs@ytdd�|j��D��}Wntjk
r2dSXt|���S)z/
        Get total amount of end-users
        css|]\}}|dkr|VqdS)rNro)r�r�rDrororpr�szBCloudlinuxSummary._get_total_amount_of_endusers.<locals>.<genexpr>N)rr�rFrrIrIr)rhrKrororp�_get_total_amount_of_enduserss
z/CloudlinuxSummary._get_total_amount_of_enduserscCs(ytt���Stjk
r"YnXdS)z)
        Get amount of resellers
        N)r:r�	resellersrIrorororp�_get_amount_of_resellerssz*CloudlinuxSummary._get_amount_of_resellerscs��j�tjjddd��j�tjjddd��j�tjjddd��j��fdd	�dd
d��j��jddd
��j��jddd��j��j	ddd��j��fdd	�ddd��jdrԈj�dd	�ddd�nd�jd<d�jd<d�jd<dS)zP
        Fill dict with statistics by varied statistics about resellers
        r]Z reseller_limits_supported_kernelz8Can't detect status of support reseller limits by kernelZ'reseller_limits_supported_control_panelz?Can't detect status of support reseller limits by control panelZreseller_limits_enabledz1Can't detect status of support of reseller limitscs���dS)Nr)�#get_users_and_resellers_with_faultsro)rhrorprz6r{z>CloudlinuxSummary._fill_resellers_statistics.<locals>.<lambda>Zusers_with_faultsz6Can't get amount of users with faults for the past 24hZresellers_totalz#Can't get total amount of resellersZ(resellers_endusers_under_reseller_limitsz>Can't get amount of end-users which belong to active resellersZresellers_endusers_totalz#Can't get total amount of end-userscs���dS)Nr-)rPro)rhrorprzNr{Zresellers_with_faultsz:Can't get amount of resellers with faults for the past 24hcSsttt����S)N)r:�listrZlvp_listrorororprzUr{Zresellers_activez$Can't get amount of active resellersN)
r�rrrZlveZis_lve10Zis_panel_supportedZreseller_limit_supportedrOrLrM)rhro)rhrp�_fill_resellers_statisticssb




z,CloudlinuxSummary._fill_resellers_statisticscs|j�t|j�jtj�jtj�ddd�|j�t|j�jtj�ddd�|j��fdd�dd	d
�|j��fdd�ddd
�|j��fdd�ddd�|j��fdd�ddd�|j��fdd�ddd�|j�t|j	�jtj
�ddd�|j�t|j	�jtj�ddd�dS)zN
        Fill dict with statistics by statistics about default limits
        r]Zdefault_limit_speedzCan't get default speed limitr\Zdefault_limit_cpu_origin_unitsz+Can't get cpu origin units of default limitcs�jtjS)N)�defaultsr*�ncpuro)�xml_cfg_providerrorprzvr{zCCloudlinuxSummary._fill_default_limits_statistics.<locals>.<lambda>Zdefault_limit_ncpuzCan't get default ncpu limitcs�jtjS)N)rSr*�ioro)rUrorprz|r{Zdefault_limit_iozCan't get default io limitcs�jtjS)N)rSr*�nprocro)rUrorprz�r{Zdefault_limit_nproczCan't get default nproc limitcs�jtjS)N)rSr*�epro)rUrorprz�r{Zdefault_limit_epzCan't get default ep limitcs�jtjS)N)rSr*�iopsro)rUrorprz�r{Zdefault_limit_iopszCan't get default iops limitZdefault_limit_vmem_mbzCan't get default vmem limitZdefault_limit_pmem_mbzCan't get default pmem limitN)r�rrr�_cpu_limit_to_percentsrSr*�cpurT�_get_cpu_limit_units�_mempages_to_mb�vmem�pmem)rhrUro)rUrp�_fill_default_limits_statistics_s`





z1CloudlinuxSummary._fill_default_limits_statisticscsR|j��fdd�ddd�|j��fdd�ddd�|j��fd	d�dd
d�dS)
z�
        Fill dict with statistics by other statistics about limits:
        packages_total, users_total, amount users/packages with custom limits
        cs
t�j�S)N)r:�packagesro)rUrorprz�r{zACloudlinuxSummary._fill_other_limits_statistics.<locals>.<lambda>r]Zpackages_totalz"Can't get total amount of packagescst����S)N)r:Zget_packages_with_custom_limitsro)rUrorprz�r{Zpackages_with_custom_limitsz/Can't get amount of packages with custom limitscst����S)N)r:Zget_users_with_custom_limitsro)rUrorprz�r{Zusers_with_custom_limitsz,Can't get amount of users with custom limitsN)r�rr)rhrUro)rUrp�_fill_other_limits_statistics�s


z/CloudlinuxSummary._fill_other_limits_statisticscs��x�tdd�D�]�}�j�t�j||�dd|���dkr<P�jj�fdd�dd|d	|d
d��jj�fdd�d
d|d|d
d��jj��fdd�d
d|d|d
d��jj��fdd�dd|d|d
d��jj�fdd�d
d|d|d
d��jj�fdd�d
d|d|d
d��jj�fdd�d
d|d|d
d��jj�fdd�d
d |d!|d
d��jj�fd"d�d
d#|d$|d
d��jj��fd%d�d
d&|d'|d
d��jj��fd(d�d
d)|d*|d
d�qWdS)+zV
        Fill dict with statistics by statistics about top packages on server
        r-r.r\z!Can't get top %s package by usersNcs
�djS)Nr-)r8ro)�
top_resultrorprz�r{zACloudlinuxSummary._fill_top_packages_statistics.<locals>.<lambda>ztop_%s_package_namez1Can't get package name of top %s package by usersF)rlcst�d�S)Nr)r]ro)rcrorprz�r{r]ztop_%s_package_users_numz4Can't get amount of users in top %s package by userscs$���djptj�djptj�S)Nr-)rZ�limitsr+r[rTro)rhrcrorprz�sztop_%s_package_limit_speedz0Can't get speed limit of top %s package by userscs���djptj�S)Nr-)r\rdr+r[ro)rhrcrorprz�sz%top_%s_package_limit_cpu_origin_unitsz>Can't get cpu origin units of limit of top %s package by userscs�djptjS)Nr-)rdr+rTro)rcrorprz�r{ztop_%s_package_limit_ncpuz/Can't get ncpu limit of top %s package by userscs�djptjS)Nr-)rdr+rVro)rcrorprz�r{ztop_%s_package_limit_ioz-Can't get io limit of top %s package by userscs�djptjS)Nr-)rdr+rWro)rcrorprz�r{ztop_%s_package_limit_nprocz0Can't get nproc limit of top %s package by userscs�djptjS)Nr-)rdr+rXro)rcrorprzr{ztop_%s_package_limit_epz-Can't get ep limit of top %s package by userscs�djptjS)Nr-)rdr+rYro)rcrorprz
r{ztop_%s_package_limit_iopsz/Can't get iops limit of top %s package by userscs���djptj�S)Nr-)r]rdr+r^ro)rhrcrorprzr{ztop_%s_package_limit_vmem_mbz/Can't get vmem limit of top %s package by userscs���djptj�S)Nr-)r]rdr+r_ro)rhrcrorprzr{ztop_%s_package_limit_pmem_mbz/Can't get pmem limit of top %s package by users)rr�rqr�#_get_top_package_by_number_of_usersrr)rhrUr1ro)rhrcrp�_fill_top_packages_statistics�s�







z/CloudlinuxSummary._fill_top_packages_statisticscCs(t�}|�|�|�|�|�|�dS)zN
        Fill dict with statistiscs by varied statistics about limits
        N)r)r`rbrf)rhrUrororp�_fill_limits_statistics$s

z)CloudlinuxSummary._fill_limits_statisticscsX|j�|jdd���dk	�rJ|jj�fdd�ddddd	�|jj�fd
d�ddddd	�|jj�fd
d�ddddd	�|jj�fdd�ddddd	�|jj�fdd�ddddd	�|jj�fdd�ddddd	�|jj�fdd�ddddd	�|jj�fdd�ddddd	�|jj�fd d�d!d"d#dd	�|jj�fd$d�dd%d&dd	�n
t|jd<dS)'zI
        Fill dict with statistics by statistics about mod_lsapi
        r\zCan't get raw mod_lsapi infoNcs�ddS)N�criur�roro)�raw_lsapi_infororprz:r{z:CloudlinuxSummary._fill_lsapi_statistics.<locals>.<lambda>Zlsapi_criu_service_statusz Can't get status of criu serviceF)rlcs�ddS)Nrhr2roro)rirorprzAr{Zlsapi_criu_service_versionz!Can't get version of criu servicecs�ddS)N�	lsapiConfZ
lsapi_criuroro)rirorprzHr{Zlsapi_option_criuzCan't get state of criucs�ddS)NrjZlsapi_with_connection_poolroro)rirorprzOr{Zlsapi_option_connection_poolz,Can't get state of mod_lsapi connection poolcs�dS)NZ
libVersionroro)rirorprzVr{Zlsapi_lib_versionz"Can't get version of mod_lsapi libcs�dS)NZ	modStatusroro)rirorprz]r{Zlsapi_mod_statuszCan't get mod_lsapi statuscs�dS)NZ
modVersionroro)rirorprzdr{Zlsapi_mod_versionzCan't get mod_lsapi versioncs�dS)NZtotalDomainroro)rirorprzkr{r]Zlsapi_total_domain_countz5Can't get total amount of domains which use mod_lsapics�dS)NZ
domainStatroro)rirorprzrr{raZlsapi_domain_statz.Can't get statistics of domains with mod_lsapics�dS)NZcontrolPanelroro)rirorprzyr{Zlsapi_apache_environmentzCan't get apache environment)r�rq�get_raw_lsapi_inforrr�)rhro)rirp�_fill_lsapi_statistics.s�











z(CloudlinuxSummary._fill_lsapi_statisticscCs|j�|jddd�dS)NrbZ
wmt_configz!Cant get status of wmt_api_config)r�rr�_get_wmt_api_config)rhrororp�_fill_wmt_settings_statistics�s
z/CloudlinuxSummary._fill_wmt_settings_statisticsc
spy|��\�}�WnBtk
rT}z$t�dt|��ggi�}�Wdd}~XYnXtt��t|��}y|�|�\��Wn<tk
r�}zt�dt|��ii��Wdd}~XYnX|rЈ�dt	|�i�|j
��fdd�ddd�|j
��fd	d�d
dd�|j
��fd
d�d
dd�|j
��fdd�ddd�|j
��fdd�ddd�|j
��fdd�d
dd�dS)Nz+Error while getting WordPress statistics %sz3Error while getting WordPress plugins statistics %sZcloudlinux_wpos_object_cachecst��S)N)r:ro)�all_wp_pathsrorprz�r{z7CloudlinuxSummary._fill_wp_statistics.<locals>.<lambda>r]Zwordpress_installed_totalz,Can't get total count of installed wordpresscs�S)Nroro)�object_cache_pluginsrorprz�r{raZwordpress_object_cache_pluginsz&Can't get WP object cache plugins infocs�S)Nroro)�advanced_cache_pluginsrorprz�r{Z wordpress_advanced_cache_pluginsz(Can't get WP advanced cache plugins infocst����S)N)rIrro)rprorprz�r{Z$wordpress_object_cache_plugins_totalz,Can't get WP object cache total plugins infocst����S)N)rIrro)rqrorprz�r{Z&wordpress_advanced_cache_plugins_totalz.Can't get WP advanced cache total plugins infocs�S)Nroro)�wp_php_versionsrorprz�r{Zwordpress_php_versionszCan't get WP php version info)�_prepare_wp_statisticsrerfr�r\rQrG�_prepare_wp_plugins_statisticsr�r:r�rr)rhZpaths_with_wpos_object_cacher�Zwithout_wpos_object_cachero)rqrorprrrp�_fill_wp_statistics�sX"





z%CloudlinuxSummary._fill_wp_statisticscCs |�d|�}|�d|�}||fS)zq
        Prepares dict with plugin-counter pairs
        e.g: {'w3-total-cache': 4, 'redis': 1, 'ls': 2}
        zobject-cachezadvanced-cache)�_get_plugin_info)rh�wp_pathsrprqrororprt�sz0CloudlinuxSummary._prepare_wp_plugins_statistics)�plugin_typerwcsFttd�fdd�|D���}tt�}x|D]}||d7<q*W|S)Ncsg|]}t|���qSro)r!)r�r�)rxrorpr&�sz6CloudlinuxSummary._get_plugin_info.<locals>.<listcomp>r-)rQ�filterrr])rxrwrpZobject_plugin_counter_pairsZpluginro)rxrprv�s
z"CloudlinuxSummary._get_plugin_infoc
	Cs�gg}}tt�}t��}x�|D]�}yt�|�}Wntk
rHw YnXtt�}x*|D]"\}}	tj�	|	�rX||	�
|�qXWxb|D]Z}	|�||	|�}
|
d}|
d}|�|�|�|
d�|r�|dkr�||t
|�7<q�Wq W|||fS)z�
        Return Wordpress absolute paths, paths with install WPOS object cache module
        and counter of php versions that are used by these Wordpresses.
        rw�php_version�'wp_paths_with_enabled_wpos_object_cache�	undefined)rr]rr;�userdomainsrerQr�r�r��append�_get_docroot_wp_inforr:)
rhroZ$paths_with_enabled_wpos_object_cacheZphp_versionsZpanel_users�userZdomains_info�docroot_domains_map�domain�docroot�docroot_infoZdocroot_wp_pathsZdocroot_php_versionrororprs�s,




z(CloudlinuxSummary._prepare_wp_statistics)r�r�r�rxcs�ggdd�}��fdd�t��D�}|s,|S�fdd�|D�|d<�fdd�t||�D�|d<x,��D] }|�|�}|dkrh||d	<PqhW|S)
zM
        Return data about docroot's WP paths and used php versions.
        r|)rwr{rzcs4g|],}tj��|��ks,tj��|��kr|�qSro)r�r�r�)r�Zwp_path)r�r�rorpr&
sz:CloudlinuxSummary._get_docroot_wp_info.<locals>.<listcomp>csg|]}t���|��qSro)r�joinpath)r�r�)r�rorpr&srwcs"g|]}|dk	rt���|��qS)N)rr�)r�r�)r�rorpr&sr{rz)r r"�_get_php_version_for_domain)rhr�r�r�r�rwr�rzro)r�r�rpr�s

z&CloudlinuxSummary._get_docroot_wp_info)r�rxc
Cs�t��tjtjtjfkrdS|��\}}}|�|i�}|�dd�}t��tjkr\||kr\|Sx:|��D].\}}||krf|dkrfd|�	dd���}	|	SqfWt��tjkr�d|�	dd���}
|
St��tjkr�|�dd�}|d	t
|���d
�}|p�dS|S)z�
        Return php version that is used for specified domain.
        Determine whether of MultiPHP Manager or PHP Selector is used.
        r|Zphp_version_idr?zalt-php�.r�zda-phpZhandler_typeN�-)r�r<rZCPANEL_NAMEZDIRECTADMIN_NAMEZ
PLESK_NAME�_get_system_php_infor�r9�replacer:r�)
rhr�Zsystem_default_php_version�domains_php_info�selector_php_infoZphp_infoZmulti_php_versionr2�domainsZalt_php_versionZda_php_versionZhandlerrzrororpr�!s(z-CloudlinuxSummary._get_php_version_for_domain)�maxsizecCsXyt��d}Wntk
r(d}YnXt��}t�}|dk	rJ|d}ni}|||fS)z�
        Return info about:
        - system_php_version (implemented only on cPanel)
        - domains_php_info (php versions and handlers turned on in MultiPHP)
        - selector_php_info (php version used by panel users via PHP Selector)
        Zdefault_version_idN�domains_by_php_version)rZget_system_php_inforeZget_domains_php_inforR)Zsystem_php_versionr�Zphp_selector_usager�rororpr�Hs	

z&CloudlinuxSummary._get_system_php_infocs0|j�|jdd���dk	�r"|j��fdd�ddd�|j��fd	d�dd
d�|j��fdd�dd
d�|j��fdd�ddd�|j��fdd�ddd�|j��fdd�ddd�|j��fdd�ddd�|j��fdd�ddd�|j��fdd�dd d!�|j��fd"d�d#d$d%�n
t|jd
<dS)&z/
        Fill dict with SSA statistics
        r\zCan't get SSA statisticsNcs�dS)N�configroro)�	ssa_statsrorprzmr{z8CloudlinuxSummary._fill_ssa_statistics.<locals>.<lambda>rbZ
ssa_configzCan't get SSA configcs�dS)Nr2roro)r�rorprzsr{Zssa_versionzCan't get version of SSAcs�dS)Nr�roro)r�rorprzyr{Z
ssa_statuszCan't get state of SSAcs�dS)NZagent_statusroro)r�rorprzr{Zssa_agent_statuszCan't get state of SSA agentcs�ddS)N�autotracingr�roro)r�rorprz�r{Zautotracing_statusz"Can't get state of SSA Autotracingcs�ddS)Nr�Zdisabled_users_quantityroro)r�rorprz�r{r]Z#autotracing_disabled_users_quantityz1Can't get state of SSA Autotracing disabled userscs�ddS)Nr�Z
rules_versionroro)r�rorprz�r{Zautotracing_rules_versionz'Can't get SSA Autotracing rules versioncs�ddS)Nr�Zurls_processedroro)r�rorprz�r{Zautotracing_urls_processedz.Can't get SSA Autotracing processed URLs countcs�ddS)Nr�Z
urls_selectedroro)r�rorprz�r{Zautotracing_urls_selectedz-Can't get SSA Autotracing selected URLs countcs�ddS)Nr�Zrejectsroro)r�rorprz�r{raZautotracing_rejectsz!Can't get SSA Autotracing rejects)r�rq�
get_ssa_statsrrr�)rhro)r�rp�_fill_ssa_statistics`sp










z&CloudlinuxSummary._fill_ssa_statisticscs@|j�dd�dd�}|dk	�r2|j�t|jd�ddd	�|jjt|j|t�d
ddd
d�|jjt|j|t�d
ddd
d�|j�tddd�|j�t	ddd�|j�t
d�jddd�|j�tddd�|j�t
dd���dkr�d|jd<d|jd<n<|jj�fdd�dddd
d�|jj�fdd�ddd d
d�n
t|jd<dS)!zS
        Fill dict with statistics by varied statistics about PHP selector
        cSstd�S)Nr�)rProrororprz�r{zACloudlinuxSummary._fill_php_selector_statistics.<locals>.<lambda>r]z+Can't get statistics about PHP interpretersNr�r\Zselector_php_statusz Can't get status of PHP selectorr_Zselector_php_versions_installedz8Can't get list of versions of installed PHP interpretersF)rlZselector_php_versions_enabledz6Can't get list of versions of enabled PHP interpretersZselector_php_version_defaultz,Can't get default version of PHP interpreterZselector_php_version_nativez+Can't get native version of PHP interpreterZselector_php_enabled_uiz%Can't get state of UI of PHP selectorZselector_php_modezCan't get mode of PHP selectorz'Can't get summary usage of PHP selectorZ'selector_php_num_domains_by_interpreterZ%selector_php_num_users_by_interpretercsdd��d��D�S)NcSsi|]\}}t|�|�qSro)r:)r��vr�rororp�
<dictcomp>�szUCloudlinuxSummary._fill_php_selector_statistics.<locals>.<lambda>.<locals>.<dictcomp>r�)r9ro)�php_usage_summaryrorprz�r{razBCan't get amount of domains which use PHP selector per PHP versioncsdd��d��D�S)NcSsi|]\}}t|�|�qSro)r:)r�r�r�rororpr��szUCloudlinuxSummary._fill_php_selector_statistics.<locals>.<lambda>.<locals>.<dictcomp>Zusers_by_php_version)r9ro)r�rorprz�r{z@Can't get amount of users which use PHP selector per PHP version)r�rqrrrr��"_get_list_versions_of_interperters�	INSTALLEDr�rTrQrVZphp_selector_is_enabledrUrRr�)rhZphp_interpretersro)r�rp�_fill_php_selector_statistics�s|





z/CloudlinuxSummary._fill_php_selector_statisticscCs|dks|dkrdS||S)z�
        Get average amount of applications per domain
        :param total_apps: total amount of applications
        :param amount_of_apps_per_domain: amount of applications per domain
        r-Nro)�
total_appsZamount_of_apps_per_domainrororp�_get_average_apps_per_domain�s	z.CloudlinuxSummary._get_average_apps_per_domaincCs|dks|dkrdS||S)z�
        Get average amount of applications per user
        :param total_apps: total amount of applications
        :param amount_of_apps_per_user: amount of applications per user
        r-Nro)r�Zamount_of_apps_per_userrororp�_get_average_apps_per_users	z,CloudlinuxSummary._get_average_apps_per_usercCstdd�|D��S)z
        Get amount of running applications on server
        :param apps: list of applications for something selector
        cSs g|]}|jr|jdkr|�qS)Zstarted)Z
app_status)r��apprororpr& sz@CloudlinuxSummary._get_amount_of_runned_apps.<locals>.<listcomp>)r:)�appsrororp�_get_amount_of_runned_appssz,CloudlinuxSummary._get_amount_of_runned_appscCsBt�}x|D]}||jd7<qW|�d�p4dg}|ddS)z�
        Get maximum amount of applications per domain
        :param apps: list of applications for something selector
        r-)rrr)r�doc_root�most_common)r�Zapps_per_domainr��most_commonsrororp�_get_max_apps_per_domain"s

z*CloudlinuxSummary._get_max_apps_per_domaincCsBt�}x|D]}||jd7<qW|�d�p4dg}|ddS)z~
        Get maximum amount of applications per user
        :param apps: list of applications for something selector
        r-)rrr)rr�r�)r�Z
apps_per_userr�r�rororp�_get_max_apps_per_user3s

z(CloudlinuxSummary._get_max_apps_per_usercCs*t�}x|D]}||jd7<qW|S)z�
        Get Counter object which contains amount applications per version of interpreter
        :param apps: list of applications for something selector
        r-)rr2)r��apps_per_versionr�rororp�_get_counter_apps_per_versionDs
z/CloudlinuxSummary._get_counter_apps_per_versioncCs&|�|�}|�d�pdg}|ddS)z�
        Get maximum amount of applications per version of interpreter
        :param apps: list of applications for something selector
        r-)rrr)r�r�)rhr�r�r�rororp�_get_max_apps_per_versionRs
z+CloudlinuxSummary._get_max_apps_per_versioncCs(t�}x|D]}|�|j�qWt|�S)zz
        Get amount of domains with applications
        :param apps: list of applications for something selector
        )rG�addr�r:)r�r�r�rororp� _get_amount_of_domains_with_apps`s
z2CloudlinuxSummary._get_amount_of_domains_with_appscCs(t�}x|D]}|�|j�qWt|�S)zx
        Get amount of users with applications
        :param apps: list of applications for something selector
        )rGr�r�r:)r��usersr�rororp�_get_amount_of_users_with_appsns
z0CloudlinuxSummary._get_amount_of_users_with_appscCs|�|�}t|�S)z�
        Get amount of applications per each versoin of interpeters
        :param apps: list of applications for something selector
        )r�r�)rhr�r�rororp�4_get_amount_of_apps_per_each_version_of_interpreters|s
zFCloudlinuxSummary._get_amount_of_apps_per_each_version_of_interpreterscs�fdd�t|�D�S)z�
        Get list of versions of interpreters on server
        :param interpreters_stats: dict with varied statistics about each version of interpeters
        :param state: state of interpeters (installed, enabled)
        csg|]\}}|�r|�qSroro)r�Zinterpreter_stats�stat)�staterorpr&�szHCloudlinuxSummary._get_list_versions_of_interperters.<locals>.<listcomp>)r
)�interpreters_statsr�ro)r�rpr��s	z4CloudlinuxSummary._get_list_versions_of_interperterscCs"t|�}|dk	rt|�St�SdS)zI
        Get list of apllications on server for defined selector
        N)rSrQ)r�Z	iter_appsrororp�_get_list_of_applications�sz+CloudlinuxSummary._get_list_of_applicationsc	s�x|jD�]
}|j�tt|�dd�|��}|dkrHt|jd|d<q
|j�t|j|�dd|dd�|��|jjt|j	|t
�dd|d	d
�|�dd�|jjt|j	|t�dd|d
d�|�dd�|j�t|j|�dd�|���|jj�fdd�dd|dd�|�dd�|jd|ddk|jd|d<|jjt|j
��dd|dd�|�dd�|j�tt|�dd�|��}|dk	�r�||jd|d<|j�t|j��dd|dd�|��|j�t|j��dd|dd�|��|j�t|j��dd|dd�|��|j�t|j��dd|d d!�|��|j�t|j|jd|d|jd|d �dd"�|��}|dk	�r�||jd|d#<|j�t|j|jd|d|jd|d�dd$�|��}|dk	�r�||jd|d%<|j�t|j��d&d|d'd(�|��q
WdS))zc
        Fill dict with statistics by varied statistics about ruby/nodejs/python selectors
        r]z*Can't get statistics about {} interpretersNZ	selector_Z_statusr\zCan't get status of {} selectorr_Z_versions_installedz7Can't get list of versions of installed {} interpretersF)rlZ_versions_enabledz5Can't get list of versions of enabled {} interpretersz!Can't get list of {} applicationscst��S)N)r:ro)�interpreter_appsrorprz�r{z>CloudlinuxSummary._fill_selectors_statistics.<locals>.<lambda>Z_applications_amountz)Can't get total amount of {} applicationsrZ_usedZ_applications_runningz6Can't get amount of runned application for {} selectorz(Can't get default version of {} selectorZ_default_versionZ_max_applications_per_domainz8Can't get max applications per domain for {} interpreterZ_max_applications_per_userz6Can't get max applications per user for {} interpreterZ_num_users_with_appsz=Can't get amount of users with applications for {} interpeterZ_num_domains_with_appsz?Can't get amount of domains with applications for {} interpeterzFCan't get average amount of applications per domain for {} interpreterZ _average_applications_per_domainzDCan't get average amount of applications per user for {} interpreterZ_average_applications_per_userraZ _num_applications_by_interpreterzDCan't get amount of applications per each version of {} interpreters)�	SELECTORSr�rqrrPr�r�rrr�r�r�r�r�r�rWr�r�r�r�r�r�r�)rhZselectorr�Zdefault_version_of_selectorZaverage_apps_per_domainZaverage_apps_per_userro)r�rp�_fill_selectors_statistics�s�






$














z,CloudlinuxSummary._fill_selectors_statisticscCs~ddg}t|dd�\}}}|dkr*t|��t�|�}t�}|d|d<dd�|d	D�}x tD]}|�|t�|d
|<q^W|S)zA
        Get wizard status and list of installed modules
        z/usr/sbin/cloudlinux-wizardr�T)r$r�
wizard_statuscSsi|]}|d|d�qS)r�r8ro)r�ryrororpr�'sz<CloudlinuxSummary._get_wizard_statistics.<locals>.<dictcomp>r|�wizard_module_)rrr�r)r�rJr��NOT_SELECTED)r+Zret_codeZstd_outZstd_errZparsed_json�wizard_statisticsZparsed_modulesryrororp�_get_wizard_statisticss

z(CloudlinuxSummary._get_wizard_statisticscsl|j�|jdd��|jj�fdd�ddddd�x6tD].�|jj��fd	d�dd
�d���dd�q6WdS)
zX
        Fill dict with statistics by varied statistics about cloudlinux-wizard
        r\z,Can't get statistics about cloudlinux-wizardcs�dS)Nr�roro)r�rorprz:r{z;CloudlinuxSummary._fill_wizard_statistics.<locals>.<lambda>r�z%Can't get status of cloudlinux-wizardF)rlcs�d�S)Nr�roro)ryr�rorprzBr{r�z;Can't get statistics about module "{}" of cloudlinux-wizardN)r�rqr�rrrJr�)rhro)ryr�rp�_fill_wizard_statistics-s"

z)CloudlinuxSummary._fill_wizard_statisticscCs0t�}g}x |D]}|t||���7}qW|S)zH
        Returns list of implemented scripts in integration.ini
        )rMrQ�keys)r�ZscriptsZsectionrororp�$_get_implemented_integration_scriptsIs

z6CloudlinuxSummary._get_implemented_integration_scriptscCs2dgd�}tj�t�s|Sd|d<|��|d<|S)zh
        Checks integration script exists and if exists
        get list of implemented scripts
        F)�integration_scripts_used�integration_scripts_specifiedTr�r�)r�r�r�rLr�)rhrmrororp�_get_integration_infoTs
z'CloudlinuxSummary._get_integration_infocCs8ddl}d}|��}t|j�|}t|j�|}||fS)zG
        Gets memory usage: total and used memory in megabytes
        rNi)�psutilZvirtual_memoryr^�totalZused)r�Zbytes_in_mbZmemZ	mem_totalZmem_usedrororp�_get_memory_used`sz"CloudlinuxSummary._get_memory_usedc	CsNt��}d}d}tdd�sFtj�|�rFt|��}|����}WdQRX||fS)za
        Gets kernel info release and module version (starting from 7h)
        :return:
        z/sys/module/kmodlve/versionNT)r)	�platform�releaserr�r�r�r�r�r�)�kernel_releaseZkmodlve_version_fileZkmodlve_versionr�rororp�_get_kernel_infols
z"CloudlinuxSummary._get_kernel_infocCs
tt��S)zp
        Gets info about lve extensions usage
        Calculates amount of packages with lve extensions
        )r:r@rorororp�#_get_lve_extensions_packages_amount{sz5CloudlinuxSummary._get_lve_extensions_packages_amountc	Cs@d}tj�|�sdSt|��}|����}WdQRXtt|��S)zk
        Reads /proc/sys/fs/datacycle/enable in order to check
        datacycle enabled parameter
        z/proc/sys/fs/datacycle/enableFN)r�r�r�r�r�r�r�r])Zdatacycle_filer�r�rororp�$_is_kernel_datacycle_enabled_in_file�s
z6CloudlinuxSummary._is_kernel_datacycle_enabled_in_filec	CsFd\}}tj�|�sdSt|��}|�����d�}WdQRX||kS)zR
        Checks if datacycle parameter was given
        for current boot
        )z
/proc/cmdlineZ	datacycleF� N)r�r�r�r�r�r��split)Zcmdline_filer�r�r�rororp�_is_datacycle_param_was_passed�s
z0CloudlinuxSummary._is_datacycle_param_was_passedcCs0t��}d}x|D]}|tt�|��7}qW|S)z=
        Returns general amount of domains on server
        r)rr;r:r})Zcpusers_listZ
domains_countr�rororp�_get_total_domains_amount�s

z+CloudlinuxSummary._get_total_domains_amountcCsNt�}tt|�d���}tt|�d���}|r6|r6dS|r>dS|rFdSdSdS)zi
        Returns is links traversal protection enabled on server
        (symlinks or hardlinks)
        zfs.protected_symlinks_createzfs.protected_hardlinks_create�allZ
symlinks_onlyZhardlinks_onlyZnoN)r#r�r]r�)r�Zsymlink_protection_enabledZhardlink_protection_enabledrororp�%_is_link_traversal_protection_enabled�sz7CloudlinuxSummary._is_link_traversal_protection_enabledcCs@d}d}d}tj�d�s$tj�d�r(|Stj�d�r8|S|SdS)N�iso�cldeploy�qcow2z/etc/cl-convert-savedz/var/log/cldeployz/etc/cloudlinux-qcow2-install)r�r�r�)r�r�r�rororp�_get_cl_installation_source�sz-CloudlinuxSummary._get_cl_installation_sourcecs�����tdd�sv�jj�jddddd��jjtddd	dd��jj�jd
dddd��jj�fd
d�d
dddd��jjtd
dddd��jjtddddd��j��j	d
dd��j��j
d
dd��jj�fdd�d
dddd������jj�fdd�ddddd��jj�fdd�d d!d"dd������jj�fd#d�d$d%d&dd��jj�fd'd�d$d(d)dd��jj�j
dd*d+dd�tdd��s��jj�fd,d�dd-d.dd��jjtd
d/d0dd��jjtd
d1d2dd��jj�jd
d3d4dd�dS)5NT)rr]Zlve_extension_packages_amountz"Can't get lve extension usage infoF)rlZcldiag_cron_check_enabledz&Can't get is cldiag cron check enabledr\Z!link_traversal_protection_enabledz5Can't get link traversal protection enabled parametercs�dS)Nr-roro)�kernel_infororprz�r{z;CloudlinuxSummary._fill_system_statistics.<locals>.<lambda>Zinstalled_kmod_lve_versionz)Can't get installed kmod-lve version infoZ
cpu_amountzCan't get cpu amountZtesting_repository_enabledz#Can't get testing repository statusZbase_distrozCan't detect platform for CLOSZsecureboot_statusz+Can't detect whether secure boot is enabledcs�dS)Nrroro)r�rorprz	r{r�zCan't get kernel release infocs�dS)Nr�roro)�vendor_integration_infororprz	r{r�z'Can't get integration_scripts_used infocs�dS)Nr�roro)r�rorprz	r{r_r�z-Can't get integrations scripts specified infocs�dS)Nrroro)�memory_usagerorprz$	r{r^Zmemory_total_mbzCan't get total memory infocs�dS)Nr-roro)r�rorprz+	r{Zmemory_used_mbzCan't get used memory infoZ
domains_totalzCan't get domains amountcs���p���S)N)r�r�ro)rhrorprz;	sZkernel_datacycle_usage_enabledz,Can't get kernel datacycle enabled parameterZ	virt_typez!Can't get the virtualization typeZhostnamezCan't get the hostnameZcloudlinux_installation_sourcezCan't get installation source)r�rr�rrr�rNr�rrr�r�r�r�r�rr
r�)rhro)r�r�rhr�rp�_fill_system_statistics�s�







z)CloudlinuxSummary._fill_system_statisticscsB�j��fdd�ddd��j�tddd��j�tdd	d
�dS)Ncs
�jptS)N)r��UNKNOWN_RHN_IDro)rhrorprz]	r{zKCloudlinuxSummary._fill_dict_with_rpm_packages_statistics.<locals>.<lambda>r\r�zCan't get system IDZ
os_versionzCan't get version of OSrcraz*Can't get info about client's rpm packages)r�rrrrF)rhro)rhrpr4[	s
z9CloudlinuxSummary._fill_dict_with_rpm_packages_statistics)�paramrxcCst�|j�|��S)z�
        Retrieve data from proc/mounts for param
        :return: param_value - Optional[int],
        if there is no value - None
        )r&Z
str_to_intr�r�)rhr�rororp�_get_proc_paramo	sz!CloudlinuxSummary._get_proc_paramcCs�dg}tdd�s.|�dddddd	d
ddd
g
�x8|D]0}|jjt|j|�d|�dd�d�|�dd�q4W|jjtddddd�dS)z�
        Filling stats about mounting
        - mount params from parameters list
        - separate hidepid getting, since it is more complicated than
        other mounting params
        zkernel.memcg_oom_disableT)rzfs.enforce_symlinksifownerzfs.symlinkown_gidzfs.protected_symlinks_allow_gidz fs.protected_hardlinks_allow_gidzfs.global_root_enablezfs.proc_can_see_other_uidzfs.proc_super_gidzfs.xfs.cap_res_quota_disablezubc.ubc_oom_disablezfs.process_symlinks_by_taskr]r�r�z
Can't get {0})rlZhidepidzCan't get hidepid valueN)	rrr�rrrr�r�r�rO)rhZ
parameters�prororp�_fill_proc_params_statisticsw	s4



z.CloudlinuxSummary._fill_proc_params_statisticscsZtdd��|j��fdd�ddd�|j��fdd�dd	d
�|j�tt��ddd�d
S)zv
        Fill statistics fields from jwt token.
        - jwt token metrics (cl_plus existence and client_id)
        F)Zcheck_expirationcs�dkrdS��dd�S)N�cl_plus)r�ro)r�rorprz�	sz=CloudlinuxSummary._fill_cln_jwt_information.<locals>.<lambda>r]r�zCan't get cl_plus informationcs�dkrdS��dd�S)N�	client_id)r�ro)r�rorprz�	sr�zCan't get client_id valueZlicense_activezCan't get license status valueN)rCr�rrrrE)rhro)r�rp�_fill_cln_jwt_information�	s 


z+CloudlinuxSummary._fill_cln_jwt_informationcs0�j��fdd�ddd��j�tddd�d	S)
za
        Filling stats centralized management
        - centralized management existence
        cstj��j�S)N)r�r�r��CL_PLUS_CM_DISABLED_PATHro)rhrorprz�	r{zKCloudlinuxSummary._fill_centralized_management_statistics.<locals>.<lambda>r]Zcentralized_management_disabledzCan't check CM disabling statusr\Zcl_plus_sender_service_statusz)Can't check cl plus sender service statusN)r�rrrD)rhro)rhrp�'_fill_centralized_management_statistics�	s
z9CloudlinuxSummary._fill_centralized_management_statisticsc
Cs�t�}yFt�}x:|D]2}x,|��D] \}}|�d|��g��|�q"WqWWn2ttfk
r~}zt�d|�Wdd}~XYnX|S)a�
        Prepare list of dicts with CPU metrics

        Method get_cpu_metrics returns data in following format:
        [
            {
              "id": 0,
              "model": "QEMU Virtual CPU version 2.5+"
            },
            {
              "id": 0,
              "model": "QEMU Virtual CPU version 2.5+"
            }
        ]

        This helper produces a dict, where each key - metric_name,
        value - list of values for all CPUs

        "cpu_model": [
            "QEMU Virtual CPU version 2.5+",
            "QEMU Virtual CPU version 2.5+"
        ],
        "cpu_id": [
            0,
            0
        ]
        Zcpu_zCPU metrics getting error: %sN)	r�rGr9�
setdefaultr~r�rIrfr�)rmZ	cpu_coresZcpu_core�metric�valueZexrororp�make_flat_cpu_metrics�	s
$z'CloudlinuxSummary.make_flat_cpu_metricsc	Cslddddg}|��}x@|D]8}|dkr*dnd}|j�t|j|d�||d|�d	��qW|j�td
dd�dS)
z�
        Filling stats about hardware metrics, specifically:
        CPU:
            - cache
            - frequency
            - model
            - id
        RAM:
            - ram
            - swap
        Zcpu_idZcpu_cache_mbZ	cpu_modelZcpu_frequency_mhzr`r_NzCan't parse z metric for all coresraZmemoryzCan't parse memory metrics)r�r�rrrr�rH)rhZexp_metricsZcpu_metricsr�Zmetric_typerororp�_fill_hardware_statistics�	s

z+CloudlinuxSummary._fill_hardware_statisticscCs6|��\|jd<|jd<|jd<tj�d�|jd<dS)am
        Collect CageFS enter errors number:
        1. "Act like CageFS is disabled (unable to create LVE).. %d"
        2. "Act like CageFS is disabled (unable to enter into NameSpace).. %d"
        3. "Act like CageFS is disabled (unable to acquire lock for user %s uid %d)"
        4. File-marker /etc/cagefs/fail.on.error presense
        :return None
        Z(act_cagefs_disabled_unable_to_create_lveZ&act_cagefs_disabled_unable_to_enter_nsZ)act_cagefs_disabled_unable_to_acqure_lockz/etc/cagefs/fail.on.errorZ"act_cagefs_disabled_marker_presentN)�*_scan_log_for_act_cagefs_disabled_messagesr�r�r�r�)rhrororp�$_fill_act_cagefs_disabled_statistics
s z6CloudlinuxSummary._fill_act_cagefs_disabled_statisticsc	Cs�y�|��\}}|dkrv|�d�}ttddd|d��}t|�}ttddd|d��}t|�}ttddd|d��}t|�}nd}d}d}|||fSttfk
r�dSXd	S)
a�
        Scan /var/log/messages for all needed "Act like CageFS is disabled ..." messages for yesterday
        :return tuple of ints:
            Number of "Act like CageFS is disabled (unable to create LVE).. " messages,
            Number of "Act like CageFS is disabled (unable to enter into NameSpace).. " messages,
            Number of "Act like CageFS is disabled (unable to acquire lock for user %s uid %d)" messages
        r�
z2Act like CageFS is disabled (unable to create LVE)T)Zfixed_stringZmultiple_searchZdata_from_filez<Act like CageFS is disabled (unable to enter into NameSpace)z<Act like CageFS is disabled (unable to acquire lock for user)i���i���i���N)�_get_data_from_logr�rQrr:r�r�)rh�
returncode�stdoutZ
lines_listZfound_lines_listZnum_unable_to_create_lveZnum_unable_to_enter_nsZnum_unable_to_acqure_lockrororpr�,
s&


z<CloudlinuxSummary._scan_log_for_act_cagefs_disabled_messagescCsft�}|dkrd}n,tj��tjdd�}|�d�}d|�d�}tj|ddd	tjtj	d
�}|j
|jfS)z�
        Scan /var/log/messages for all needed "Act like CageFS is disabled ..." messages for yesterday
        :return: Tuple (ret code, std_out string)
        )Zcl7Zcl7hZcl8za/usr/bin/journalctl --since yesterday --until today | /usr/bin/grep 'Act like CageFS is disabled'r-)Zdaysz%Y-%m-%dz/usr/share/cloudlinux/logs-at z< /var/log/messages | /bin/grep 'Act like CageFS is disabled'Tz	/bin/bash)r�r�r�r��stderr)r�datetimeZdateZtodayZ	timedeltaZstrftimerr�PIPEZDEVNULLr�r�)Zos_typeZs_cmdZyesterday_dateZdate_to_scanr�rororpr�L
s
z$CloudlinuxSummary._get_data_from_logcs�tdd�s�j��jddd�t�jd<�j�ttjdd�d	d
d��j��jddd
��j��fdd�d	dd��j��fdd�d	dd�t	dk	r�t
dk	r�d�t	t
��jd<n
d�jd<���dS)NT)rr]�old_way_of_integration_usedz*Can't detect old LVE integration mechanism�lve_utils_versionF)Z
verify_expr\Z
cl_editionzCan't get CloudLinux editionZis_solo_meta_file_presentz$Can't detect solo meta file presencecs�jS)N)r�ro)rhrorprzz
r{zACloudlinuxSummary._prepare_cl_normal_statistics.<locals>.<lambda>r�z$Can't get or generate security tokencs
�jptS)N)r�r�ro)rhrorprz�
r{r�zCan't get system IDz{}-{}�lvemanager_version)
rr�rrr��LVE_UTILS_PKG_VERSIONrrZget_cl_edition�_is_solo_marker_exists�LVEMANAGER_PKG_VERSION�LVEMANAGER_PKG_RELEASEr��_fill_dict_with_statistics)rhro)rhrpr3c
sD




z/CloudlinuxSummary._prepare_cl_normal_statisticscs�d�d�d�d�d�tj���s$dSGdd�dt��������fd	d
�}�d�d�di}dd
�t���D�}|��yx|D]}|||�q~WWn<�k
r�Yn*tk
r�x|D]}d||<q�WYnX|�|j�<|�|j�<|�|j�<dS)z�
        Filling stats lvemanager
        - number of visits on X-Ray tab
        - number of X-Ray installation
        - number of LVEManager opens
        z/var/log/cloudlinux/i�QZlve_number_of_opensZxray_number_of_installationZxray_number_of_visitsNc@seZdZdS)zECloudlinuxSummary._fill_lvemanager_statistics.<locals>.BreakOuterLoopN)rsrtrurorororp�BreakOuterLoop�
sr�c	
stj����}t�|d�}d}x�|D]�}y�d�|�d�dd��}tj�|d���}|�|krn|kr�nnjd�|���d�dd��}|dkr�|�d7<q�|dkr�|�d7<q�|d	kr�|�d7<nd
}Wq&tt	fk
r�w&Yq&Xq&W|�r��dS)N�rFr�r�z%Y-%m-%d %H:%M:%S,%fzlvemanager-openedr-zxray-install-startedzxray-tab-clickedT)
r�Znowr�r�r�r�Zstrptimer�r��AttributeError)	Zstats�fileZcurrent_date_timestampr�Zbreak_outer_loop�lineZlog_timeZlog_date_timestampr�)r��COLLECTING_PERIOD�
LOG_FOLDER�LVE_NUMBER_OF_OPENS�XRAY_NUMBER_OF_INSTALLS�XRAY_NUMBER_OF_VISITSrorp�fill_statistics�
s(
zFCloudlinuxSummary._fill_lvemanager_statistics.<locals>.fill_statisticsrcSs"g|]}d|kr|�d�s|�qS)zlvemanager.logz.gz)�endswith)r�r8rororpr&�
szACloudlinuxSummary._fill_lvemanager_statistics.<locals>.<listcomp>r)r�r��isdirre�listdir�sortr�r�)rhrr�Z	log_filesZlog_filer�ro)r�r�r�r�r�rrp�_fill_lvemanager_statistics�
s2

z-CloudlinuxSummary._fill_lvemanager_statisticscs��j��fdd�ddd��j��fdd�ddd�t�jd	<tdk	rbtdk	rbd
�tt��jd<n
d�jd<�j��jdd
d����dS)Ncs�jS)N)r�ro)rhrorprz�
r{z7CloudlinuxSummary._prepare_statistics.<locals>.<lambda>r\r�z$Can't get or generate security tokencs
�jptS)N)r�r�ro)rhrorprz�
r{r�zCan't get system IDr�z{}-{}r�r]r�z*Can't detect old LVE integration mechanism)r�rrr�r�r�r�r�r�)rhro)rhrp�_prepare_statistics�
s,



z%CloudlinuxSummary._prepare_statisticscCs�tdd�sZ|��|��|��|��|��|��|��|��|�	�|�
�|��|��|�
�|��|��|��|��|��|��|��dS)NT)r)rr6rCrRrgr�r�r�rlr�r��_fill_wpos_statisticsr�r>r�r�r�rnr�rru)rhrororpr��
s*
z,CloudlinuxSummary._fill_dict_with_statisticscs�|j�|jdd��t�t�rƈ�d�dkr�|j��fdd�ddd	�|j��fd
d�ddd	�|j��fdd�dd
d	�|j��fdd�ddd	�|j��fdd�ddd	�|j��fdd�ddd	�dS)Nr\z!Can't get AccelerateWP statisticsrmr�cs�ddS)N�
enabled_sitesr�roro)�
wpos_statsrorprzr{z9CloudlinuxSummary._fill_wpos_statistics.<locals>.<lambda>r]Zsites_count_with_enabled_wposz*Can't get statistic of using AccelerateWP.cs�ddS)N�
allowed_usersr�roro)r
rorprzr{Zusers_count_with_allowed_wposcs�ddS)Nr	�object_cacheroro)r
rorprz r{Z%sites_count_with_enabled_object_cachecs�ddS)Nrrroro)r
rorprz&r{Z%users_count_with_allowed_object_cachecs�ddS)Nr	�site_optimizationroro)r
rorprz,r{Z*sites_count_with_enabled_site_optimizationcs�ddS)Nrr
roro)r
rorprz2r{Z*users_count_with_allowed_site_optimization)r�rq�get_wpos_statsr�r�r�rr)rhro)r
rprsF





z'CloudlinuxSummary._fill_wpos_statisticsc
Csndg|}yt|dd�\}}}Wn.tk
rN}zt�d|�dSd}~XYnX|dkr\|St�d||�dS)z�
        Run cloudlinux-statistics using subprocess and handle errors.
        :type args: list[str]
        :rtype: str or None
        z/usr/sbin/cloudlinux-statisticsT)r$z.Unable to run cloudlinux-statistics, error: %sNrz;cloudlinux-statistics failed with exit code: %i, output: %s)rrrfr(r�)rr+rr,r�r�rororp�_run_cloudlinux_statistics8s
z,CloudlinuxSummary._run_cloudlinux_statisticsc
Cs�|�ddddg�}|dkrdSyt�|�}|d}|d}Wn8tttfk
rt}zt�d	t|��dSd}~XYnXt	|�t	|�fS)
zp
        Get number of users and resellers with faults for the past 24h
        :rtype: tuple[int, int]
        z
--by-faultr}z--jsonz--period=1dN)NNrNr�zISomething really bad happened to cloudlinux-statistics, The reason is: %s)
rr�r)r�r�r�rfr(r\r:)rhr,Z	json_datarNr�r�rororprPLs

z5CloudlinuxSummary.get_users_and_resellers_with_faultscCsV|dkrdS|��}|�d�r"dS|�d�r0dS|�d�r>dS|��rJdSd|SdS)zGet config cpu limit formatN�%�speedZmhzZghzZold_cpu_formatzunknown: %s)�lowerr�isdigit)�clsr[Zunitrororpr\as


z&CloudlinuxSummary._get_cpu_limit_unitscCs|dkrdSd|dS)z/Convert memory limit from mempages to megabytesNr.iro)r�rororpr]ssz!CloudlinuxSummary._mempages_to_mbcCs8|dkrdStj||pdd�}|dkr*dSt|dd�S)z,Convert cpu and ncpu to percents of one coreNr)ZlncpugY@r-)rZconvert_to_kernel_format�round)r[rTrrororprZzsz(CloudlinuxSummary._cpu_limit_to_percentscCs�|jdkr|ttdd�|j��D��td�d�}g}x6t|td�d�D]"\}}tt|��}|�||f�qDW|j	dd�||_|jS)zC
        Return list of tuples [users_in_package, package]
        Ncss|]}|jjdk	r|VqdS)N)�packager8)r�r�rororpr��sz>CloudlinuxSummary.get_users_amount_per_plan.<locals>.<genexpr>r)�keyT)�reverse)
r��sortedrQr�rrr	r:r~r)rhrUZusers_sorted_by_packager�r�groupZ	num_usersrororp�get_users_amount_per_plan�s
z+CloudlinuxSummary.get_users_amount_per_plancCs,y|�|�|dStk
r&dSXdS)Nr-)r�
IndexError)rhZ
number_of_toprUrororpre�sz5CloudlinuxSummary._get_top_package_by_number_of_userscCs"t|�\}}|s|�t|��|S)z%
        Parse CLI arguments
        )r.r�r()rhrr�r�rororpr�szCloudlinuxSummary._parse_argsr�rcCs8|t��|d�}|dk	r ||d<t|d�t�|�dS)z?
        Print data in default format for web and exit
        )rmr�r�NZstatistic_enabledT)r�r'�sysr�)rmr�Z	exit_coder7r�rororpr��s
z(CloudlinuxSummary._print_result_and_exitr-cCs*|�dt��i�t|d�t�|�dS)z�
        Print error and exit
        :param dict message: Dictionary with keys "result" as string and optional "context" as dict
        r�TN)r�r�r'rr�)r�r�rororpr��s
z!CloudlinuxSummary._error_and_exitcCs6tj�d�r2tjddgdtjtjd�}t�|j�SdS)zD
        Return mod_lsapi info from switch_mod_lsapi script
        z/usr/bin/switch_mod_lsapiz--statT)r�r�r�N)	r�r�r�rrr�r�r)r�)r�rororprk�s

z$CloudlinuxSummary.get_raw_lsapi_infocCsdd}t�r|d7}tj�|�r`tj|dgdtjtjd�}t�|j	��
d�}dd�|��D�}|Sd	S)
zd
        Return wmt-api config
        if is_solo_edition True, run wpt-api-solo is command
        z-/usr/share/web-monitoring-tool/wmtbin/wmt-apiz-soloz--config-getT)r�r�r�r�cSsi|]\}}t|�|�qSro)r\)r��kr�rororpr��sz9CloudlinuxSummary._get_wmt_api_config.<locals>.<dictcomp>N)rr�r�r�rrr�r�r)r�r�r9)Zcommandr�Zwmt_api_config_summaryrororprm�sz%CloudlinuxSummary._get_wmt_api_configcCstj�d�S)Nz/etc/cloudlinux-edition-solo)r�r�r�rorororpr��sz(CloudlinuxSummary._is_solo_marker_existscCs0tj�d�r,tjddgddd�}t�|j�SdS)zL
        Return statistics from cloudlinux-ssa-manager get-stat API
        z /usr/sbin/cloudlinux-ssa-managerzget-statT)r��capture_outputN)r�r�r�rrr�r)r�)r�rororpr��s

zCloudlinuxSummary.get_ssa_statscCs:d}tj�|�r6tj|dgddd�}t�|j��d�SdS)zE
        Return statistics from cl-smart-advice counters API
        z/usr/sbin/cl-smart-adviceZcountersT)r�rr�N)	r�r�r�rrr�r)r�r�)�utilr�rororp�get_smart_advice_stats�sz(CloudlinuxSummary.get_smart_advice_statsc
Cs~tddd�}tdd�sz|j�|jdd�}|dk	rzyt|d|dd�Stk
rx}zt�d	t|�|�|Sd}~XYnX|S)
z=
        Construct dict with Smart Advice statistics
        N)Zsmart_advice_totalZsmart_advice_appliedT)Zskip_marker_checkrbz!Can't get Smart Advice statisticsr�ZappliedzAUnexpected json response from server, field %s is missing in "%s")	r�rr�rqr!r�rfrgr\)rhrmZsa_statsr�rororpr�	s&
z.CloudlinuxSummary._get_smart_advice_statisticscCs4d}tj�|�r0tj|dgddd�}t�|j�SdS)zE
        Return statistics from cl-smart-advice counters API
        z/usr/bin/clwpos-adminzget-statT)r�rN)r�r�r�rrr�r)r�)r r�rororpr)sz CloudlinuxSummary.get_wpos_stats)r�NrN)r-)�rsrtrur�r��	itertools�compressrZis_panel_feature_supportedrZPYTHON_SELECTORZ
RUBY_SELECTORZNODEJS_SELECTORr�r�r�r�r��staticmethodr��propertyr�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�rrr
r/r4r�rrrr]r6r!r�rr"rr�r5r6r>r=rBrCrLrMrOrRr`rbrfrgrlrnrurtr\r3rvr5rrsr2rr�rr�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r4r0r�r�r�r�r�r�r�r�r�r3rrr�rrrP�classmethodr\r]rZrrerr�r�rkrmr�r�r!r�rrorororpr��s


	7	z+(
-"DAj
T:	(%
#'LR



v
-&! ,F-


 r�cCst�}|j��|S)N)r,rHZ
link_xml_node)r�rororpr�8s
r�)�Z
__future__rrr"r�builtinsrr�r�rr'r�r�r�rr�r�r��	functoolsrr�collectionsrrr	Zfuture.utilsr
�operatorrZmultiprocessingrZsocketr
Zpathlibrr�Zcldetectlibr�rZclcommonrZclcommon.constrZclcommon.librZclcommon.lib.cleditionrrrrrZclcommon.utilsrrrrrrrrrZclcommon.clwpos_libr r!r"Zclcommon.sysctlr#Zclconfigr$r%r&Z	cli_utilsr'r(Zcllimitslib_v2r)r*Z
clveconfigr+Zlveapir,Zcl_summary_arg_parser.�typingr/r0r1r2r3r4r5r6Zclsummary.cl_summary_utilsr7r8r9r:r;r<r=r>r?r@rArBrCrDrEZ!clsummary.rpm_packages_statisticsrFZclsummary.hardware_statisticsrGrHrIZclwizard.modulesrJZ	lve_utilsrKr�Zvendors_api.configrLrMZ	cldiaglibrNZcl_proc_hidepidrOZclselect.clselectstatisticsrPrQrRrSrTrUZclselector.selectorlibrVZclselect.clselectctlrWZ
lvemanagerr�rXr��ImportErrorr�rrfr�r�r�r@r�r�r�ZERRORr�r[r�r��objectr�r�rorororp�<module>s�,(D 

Rb

Zerion Mini Shell 1.0