|
Server : Apache System : Linux cvar2.toservers.com 3.10.0-962.3.2.lve1.5.73.el7.x86_64 #1 SMP Wed Aug 24 21:31:23 UTC 2022 x86_64 User : njnconst ( 1116) PHP Version : 8.4.18 Disable Function : NONE Directory : /proc/self/root/opt/alt/python37/lib/python3.7/site-packages/clwpos/ |
Upload File : |
# -*- coding: utf-8 -*-
# Copyright © Cloud Linux GmbH & Cloud Linux Software, Inc 2010-2021 All Rights Reserved
#
# Licensed under CLOUD LINUX LICENSE AGREEMENT
# http://cloudlinux.com/docs/LICENSE.TXT
import os
import pwd
from typing import List
from clcommon import cpapi
from clcommon.clpwd import drop_privileges
from clwpos.logsetup import setup_logging
from clwpos.optimization_modules import (
get_allowed_modules,
ALL_OPTIMIZATION_MODULES
)
from clwpos.utils import get_admin_options
from clwpos.user.config import UserConfig
_logger = setup_logging('clwpos_statistics')
def get_sites_count_with_enabled_wpos_statistics(user: str, checked_modules: List[str]) -> int:
"""
Calculates total count of sites with at least one enabled
module per user
"""
count = 0
try:
with drop_privileges(user):
uc = UserConfig(user)
count = uc.get_enabled_sites_count_by_modules(checked_modules)
except Exception as e:
_logger.warning('Error while getting info from clwpos user config %s', str(e))
return count
return count
def get_users_count_with_allowed_wpos_statistics(user: str, checked_modules: List[str]) -> int:
"""
Checks if there are any allowed modules for user
"""
try:
uid = pwd.getpwnam(user).pw_uid
except KeyError:
_logger.debug('Unable to get uid for %s', user)
return False
allowed_modules = get_allowed_modules(uid)
return any(checked_module in allowed_modules for checked_module in checked_modules)
def _get_wpos_statistics_total_count(modules: List[str], kind: str = 'allowed'):
"""
Returns total count of users with allowed wpos module
or total count of sites with enabled wpos module
for all panel users on server
"""
total_count = 0
panel_users = cpapi.cpusers()
for user in panel_users:
if kind == 'allowed':
if get_users_count_with_allowed_wpos_statistics(user, modules):
total_count += 1
else:
total_count += get_sites_count_with_enabled_wpos_statistics(user, modules)
return total_count
def is_accelerate_wp_icon_enabled():
"""
Obtains admin`s options, e.g show_icon
"""
try:
options = get_admin_options()
except Exception as e:
_logger.error('Error when getting admin options: %s', str(e))
return -42
return options.get('show_icon')
def fill_current_wpos_statistics():
"""
Returns current statistics with enabled sites/allowed users counters
per module and in total
"""
is_feature_flag = os.path.isfile('/var/lve/enable-wpos.flag')
is_feature_icon_enabled = is_accelerate_wp_icon_enabled()
result = {
'allowed_users': dict(),
# total:
# object_cache:
# site_optimization:
'enabled_sites': dict(),
# total:
# object_cache:
# site_optimization:
'is_accelerate_wp_flag_enabled': is_feature_flag,
'is_accelerate_wp_icon_enabled': is_feature_icon_enabled
}
result['allowed_users']['total'] = _get_wpos_statistics_total_count(
ALL_OPTIMIZATION_MODULES,
'allowed')
result['enabled_sites']['total'] = _get_wpos_statistics_total_count(
ALL_OPTIMIZATION_MODULES, 'enabled')
for module in ALL_OPTIMIZATION_MODULES:
result['allowed_users'][module] = _get_wpos_statistics_total_count(
[module], 'allowed')
result['enabled_sites'][module] = _get_wpos_statistics_total_count(
[module], 'enabled')
return result