
­­­­­­­­­­­­­­­­­­
<!DOCTYPE html>
<html>
3
{j:                 @   s   d dl Z d dlZd dlZd dlZd dlZd dlZd dlmZ d dlm	Z	 d dlm
Z
 d dlmZ d dlmZmZmZmZmZ d dlmZmZmZmZmZmZmZ d dlZG dd	 d	eZdddZdddZdddZdddZdd Z dd Z!dd Z"dS )    N)get_cp)criu_settings_malfunctions)lsapi_settings_malfunctions)liblsapi_malfunctions)cpanel_whmapiplesk_bin_php_handlerdump_loaded_modules
dump_lsapiquery_strings)get_da_domainsget_da_php_optionsread_da_configliblsapi_pathpretty_version_keyscount_domainsStatUtilsExceptionc               @   s   e Zd ZdS )ModLsapiStatExceptionN)__name__
__module____qualname__ r   r   //usr/share/lve/modlscapi/user/mod_lsapi_stat.pyr      s   r   Fc             C   s  d}i }t  }yd|jdkr8tjjdr2t |d< qpd}n8|jdkrNt |d< n"|jdkrdt |d< ndj|j}W n* t	k
r } zt
|}W Y dd}~X nX |dk	rt |d< ||d	< t
||d
< |jt  |jt  |jdt|d j i t| | rtj|ddS |S dS )a  
    Return statistics data
    :param as_json: return json string if True, dict otherwise
    :return: statistics data dict(
                `controlPanel`: EA3/EA4/Plesk/DirectAdmin/Unknown CP,
                `criu`: dict(
                        `status`: running/stopped,
                        `version`: version
                    ),
                `domainStat`: dict(
                        `version`: `domains_num`,
                        ...
                    ) or dict(`error`: description) if some error occurred,
                `lsapiConf`: dict(
                        lsapi_with_connection_pool: on/off,
                        lsapi_criu: on/off
                    )
            ) or its json-packed version
    NcPanelz/etc/cpanel/ea4/is_ea4Z
domainStatz7mod_lsapi domains stat is currently unsupported for EA3PleskDirectAdminz7mod_lsapi domains stat is currently unsupported for {0}ZdomainStatErrorZcontrolPanelZtotalDomainT)Z	sort_keys)r   nameospathexistsget_cpanel_ea4_statget_plesk_statget_da_statformatr   strdictupdateget_lsapi_confget_crui_statsumvaluesanalyze_malfunctionsjsondumps)as_jsonerrorZstatsZcontrol_paneler   r   r   get"   s4    



