191 lines
6.9 KiB
PHP
191 lines
6.9 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Services\vCenter\Concerns;
|
|
|
|
use App\Support\TlsHttpClientFactory;
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
trait ReadsVirtualMachineInfo
|
|
{
|
|
public function getAllVirtualMachine($datacenter = '', $host = '')
|
|
{
|
|
if ($this->client->checkApiExits()) {
|
|
$params = [];
|
|
if (!empty($datacenter)) {
|
|
$params['filter.datacenters'] = $datacenter;
|
|
}
|
|
if (!empty($host)) {
|
|
$params['filter.hosts'] = $host;
|
|
}
|
|
|
|
$response = TlsHttpClientFactory::vCenter()
|
|
->withHeaders([
|
|
'vmware-api-session-id' => $this->client->token
|
|
])
|
|
->get("https://{$this->client->apiUri}/rest/vcenter/vm", $params)->json();
|
|
|
|
if (!isset($response['type'])) {
|
|
return $response['value'];
|
|
}
|
|
return false;
|
|
}
|
|
return "Login_Again";
|
|
}
|
|
|
|
public function CheckVirtualMachine($vmware_id = '')
|
|
{
|
|
if ($this->client->checkApiExits()) {
|
|
$response = TlsHttpClientFactory::vCenter()
|
|
->withHeaders([
|
|
'vmware-api-session-id' => $this->client->token
|
|
])
|
|
->get("https://{$this->client->apiUri}/rest/vcenter/vm/$vmware_id")->json();
|
|
|
|
if (!isset($response['type'])) {
|
|
return $response['value'];
|
|
}
|
|
return false;
|
|
}
|
|
return "Login_Again";
|
|
}
|
|
|
|
/**
|
|
* Search VM by exact name in vCenter.
|
|
* Returns the first matching VM array or null if not found.
|
|
*/
|
|
public function findVirtualMachineByName(string $vmName): ?array
|
|
{
|
|
$check = $this->client->checkApiExits();
|
|
if (!$check) {
|
|
Log::warning('findVirtualMachineByName: API check failed');
|
|
return null;
|
|
}
|
|
|
|
$response = TlsHttpClientFactory::vCenter()
|
|
->withHeaders([
|
|
'vmware-api-session-id' => $this->client->token
|
|
])
|
|
->get("https://{$this->client->apiUri}/rest/vcenter/vm", [
|
|
'filter.names.1' => $vmName,
|
|
]);
|
|
|
|
if (!$response->successful()) {
|
|
Log::warning('findVirtualMachineByName: HTTP failed', ['status' => $response->status()]);
|
|
return null;
|
|
}
|
|
|
|
$json = $response->json();
|
|
if (isset($json['type']) || empty($json['value'])) {
|
|
return null;
|
|
}
|
|
|
|
return $json['value'][0] ?? null;
|
|
}
|
|
|
|
public function GetDetailVirtualMachine($vmware_id = ''): array
|
|
{
|
|
$response = TlsHttpClientFactory::vCenter()
|
|
->withHeaders([
|
|
'vmware-api-session-id' => $this->client->token
|
|
])
|
|
->get("https://{$this->client->apiUri}/rest/vcenter/vm/$vmware_id")->json();
|
|
if (!isset($response['type'])) {
|
|
return [
|
|
'error' => false,
|
|
'data' => $response['value']
|
|
];
|
|
}
|
|
return [
|
|
'error' => true,
|
|
'message' => $response['value']
|
|
];
|
|
}
|
|
|
|
public function GetIPAddressVirtualMachine($vmware_id = ''): array
|
|
{
|
|
$response = TlsHttpClientFactory::vCenter()
|
|
->withHeaders([
|
|
'vmware-api-session-id' => $this->client->token
|
|
])
|
|
->get("https://{$this->client->apiUri}/rest/vcenter/vm/$vmware_id/guest/identity")->json();
|
|
if (!isset($response['type'])) {
|
|
return [
|
|
'error' => false,
|
|
'data' => $response['value']
|
|
];
|
|
}
|
|
return [
|
|
'error' => true,
|
|
'message' => $response['value']
|
|
];
|
|
}
|
|
|
|
public function GetVMStats($vmware_id = ''): array
|
|
{
|
|
$stats = [
|
|
'cpu' => null,
|
|
'memory' => null,
|
|
'disk' => null,
|
|
];
|
|
|
|
try {
|
|
$hardwareResponse = TlsHttpClientFactory::vCenter()
|
|
->withHeaders([
|
|
'vmware-api-session-id' => $this->client->token
|
|
])
|
|
->get("https://{$this->client->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 = TlsHttpClientFactory::vCenter()
|
|
->withHeaders([
|
|
'vmware-api-session-id' => $this->client->token
|
|
])
|
|
->get("https://{$this->client->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 = TlsHttpClientFactory::vCenter()
|
|
->withHeaders([
|
|
'vmware-api-session-id' => $this->client->token
|
|
])
|
|
->get("https://{$this->client->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];
|
|
}
|
|
}
|
|
}
|