a Ð_qiÈ:ã@süddlZddlZddlZddlZddlZddlZddlmZddlm Z ddlm Z ddlm Z ddl m Z mZmZmZmZddl mZmZmZmZmZmZmZddlZGdd „d eƒZdd d „Zddd„Zddd„Zddd„Zdd„Z dd„Z!dd„Z"dS)é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 eZdZdS)ÚModLsapiStatExceptionN)Ú__name__Ú __module__Ú __qualname__©rrú//usr/share/lve/modlscapi/user/mod_lsapi_stat.pyrsrFc Csd}i}tƒ}zd|jdkr8tj d¡r2tƒ|d<qpd}n8|jdkrNtƒ|d<n"|jdkrdtƒ|d<n d |j¡}Wn,t yž}zt |ƒ}WYd}~n d}~00|durºt ƒ|d<||d <t |ƒ|d <|  t ƒ¡|  tƒ¡|  d t|d ¡ƒi¡t|ƒ|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 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}ZdomainStatErrorZ 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ÚerrorZstatsZ control_panelÚerrrÚget"s4           r0Tc CsPtƒ}tƒ}tƒ}zþtdƒ d¡}|D]8}| | d¡tƒ¡ | d¡¡| d¡|| d¡<q&tdƒ d¡}tdd „|Dƒƒ}t| ¡ƒ} | ¡D]*\} } | | tƒ¡  | | | tƒ¡i¡q”|rÖt   |||¡} | }| ¡D].\} }|  t  t| ƒ  t| ¡ƒ¡tƒ¡¡qÞWn2tttfyBtd  t ¡ d ¡¡ƒ‚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_versionsZversionsÚversionÚvhostZaccountZphp_get_handlersÚversion_handlerscSs g|]}| d¡| d¡f‘qS)r1Zcurrent_handler©r0©Ú.0ÚhrrrÚ tóz'get_cpanel_ea4_stat..ÚÚ )r$rr0Ú setdefaultÚsetÚaddÚlistÚkeysÚitemsr%Úselector_usage_libZea4_selector_checkÚfromkeysÚ differenceÚKeyErrorÚ TypeErrorrrÚjoinÚ tracebackÚ format_excÚsplitr)Ú lsapi_onlyÚ with_selectorZdomains_per_versionZ handlers_statZ domain_usersZ vhosts_datar2Z handlers_datar3Z all_versionsZverÚhandlerZs_checked_version_handlersr7ÚvrrrrYs*  $,rc Csd}d}tƒ}z¤tdƒ}dd„|Dƒ}|D]t}| d¡}d | d¡ d ¡d d …¡} td d„td |dDƒƒ} d|vrŠd} |j| d}n |j| d} | || <q*|r°t ||¡}Wn6t t t t tj fyètd t ¡ d¡¡ƒ‚Yn0ddd„| ¡Dƒi} |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| d¡vr|‘qS)ZlsphpÚidr4r5rrrr8žr9z"get_plesk_stat..rOr:Z fullVersionÚ.NéÿÿÿÿcSsg|]}| d¡‘qS)Údomainr4)r6Údrrrr8¢r9z get-usage)rOZcustom)rNr;ZlsapicSsi|]\}}|t|ƒ“qSr)Úlen)r6ÚkrNrrrÚ ±r9z"get_plesk_stat..)r$rr0rGrJr=r"rBZplesk_selector_checkrErFÚAttributeErrorrZSelectorStatExceptionrrHrIrA) rKrLZ handler_tmplZcustom_versionZdomain_version_statZ all_handlersZlsphp_handlersrMZ handler_idZ php_versionÚdomainsZ version_idZ result_statrrrr Šs,   ÿr c sd}tƒ}zÂtƒ}|dd}|dd}|dd}|dd}t|ƒ} t|ƒ} tƒ} tƒ‰‡fdd„|  ¡Dƒ|dkr–| ˆi} | ˆi||<n |dkr´| ˆi} | ˆi||<nî| tƒ| tƒi} |  ¡D]„\} }|D]v}|j| |d }z/usr/local/directadmin/data/users/{user}/domains/{domain}.confér1érMcsg|]}ˆ |¡‘qSr)r%)r6rN©Zjoinedrrr8Ör9zget_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!cCsjd t d¡¡}z tjgd¢tjtjdd}WntjyHd}Yn0d||r`| d¡d 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/serviceÚcriuÚstatus)ÚstdoutÚstderrZrunningÚstoppedr^ú rYz not installed)r_r1)rGÚ exec_commandÚ subprocessZ check_callZDEVNULLZCalledProcessErrorrJ)Z criu_versionZcriu_service_statusrrrr's  þÿr'cCsTtƒ}tƒ}| dd¡}| dd¡| dd¡dœ| dd¡ttƒdƒ|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_criuZoffÚlsapi_with_connection_pool)rfrgr1Zliblsapi_versionZdisabledZenabled)Ú lsapiConfZ modVersionÚ libVersionÚ modStatus)r rr0r r)Z apache_confZapache_modulesZ mod_statusrrrr&s   þ   ùr&csttƒ‰‡fdd„}dj|dd|ddd}|d }|t|d ƒ|t|ƒ|t|ƒˆsbd S| 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 cs*zˆ ||¡Wnty$Yn0dS)N)ÚappendrE)Z malfunc_dictÚkey©Ú malfunctionsrrÚadd_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]rdÚlve_diagnosticrZdashboard_malfunctionsrrrÚ stat_utilsrrrr r r r r rrrrrBÚ Exceptionrr0rr r!r'r&r*rrrrÚs(    $ 7 1 , O