initial commit
This commit is contained in:
46
app/Services/Hosting/Metrics/MetricsCollectorService.php
Normal file
46
app/Services/Hosting/Metrics/MetricsCollectorService.php
Normal file
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Hosting\Metrics;
|
||||
|
||||
use App\Models\Customer;
|
||||
use App\Models\VmMetric;
|
||||
use App\Services\Hosting\Proxmox\ProxmoxClient;
|
||||
|
||||
class MetricsCollectorService
|
||||
{
|
||||
public function __construct(private readonly ProxmoxClient $proxmox) {}
|
||||
|
||||
public function collectAll(): int
|
||||
{
|
||||
$count = 0;
|
||||
$vms = Customer::query()->where('status', 'active')->whereNotNull('vmid')->get();
|
||||
|
||||
foreach ($vms as $vm) {
|
||||
try {
|
||||
$raw = $this->proxmox->getVMStatus((int) $vm->vmid);
|
||||
$normalized = $this->proxmox->normalizeLiveStatus($raw);
|
||||
|
||||
VmMetric::query()->create([
|
||||
'customer_id' => $vm->id,
|
||||
'cpu' => $normalized['cpu'],
|
||||
'mem' => $normalized['mem'],
|
||||
'maxmem' => $normalized['maxmem'],
|
||||
'disk' => $normalized['disk'],
|
||||
'maxdisk' => $normalized['maxdisk'],
|
||||
'recorded_at' => now(),
|
||||
]);
|
||||
|
||||
VmMetric::query()
|
||||
->where('customer_id', $vm->id)
|
||||
->where('recorded_at', '<', now()->subDays(7))
|
||||
->delete();
|
||||
|
||||
$count++;
|
||||
} catch (\Throwable) {
|
||||
//
|
||||
}
|
||||
}
|
||||
|
||||
return $count;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user