ÿØÿà JFIF      ÿÛ „ 	 ( %!1!%)+//.383,7(-.+



-%%-////---/-.+/--+------/------/--0+--/-/-----.-----ÿÀ  ¥2" ÿÄ               ÿÄ J  	     ! 1AQ"aq2‘#BR‚¡ÁÑ3br’¢±Âð$CSƒ²á4c“%DsÓñÿÄ              ÿÄ *        !1AQa‘"2q3±ð#b¡ÿÚ   ? ¼QxJQaÍuò¸Zö Úü8,ÐÚú
"SSn<rçù–´âE—^ªBÖ9À\†¸ÔÁT­ÃÛ5
ëd´³Í#Ý;Þ38œî ¶H£M:wÎ3…³…âpÔF&‚FK¸9„â4àGEõªfÿ ‘ñ(ßw­pŽF|È¥ù®häðÍÑ¶¹‘[ÒinÙW¶ùñY˜Q{›K"išÒ[Ú8žë\F¹@-?v"ÔU”,ìöžkÿ {I‡£šÍ?e
ríV
..............................................................................................................................................................................
.............................................................................                                                  
                                                                                                                                                                                     ÿØÿà JFIF      ÿÛ „ 	 ( %!1!%)+//.383,7(-.+



-%%-////---/-.+/--+------/------/--0+--/-/-----.-----ÿÀ  ¥2" ÿÄ               ÿÄ J  	     ! 1AQ"aq2‘#BR‚¡ÁÑ3br’¢±Âð$CSƒ²á4c“%DsÓñÿÄ              ÿÄ *        !1AQa‘"2q3±ð#b¡ÿÚ   ? ¼QxJQaÍuò¸Zö Úü8,ÐÚú
"SSn<rçù–´âE—^ªBÖ9À\†¸ÔÁT­ÃÛ5
ëd´³Í#Ý;Þ38œî ¶H£M:wÎ3…³…âpÔF&‚FK¸9„â4àGEõªfÿ ‘ñ(ßw­pŽF|È¥ù®häðÍÑ¶¹‘[ÒinÙW¶ùñY˜Q{›K"išÒ[Ú8žë\F¹@-?v"ÔU”,ìöžkÿ {I‡£šÍ?e
ríV
..............................................................................................................................................................................
.............................................................................                                                  
                                                                                                                                                                                     import os
from subprocess import PIPE, Popen


def get_users_count_from_cllib():
    """
    Get user count using the common CloudLinux library.
    This number is more accurate for systems
    with a control panel installed.
    """
    if not os.path.exists('/opt/cloudlinux/venv/bin'):
        raise ValueError("CloudLinux virtual environment not found")

    cmd = '/opt/cloudlinux/venv/bin/python3 -c "from clcommon.cpapi import cpusers; print(cpusers())"'
    process = Popen(cmd, shell=True, stdout=PIPE, stderr=PIPE)
    output, errors = [result.decode().strip() for result in process.communicate()]

    if errors:
        raise ValueError(f"Failed to get users from CloudLinux library: {errors}")

    return len(output[1:-1].split(', '))


def get_users_count_generic():
    """
    Fallback method to get the user count
    by looking into the /etc/passwd file.
    """
    from up2date_client.clpwd import ClPwd
    pwd = ClPwd()
    return len(pwd.get_uid_dict())


def count_server_users():
    """
    Get the total count of users on the server.
    Tries the CloudLinux library first, falls back to generic method if it fails.
    """
    try:
        users_count = get_users_count_from_cllib()
    except Exception:
        users_count = get_users_count_generic()
    return users_count
