feat: VM metrics via portal-monitor integration

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
baodev0
2026-10-17 13:04:35 +07:00
co-authored by Claude Sonnet 5.5
parent 0b08689ea4
commit 9ed77a85fd
15 changed files with 546 additions and 43 deletions
+55
View File
@@ -0,0 +1,55 @@
<?php
namespace App\Services;
use Illuminate\Http\Client\ConnectionException;
use Illuminate\Support\Facades\Http;
use RuntimeException;
class PortalMonitorClient
{
public function vmMetrics(string $vmwareId): array
{
return $this->get($vmwareId, 'metrics', 'VM metrics are unavailable');
}
public function vmStorage(string $vmwareId): array
{
return $this->get($vmwareId, 'storage', 'VM storage is unavailable');
}
private function get(string $vmwareId, string $resource, string $notFoundMessage): array
{
$url = rtrim((string) config('portal-monitor.url'), '/');
$apiKey = (string) config('portal-monitor.api_key');
if ($url === '' || $apiKey === '') {
throw new RuntimeException('Portal Monitor is not configured');
}
try {
$response = Http::acceptJson()
->withHeaders(['X-API-Key' => $apiKey])
->timeout((int) config('portal-monitor.timeout', 5))
->get($url . '/api/v1/vsphere/vms/' . rawurlencode($vmwareId) . '/' . $resource);
} catch (ConnectionException) {
throw new RuntimeException('Portal Monitor is unavailable');
}
if ($response->status() === 404) {
throw new RuntimeException($notFoundMessage, 404);
}
if (!$response->successful()) {
throw new RuntimeException('Portal Monitor request failed');
}
$data = $response->json('data');
if (!is_array($data)) {
throw new RuntimeException('Portal Monitor returned an invalid response');
}
return $data;
}
}
+41
View File
@@ -0,0 +1,41 @@
<?php
namespace App\Services;
class VmMetricsService
{
public function __construct(private readonly PortalMonitorClient $portalMonitor)
{
}
public function get(string $vmwareId, ?int $memoryTotalMb = null, ?int $cpuCores = null): array
{
$stats = $this->portalMonitor->vmMetrics($vmwareId);
$storage = $this->portalMonitor->vmStorage($vmwareId);
$provisionedDiskBytes = collect($storage['disks'] ?? [])->sum(
fn ($disk) => is_array($disk) ? (int) ($disk['capacity_bytes'] ?? 0) : 0
);
$diskSummary = is_array($storage['summary'] ?? null) ? $storage['summary'] : [];
return [
'cpu_usage_percent' => $stats['cpu_usage_percent'] ?? null,
'cpu_cores' => $cpuCores,
'cpu_mhz' => $stats['cpu_mhz'] ?? null,
'cpu_ready_ms' => $stats['cpu_ready_ms'] ?? null,
'mem_usage_percent' => $stats['guest_memory_usage_percent'] ?? null,
'mem_usage_mb' => $stats['memory_active_mb'] ?? null,
'mem_consumed_mb' => $stats['memory_consumed_mb'] ?? null,
'mem_total_mb' => $memoryTotalMb,
'disk_read_bps' => $stats['disk_read_bps'] ?? null,
'disk_write_bps' => $stats['disk_write_bps'] ?? null,
'disk_latency_ms' => $stats['disk_latency_ms'] ?? null,
'disk_total_bytes' => $diskSummary['total_bytes'] ?? ($provisionedDiskBytes ?: null),
'disk_used_bytes' => $diskSummary['used_bytes'] ?? null,
'disk_usage_percent' => $diskSummary['usage_percent'] ?? null,
'network_rx_bps' => $stats['network_rx_bps'] ?? null,
'network_tx_bps' => $stats['network_tx_bps'] ?? null,
'timestamp' => $stats['timestamp'] ?? null,
'stale' => (bool) ($stats['stale'] ?? false),
];
}
}