r0   Tc             C   s`  t  }t  }t  }y
tdjd}x@|D ]8}|j|jdt j|jd |jd||jd< q*W tdjd}t dd	 |D }t|j }	x6|j D ]*\}
}|j|t  j	|
|j|
t i qW |rt
j|||}|}x:|j D ].\}}|j	t jt|	jt|j t  qW W n4 tttfk
rR   td
jtj jdY nX t||	| S )aY  
    Collect mod_lsapi statistics for cPanel EA4 through WHM API
    :param lsapi_only: return only lsapi domains statistics if True, or full statistics if False
    :param with_selector: take into account the statistics of php selector
    :return: if lsapi_only return lsapi domains per version
                stat dict(
                    `version`: `domains_num`,
                    ...
                )
            else return full statistics per handler
                stat dict(
                    `handler`: {`version`: `domains_num`, ... }
                    ...
                )
    Zphp_get_vhost_versionsZversionsversionvhostZaccountZphp_get_handlersversion_handlersc             S   s    g | ]}|j d |j dfqS )r1   Zcurrent_handler)r0   ).0hr   r   r   
<listcomp>t   s    z'get_cpanel_ea4_stat.<locals>.<listcomp> 
)r$   r   r0   
setdefaultsetaddlistkeysitemsr%   selector_usage_libZea4_selector_checkfromkeys
differenceKeyError	TypeErrorr   r   join	traceback
format_excsplitr   )
lsapi_onlywith_selectorZdomains_per_versionZhandlers_statZdomain_usersZvhosts_datar2   Zhandlers_datar3   Zall_versionsZverhandlerZs_checked_version_handlersr5   vr   r   r   r   Y   s*    
 &.r   c             C   s  d}d}t  }ytd}dd |D }x||D ]t}|jd}dj|jdjd	d
d }	tdd td|dD }
d|krd}|j|	d}n|j|	d}|
||< q,W |rtj||}W n8 t	t
tttjfk
r   tdjtj jdY nX ddd |j D i}| r|d S |S )a  
    Collect mod_lsapi statistics for Plesk
    Collects lsapi domains ONLY
    :param lsapi_only: return only lsapi domains statistics if True, or full statistics if False
    :param with_selector: take into account the statistics of php selector
    :return: if lsapi_only return lsapi domains per version dict(
                    `version`: `domains_num`
                    ...
                )
            else return stats with handler dict(
                    `lsapi`: {`version`: `domains_num`, ... }
                )
    z
alt-php{v}z	alt-php56r<   c             S   s   g | ]}d |j dkr|qS )Zlsphpid)r0   )r4   r5   r   r   r   r6      s    z"get_plesk_stat.<locals>.<listcomp>rL   r7   ZfullVersion.N   c             S   s   g | ]}|j d qS )domain)r0   )r4   dr   r   r   r6      s    z	get-usage)rL   Zcustom)rK   r8   Zlsapic             S   s   i | ]\}}t ||qS r   )len)r4   krK   r   r   r   
<dictcomp>   s    z"get_plesk_stat.<locals>.<dictcomp>)r$   r   r0   rD   rG   r:   r"   r?   Zplesk_selector_checkrB   rC   AttributeErrorr   ZSelectorStatExceptionr   rE   rF   r>   )rH   rI   Zhandler_tmplZcustom_versionZdomain_version_statZall_handlersZlsphp_handlersrJ   Z
handler_idZphp_versiondomainsZ
version_idZresult_statr   r   r   r       s*    

r    c                s   d}t  }yt }|d d }|d d }|d d }|d d }t|}	t|}
t }t   fdd|j D  |dkr|	 i}|	 i||< n|dkr|
 i}|
 i||< n|	t |
t i}x|j D ]\}}x|D ]x}|j||d	}y<t|\}}|j	|d
}t|| d }|j
|j| W q tjk
rR   |j
|	j| Y qX qW qW ||kr|	||	 |
||
 i||< n,|	||	 |
t i||< |
||
 |	t i||< |rtj|j
|	||	}|j
|j| W n6 ttttfk
r   tdjtj jdY nX t||	|
g| S )aN  
    Collect lsapi domains statistics fro DirectAdmin
    :param lsapi_only: return only lsapi domains statistics if True, or full statistics if False
    :param with_selector: take into account the statistics of php selector
    :return: if lsapi_only return lsapi domains per version
                stat dict(
                    `version`: `domains_num`,
                    ...
                )
            else return full statistics per handler
                stat dict(
                    `handler`: {`version`: `domains_num`, ... }
                    ...
                )
    z>/usr/local/directadmin/data/users/{user}/domains/{domain}.confrN   r1      rJ   c                s   g | ]} j |qS r   )r%   )r4   rK   )joinedr   r   r6      s    zget_da_stat.<locals>.<listcomp>no)userrO   Zphp1_selectr7   r8   )r$   r   r   r   r:   r)   r>   r"   r   Zgetintr0   r;   configparserZNoOptionErrorr?   Zda_selector_checkr%   rB   rC   rU   r   r   rD   rE   rF   rG   r   )rH   rI   Zdomain_conf_pathZhandler_statZphp_optionsZphp1_verZphp2_verZphp1_handlerZphp2_handlerZ
php1_labelZ
php2_labelZuser_domainsZversion_statrZ   rV   rO   Zconfig_pathZconf_parserZglobal_sectionZphp_settingr1   Zselector_updatedr   )rX   r   r!      sV    

r!   c              C   sn   dj tjd} y"tjdddgtjtjd d}W n tjk
rL   d}Y nX d|| rd| jd	d
 nddiS )z
    Get criu service info
    :return: dict(
                `criu`: dict(
                    `status`: running/stopped,
                    `version`: version
                )
            )
    r7   z/usr/sbin/criu -Vz/sbin/servicecriustatus)stdoutstderrZrunningstopped rN   znot installed)r]   r1   )rD   exec_command
subprocessZ
check_callZDEVNULLZCalledProcessErrorrG   )Zcriu_versionZcriu_service_statusr   r   r   r'     s    

r'   c              C   sT   t  } t }|jdd}| jdd| jddd| jddtt d|sLd	nd
dS )al  
    Retrieve lsapi configuration:
    - crui
    - connection pool
    and versions of module and library
    :return: dict(
                lsapiConf: dict(
                    lsapi_with_connection_pool: on/off,
                    lsapi_criu: on/off
                ),
                modVersion: version,
                libVersion: version
            )
    Zlsapi_moduleN
lsapi_criuZofflsapi_with_connection_pool)rd   re   r1   Zliblsapi_versionZdisabledZenabled)	lsapiConfZ
modVersion
libVersion	modStatus)r	   r   r0   r
   r   )Zapache_confZapache_modulesZ
mod_statusr   r   r   r&     s    


r&   c                st   t    fdd}dj| d d | d d d}| d	 }|t| d
  |t| |t|  sbdS | jd i dS )z
    Detect configuration malfunctions and update resulting statistics dict accordingly
    For now only criu malfunctions are presented
    See malfunctions in dashboard_malfunctions.py module
    :param stats_dict: resulting statistics dict
    c                s,   y j | |  W n tk
r&   Y nX d S )N)appendrB   )Zmalfunc_dictkey)malfunctionsr   r   add_malfunctionH  s    z-analyze_malfunctions.<locals>.add_malfunctionz{opt}_{serv}rf   rd   r\   r]   )optZservrh   rg   Nrk   )r<   r"   r   r   r   r%   )Z
stats_dictrl   Zcriu_settingsZlsapi_settingsr   )rk   r   r*   ?  s    

r*   )F)TT)TT)TT)#r   r+   rE   rc   r[   rb   lve_diagnosticr   Zdashboard_malfunctionsr   r   r   
stat_utilsr   r   r   r	   r
   r   r   r   r   r   r   r   r?   	Exceptionr   r0   r   r    r!   r'   r&   r*   r   r   r   r   <module>   s(   $
7
1
,
O 