#!/usr/bin/python

#
# Copyright CloudLinux Zug GmbH 2010-2015 All Rights Reserved
#
# Licensed under CLOUD LINUX LICENSE AGREEMENT
# http://cloudlinux.com/docs/LICENCE.TXT
#

import os

CP_NAME = None

def getCPName():
    """
    Detect control panel and return its name
    """
    global CP_NAME
    if CP_NAME:
        return CP_NAME

    # cPanel check
    if os.path.isfile('/usr/local/cpanel/cpanel'):
        CP_NAME = 'cPanel'

    # Plesk check
    elif os.path.isfile('/usr/local/psa/version'):
        CP_NAME = 'Plesk'

    # DirectAdmin check
    elif os.path.isfile('/usr/local/directadmin/directadmin'):
        CP_NAME = 'DirectAdmin'

    # ISPmanager v4 or v5 check
    elif os.path.isfile('/usr/local/ispmgr/bin/ispmgr') or os.path.isfile('/usr/local/mgr5/sbin/mgrctl'):
        CP_NAME = 'ISPManager'

    # InterWorx check
    elif os.path.isfile('/usr/local/interworx/iworx.ini'):
        CP_NAME = 'InterWorx'

    # HSphere check
    elif os.path.isdir('/hsphere/shared'):
        CP_NAME = 'H-Sphere'
    elif os.path.isfile('/lib64/libnss_ng.so'):
        CP_NAME = 'HostingNG'

    # CentOS Web Panel check
    elif os.path.isfile('/etc/rc.d/init.d/cwpsrv'):
        CP_NAME = 'CentOS_WEB_Panel'

    elif os.path.isfile('/etc/httpd/conf.d/atomia-pa-apache.conf') or os.path.isdir('/storage/configuration/cloudlinux'):
        CP_NAME = 'Atomia_agent'

    else:
        CP_NAME = 'Unknown'

    return CP_NAME


def add_server_stats(status_report):
    """
    Add server statistics to status_report dict
    :param status_report: dict to add statistics to
    :type status_report: dict
    """
    from clcommon import ClPwd
    res = {}
    cp_name = getCPName()
    if cp_name != 'Unknown':
        res['cp'] = cp_name
    if cp_name == 'Plesk':
        clpwd = ClPwd(10000)
    else:
        clpwd = ClPwd()
    d = clpwd.get_uid_dict()
    users = 0
    sys_users = set(['nfsnobody', 'avahi-autoipd', 'exim', 'clamav', 'varnish', 'nagios', 'saslauth', 'mysql', 'lsadm', 'systemd-bus-proxy',
                'systemd-network', 'polkitd', 'firebird', 'nginx', 'dovecot', 'dovenull', 'roundcube_sysuser', 'avahi-autoipd',
                'cpanel', 'cpanelhorde', 'cpanelphpmyadmin', 'cpanelphppgadmin', 'cpanelroundcube', 'mailman', 'cpaneleximfilter',
                'cpanellogaholic', 'cpanellogin', 'munin', 'cpaneleximscanner', 'cpanelphpgadmin', 'cpses', 'cpanelconnecttrack',
                'cpanelrrdtool', 'admin', 'webapps' 'apache', 'diradmin', 'majordomo', 'viapm', 'iworx', 'iworx-web', 'iworx-pma',
                'iworx-backup', 'iworx-horde', 'iworx-roundcube', 'iworx-sqmail', 'iworx_support_user', 'psaadm', 'popuser',
                'psaftp', 'drweb', 'sw-cp-server', 'horde_sysuser'])
    for uid in d:
        found = False
        for entry in d[uid]:
            if entry.pw_name in sys_users:
                found = True
                break
        if not found:
            users += 1
    res['users'] = users
    status_report['cln'] = res
