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 LVEPreset {
private $XML = array();
public function __construct($XML = null) {
// if simplexml extension dos not present, add it
if ( !function_exists ( 'simplexml_load_string' ) )
{
dl ('simplexml.so');
} // if
$this->XML = @simplexml_load_string($XML);
}
public function PrintXml() {
// global $file;
// fwrite($file,PHP_EOL.'****'.PHP_EOL.$this->XML->asXML());
echo $this->XML->asXML();
}
private function CommandExecute($command,$result = true) {
if ($result) {
ob_start();
passthru($command);
$return = ob_get_contents();
ob_end_clean();
return $return;
// return explode(PHP_EOL,$return);
} else {
passthru($command);
return true;
}
}
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;
}
// Return Memory select
private function ShowMemList(&$XML, $default) {
$return = '';
$default = str_replace(' MB',' ',$default);
$default = str_replace('*',' ',$default);
$default = str_replace('M',' ',$default);
$XML->addChild('msg',$default.' MB');
$XML->addChild('msg',' Disabled');
for ($i = 128; $i <= 16256; $i+=128) {
if ($i != $default) {
$XML->addChild('msg',$i.' MB');
}
}
}
// Get Default settings
private function GetDefaultLVE() {
$default = array();
if (file_exists('/etc/container/ve.cfg')) {
$xml = simplexml_load_file('/etc/container/ve.cfg');
$default['CPU'] = trim((string)$xml->defaults->cpu['limit']);
if (is_numeric($default['CPU'])){
$SYS_NCPU = $this->get_cpu_num();
$default['CPU'] = ((string)((int)$default['CPU'] * $SYS_NCPU)).'%';
}
$default['NCPU'] = trim((string)$xml->defaults->ncpu['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['MEP'] = trim((string)($xml->defaults->other['maxentryprocs']));
$default['NPROC'] = trim((string)($xml->defaults->nproc['limit']));
$default['IO'] = trim((string)($xml->defaults->io['limit']));
} else {
return false;
}
return $default;
}
// Fill missed fields of Package settings with defaults
private function FillPackageWithDefaults($package = array(), $defaults = array()) {
foreach ($package as $key => $value) {
if (trim($value) == '') {
$package[$key] = $defaults[$key];
}
}
return $package;
}
// Get Package LVE settings
private function GetPackageLVE($Package) {
//global $file;
$settings = array();
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)) {
$defaults = LVEPreset::GetDefaultLVE();
$settings['CPU'] = (string)$item->cpu['limit'];
if (is_numeric($settings['CPU'])){
$SYS_NCPU = $this->get_cpu_num();
$settings['CPU'] = ((string)((int)$settings['CPU'] * $SYS_NCPU)).'%';
}
$settings['NCPU'] = (string)$item->ncpu['limit'];
$settings['MEM'] = (string)$item->mem['limit'];
$settings['MEP'] = (string)$item->other['maxentryprocs'];
$settings['PMEM'] = (string)$item->pmem['limit'];
$settings['NPROC'] = (string)$item->nproc['limit'];
$settings['IO'] = (string)$item->io['limit'];
$settings['MEM'] = $settings['MEM']*4/1024;
$settings['PMEM'] = $settings['PMEM']*4/1024;
$settings = LVEPreset::FillPackageWithDefaults($settings, $defaults);
}
}
if (empty($settings)) {
$settings = LVEPreset::GetDefaultLVE();
}
} else {
$settings = LVEPreset::GetDefaultLVE();
}
return $settings;
}
// Get MEM select & package LVE limits
public function GetMEMSelect($new = false) {
// Default LVE settings
if ($new) {
$default = LVEPreset::GetDefaultLVE();
$select_vmem = $this->XML->addChild('slist','');
$select_vmem->addAttribute('name','MMEM');
LVEPreset::ShowMemList($select_vmem,$default['MEM']);
$select_pmem = $this->XML->addChild('slist','');
$select_pmem->addAttribute('name','PMEM');
LVEPreset::ShowMemList($select_pmem,$default['PMEM']);
} else {
// Package LVE Settings
global $package;
$PData = LVEPreset::GetPackageLVE($package);
$select_vmem = $this->XML->addChild('slist','');
$select_vmem->addAttribute('name','MMEM');
LVEPreset::ShowMemList($select_vmem,$PData['MEM']);
$select_pmem = $this->XML->addChild('slist','');
$select_pmem->addAttribute('name','PMEM');
LVEPreset::ShowMemList($select_pmem,$PData['PMEM']);
$this->XML->addChild('MCPU',$PData['CPU']);
$this->XML->addChild('MNCPU',$PData['NCPU']);
$this->XML->addChild('MEP',$PData['MEP']);
$this->XML->addChild('NPROC',$PData['NPROC']);
$this->XML->addChild('IO',$PData['IO']);
}
}
//Create new package
private function SetLVEPackage($data) {
$default = LVEPreset::GetDefaultLVE();
$request = '/usr/sbin/lvectl package-set "'.$data['name'].'" ';
foreach ($data as $key => $value) {
if ($value == 'default') {
$data[$key] = $default[$key];
}
}
if (($data['CPU'] != 'default') && (trim($data['CPU']) != ''))
$request .= ' --speed='.urldecode($data['CPU']);
if (($data['NCPU'] != 'default') && (trim($data['NCPU']) != ''))
$request .= ' --ncpu='.$data['NCPU'];
if (($data['NPROC'] != 'default') && (trim($data['NPROC']) != ''))
$request .= ' --nproc='.$data['NPROC'];
if (($data['IO'] != 'default') && (trim($data['IO']) != ''))
$request .= ' --io='.$data['IO'];
if (($data['MEP'] != 'default') && (trim($data['MEP']) != ''))
$request .= ' --maxEntryProcs='.$data['MEP'];
if (trim($data['MEM']) == 'Disabled')
$data['MEM'] = 0;
if (trim($data['PMEM']) == 'Disabled')
$data['PMEM'] = 0;
$request .= ' --vmem='.trim(str_replace('MB','',$data['MEM'])).'M';
$request .= ' --pmem='.trim(str_replace('MB','',$data['PMEM'])).'M';
$file2 = fopen('/usr/local/ispmgr/addon/lvemanager/mini_kron.php',"w+");
$request = str_replace("'","\'",$request);
fwrite($file2,'<?php passthru(\''.$request.'\');');
fclose($file2);
}
// Delete LVE package
private function DeleteLVEPackage($Package) {
LVEPreset::CommandExecute('/usr/sbin/lvectl package-delete '.$Package);
}
// Edit|Create LVE package limits
public function EditLVEPackage() {
if ((string)$this->XML->params->func == 'preset.delete') {
LVEPreset::DeleteLVEPackage((string)$this->XML->params->elid);
} else {
$data = array();
$data['elid'] = (string)$this->XML->params->elid;
$data['name'] = (string)$this->XML->params->name;
$data['CPU'] = (string)$this->XML->params->MCPU;
$data['MEP'] = (string)$this->XML->params->MEP;
$data['MEM'] = (string)$this->XML->params->MMEM;
$data['PMEM'] = (string)$this->XML->params->PMEM;
$data['NCPU'] = (string)$this->XML->params->MNCPU;
$data['NPROC'] = (string)$this->XML->params->NPROC;
$data['IO'] = (string)$this->XML->params->IO;
LVEPreset::SetLVEPackage($data);
}
}
// Add New User or modify exist
public function AddUser() {
if ((string)$this->XML->params->func == 'user.delete') {
$uid = LVEPreset::CommandExecute('id '.(string)$this->XML->params->elid.' -u');
LVEPreset::CommandExecute('/usr/sbin/lvectl destroy '.$uid);
LVEPreset::CommandExecute('/usr/sbin/lvectl delete '.$uid);
} else {
// New User Create
if ((string)$this->XML->params->elid == '') {
$user = (string)$this->XML->params->name;
// If not custom preset
if ((string)$this->XML->params->preset != 'custom') {
$file2 = fopen('/usr/local/ispmgr/addon/lvemanager/mini_kron.php',"w+");
fwrite($file2,'<?php LVEPreset::ApplyUser(\''.$user.'\');');
fclose($file2);
}
// Exist User modify
} else {
$user = (string)$this->XML->params->elid;
// If not custom preset
if ((string)$this->XML->params->preset != 'custom') {
$file2 = fopen('/usr/local/ispmgr/addon/lvemanager/mini_kron.php',"w+");
fwrite($file2,'<?php LVEPreset::ApplyUser(\''.$user.'\');');
fclose($file2);
}
}
// fwrite($file,PHP_EOL.$this->XML->asXML());
}
}
// Static void to apply user settings
public static function ApplyUser($user) {
$uid = LVEPreset::CommandExecute('id '.trim($user).' -u');
LVEPreset::CommandExecute('/usr/sbin/lvectl apply '.trim($uid));
}
}
Zerion Mini Shell 1.0