$sIPAdd = new IP(0); $sIPAdd->uIPAddress = $sIP["ipaddress"]; $sIPAdd->uVPSId = $sVPS->sId; $sIPAdd->uBlockId = $sBlock->sId; $sIPAdd->InsertIntoDatabase(); } else { echo "Using existing IP.\n"; $sIPAdd = new IP($sIPExists->data[0]["id"]); $sIPAdd->uVPSId = $sVPS->sId; $sIPAdd->InsertIntoDatabase(); } $sIPTotal++; } } $sVPS->uPrimaryIP = $sValue["mainipaddress"]; $sVPS->InsertIntoDatabase(); echo "Moving VPS configs to new ID...\n"; $sCommandList = "vzctl chkpnt {$sValue["ctid"]} --dumpfile /tmp/Dump.{$sValue["ctid"]};"; $sCommandList .= "mv /etc/vz/conf/{$sValue["ctid"]}.conf /etc/vz/conf/{$sVPS->sContainerId}.conf;"; $sCommandList .= "mv /vz/private/{$sValue["ctid"]} /vz/private/{$sVPS->sContainerId};"; $sCommandList .= "mv /vz/root/{$sValue["ctid"]} /vz/root/{$sVPS->sContainerId};"; $sCommandList .= "vzctl restore {$sVPS->sContainerId} --dumpfile /tmp/Dump.{$sValue["ctid"]};"; $sServerConnect = $sServer->server_connect($sServer); $sUpdate = $sServerConnect->exec($sCommandList); $sStatus = $sServerConnect->exec("vzctl status {$sVPS->sContainerId};"); if (strpos($sStatus, 'running') !== false) { echo "VPS Old: {$sValue["vserverid"]} now Feathurized: {$sVPS->sId}\n"; } else { echo "Something borked with the conversion of {$sValue["vserverid"]} ({$sValue["hostname"]} / {$sValue["mainipaddress"]}), skipping...\n"; } } else {
public function database_kvm_create($sUser, $sRequested, $sAPI = 0) { $sUserPermissions = $sUser->sPermissions; if ($sUserPermissions == 7) { global $database; global $sRequested; $uServer = $sRequested["POST"]["server"]; $uUser = $sRequested["POST"]["user"]; $uTemplate = $sRequested["POST"]["template"]; $uRAM = $sRequested["POST"]["ram"]; $uDisk = $sRequested["POST"]["disk"]; $uIPAddresses = $sRequested["POST"]["ipaddresses"]; $uHostname = $sRequested["POST"]["hostname"]; $uNameserver = $sRequested["POST"]["nameserver"]; $uCPULimit = $sRequested["POST"]["cpulimit"]; $uBandwidthLimit = $sRequested["POST"]["bandwidthlimit"]; if (!empty($uServer) && is_numeric($uServer)) { if (!empty($uUser) && is_numeric($uUser)) { if (!empty($uRAM) && is_numeric($uRAM)) { if (!empty($uDisk) && is_numeric($uDisk)) { if (!empty($uCPULimit) && is_numeric($uCPULimit)) { if (!empty($uBandwidthLimit) && is_numeric($uBandwidthLimit)) { $sServer = new Server($uServer); $sOwner = new User($uUser); if (!empty($uTemplate)) { $sTemplate = new Template($uTemplate); } if (empty($sAPI)) { $sIPCheck = VPS::check_ipspace($sServer->sId, $uIPAddresses); if (is_array($sIPCheck)) { return $sIPCheck; } } if (empty($uHostname)) { $uHostname = "vps.example.com"; } if (empty($uNameserver)) { $uNameserver = "8.8.8.8"; } while ($sTotalMacs < $uIPAddresses) { if (empty($sTotalMacs)) { $sMac = generate_mac(); } else { $sMac .= "," . generate_mac(); } $sTotalMacs++; } // VPS Database setup $sVPSId = Core::GetSetting('container_id'); $sUpdate = Core::UpdateSetting('container_id', $sVPSId->sValue + 1); $sVPS = new VPS(0); $sVPS->uType = $sServer->sType; $sVPS->uHostname = preg_replace('/[^A-Za-z0-9-.]/', '', $uHostname); $sVPS->uNameserver = $uNameserver; $sVPS->uUserId = $sOwner->sId; $sVPS->uServerId = $sServer->sId; $sVPS->uContainerId = $sVPSId->sValue; $sVPS->uRAM = $uRAM; $sVPS->uDisk = $uDisk; $sVPS->uMac = $sMac; $sVPS->uCPULimit = $uCPULimit; if (!empty($uTemplate)) { $sVPS->uTemplateId = $sTemplate->sId; } $sVPS->uBandwidthLimit = $uBandwidthLimit; $sVPS->uVNCPort = $sVPSId->sValue + 5900; $sVPS->uBootOrder = "hd"; $sVPS->InsertIntoDatabase(); if ($sBlocks = $database->CachedQuery("SELECT * FROM server_blocks WHERE `server_id` = :ServerId AND `ipv6` = 0", array('ServerId' => $sServer->sId))) { foreach ($sBlocks->data as $key => $value) { if ($sIPs = $database->CachedQuery("SELECT * FROM ipaddresses WHERE `block_id` = :BlockId AND `vps_id` = 0", array('BlockId' => $value["block_id"]))) { foreach ($sIPs->data as $subvalue) { if ($sCurrentIPs < $uIPAddresses) { $sIPList[] = array("id" => $subvalue["id"], "ip_address" => $subvalue["ip_address"], "block" => $subvalue["block_id"]); $sUpdate = $database->CachedQuery("UPDATE ipaddresses SET `vps_id` = :VPSId WHERE `id` = :Id", array('VPSId' => $sVPS->sId, 'Id' => $subvalue["id"])); if (empty($sFirst)) { $sVPS->uPrimaryIP = $subvalue["ip_address"]; $sVPS->InsertIntoDatabase(); $sFirst = 1; } $sCurrentIPs++; } } } } } $sRequested["POST"]["VPS"] = $sVPS->sId; $sRequested["POST"]["IPList"] = $sIPList; if (!empty($sAPI)) { return $sVPS->sId; } return true; } else { return $sArray = array("json" => 1, "type" => "caution", "result" => "You must input the bandwidth limit!"); } } else { return $sArray = array("json" => 1, "type" => "caution", "result" => "You must input the CPU limit!"); } } else { return $sArray = array("json" => 1, "type" => "caution", "result" => "You must input the disk limit!"); } } else { return $sArray = array("json" => 1, "type" => "caution", "result" => "You must input the available RAM!"); } } else { return $sArray = array("json" => 1, "type" => "caution", "result" => "You must input the owner of this VPS!"); } } else { return $sArray = array("json" => 1, "type" => "caution", "result" => "You must input the server!"); } } else { return $sArray = array("json" => 1, "type" => "error", "result" => "Permissions invalid for selected action."); } }
$sRequested["POST"]["cpulimit"] = "100"; $sRequested["POST"]["bandwidthlimit"] = "512"; $sDatabase = $sStart->database_openvz_create($sUser, $sRequested); $sCreateVPS = $sStart->openvz_create($sUser, $sRequested); if (is_numeric($sCreateVPS["vps"])) { $sNewVPS = new VPS($sCreateVPS["vps"]); $sIPs = substr($sVPS["coma_vmipaddress_a"], 1); $sIPs = explode(",", $sIPs); if (empty($sAssignNewIP)) { $sAction = "assignip"; $sDBAction = "database_{$sNewVPS->sType}_{$sAction}"; $sServerAction = "{$sNewVPS->sType}_{$sAction}"; foreach ($sIPs as $sIP) { if (empty($sFirst)) { $sNewVPS->uPrimaryIP = $sIP; $sNewVPS->InsertIntoDatabase(); $sFirst = 1; } if (!empty($sIP)) { $sRequested["GET"]["ip"] = $sIP; $sDBResult = $sStart->{$sDBAction}($sUser, $sNewVPS, $sRequested); $sServerResult = $sStart->{$sServerAction}($sUser, $sNewVPS, $sRequested); } } } else { $sTotalIPs = count($sIPs); $sAction = "addip"; $sDBAction = "database_{$sNewVPS->sType}_{$sAction}"; $sServerAction = "{$sNewVPS->sType}_{$sAction}"; $sRequested["GET"]["ip"] = $sTotalIPs; $sDBResult = $sStart->{$sDBAction}($sUser, $sNewVPS, $sRequested);
public static function pull_status($sServer) { echo "Setting up prerequisites...\n"; $sTimestamp = time(); global $database; // Insert History $sHistory = new History(0); $sHistory->uServerId = $sServer; $sHistory->uTimestamp = $sTimestamp; $sHistory->uStatus = false; $sHistory->InsertIntoDatabase(); // Insert Statistics $sStatistics = new Statistics(0); $sStatistics->uServerId = $sServer; $sStatistics->uStatus = false; $sStatistics->uTimestamp = $sTimestamp; $sStatistics->InsertIntoDatabase(); // Connect to Server $sServer = new Server($sServer); $sSSH = Server::server_connect($sServer, "1"); if (is_array($sSSH)) { die; } // Setup and start anti-abuse script on node. if ($sServer->sType == 'openvz') { $sAbuse = escapeshellarg(file_get_contents('/var/feathur/Scripts/abuse.sh')); $sDumpCode = $sSSH->exec("mkdir -p /var/feathur/data;cd /var/feathur/data/;echo {$sAbuse} > abuse.sh;screen -dmS abuse bash -c \"cd /var/feathur/data/;bash abuse.sh;\";"); // Pull list of suspended users: $sSuspended = explode("\n", $sSSH->exec("cat /var/feathur/data/suspended.txt")); foreach ($sSuspended as $sValue) { $sValue = preg_replace('/[^0-9]/', '', $sValue); if (!empty($sValue) && $sValue >> 10) { try { $sVPS = new VPS($sValue); $sVPS->uSuspended = 2; $sVPS->InsertIntoDatabase(); } catch (Exception $e) { echo "Odd data in suspend tracker. Skipping"; } } } $sClean = $sSSH->exec("rm -rf /var/feathur/data/suspended.txt"); } // Pull system stats. echo "Connected to server...\n"; $sUptime = explode(' ', $sSSH->exec("cat /proc/uptime")); $sCPU = explode(' ', $sSSH->exec("cat /proc/loadavg")); $sUsedRAM = preg_replace('/[^0-9]/', '', $sSSH->exec("free | head -n 3 | tail -n 1 | awk '{print \$3}'")); $sTotalRAM = preg_replace('/[^0-9]/', '', $sSSH->exec("free | head -n 2 | tail -n 1 | awk '{print \$2}'")); $sDisk = $sSSH->exec("df"); $sDisk = explode("\n", trim($sDisk)); array_shift($sDisk); foreach ($sDisk as $sValue) { $sValue = explode(" ", preg_replace("/\\s+/", " ", $sValue)); if (is_numeric($sValue[2])) { $sDiskUsed = $sDiskUsed + $sValue[2]; $sDiskTotal = $sDiskTotal + $sValue[1]; } } $sDiskUsed = $sDiskUsed / 1048576; $sDiskTotal = $sDiskTotal / 1048576; $sPullBandwidth = explode("\n", $sSSH->exec("ifconfig {$interface} | grep 'RX bytes' | awk -F: '{print \$2,\$3}' | awk '{print \$1,\$6}'")); foreach ($sPullBandwidth as $sData) { if (empty($sNewBandwidth)) { $sData = explode(" ", $sData); $sData[0] = preg_replace('/[^0-9]/', '', $sData[0]); $sData[1] = preg_replace('/[^0-9]/', '', $sData[1]); $sNewBandwidth = round($sData[0] / 131072 + $sData[1] / 131072, 2); } } // Update server row. // Check to make sure that the current bandwidth is higher than last bandwidth. // If higher, update statuses, otherwise replace both values with current value. // This prevents bandwidth accounting from becoming negative. $sBandwidthNegative = $sServer->sBandwidth; if ($sNewBandwidth > $sBandwidthNegative) { $sServer->uPreviousCheck = $sServer->sLastCheck; $sServer->uLastCheck = $sTimestamp; $sServer->uLastBandwidth = $sServer->sBandwidth; $sServer->uBandwidth = $sNewBandwidth; } else { $sServer->uPreviousCheck = $sTimestamp; $sServer->uLastCheck = $sTimestamp; $sServer->uLastBandwidth = $sNewBandwidth; $sServer->uBandwidth = $sNewBandwidth; } $sServer->uLoadAverage = $sCPU[0]; $sServer->uHardDiskTotal = $sDiskTotal; $sServer->uHardDiskFree = $sDiskTotal - $sDiskUsed; $sServer->uTotalMemory = $sTotalRAM; $sServer->uFreeMemory = $sTotalRAM - $sUsedRAM; $sServer->uStatus = true; $sServer->uStatusWarning = false; $sServer->uHardwareUptime = $sUptime[0]; $sServer->InsertIntoDatabase(); // Update history $sHistory->uStatus = true; $sHistory->InsertIntoDatabase(); // Update statistics $sStatistics->uStatus = true; $sStatistics->uHardwareUptime = $sUptime[0]; $sStatistics->uTotalMemory = $sTotalRAM; $sStatistics->uFreeMemory = $sTotalRAM - $sUsedRAM; $sStatistics->uLoadAverage = $sCPU[0]; $sStatistics->uHardDiskTotal = $sDiskTotal; $sStatistics->uHardDiskFree = $sDiskTotal - $sDiskUsed; $sStatistics->uBandwidth = $sNewBandwidth; $sStatistics->InsertIntoDatabase(); // Cleanup unset($sPullBandwidth); echo "Server polling completed...\n"; // Bandwidth polling for each VPS on this server. $sBandwidthAccounting = Core::GetSetting('bandwidth_accounting'); echo "Beginning bandwidth accounting\n"; if ($sListVPS = $database->CachedQuery("SELECT * FROM `vps` WHERE `server_id` = :ServerId", array("ServerId" => $sServer->sId))) { foreach ($sListVPS->data as $sVPS) { $sVPS = new VPS($sVPS["id"]); if ($sServer->sType == 'openvz') { $sPullBandwidth = explode("\n", $sSSH->exec("vzctl exec {$sVPS->sContainerId} ifconfig {$interface} | grep 'RX bytes' | awk -F: '{print \$2,\$3}' | awk '{print \$1,\$6}';")); } if ($sServer->sType == 'kvm') { $sPullBandwidth = explode("\n", $sSSH->exec('for i in $(ip link show | grep kvm' . $sVPS->sContainerId . ' | awk \'{print $2}\' | awk -F: \'{print $1}\' | sort -u); do ifconfig $i | grep \'RX bytes\' | awk -F: \'{print $2,$3}\' | awk \'{print $1,$6}\'; done')); } foreach ($sPullBandwidth as $sData) { $sData = explode(" ", $sData); $sData[0] = round(preg_replace('/[^0-9]/', '', $sData[0]) / 1024 / 1024, 2); $sData[1] = round(preg_replace('/[^0-9]/', '', $sData[1]) / 1024 / 1024, 2); if ($sBandwidthAccounting->sValue == 'upload') { $sTotal = $sTotal + $sData[1]; } if ($sBandwidthAccounting->sValue == 'download') { $sTotal = $sTotal + $sData[2]; } if ($sBandwidthAccounting->sValue == 'both') { $sTotal = $sTotal + $sData[0] + $sData[1]; } } $sLastBandwidth = $sVPS->sLastBandwidth; if ($sLastBandwidth < $sTotal) { $sChange = round($sTotal - $sVPS->sLastBandwidth, 2); } else { if (!empty($sVPS->sBandwidthUsage)) { $sChange = round($sTotal, 2); } } echo "Bandwidth for: {$sVPS->sId} - Total: {$sTotal} - Change: +{$sChange}\n"; $sVPS->uBandwidthUsage = $sVPS->sBandwidthUsage + $sChange; $sVPS->uLastBandwidth = $sTotal; $sVPS->InsertIntoDatabase(); unset($sData); unset($sTotal); unset($sChange); } unset($sPullBandwidth); } echo "Completed.\n"; return true; }
public function database_openvz_create($sUser, $sRequested, $sAPI = 0) { $sUserPermissions = $sUser->sPermissions; if ($sUserPermissions == 7) { global $database; global $sRequested; $uServer = $sRequested["POST"]["server"]; $uUser = $sRequested["POST"]["user"]; $uTemplate = $sRequested["POST"]["template"]; $uRAM = $sRequested["POST"]["ram"]; $uSWAP = $sRequested["POST"]["swap"]; $uDisk = $sRequested["POST"]["disk"]; $uInodes = $sRequested["POST"]["inodes"]; $uNumProc = $sRequested["POST"]["numproc"]; $uNumIPTent = $sRequested["POST"]["numiptent"]; $uIPAddresses = $sRequested["POST"]["ipaddresses"]; $uHostname = $sRequested["POST"]["hostname"]; $uNameserver = $sRequested["POST"]["nameserver"]; $uPassword = $sRequested["POST"]["password"]; $uCPUUnits = $sRequested["POST"]["cpuunits"]; $uCPULimit = $sRequested["POST"]["cpulimit"]; $uBandwidthLimit = $sRequested["POST"]["bandwidthlimit"]; if (!empty($uServer) && is_numeric($uServer)) { if (!empty($uUser) && is_numeric($uUser)) { if (!empty($uTemplate) && is_numeric($uTemplate)) { if (!empty($uRAM) && is_numeric($uRAM)) { if (!empty($uSWAP) && is_numeric($uSWAP)) { if (!empty($uDisk) && is_numeric($uDisk)) { if (!empty($uInodes) && is_numeric($uInodes)) { if (!empty($uNumProc) && is_numeric($uNumProc)) { if (!empty($uNumIPTent) && is_numeric($uNumIPTent)) { if (!empty($uInodes) && is_numeric($uInodes)) { if (!empty($uCPUUnits) && is_numeric($uCPUUnits)) { if (!empty($uCPULimit) && is_numeric($uCPULimit)) { if (!empty($uBandwidthLimit) && is_numeric($uBandwidthLimit)) { $sServer = new Server($uServer); $sOwner = new User($uUser); $sTemplate = new Template($uTemplate); if ($uIPAddressess > 0) { $sIPCheck = VPS::check_ipspace($sServer->sId, $uIPAddresses); if (is_array($sIPCheck)) { return $sIPCheck; } } if (!is_array($sSSH)) { if (empty($uHostname)) { $uHostname = "vps.example.com"; } if (empty($uNameserver)) { $uNameserver = "8.8.8.8"; } // VPS Database setup $sVPSId = Core::GetSetting('container_id'); $sUpdate = Core::UpdateSetting('container_id', $sVPSId->sValue + 1); $sVPS = new VPS(0); $sVPS->uType = $sServer->sType; $sVPS->uHostname = preg_replace('/[^A-Za-z0-9-.]/', '', $uHostname); $sVPS->uNameserver = $uNameserver; $sVPS->uUserId = $sOwner->sId; $sVPS->uServerId = $sServer->sId; $sVPS->uContainerId = $sVPSId->sValue; $sVPS->uNumIPTent = $uNumIPTent; $sVPS->uNumProc = $uNumProc; $sVPS->uInodes = $uInodes; $sVPS->uRAM = $uRAM; $sVPS->uSWAP = $uSWAP; $sVPS->uDisk = $uDisk; $sVPS->uCPUUnits = $uCPUUnits; $sVPS->uCPULimit = $uCPULimit; $sVPS->uTemplateId = $sTemplate->sId; $sVPS->uBandwidthLimit = $uBandwidthLimit; $sVPS->InsertIntoDatabase(); if ($uIPAddresses > 0) { if ($sBlocks = $database->CachedQuery("SELECT * FROM server_blocks WHERE `server_id` = :ServerId AND `ipv6` = 0", array('ServerId' => $sServer->sId))) { foreach ($sBlocks->data as $key => $value) { if ($sIPs = $database->CachedQuery("SELECT * FROM ipaddresses WHERE `block_id` = :BlockId AND `vps_id` = 0", array('BlockId' => $value["block_id"]))) { foreach ($sIPs->data as $subvalue) { if ($sCurrentIPs < $uIPAddresses) { $sIPList[] = array("id" => $subvalue["id"], "ip_address" => $subvalue["ip_address"]); $sUpdate = $database->CachedQuery("UPDATE ipaddresses SET `vps_id` = :VPSId WHERE `id` = :Id", array('VPSId' => $sVPS->sId, 'Id' => $subvalue["id"])); if (empty($sFirst)) { $sVPS->uPrimaryIP = $subvalue["ip_address"]; $sVPS->InsertIntoDatabase(); $sFirst = 1; } $sCurrentIPs++; } } } } } $sRequested["POST"]["IPList"] = $sIPList; } $sRequested["POST"]["VPS"] = $sVPS->sId; $sRequested["POST"]["IPList"] = $sIPList; if (!empty($sAPI)) { return $sVPS->sId; } return true; } else { return $sArray = array("json" => 1, "type" => "error", "result" => "Unable to connect to the node selected!"); } } else { return $sArray = array("json" => 1, "type" => "caution", "result" => "You must input a bandwidth limit!"); } } else { return $sArray = array("json" => 1, "type" => "caution", "result" => "You must input the CPU limit!"); } } else { return $sArray = array("json" => 1, "type" => "caution", "result" => "You must input the number of CPU units!"); } } else { return $sArray = array("json" => 1, "type" => "caution", "result" => "You must input the number of inodes"); } } else { return $sArray = array("json" => 1, "type" => "caution", "result" => "You must input the maximum number of connections to create a VPS!"); } } else { return $sArray = array("json" => 1, "type" => "caution", "result" => "You must input the maximum number of processes to create a VPS!"); } } else { return $sArray = array("json" => 1, "type" => "caution", "result" => "You must input the maximum inodes to create a VPS!"); } } else { return $sArray = array("json" => 1, "type" => "caution", "result" => "You must input the maximum disk to create a VPS!"); } } else { return $sArray = array("json" => 1, "type" => "caution", "result" => "You must input the maximum SWAP to create a VPS!"); } } else { return $sArray = array("json" => 1, "type" => "caution", "result" => "You must input the maximum RAM to create a VPS!"); } } else { return $sArray = array("json" => 1, "type" => "caution", "result" => "You must select a template to create a VPS!"); } } else { return $sArray = array("json" => 1, "type" => "caution", "result" => "You must specify a user to create a VPS!"); } } else { return $sArray = array("json" => 1, "type" => "caution", "result" => "You must specify a server to create a VPS!"); } } else { return $sArray = array("json" => 1, "type" => "error", "result" => "Permissions invalid for selected action."); } }