Mini Shell

Direktori : /usr/share/l.v.e-manager/ispmanager/liblve0.8/addon/lvemanager/
Upload File :
Current File : //usr/share/l.v.e-manager/ispmanager/liblve0.8/addon/lvemanager/system.class.php

<?php

/**
 * Copyright © Cloud Linux GmbH & Cloud Linux Software, Inc 2010-2019 All Rights Reserved
 *
 * Licensed under CLOUD LINUX LICENSE AGREEMENT
 * http://cloudlinux.com/docs/LICENSE.TXT
 */
    
    class SystemComands {
        
        private $Lve_default;
        private $DomainList = array();
        
        private $SERVER_IP = '';
        
        public function __construct() {
            if ('' != $_ENV['SERVER_ADDR'])
                $this->SERVER_IP = $_ENV['SERVER_ADDR'];
            if ('' != $_SERVER['SERVER_ADDR'])
                $this->SERVER_IP = $_SERVER['SERVER_ADDR'];
        }

	public function get_cpu_num() {
	    $numCpus = 1;
	    if (is_file('/proc/cpuinfo')) {
		$cpuinfo = file_get_contents('/proc/cpuinfo');
		preg_match_all('/^processor/m', $cpuinfo, $matches);
		$numCpus = count($matches[0]);
	    }
	    return $numCpus;
	}
    
            
        // runs system comand and ger Array Result
        private function RunCommand($command, $JustRun = 0) {
            if (0 != $JustRun) {
                ob_clean();
                ob_start ();
                passthru ($command);
                ob_end_clean ();
                return true;
            } else {
                $result = '';
                ob_clean();
                ob_start ();
                passthru ($command);
                $result = ob_get_contents();
                ob_end_clean ();
                $lveps = array();
                $result = explode(PHP_EOL,$result);
                unset($result[count($result)-1]);
                $i = 0;
                foreach ($result as $temp) {
                    $i++;
                    $temp=trim(preg_replace('/\s+/', ' ', $temp));
                    $temp = explode(' ',$temp);
                    $lveps[$i] = $temp;
                }
                unset($lveps[1]);
                return ($lveps);
            }
        }
        
        // Get array [System UID] => UserName of users in ISP manager
        private function GetIspUsers() {
            $UList = array();

//            $stringUsers = file_get_contents('https://'.$this->SERVER_IP.'/manager/ispmgr?func=user&out=text');

            $stringUsers = '';
            ob_clean();
            ob_start ();
            passthru ('/usr/local/ispmgr/sbin/mgrctl user');
            $stringUsers = ob_get_contents();
            ob_end_clean ();
           
            if ($stringUsers == '') {
                return false;
            } else {
                $result = explode(PHP_EOL,$stringUsers);
                unset($result[count($result)-1]);
                
                if (0 != count($result)) {
                    foreach ($result as $user) {
                        if (!strstr($user, 'virtuser_')) {
                            $user = explode(' ',$user);
                            ob_clean();
                            ob_start ();
                            passthru ("id ".str_replace('name=','',$user[0]).' -u');
                            $UID = ob_get_contents();
                            ob_end_clean ();
                            $UList[trim($UID)] = str_replace('name=','',$user[0]);
                        }
                    }
                }
                return $UList;
            }
        }
        
        // Get Users Domain's
        private function GetUserDomain($login) {
            $domains = '';
            
            if (empty($this->DomainList)) {

                $stringDomains = '';
                ob_clean();
                ob_start ();
                $return_val = 0;
                passthru ('/usr/local/ispmgr/sbin/mgrctl domain', $return_val );
                $stringDomains = ob_get_contents();
                ob_end_clean ();
                
                if ( $return_val != 0)
                { // Error executing mgrctl
                    return false;
                }
                if ($stringDomains == '')
                { // no domains present
                    return 'none';
                }

                $result = explode(PHP_EOL,$stringDomains);
                unset($result[count($result)-1]);
                $this->DomainList = $result;
            }
            if (!empty($this->DomainList)) {
                foreach ($this->DomainList as $row) {
                    if (strstr($row, $login) && '' == $domains) {
                        $row = explode(' ',$row);
                        $domains = str_replace('name=','',$row[0]);
                    }
                }
            }
            if ('' == $domains)
                return 'none';
            else
                return $domains;
        }
        
        // Get Default settings
        public function GetDefaultLVE() {
            if ( !function_exists ( 'simplexml_load_file' ) )
            {
                dl ('simplexml.so');
            } // if
            $default = array();
            if (file_exists('/etc/container/ve.cfg')) {
                $xml = simplexml_load_file('/etc/container/ve.cfg');
                $this->Lve_default = $xml;
                
                $default['SPEED'] = trim((string)$xml->defaults->cpu['limit']);
                if (is_numeric($default['SPEED'])){
                    $SYS_NCPU = $this->get_cpu_num();
                    $default['SPEED'] = ((string)((int)$default['SPEED'] * $SYS_NCPU)).'%';
                }
                $default['NCPU'] = trim((string)$xml->defaults->ncpu['limit']);
                $default['IO'] = trim((string)$xml->defaults->io['limit']);
                $default['MEM'] = trim((string)(round(((float)$xml->defaults->mem['limit']*4/1024),2).' MB'));
                $default['maxentryprocs'] = trim((string)($xml->defaults->other['maxentryprocs']));
                $default['default'] = 'YES';
            } else {
                return false;
            }
            return $default;
        }
        
        // Set Default LVE
        public function SetDefaultLVE($params, $request_end) {
            $params = explode($request_end,$params);
            $params = str_replace('&',' --',$params[0]);
            $params = '--'.$params;
            $params = str_replace('+','',$params);
            $params = str_replace('--cpu','--speed',$params);
            $params = str_replace('%25','%',$params);
            $params = '/usr/sbin/lvectl set default '.$params.' --save';
            return self::RunCommand($params,1);
        }
        
        public function SetUserLVE($params,$request_end) {
            // /usr/sbin/lvectl set '.$uid.' --cpu='.$cpu.' --ncpu='.$ncpu.' --mem='.$mem.' --maxEntryProcs='.$ep.' --save'
            $params = explode($request_end,$params);
            $params = str_replace('&',' --',$params[0]);
            $params = str_replace('+','',$params);
            $params = str_replace('--cpu','--speed',$params);
            $params = str_replace('%25','%',$params);
            $params = str_replace('uid=','/usr/sbin/lvectl set ',$params);
            $params .= ' --save';
            return self::RunCommand($params,1);
        }
        
        // Get User LVE settings
        public function GetUserLVE($params) {
            $UName = explode('&',$params);
            foreach ($UName as $item) {
                if (strstr($item,'ULogin')) {
                    $Ustr = $item;
                }
            }
            $UName = str_replace('ULogin=','',$Ustr);
            ob_clean();
            ob_start ();
            passthru ("id ".$UName.' -u');
            $UID = ob_get_contents();
            ob_end_clean ();

            $User['id'] = trim($UID);
            $User['login'] = trim($UName);
            
            $default = self::GetDefaultLVE();
            $xml = $this->Lve_default;
            
            $data = $default;
            $data['UserName'] = $User['login'];
            $data['UserID'] = $User['id'];
            
            foreach ($xml->lve as $child) {
                if ($User['id'] == (string)$child['id']) {
                    if (isset($child->cpu['limit'])) {
                        if ($default['SPEED'] != trim((string)$child->cpu['limit'])) {
                            $data['SPEED'] = trim((string)$child->cpu['limit']);
                            if (is_numeric($data['SPEED'])){
                                $SYS_NCPU = $this->get_cpu_num();
                                $data['SPEED'] = ((string)((int)$data['SPEED'] * $SYS_NCPU)).'%';
                            }
                            $data['default'] = 'NO';
                        }
                    }
                    if (isset($child->ncpu['limit'])) {
                        if ($default['NCPU'] != trim((string)$child->ncpu['limit'])) {
                            $data['NCPU'] = trim((string)$child->ncpu['limit']);
                            $data['default'] = 'NO';
                        }
                    }
                    if (isset($child->mem['limit'])) {
                        if ($default['MEM'] != trim((string)(round(((float)$child->mem['limit']*4/1024),2).' MB'))) {
                            $data['MEM'] = trim((string)(round(((float)$child->mem['limit']*4/1024),2).' MB*'));
                            $data['default'] = 'NO';
                        }
                    }
                    if (isset($child->other['maxentryprocs'])) {
                        if ($default['maxentryprocs'] != trim((string)$child->other['maxentryprocs'])) {
                            $data['maxentryprocs'] = trim((string)$child->other['maxentryprocs']);
                            $data['default'] = 'NO';
                        }
                    }
                }
            }

            $data['MEM'] = str_replace('MB','',trim($data['MEM']));
            return $data;
        }
        
        
        // India red for Non default values
        private function GetIndiaRed($value) {
            return '<![CDATA[<span style="color:#FF0000;">'.$value.'</span>]]>';
        }
        
        // Return Account information
        public function GetAccounts() {
            global $output_XML;
            $data = array();
            $IspUserList = self::GetIspUsers();
            
            
            if ($IspUserList == false) {
                $output_XML->SetError('Error executing shell command GetISPUsers. passthru error.');
                return false;
            }
            
            $default = self::GetDefaultLVE();
            
            if ($default == false) {
                $output_XML->SetError('Failed to open ve.cfg');
                return false;
            }
            $xml = $this->Lve_default;

            foreach ($IspUserList as $UID => $Uname) {
                $data[$UID] = $default;
                $data[$UID]['LveID'] = $UID;
                $data[$UID]['Domains'] = self::GetUserDomain($Uname);
                if ($data[$UID]['Domains'] == false) {
                    $output_XML->SetError('Error executing shell command GetUserDomain. passthru error.');
                    return false;
                }
                $data[$UID]['UserName'] = $Uname;

                foreach ($xml->lve as $child) {
                        if ($UID == (string)$child['id']) {
                            if (isset($child->cpu['limit'])) {
                                if ($default['SPEED'] != trim((string)$child->cpu['limit'])) {
                                    if (is_numeric(trim((string)$child->cpu['limit']))){
                                        $SYS_NCPU = $this->get_cpu_num();
                                        $data[$UID]['SPEED'] =  self::GetIndiaRed(((string)((int)trim($child->cpu['limit']) * $SYS_NCPU)).'%'.'*');
                                    } else {
                                    $data[$UID]['SPEED'] = self::GetIndiaRed(trim((string)$child->cpu['limit'].'*'));
                                    }
                                    $data[$UID]['default'] = 'NO';
                                }
                            }
                            if (isset($child->ncpu['limit'])) {
                                if ($default['NCPU'] != trim((string)$child->ncpu['limit'])) {
                                    $data[$UID]['NCPU'] = self::GetIndiaRed(trim((string)$child->ncpu['limit'].'*'));
                                    $data[$UID]['default'] = 'NO';
                                }
                            }
                            if (isset($child->mem['limit'])) {
                                if ($default['MEM'] != trim((string)(round(((float)$child->mem['limit']*4/1024),2).' MB'))) {
                                    $data[$UID]['MEM'] = self::GetIndiaRed(trim((string)(round(((float)$child->mem['limit']*4/1024),2).' MB*')));
                                    $data[$UID]['default'] = 'NO';
                                }
                            }
                            if (isset($child->other['maxentryprocs'])) {
                                if ($default['maxentryprocs'] != trim((string)$child->other['maxentryprocs'])) {
                                    $data[$UID]['maxentryprocs'] = self::GetIndiaRed(trim((string)$child->other['maxentryprocs'].'*'));
                                    $data[$UID]['default'] = 'NO';
                                }
                            }
                        }

                 }
            }
            print_r($data);
            return $data;
        }
        
        // Search User Login
        private function GetUserLogin($uid) {
            $result = '';
            $passwd = fopen('/etc/passwd',"r");
            while ($line = fgets($passwd)) {
            	if (strstr($line,$uid)) {
            	    $tmp = explode(':',$line);
            	    if ($tmp[2] == $uid) {
            	    	$result = $tmp[0];
            	    	break;
            	    }
            	    unset($tmp);
            	}
            }
            fclose($passwd);
            return $result;
        }
        
        // Show Stat Data
        public function GetStatData($str) {
            $return = array();
            $str = explode('&display=yes',$str);
            if (strstr($str[0],'period')) {
                $params = str_replace('&',' --',$str[0]);
                $params = '/usr/sbin/lveinfo --csv --'.$params;
            } else {
                $params = str_replace('&',' --',$str[1]);
                $params = '/usr/sbin/lveinfo --csv'.$params;
            }
            ob_clean();
            ob_start ();
            passthru ($params);
            $result = ob_get_contents();
            ob_end_clean ();

            $result = explode(PHP_EOL,$result);
            unset($result[count($result)-1],$result[0]);
            $i = 0;
            foreach ($result as $item) {
                $temp = explode(',',$item);
                $return[$i][] = $temp[0];
                $temp[0] = self::GetUserLogin($temp[0]);
                $temp[7] = round($temp[7]/1024/1024,3).' MB';
                $temp[8] = round($temp[8]/1024/1024,3).' MB';
                $temp[9] = round($temp[9]/1024/1024,3).' MB';
                foreach ($temp as $val)
                    $return[$i][] = $val;
                $i++;
            }
            return $return;
        }
        
        // Get Stat Data
        public function GetStat($str) {
            $data = array();
            $str = explode('&func',$str);
            $params = explode('&',$str[0]);
            foreach ($params as $item) {
                $tmp = explode("=",$item);
                $data[$tmp[0]] = $tmp[1];
            }
            
            $redirect = "?func=lvemanager.statistic&clickstat=yes&display=yes";

            switch ($data['filter_type']) {
                
                case 'order-by': 
                    $redirect .= '&period='.$data['Timeframe'].'&order-by='.$data['order-by'].'&limit='.$data['limit'];
                break;
                
                case 'by-usage': 
                    $redirect .= '&period='.$data['Timeframe'].'&by-usage='.$data['by-usage'].'&percentage='.$data['percentage'];
                break;  

                case 'by-fault': 
                    $redirect .= '&period='.$data['Timeframe'].'&by-fault='.$data['by-fault'].'&threshold='.$data['threshold'];
                break;  
            }
            return 'top.mn_load_loc ( "binary", "'.$redirect.'", "list" )';
        }
        
        // User interface for run command
        public function GetLiveData() {
            $data = array();
            $data = self::RunCommand('/usr/sbin/lveps -d');
            return $data;
        }
        
        //top.mn_load_loc ( 'binary', '?func=lvemanager.detail?user=anton&period=7d', 'list' );
        public function GetUserDetailInfo($str) {
            $return = array();
            if (strstr($str, 'user')) {
                $temp = explode('user=',$str);
                $temp = explode('&',$temp[1]);
                $user = $temp[0];
                $date = $_SESSION['period'];
            } else {
                $user = $_SESSION['user'];
                $date = $_SESSION['period'];
            }
            switch ($date) {
                case '30m': $date = date("Y-m-d H:i", mktime(date('H'), date('i')-30, date('s'), date('m'), date('d'), date('Y'))); break;
                case '1h': $date = date("Y-m-d H:i", mktime(date('H')-1, date('i'), date('s'), date('m'), date('d'), date('Y'))); break;
                case '4h': $date = date("Y-m-d H:i", mktime(date('H')-4, date('i'), date('s'), date('m'), date('d'), date('Y'))); break;
                case 'today': $date = date("Y-m-d H:i", mktime(0, 0, date('s'), date('m'), date('d'), date('Y'))); break;
                case 'yesterday': $date = date("Y-m-d H:i", mktime(date('H'), date('i'), date('s'), date('m'), date('d')-1, date('Y'))); break;
                case '7d': $date = date("Y-m-d H:i", mktime(date('H'), date('i'), date('s'), date('m'), date('d')-7, date('Y'))); break;
                case '30d': $date = date("Y-m-d H:i", mktime(date('H'), date('i'), date('s'), date('m'), date('d')-30, date('Y'))); break;
                default: $date = date("Y-m-d H:i", mktime(date('H'), date('i')-10, date('s'), date('m'), date('d'), date('Y'))); break;
            }
            ob_clean();
            ob_start ();
            passthru ('/usr/sbin/lveinfo --csv -u '.$user.' -f '.$date);
            $result = ob_get_contents();
            ob_end_clean ();

            $temp = explode(PHP_EOL,$result);
            unset($temp[count($temp)-1],$temp[0]);
            $i = 0;
            foreach ($temp as $item) {
                $result = explode(',',$item);
                $return[$i]['time'] = $result[0].' - '.$result[1];
                $return[$i]['User'] = $user;
                $return[$i]['date'] = $date;
                $return[$i]['aCPU'] = $result[2];
                $return[$i]['mCPU'] = $result[3];
                $return[$i]['lCPU'] = $result[4];
                $return[$i]['aEP'] = $result[5];
                $return[$i]['mEP'] = $result[6];
                $return[$i]['lEP'] = $result[7];
                $return[$i]['aMem'] = $result[8];
                $return[$i]['mMem'] = $result[9];
                $return[$i]['lMem'] = $result[10];
                $return[$i]['MemF'] = $result[11];
                $return[$i]['MepF'] = $result[12];
                $i++;
            }
            return $return;
        }
        
        public function GetUserLveInfo($user,$date) {
            $return = array();
            $time = explode('time=',$date);
            $time = explode('&',$time[1]);
            $date = $time[0];
            switch ($date) {
                case '30m': $date = date("Y-m-d H:i", mktime(date('H'), date('i')-30, date('s'), date('m'), date('d'), date('Y'))); break;
                case '1h': $date = date("Y-m-d H:i", mktime(date('H')-1, date('i'), date('s'), date('m'), date('d'), date('Y'))); break;
                case '4h': $date = date("Y-m-d H:i", mktime(date('H')-4, date('i'), date('s'), date('m'), date('d'), date('Y'))); break;
                case 'today': $date = date("Y-m-d H:i", mktime(0, 0, date('s'), date('m'), date('d'), date('Y'))); break;
                case 'yesterday': $date = date("Y-m-d H:i", mktime(date('H'), date('i'), date('s'), date('m'), date('d')-1, date('Y'))); break;
                case '7d': $date = date("Y-m-d H:i", mktime(date('H'), date('i'), date('s'), date('m'), date('d')-7, date('Y'))); break;
                case '30d': $date = date("Y-m-d H:i", mktime(date('H'), date('i'), date('s'), date('m'), date('d')-30, date('Y'))); break;
                default: $date = date("Y-m-d H:i", mktime(date('H'), date('i')-10, date('s'), date('m'), date('d'), date('Y'))); break;
            }
            ob_clean();
            ob_start ();
            passthru ('/usr/sbin/lveinfo --csv -u '.$user.' -f '.$date);
            $result = ob_get_contents();
            ob_end_clean ();
            
            $temp = explode(PHP_EOL,$result);
            unset($temp[count($temp)-1],$temp[0]);
            $i = 0;
            foreach ($temp as $item) {
                $result = explode(',',$item);
                $return[$i]['time'] = $result[0].' - '.$result[1];
                $return[$i]['aCPU'] = $result[2];
                $return[$i]['mCPU'] = $result[3];
                $return[$i]['lCPU'] = $result[4];
                $return[$i]['aEP'] = $result[5];
                $return[$i]['mEP'] = $result[6];
                $return[$i]['lEP'] = $result[7];
                $return[$i]['aMem'] = $result[8];
                $return[$i]['mMem'] = $result[9];
                $return[$i]['lMem'] = $result[10];
                $return[$i]['MemF'] = $result[11];
                $return[$i]['MepF'] = $result[12];
                $i++;
            }
            return $return;
        }
        
        public function GetDetailLveInfo($user,$date) {
            $return = array();
            switch ($date) {
                case '30m': $date = date("Y-m-d H:i", mktime(date('H'), date('i')-30, date('s'), date('m'), date('d'), date('Y'))); break;
                case '1h': $date = date("Y-m-d H:i", mktime(date('H')-1, date('i'), date('s'), date('m'), date('d'), date('Y'))); break;
                case '4h': $date = date("Y-m-d H:i", mktime(date('H')-4, date('i'), date('s'), date('m'), date('d'), date('Y'))); break;
                case 'today': $date = date("Y-m-d H:i", mktime(0, 0, date('s'), date('m'), date('d'), date('Y'))); break;
                case 'yesterday': $date = date("Y-m-d H:i", mktime(date('H'), date('i'), date('s'), date('m'), date('d')-1, date('Y'))); break;
                case '7d': $date = date("Y-m-d H:i", mktime(date('H'), date('i'), date('s'), date('m'), date('d')-7, date('Y'))); break;
                case '30d': $date = date("Y-m-d H:i", mktime(date('H'), date('i'), date('s'), date('m'), date('d')-30, date('Y'))); break;
                default: $date = date("Y-m-d H:i", mktime(date('H'), date('i')-10, date('s'), date('m'), date('d'), date('Y'))); break;
            }
            ob_clean();
            ob_start ();
            passthru ('/usr/sbin/lveinfo --csv -u '.$user.' -f '.$date);
            $result = ob_get_contents();
            ob_end_clean ();
            
            $temp = explode(PHP_EOL,$result);
            unset($temp[count($temp)-1],$temp[0]);
            $i = 0;
            foreach ($temp as $item) {
                $result = explode(',',$item);
                $return[$i]['time'] = $result[0].' - '.$result[1];
                $return[$i]['aCPU'] = $result[2];
                $return[$i]['mCPU'] = $result[3];
                $return[$i]['lCPU'] = $result[4];
                $return[$i]['aEP'] = $result[5];
                $return[$i]['mEP'] = $result[6];
                $return[$i]['lEP'] = $result[7];
                $return[$i]['aMem'] = $result[8];
                $return[$i]['mMem'] = $result[9];
                $return[$i]['lMem'] = $result[10];
                $return[$i]['MemF'] = $result[11];
                $return[$i]['MepF'] = $result[12];
                $i++;
            }
            return $return;
        }
    }

Zerion Mini Shell 1.0