a q?h:@sddlZddlZddlZddlZddlZddlZddlmZddlm Z ddlm Z ddlm Z ddl m Z mZmZmZmZddl mZmZmZmZmZmZmZddlZGdd d eZdd d ZdddZdddZdddZddZ ddZ!ddZ"dS)N)get_cp)criu_settings_malfunctions)lsapi_settings_malfunctions)liblsapi_malfunctions) cpanel_whmapiplesk_bin_php_handlerdump_loaded_modules dump_lsapi query_strings)get_da_domainsget_da_php_optionsread_da_config liblsapi_pathpretty_version_keys count_domainsStatUtilsExceptionc@s eZdZdS)ModLsapiStatExceptionN)__name__ __module__ __qualname__rr//usr/share/lve/modlscapi/user/mod_lsapi_stat.pyrsrFc Csd}i}t}zd|jdkr8tjdr2t|d<qpd}n8|jdkrNt|d<n"|jdkrdt|d<n d|j}Wn,t y}zt |}WYd}~n d}~00|durt |d<||d <t ||d <| t | t| d t|dit||rtj|d d S|SdS)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 NcPanelz/etc/cpanel/ea4/is_ea4Z domainStatz7mod_lsapi domains stat is currently unsupported for EA3Plesk DirectAdminz7mod_lsapi domains stat is currently unsupported for {0}ZdomainStatErrorZ controlPanelZ totalDomainT)Z sort_keys)rnameospathexistsget_cpanel_ea4_statget_plesk_stat get_da_statformatrstrdictupdateget_lsapi_conf get_crui_statsumvaluesanalyze_malfunctionsjsondumps)as_jsonerrorZstatsZ control_panelerrrget"s4           r0Tc CsPt}t}t}ztdd}|D]8}||dt|d|d||d<q&tdd}tdd |D}t|} |D]*\} } || t | || tiq|rt |||} | }|D].\} }| t t|  t|tqWn2tttfyBtd td Yn0t|| |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`, ... } ... ) Zphp_get_vhost_versionsZversionsversionvhostZaccountZphp_get_handlersversion_handlerscSs g|]}|d|dfqS)r1Zcurrent_handlerr0.0hrrr tz'get_cpanel_ea4_stat.. )r$rr0 setdefaultsetaddlistkeysitemsr%selector_usage_libZea4_selector_checkfromkeys differenceKeyError TypeErrorrrjoin traceback format_excsplitr) lsapi_only with_selectorZdomains_per_versionZ handlers_statZ domain_usersZ vhosts_datar2Z handlers_datar3Z all_versionsZverhandlerZs_checked_version_handlersr7vrrrrYs*  $,rc Csd}d}t}ztd}dd|D}|D]t}|d}d|dd d d } td dtd |dD} d|vrd} |j| d}n |j| d} | || <q*|rt||}Wn6t t t t tj fytdtdYn0ddd|Di} |r| dS| 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?cSsg|]}d|dvr|qS)Zlsphpidr4r5rrrr8r9z"get_plesk_stat..rOr:Z fullVersion.NcSsg|]}|dqS)domainr4)r6drrrr8r9z get-usage)rOZcustom)rNr;ZlsapicSsi|]\}}|t|qSr)len)r6krNrrr r9z"get_plesk_stat..)r$rr0rGrJr=r"rBZplesk_selector_checkrErFAttributeErrorrZSelectorStatExceptionrrHrIrA) rKrLZ handler_tmplZcustom_versionZdomain_version_statZ all_handlersZlsphp_handlersrMZ handler_idZ php_versiondomainsZ version_idZ result_statrrrr s,   r c sd}t}zt}|dd}|dd}|dd}|dd}t|} t|} t} tfdd| D|dkr| i} | i||<n |dkr| i} | i||<n| t| ti} | D]\} }|D]v}|j| |d }z/usr/local/directadmin/data/users/{user}/domains/{domain}.confr1rMcsg|]}|qSr)r%)r6rNZjoinedrrr8r9zget_da_stat..Zno)userrRZ php1_selectr:r;)r$r rr r=r)rAr"r Zgetintr0r> configparserZ NoOptionErrorrBZda_selector_checkr%rErFrWrrrGrHrIrJr)rKrLZdomain_conf_pathZ handler_statZ php_optionsZphp1_verZphp2_verZ php1_handlerZ php2_handlerZ php1_labelZ php2_labelZ user_domainsZ version_statr\rXrRZ config_pathZ conf_parserZglobal_sectionZ php_settingr1Zselector_updatedrr[rr!s\        r!cCsjdtd}z tjgdtjtjdd}WntjyHd}Yn0d||r`|dd nd d iS) z Get criu service info :return: dict( `criu`: dict( `status`: running/stopped, `version`: version ) ) r:z/usr/sbin/criu -V)z /sbin/servicecriustatus)stdoutstderrZrunningstoppedr^ rYz not installed)r_r1)rG exec_command subprocessZ check_callZDEVNULLZCalledProcessErrorrJ)Z criu_versionZcriu_service_statusrrrr's  r'cCsTt}t}|dd}|dd|ddd|ddttd|sLd nd 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_criuZofflsapi_with_connection_pool)rfrgr1Zliblsapi_versionZdisabledZenabled) lsapiConfZ modVersion libVersion modStatus)r rr0r r)Z apache_confZapache_modulesZ mod_statusrrrr&s      r&csttfdd}dj|dd|ddd}|d }|t|d |t||t|sbd S|d id 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 cs*z||Wnty$Yn0dS)N)appendrE)Z malfunc_dictkey malfunctionsrradd_malfunctionHs z-analyze_malfunctions..add_malfunctionz {opt}_{serv}rhrfr^r_)optZservrjriNrn)r?r"rrrr%)Z stats_dictroZ criu_settingsZlsapi_settingsrrmrr*?s    r*)F)TT)TT)TT)#rr+rHrer]rdlve_diagnosticrZdashboard_malfunctionsrrr stat_utilsrrrr r r r r rrrrrB Exceptionrr0rr r!r'r&r*rrrrs(    $ 7 1 , O