feat: Implement VM console access, display VM power state, and add VM statistics retrieval.
This commit is contained in:
@@ -66,6 +66,7 @@ class VirtualMachineController extends Controller
|
||||
if (!$vm) {
|
||||
return back()->withErrors('Invalid request.');
|
||||
}
|
||||
$vm->load('datacenter', 'host');
|
||||
$title = 'Edit Virtual Machine';
|
||||
return view('administrator.pages.package.vm.edit', compact('title', 'vm'));
|
||||
}
|
||||
@@ -137,10 +138,18 @@ class VirtualMachineController extends Controller
|
||||
->editColumn('updated_at', function ($d) {
|
||||
return $d->updated_at ? $d->updated_at->format('H:i:s d/m/Y') : '';
|
||||
})
|
||||
->editColumn('power_state', function ($d) {
|
||||
if ($d->power_state == 'POWERED_ON') {
|
||||
return '<span class="badge bg-success">Đang bật</span>';
|
||||
} elseif ($d->power_state == 'POWERED_OFF') {
|
||||
return '<span class="badge bg-secondary">Đang tắt</span>';
|
||||
}
|
||||
return $d->power_state ?? 'N/A';
|
||||
})
|
||||
->editColumn('datacenter.name', function ($d) {
|
||||
return $d->datacenter ? $d->datacenter->name . ' - ' . $d->datacenter->datacenter_id : 'N/A';
|
||||
})
|
||||
->rawColumns(['action'])
|
||||
->rawColumns(['action', 'power_state'])
|
||||
->make(true);
|
||||
}
|
||||
}
|
||||
@@ -1141,6 +1141,74 @@ PS;
|
||||
];
|
||||
}
|
||||
|
||||
public function GetVMStats($vmware_id = ''): array
|
||||
{
|
||||
$stats = [
|
||||
'cpu' => null,
|
||||
'memory' => null,
|
||||
'disk' => null,
|
||||
];
|
||||
|
||||
try {
|
||||
$hardwareResponse = Http::withoutVerifying()
|
||||
->withHeaders([
|
||||
'vmware-api-session-id' => $this->token
|
||||
])
|
||||
->get("https://$this->apiUri/rest/vcenter/vm/$vmware_id/hardware")->json();
|
||||
|
||||
if (is_array($hardwareResponse) && !isset($hardwareResponse['type']) && isset($hardwareResponse['value'])) {
|
||||
foreach ($hardwareResponse['value'] as $hardware) {
|
||||
if (isset($hardware['label']) && $hardware['label'] === 'CPU') {
|
||||
$stats['cpu'] = [
|
||||
'cores' => $hardware['spec']['num_cores'] ?? 0,
|
||||
'cores_per_socket' => $hardware['spec']['cores_per_socket'] ?? 0,
|
||||
];
|
||||
}
|
||||
if (isset($hardware['label']) && $hardware['label'] === 'Memory') {
|
||||
$stats['memory'] = [
|
||||
'size_MiB' => $hardware['spec']['size_MiB'] ?? 0,
|
||||
'size_GB' => round(($hardware['spec']['size_MiB'] ?? 0) / 1024, 2),
|
||||
];
|
||||
}
|
||||
if (isset($hardware['label']) && $hardware['label'] === 'Hard disk') {
|
||||
$stats['disk'][] = [
|
||||
'capacity' => $hardware['capacity'] ?? 0,
|
||||
'capacity_GB' => round(($hardware['capacity'] ?? 0) / (1024 * 1024 * 1024), 2),
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$guestMemoryResponse = Http::withoutVerifying()
|
||||
->withHeaders([
|
||||
'vmware-api-session-id' => $this->token
|
||||
])
|
||||
->get("https://$this->apiUri/rest/vcenter/vm/$vmware_id/guest/memory")->json();
|
||||
|
||||
if (is_array($guestMemoryResponse) && !isset($guestMemoryResponse['type']) && isset($guestMemoryResponse['value'])) {
|
||||
$stats['memory']['used_MiB'] = $guestMemoryResponse['value']['used'] ?? 0;
|
||||
$stats['memory']['used_GB'] = round(($guestMemoryResponse['value']['used'] ?? 0) / 1024, 2);
|
||||
$stats['memory']['usage_percent'] = $stats['memory']['size_MiB'] > 0
|
||||
? round(($guestMemoryResponse['value']['used'] ?? 0) / $stats['memory']['size_MiB'] * 100, 1)
|
||||
: 0;
|
||||
}
|
||||
|
||||
$guestCpuResponse = Http::withoutVerifying()
|
||||
->withHeaders([
|
||||
'vmware-api-session-id' => $this->token
|
||||
])
|
||||
->get("https://$this->apiUri/rest/vcenter/vm/$vmware_id/guest/cpu")->json();
|
||||
|
||||
if (is_array($guestCpuResponse) && !isset($guestCpuResponse['type']) && isset($guestCpuResponse['value'])) {
|
||||
$stats['cpu']['used_percent'] = $guestCpuResponse['value']['used'] ?? 0;
|
||||
}
|
||||
|
||||
return ['error' => false, 'data' => $stats];
|
||||
} catch (\Exception $e) {
|
||||
return ['error' => true, 'message' => $e->getMessage(), 'data' => $stats];
|
||||
}
|
||||
}
|
||||
|
||||
public function ConfigCentOSVersion8($vmware_id = '', $product = [], $password = ''): void
|
||||
{
|
||||
$this->SetConfig($product->IpAddress->Host->Datacenter->vCenter);
|
||||
|
||||
Reference in New Issue
Block a user