예제 #1
0
             $sVPS->InsertIntoDatabase();
             $sTotalMacs = 1;
             $sMac = $sVPS->sMac;
             while ($sTotalMacs < $sIPTotal) {
                 $sMac .= "," . generate_mac();
                 $sTotalMacs++;
             }
             if ($sMac != $sVPS->sMac) {
                 $sVPS->uMac = $sMac;
                 $sVPS->InsertIntoDatabase();
             }
             echo "Generating new configs for VPS...\n";
             $sCreateConfig = $sServerType->kvm_config($sUser, $sVPS, $sRequested);
             $sCreateDHCP = $sServerType->kvm_dhcp($sUser, $sVPS, $sRequested);
             echo "Shutting down VPS under SolusVM's control, renaming LV...\n";
             $sServerConnect = $sServer->server_connect($sServer);
             $sShutdown = $sServerConnect->exec("virsh destroy kvm{$sKVMData["xid"]};lvrename {$sServer->sVolumeGroup} kvm{$sKVMData["xid"]}_img kvm{$sVPS->sContainerId}_img;");
             echo "Starting up VPS under Feathur's control.\n";
             $sBoot = $sServerType->kvm_boot($sUser, $sVPS, $sRequested);
             echo $sBoot['result'] . "\n";
         } else {
             echo "Could not find VPS data in database for VPS ID: {$sValue["vserverid"]} ({$sValue["hostname"]} / {$sValue["mainipaddress"]}), skipping...\n";
         }
     } else {
         echo "Could not find SolusVM client for VPS ID: {$sValue["vserverid"]} ({$sValue["hostname"]} / {$sValue["mainipaddress"]}), skipping...\n";
     }
 }
 if ($sSolusNode["type"] == 'openvz') {
     echo "Starting conversion of VPS ID: {$sValue["vserverid"]} ({$sValue["hostname"]} / {$sValue["mainipaddress"]})\n";
     if ($sSolusUser = $database->CachedQuery("SELECT * FROM `solusvm`.`clients` WHERE `clientid` = :ClientId", array("ClientId" => $sValue["clientid"]))) {
         $sSolusUser = $sSolusUser->data[0];
예제 #2
0
 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;
 }
예제 #3
0
 public function database_kvm_terminate($sUser, $sVPS, $sRequested)
 {
     global $database;
     $sUserPermissions = $sUser->sPermissions;
     if ($sUserPermissions == 7) {
         $sServer = new Server($sVPS->sServerId);
         $sSSH = Server::server_connect($sServer);
         $sCommandList .= "virsh autostart --disabled kvm{$sVPS->sContainerId}}";
         $sCommandList .= "virsh destroy kvm{$sVPS->sContainerId};";
         $sCommandList .= "rm -rf /var/feathur/configs/kvm{$sVPS->sContainerId}-vps.xml;rm -rf /var/feathur/configs/kvm{$sVPS->sContainerId}-dhcp.conf;";
         $sCommandList .= "cat /var/feathur/configs/dhcpd.head /var/feathur/configs/*-dhcp.conf > /etc/dhcp/dhcpd.conf;service isc-dhcp-server restart;";
         $sCommandList .= "dd if=/dev/zero of=/dev/{$sServer->sVolumeGroup}/kvm{$sVPS->sContainerId}_img;";
         $sCommandList .= "lvremove -f /dev/{$sServer->sVolumeGroup}/kvm{$sVPS->sContainerId}_img;exit;";
         $sCommandList = escapeshellarg($sCommandList);
         $sLog[] = array("command" => "VPS Termination via Screen", "result" => $sSSH->exec("screen -dm -S {$sVPS->sContainerId} bash -c {$sCommandList};"));
         $sSave = VPS::save_vps_logs($sLog, $sVPS);
         $sTerminate = $database->CachedQuery("DELETE FROM vps WHERE `id` = :VPSId", array('VPSId' => $sVPS->sId));
         $sCleanIPs = $database->CachedQuery("UPDATE ipaddresses SET `vps_id` = 0 WHERE `vps_id` = :VPSId", array('VPSId' => $sVPS->sId));
         return $sArray = array("json" => 1, "type" => "error", "result" => "This VPS has been terminated.", "reload" => 1);
     } else {
         return $sArray = array("json" => 1, "type" => "error", "result" => "Insufficient permissions for this action.", "reload" => 1);
     }
 }
예제 #4
0
 public function openvz_statistics($sUser, $sVPS, $sRequested)
 {
     global $sTemplate;
     global $database;
     global $locale;
     $sServer = new Server($sVPS->sServerId);
     $sSSH = Server::server_connect($sServer);
     $sLog[] = array("command" => "vzctl status {$sVPS->sContainerId}", "result" => $sSSH->exec("vzctl status {$sVPS->sContainerId}"));
     if (strpos($sLog[0]["result"], 'running') === false) {
         return $sArray = array("json" => 1, "type" => "status", "result" => "offline", "hostname" => $sVPS->sHostname);
     } else {
         $sUptime = explode(' ', $sSSH->exec("vzctl exec {$sVPS->sContainerId} cat /proc/uptime"));
         $sRAM = explode('kB', $sSSH->exec("vzctl exec {$sVPS->sContainerId} cat /proc/meminfo"));
         $sUsedRAM = preg_replace("/[^0-9]/", "", $sRAM[0]) - preg_replace("/[^0-9]/", "", $sRAM[1]);
         $sTotalRAM = preg_replace("/[^0-9]/", "", $sRAM[0]);
         $sUsedSWAP = preg_replace("/[^0-9]/", "", $sRAM[11]) - preg_replace("/[^0-9]/", "", $sRAM[12]);
         if ($sUsedSWAP < 0) {
             $sUsedSWAP = preg_replace("/[^0-9]/", "", $sRAM[12]) - preg_replace("/[^0-9]/", "", $sRAM[13]);
         }
         $sTotalSWAP = $sVPS->sSWAP;
         $sDisk = $sSSH->exec("vzctl exec {$sVPS->sContainerId} 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];
             }
         }
         $sDiskUsed = $sDiskUsed / 1048576;
         $sDiskTotal = $sVPS->sDisk;
         $sCPU = explode(' ', $sSSH->exec("vzctl exec {$sVPS->sContainerId} cat /proc/loadavg"));
         if ($sUsedRAM > 0) {
             $sUsedRAM = round($sUsedRAM / 1024, 0);
         }
         if ($sTotalRAM > 0) {
             $sTotalRAM = round($sTotalRAM / 1024, 0);
         }
         if ($sUsedRAM > 0 && $sTotalRAM > 0) {
             $sPercentRAM = round(100 / $sTotalRAM * $sUsedRAM, 0);
         } else {
             $sPercentRAM = 0;
         }
         if ($sUsedSWAP > 0) {
             $sUsedSWAP = round($sUsedSWAP / 1024, 0);
         }
         if ($sUsedSWAP > 0 && $sTotalSWAP > 0) {
             $sPercentSWAP = round(100 / $sTotalSWAP * $sUsedSWAP, 0);
         } else {
             $sPercentSWAP = 0;
         }
         if ($sDiskUsed > 0 && $sDiskTotal > 0) {
             $sPercentDisk = round(100 / $sDiskTotal * $sDiskUsed, 0);
         } else {
             $sPercentDisk = 0;
         }
         if ($sCPU[0] > 0 && $sVPS->sCPULimit > 0) {
             $sPercentCPU = round(100 / $sVPS->sCPULimit * ($sCPU[0] * 100), 0);
         } else {
             $sPercentCPU = 0;
         }
         if ($sVPS->sCPULimit > 0) {
             $sCores = round($sVPS->sCPULimit / 100, 0);
         } else {
             $sCores = 0;
         }
         if ($sDiskUsed < 1) {
             $sDiskUsed = round($sDiskUsed, 2);
         } else {
             $sDiskUsed = round($sDiskUsed, 0);
         }
         if ($sTemplates = $database->CachedQuery("SELECT * FROM templates WHERE `id` = :TemplateId", array('TemplateId' => $sVPS->sTemplateId))) {
             $sVPSTemplate = new Template($sVPS->sTemplateId);
             $sTemplateName = $sVPSTemplate->sName;
         } else {
             $sTemplateName = "N/A";
         }
         if ($sVPS->sBandwidthUsage > 0) {
             $sBandwidthUsage = FormatBytes($sVPS->sBandwidthUsage, 2);
         } else {
             $sBandwidthUsage = "0 MB";
         }
         if (!empty($sVPS->sBandwidthLimit) && !empty($sVPS->sBandwidthUsage)) {
             $sPercentBandwidth = round(100 / ($sVPS->sBandwidthLimit * 1024) * $sVPS->sBandwidthUsage, 0);
             if (empty($sPercentBandwidth)) {
                 $sPercentBandwidth = 0;
             }
         } else {
             $sPercentBandwidth = 0;
         }
         $sStatistics = array("info" => array("uptime" => ConvertTime($sUptime[0]), "used_ram" => $sUsedRAM, "total_ram" => $sTotalRAM, "percent_ram" => $sPercentRAM, "used_swap" => $sUsedSWAP, "total_swap" => $sTotalSWAP, "percent_swap" => $sPercentSWAP, "used_disk" => $sDiskUsed, "total_disk" => round($sDiskTotal, 0), "percent_disk" => $sPercentDisk, "total_cpu" => $sVPS->sCPULimit, "total_cores" => $sCores, "used_cores" => $sCPU[0], "used_cpu" => $sCPU[0] * 100, "percent_cpu" => $sPercentCPU, "load_average" => "{$sCPU['0']} {$sCPU['1']} {$sCPU['2']}", "bandwidth_usage" => $sBandwidthUsage, "bandwidth_limit" => FormatBytes($sVPS->sBandwidthLimit * 1024, 0), "percent_bandwidth" => round(100 / ($sVPS->sBandwidthLimit * 1024) * $sVPS->sBandwidthUsage, 0), "operating_system" => $sTemplateName, "hostname" => $sVPS->sHostname, "primary_ip" => $sVPS->sPrimaryIP));
         $sStatistics = Templater::AdvancedParse($sTemplate->sValue . '/' . $sVPS->sType . '.statistics', $locale->strings, array("Statistics" => $sStatistics));
         return $sArray = array("json" => 1, "type" => "status", "result" => "online", "content" => $sStatistics, "hostname" => $sVPS->sHostname);
     }
 }