feat: implement bandwidth enforcement mechanism in Proxmox server component to throttle servers exceeding usage limits, enhancing resource management
This commit is contained in:
@@ -2,7 +2,9 @@
|
||||
|
||||
namespace Paymenter\Extensions\Servers\Proxmox\Console\Commands;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Console\Command;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Paymenter\Extensions\Servers\Proxmox\Models\Server;
|
||||
use Paymenter\Extensions\Servers\Proxmox\Models\ServerStatistics;
|
||||
use Paymenter\Extensions\Servers\Proxmox\Proxmox;
|
||||
@@ -166,6 +168,145 @@ class PollServerStatistics extends Command
|
||||
}
|
||||
|
||||
$this->info('Statistics polling completed. Success: ' . $successCount . ', Errors: ' . $errorCount);
|
||||
|
||||
// Enforce bandwidth throttling for servers exceeding their limits
|
||||
$this->info('Checking bandwidth limits...');
|
||||
$this->enforceBandwidthLimits();
|
||||
}
|
||||
|
||||
protected function enforceBandwidthLimits()
|
||||
{
|
||||
$threshold = $this->calculateBandwidthThreshold();
|
||||
$this->info('Bandwidth threshold: ' . $threshold . ' GiB');
|
||||
|
||||
$throttledCount = 0;
|
||||
$servers = Server::all();
|
||||
|
||||
foreach ($servers as $server) {
|
||||
try {
|
||||
$monthlyTraffic = $this->calculateServerMonthlyTraffic($server);
|
||||
|
||||
if ($monthlyTraffic > $threshold) {
|
||||
$this->warn('Server ID ' . $server->id . ' (' . $server->hostname . ') exceeded bandwidth: ' .
|
||||
round($monthlyTraffic, 2) . ' GiB / ' . $threshold . ' GiB');
|
||||
$this->throttleServerBandwidth($server);
|
||||
$throttledCount++;
|
||||
}
|
||||
} catch (\Throwable $e) {
|
||||
$this->error('Error checking bandwidth for server ID ' . $server->id . ': ' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
$this->info('Bandwidth enforcement completed. Throttled: ' . $throttledCount . ' servers');
|
||||
}
|
||||
|
||||
protected function calculateBandwidthThreshold()
|
||||
{
|
||||
return Cache::remember('bandwidth_threshold_95_' . Carbon::now()->format('Y-m'), 15, function () {
|
||||
$startOfMonth = Carbon::now()->startOfMonth();
|
||||
$allServers = Server::all();
|
||||
|
||||
if ($allServers->isEmpty()) {
|
||||
return 10;
|
||||
}
|
||||
|
||||
$totalTraffic = 0;
|
||||
$serverCount = 0;
|
||||
|
||||
foreach ($allServers as $server) {
|
||||
$firstStat = ServerStatistics::where('server_id', $server->id)
|
||||
->where('DateTime', '>=', $startOfMonth)
|
||||
->orderBy('DateTime', 'asc')
|
||||
->first();
|
||||
|
||||
$latestStat = ServerStatistics::where('server_id', $server->id)
|
||||
->where('DateTime', '>=', $startOfMonth)
|
||||
->orderBy('DateTime', 'desc')
|
||||
->first();
|
||||
|
||||
if ($firstStat && $latestStat) {
|
||||
$netinDiff = max(0, $latestStat->netin - $firstStat->netin);
|
||||
$netoutDiff = max(0, $latestStat->netout - $firstStat->netout);
|
||||
$totalBytes = $netinDiff + $netoutDiff;
|
||||
$totalTraffic += $totalBytes / (1024 * 1024 * 1024);
|
||||
$serverCount++;
|
||||
}
|
||||
}
|
||||
|
||||
if ($serverCount === 0) {
|
||||
return 10;
|
||||
}
|
||||
|
||||
$averageBandwidth = $totalTraffic / $serverCount;
|
||||
$threshold95Percent = $averageBandwidth * 0.95;
|
||||
|
||||
return round(max($threshold95Percent, 10), 2);
|
||||
});
|
||||
}
|
||||
|
||||
protected function calculateServerMonthlyTraffic(Server $server)
|
||||
{
|
||||
$startOfMonth = Carbon::now()->startOfMonth();
|
||||
|
||||
$firstStat = ServerStatistics::where('server_id', $server->id)
|
||||
->where('DateTime', '>=', $startOfMonth)
|
||||
->orderBy('DateTime', 'asc')
|
||||
->first();
|
||||
|
||||
$latestStat = ServerStatistics::where('server_id', $server->id)
|
||||
->where('DateTime', '>=', $startOfMonth)
|
||||
->orderBy('DateTime', 'desc')
|
||||
->first();
|
||||
|
||||
if (!$firstStat || !$latestStat) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
$netinDiff = max(0, $latestStat->netin - $firstStat->netin);
|
||||
$netoutDiff = max(0, $latestStat->netout - $firstStat->netout);
|
||||
$totalBytes = $netinDiff + $netoutDiff;
|
||||
|
||||
return round($totalBytes / (1024 * 1024 * 1024), 2);
|
||||
}
|
||||
|
||||
protected function throttleServerBandwidth(Server $server)
|
||||
{
|
||||
try {
|
||||
$proxmox = new Proxmox;
|
||||
$config = $proxmox->request('/nodes/' . $server->node->name . '/qemu/' . $server->vm_id . '/config')->json()['data'];
|
||||
|
||||
$updates = [];
|
||||
|
||||
foreach ($config as $key => $value) {
|
||||
if (preg_match('/^net\d+$/', $key)) {
|
||||
$parts = explode(',', $value);
|
||||
$newParts = [];
|
||||
$hasRate = false;
|
||||
|
||||
foreach ($parts as $part) {
|
||||
if (strpos($part, 'rate=') === 0) {
|
||||
$newParts[] = 'rate=1';
|
||||
$hasRate = true;
|
||||
} else {
|
||||
$newParts[] = $part;
|
||||
}
|
||||
}
|
||||
|
||||
if (!$hasRate) {
|
||||
$newParts[] = 'rate=1';
|
||||
}
|
||||
|
||||
$updates[$key] = implode(',', $newParts);
|
||||
}
|
||||
}
|
||||
|
||||
if (!empty($updates)) {
|
||||
$proxmox->request('/nodes/' . $server->node->name . '/qemu/' . $server->vm_id . '/config', 'put', $updates);
|
||||
$this->info('Throttled network interfaces for server ID ' . $server->id . ' to 1 MiB/s');
|
||||
}
|
||||
} catch (\Throwable $e) {
|
||||
$this->error('Failed to throttle bandwidth for server ID ' . $server->id . ': ' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user