Mini Shell
<?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'];
}
// 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;
} // GetUserDomain
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;
}
// Get Default settings
public function GetDefaultLVE() {
// if simplexml extension dos not present, add it
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']);
$SYS_NCPU = $this->get_cpu_num();
if (is_numeric($default['SPEED'])){
$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['PMEM'] = trim((string)(round(((float)$xml->defaults->pmem['limit']*4/1024),2).' MB'));
$default['maxentryprocs'] = trim((string)($xml->defaults->other['maxentryprocs']));
$default['NPROC'] = trim((string)$xml->defaults->nproc['limit']);
$default['IO'] = trim((string)$xml->defaults->io['limit']);
$default['default'] = 'YES';
} else {
return false;
}
return $default;
}
// Get Package settings
public function GetPackageLVE($UID) {
$result = array();
$package = '';
ob_clean();
ob_start ();
passthru ('/usr/bin/getcontrolpaneluserspackages --userid='.$UID);
$package = ob_get_contents();
ob_end_clean ();
if (file_exists('/etc/container/ve.cfg')) {
$xml = simplexml_load_file('/etc/container/ve.cfg');
foreach ($xml->package as $item) {
if ($item['id'] == trim($package)) {
$result['SPEED'] = trim((string)$item->cpu['limit']);
$result['NCPU'] = trim((string)$item->ncpu['limit']);
$result['MEM'] = trim((string)(round(((float)$item->mem['limit']*4/1024),2).' MB'));
$result['PMEM'] = trim((string)(round(((float)$item->pmem['limit']*4/1024),2).' MB'));
$result['maxentryprocs'] = trim((string)$item->other['maxentryprocs']);
$result['NPROC'] = trim((string)$item->nproc['limit']);
}
}
} else {
return false;
}
return $result;
}
// Set Default LVE
public function SetDefaultLVE($params, $request_end) {
$params = explode($request_end,$params);
$params = str_replace('&',' --',$params[0]);
$params = str_replace('+','',$params);
$params = ' --'.$params;
$params = str_replace('--cpu','--speed',$params);
$params = str_replace('%25','%',$params);
$params = '/usr/sbin/lvectl set default'.$params;
self::RunCommand($params,1);
return self::RunCommand('/usr/sbin/lvectl apply all',1);
}
public function SetUserLVE($params,$request_end) {
// /usr/sbin/lvectl set '.$uid.' --cpu='.$cpu.' --ncpu='.$ncpu.' --mem='.$mem.' --maxEntryProcs='.$ep.' --save'
$save = False;
$params = explode($request_end,$params);
$params = str_replace('&',' --',$params[0]);
$params = str_replace('+','',$params);
$str = str_replace('--','',$params);
$tmp = explode(' ',$str);
$data = array();
foreach ($tmp as $item) {
$tmp1 = explode('=',$item);
$data[$tmp1[0]] = $tmp1[1];
}
if (isset($data['reset'])) {
return self::RunCommand('/usr/sbin/lvectl delete '.$data['uid'],1);
}
$UsersSettings = self::GetUsersSettings();
$params = '/usr/sbin/lvectl set '.$data['uid'];
if ($data['cpu'] != str_replace('*','',$UsersSettings[$data['uid']]['CPU'])) {
$save = True;
$params .= ' --speed='.urldecode($data['cpu']);
}
if ($data['ncpu'] != str_replace('*','',$UsersSettings[$data['uid']]['NCPU'])) {
$save = True;
$params .= ' --ncpu='.$data['ncpu'];
}
if ($data['mem'] != str_replace('*','',str_replace('B','',trim($UsersSettings[$data['uid']]['VMEM'])))) {
$save = True;
$params .= ' --mem='.$data['mem'];
}
if ($data['pmem'] != str_replace('*','',str_replace('B','',trim($UsersSettings[$data['uid']]['PMEM'])))) {
$save = True;
$params .= ' --pmem='.$data['pmem'];
}
if ($data['maxEntryProcs'] != str_replace('*','',$UsersSettings[$data['uid']]['EP'])) {
$save = True;
$params .= ' --maxEntryProcs='.$data['maxEntryProcs'];
}
if ($data['nproc'] != str_replace('*','',$UsersSettings[$data['uid']]['NPROC'])) {
$save = True;
$params .= ' --nproc='.$data['nproc'];
}
if ($data['io'] != str_replace('*','',$UsersSettings[$data['uid']]['IOPRIO'])) {
$save = True;
$params .= ' --io='.$data['io'];
}
if ($save) {
return self::RunCommand($params,1);
} else {
return true;
}
}
// Get UsersSettings
public function GetUsersSettings() {
$count = 0;
ob_clean();
ob_start ();
passthru ('/usr/sbin/lvectl paneluserslimits --no-iops');
$result = trim(ob_get_contents());
ob_end_clean ();
while (strpos($result,' ') !== false) {
$result = str_replace(' ',' ',$result);
}
$tmp = explode("\n",$result);
foreach ($tmp as $key => $value){
$line = explode (' ',$value);
if ($count == 0){
$count = count($line); //$count = number of fields
for ($i=0; $i<$count; $i++) {
$blank[$i] = trim($line[$i]);
}
}
else {
$uid = $line[1];
for($i=1;$i<$count;$i++){
if ($uid != 'ID')
$data[$uid][$blank[$i]] = $line[$i+1];
}
if (count($line) > $count){ // last fields in line = pkg_name
$data[$uid][$blank[$count-1]] = implode(' ',array_slice($line,$count));
}
}
}
return $data;
}
// 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);
$data['UserName'] = $User['login'];
$data['UserID'] = $User['id'];
$UsersSettings = self::GetUsersSettings();
$data['SPEED'] = str_replace('*','',$UsersSettings[$User['id']]['SPEED']).'%';
$data['NCPU'] = str_replace('*','',$UsersSettings[$User['id']]['NCPU']);
$data['VMEM'] = str_replace('*','',str_replace('MB','',trim($UsersSettings[$User['id']]['VMEM'])));
$data['PMEM'] = str_replace('*','',str_replace('MB','',trim($UsersSettings[$User['id']]['PMEM'])));
$data['NPROC'] = str_replace('*','',$UsersSettings[$User['id']]['NPROC']);
$data['IO'] = str_replace('*','',$UsersSettings[$User['id']]['IO']);
$data['EP'] = str_replace('*','',$UsersSettings[$User['id']]['EP']);
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;
}
$UsersSettings = self::GetUsersSettings();
foreach ($IspUserList as $UID => $Uname) {
if (array_key_exists($UID,$UsersSettings)) {
$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;
if (strchr($UsersSettings[$UID]['SPEED'],'*')) {
$data[$UID]['SPEED'] = self::GetIndiaRed($UsersSettings[$UID]['SPEED']);
} else {
$data[$UID]['SPEED'] = $UsersSettings[$UID]['SPEED'];
}
if (strchr($UsersSettings[$UID]['NCPU'],'*')) {
$data[$UID]['NCPU'] = self::GetIndiaRed($UsersSettings[$UID]['NCPU']);
} else {
$data[$UID]['NCPU'] = $UsersSettings[$UID]['NCPU'];
}
if (strchr($UsersSettings[$UID]['VMEM'],'*')) {
$data[$UID]['VMEM'] = self::GetIndiaRed($UsersSettings[$UID]['VMEM']);
} else {
$data[$UID]['VMEM'] = $UsersSettings[$UID]['VMEM'];
}
if (strchr($UsersSettings[$UID]['PMEM'],'*')) {
$data[$UID]['PMEM'] = self::GetIndiaRed($UsersSettings[$UID]['PMEM']);
} else {
$data[$UID]['PMEM'] = $UsersSettings[$UID]['PMEM'];
}
if (strchr($UsersSettings[$UID]['EP'],'*')) {
$data[$UID]['EP'] = self::GetIndiaRed($UsersSettings[$UID]['EP']);
} else {
$data[$UID]['EP'] = $UsersSettings[$UID]['EP'];
}
if (strchr($UsersSettings[$UID]['NPROC'],'*')) {
$data[$UID]['NPROC'] = self::GetIndiaRed($UsersSettings[$UID]['NPROC']);
} else {
$data[$UID]['NPROC'] = $UsersSettings[$UID]['NPROC'];
}
if (strchr($UsersSettings[$UID]['IO'],'*')) {
$data[$UID]['IO'] = self::GetIndiaRed($UsersSettings[$UID]['IO']);
} else {
$data[$UID]['IO'] = $UsersSettings[$UID]['IO'];
}
$data[$UID]['PACKAGE'] = $UsersSettings[$UID]['PACKAGE'];
}
}
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';
$temp[12] = round($temp[12]/1024/1024,3).' MB';
$temp[13] = round($temp[13]/1024/1024,3).' MB';
$temp[14] = round($temp[14]/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 --style=admin -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]['vMemF'] = $result[11];
$return[$i]['EPf'] = $result[12];
$return[$i]['apMem'] = $result[13];
$return[$i]['mpMem'] = $result[14];
$return[$i]['lpMem'] = $result[15];
$return[$i]['aNproc'] = $result[16];
$return[$i]['mNproc'] = $result[17];
$return[$i]['lNproc'] = $result[18];
$return[$i]['pMemF'] = $result[19];
$return[$i]['NprocF'] = $result[20];
$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]['vMemF'] = $result[11];
$return[$i]['EPf'] = $result[12];
$return[$i]['apMem'] = $result[13];
$return[$i]['mpMem'] = $result[14];
$return[$i]['lpMem'] = $result[15];
$return[$i]['aNproc'] = $result[16];
$return[$i]['mNproc'] = $result[17];
$return[$i]['lNproc'] = $result[18];
$return[$i]['pMemF'] = $result[19];
$return[$i]['NprocF'] = $result[20];
$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 --style=admin -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]['vMemF'] = $result[11];
$return[$i]['EPf'] = $result[12];
$return[$i]['apMem'] = $result[13];
$return[$i]['mpMem'] = $result[14];
$return[$i]['lpMem'] = $result[15];
$return[$i]['aNproc'] = $result[16];
$return[$i]['mNproc'] = $result[17];
$return[$i]['lNproc'] = $result[18];
$return[$i]['pMemF'] = $result[19];
$return[$i]['NprocF'] = $result[20];
$i++;
}
return $return;
}
}
Zerion Mini Shell 1.0