feat: VM metrics via portal-monitor integration
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 5.5
parent
0b08689ea4
commit
9ed77a85fd
@@ -6,12 +6,18 @@ use App\Http\Controllers\Controller;
|
||||
use App\Mail\VmSuspendedMail;
|
||||
use App\Models\ModuleVmware;
|
||||
use App\Models\VirtualMachines;
|
||||
use App\Services\VmMetricsService;
|
||||
use Illuminate\Http\Request;
|
||||
use App\Support\TlsHttpClientFactory;
|
||||
use Illuminate\Support\Facades\Mail;
|
||||
use RuntimeException;
|
||||
|
||||
class VCenterManagerController extends Controller
|
||||
{
|
||||
public function __construct(private readonly VmMetricsService $vmMetrics)
|
||||
{
|
||||
}
|
||||
|
||||
public function powerState(Request $request)
|
||||
{
|
||||
$vmId = (int) $request->input('vm_id');
|
||||
@@ -264,54 +270,22 @@ class VCenterManagerController extends Controller
|
||||
return response()->json(['error' => true, 'message' => 'VM is not powered on'], 400);
|
||||
}
|
||||
|
||||
$vcenter = $vm->datacenter?->vCenter ?? $vm->host?->datacenter?->vCenter;
|
||||
try {
|
||||
$stats = $this->vmMetrics->get($vmwareId, $vm->ram_mb, $vm->cpu);
|
||||
} catch (RuntimeException $exception) {
|
||||
$status = $exception->getCode() === 404 ? 404 : 502;
|
||||
|
||||
if (!$vcenter) {
|
||||
return response()->json(['error' => true, 'message' => 'vCenter not found'], 400);
|
||||
}
|
||||
|
||||
$vCenterService = new \App\Http\Services\vCenter();
|
||||
|
||||
if (!$vCenterService->loginServer($vcenter)) {
|
||||
return response()->json(['error' => true, 'message' => 'Cannot login to vCenter'], 400);
|
||||
}
|
||||
|
||||
$result = $vCenterService->GetVMStats($vmwareId);
|
||||
|
||||
if ($result['error'] ?? true) {
|
||||
return response()->json([
|
||||
'error' => true,
|
||||
'message' => $result['message'] ?? 'Failed to fetch VM stats',
|
||||
], 400);
|
||||
'message' => $status === 404
|
||||
? 'VM metrics are unavailable'
|
||||
: 'Unable to fetch VM metrics',
|
||||
], $status);
|
||||
}
|
||||
|
||||
$stats = $result['data'];
|
||||
$cpuPercent = $stats['cpu']['used_percent'] ?? 0;
|
||||
$memPercent = $stats['memory']['usage_percent'] ?? 0;
|
||||
|
||||
// ponytail: overall_status is a CPU/RAM usage threshold heuristic, not real vCenter
|
||||
// health/alarms — that needs the SOAP EventManager/AlarmManager, not wired up in this
|
||||
// codebase (REST Automation API only). Upgrade path: add a SOAP client and read
|
||||
// triggeredAlarmState off the VM's ManagedEntity if per-alarm detail is ever needed.
|
||||
$overallStatus = match (true) {
|
||||
$cpuPercent >= 90 || $memPercent >= 90 => 'red',
|
||||
$cpuPercent >= 75 || $memPercent >= 75 => 'yellow',
|
||||
default => 'green',
|
||||
};
|
||||
|
||||
return response()->json([
|
||||
'error' => false,
|
||||
'data' => [
|
||||
'overall_status' => $overallStatus,
|
||||
'cpu_usage_percent' => round($cpuPercent, 1),
|
||||
'mem_usage_percent' => round($memPercent, 1),
|
||||
'mem_usage_mb' => $stats['memory']['used_MiB'] ?? 0,
|
||||
'mem_total_mb' => $stats['memory']['size_MiB'] ?? 0,
|
||||
// ponytail: no uptime source wired up (would need SOAP PerfManager
|
||||
// sys.uptime.latest); frontend renders "—" when null.
|
||||
'uptime_seconds' => null,
|
||||
'alarms' => [],
|
||||
],
|
||||
'data' => $stats,
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user