update optimize code
This commit is contained in:
@@ -1201,10 +1201,10 @@ class vCenter
|
||||
return "Login_Again";
|
||||
}
|
||||
|
||||
public function RebootVirtualMachine($vmware_id = ''): array|string
|
||||
public function RebootVirtualMachine($product, $vmware_id = ''): array|string
|
||||
{
|
||||
|
||||
$check = $this->checkApiExits();
|
||||
$check = $this->loginServer($product->ipaddress->host->datacenter->vCenter);
|
||||
if ($check) {
|
||||
$response = Http::withoutVerifying()
|
||||
->withHeaders([
|
||||
@@ -1225,10 +1225,10 @@ class vCenter
|
||||
return "Login_Again";
|
||||
}
|
||||
|
||||
public function StartVirtualMachine($vmware_id = ''): array|string
|
||||
public function StartVirtualMachine($product, $vmware_id = ''): array|string
|
||||
{
|
||||
|
||||
$check = $this->checkApiExits();
|
||||
$check = $this->loginServer($product->ipaddress->host->datacenter->vCenter);
|
||||
if ($check) {
|
||||
$response = Http::withoutVerifying()
|
||||
->withHeaders([
|
||||
@@ -1249,9 +1249,9 @@ class vCenter
|
||||
return "Login_Again";
|
||||
}
|
||||
|
||||
public function ShutdownGuestVirtualMachine($vmware_id = ''): array|string
|
||||
public function ShutdownGuestVirtualMachine($product, $vmware_id = ''): array|string
|
||||
{
|
||||
$check = $this->checkApiExits();
|
||||
$check = $this->loginServer($product->ipaddress->host->datacenter->vCenter);
|
||||
|
||||
if ($check) {
|
||||
$response = Http::withoutVerifying()
|
||||
@@ -1391,7 +1391,7 @@ class vCenter
|
||||
public function getSnapShot($product): string|array
|
||||
{
|
||||
$startTime = microtime(true);
|
||||
|
||||
|
||||
try {
|
||||
$this->username = $product->ipaddress->host->datacenter->vCenter->user;
|
||||
$this->password = SignRSA::DecryptFunc($product->ipaddress->host->datacenter->vCenter->password);
|
||||
@@ -1399,21 +1399,21 @@ class vCenter
|
||||
} catch (\Exception $e) {
|
||||
return ['error' => true, 'message' => 'Lỗi setup credentials: ' . $e->getMessage()];
|
||||
}
|
||||
|
||||
|
||||
$vmware_name = (string) $product->name;
|
||||
|
||||
|
||||
// Check cache first
|
||||
$cacheKey = "snapshots_{$product->id}_{$vmware_name}";
|
||||
$cachedResult = Cache::get($cacheKey);
|
||||
|
||||
|
||||
if ($cachedResult !== null) {
|
||||
$executionTime = round((microtime(true) - $startTime) * 1000, 2);
|
||||
Log::channel('Custom')->info("📸 Snapshot cache hit - Execution time: {$executionTime}ms");
|
||||
return $cachedResult;
|
||||
}
|
||||
|
||||
|
||||
$check = $this->checkApiExits();
|
||||
|
||||
|
||||
if ($check) {
|
||||
try {
|
||||
Log::channel('Custom')->info("===================================================================");
|
||||
@@ -1440,22 +1440,22 @@ class vCenter
|
||||
try {
|
||||
// Clean up the result - remove any PowerShell formatting
|
||||
$cleanResult = trim($result);
|
||||
|
||||
|
||||
// Parse the text output from the script
|
||||
$snapshots = $this->parseSnapshotTextOutput($cleanResult);
|
||||
|
||||
|
||||
if (!empty($snapshots)) {
|
||||
$response = [
|
||||
'error' => false,
|
||||
'data' => $snapshots
|
||||
];
|
||||
|
||||
|
||||
// Cache the result for 5 minutes
|
||||
Cache::put($cacheKey, $response, now()->addMinutes(5));
|
||||
|
||||
|
||||
$totalTime = round((microtime(true) - $startTime) * 1000, 2);
|
||||
Log::channel('Custom')->info("📸 Snapshot retrieved successfully - Total time: {$totalTime}ms, Command time: {$commandTime}ms");
|
||||
|
||||
|
||||
return $response;
|
||||
} else {
|
||||
$response = [
|
||||
@@ -1463,10 +1463,10 @@ class vCenter
|
||||
'message' => 'Không tìm thấy snapshot nào hoặc lỗi parse dữ liệu',
|
||||
'raw_result' => $result
|
||||
];
|
||||
|
||||
|
||||
$totalTime = round((microtime(true) - $startTime) * 1000, 2);
|
||||
Log::channel('Custom')->info("📸 No snapshots found - Total time: {$totalTime}ms");
|
||||
|
||||
|
||||
return $response;
|
||||
}
|
||||
} catch (\Exception $parseError) {
|
||||
@@ -1476,10 +1476,10 @@ class vCenter
|
||||
'message' => 'Lỗi parse kết quả snapshot: ' . $parseError->getMessage(),
|
||||
'raw_result' => $result
|
||||
];
|
||||
|
||||
|
||||
$totalTime = round((microtime(true) - $startTime) * 1000, 2);
|
||||
Log::channel('Custom')->info("📸 Parse error - Total time: {$totalTime}ms");
|
||||
|
||||
|
||||
return $response;
|
||||
}
|
||||
} else {
|
||||
@@ -1488,10 +1488,10 @@ class vCenter
|
||||
'message' => 'Không thể thực thi lệnh hoặc đăng nhập thất bại',
|
||||
'raw_result' => $result
|
||||
];
|
||||
|
||||
|
||||
$totalTime = round((microtime(true) - $startTime) * 1000, 2);
|
||||
Log::channel('Custom')->info("📸 Command failed - Total time: {$totalTime}ms");
|
||||
|
||||
|
||||
return $response;
|
||||
}
|
||||
} catch (\Exception $error) {
|
||||
@@ -1500,20 +1500,20 @@ class vCenter
|
||||
Log::channel('Custom')->info("Error Log: ");
|
||||
Log::channel('Custom')->info($error->getMessage());
|
||||
Log::channel('Custom')->info("===================================================================");
|
||||
|
||||
|
||||
$totalTime = round((microtime(true) - $startTime) * 1000, 2);
|
||||
Log::channel('Custom')->info("📸 Exception occurred - Total time: {$totalTime}ms");
|
||||
|
||||
|
||||
return [
|
||||
'error' => true,
|
||||
'message' => $error->getMessage()
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
$totalTime = round((microtime(true) - $startTime) * 1000, 2);
|
||||
Log::channel('Custom')->info("📸 Login again required - Total time: {$totalTime}ms");
|
||||
|
||||
|
||||
return "Login_Again";
|
||||
}
|
||||
|
||||
@@ -1767,7 +1767,8 @@ class vCenter
|
||||
// Kiểm tra hệ điều hành
|
||||
if (str_contains($this->os, "Windows")) {
|
||||
$user = $data['user'] ?? 'administartor';
|
||||
$this->process = new WinRMClient($ip, $user, $password);;
|
||||
$this->process = new WinRMClient($ip, $user, $password);
|
||||
;
|
||||
return $this->process;
|
||||
}
|
||||
|
||||
@@ -2129,35 +2130,39 @@ class vCenter
|
||||
private function parseSnapshotTextOutput($output): array
|
||||
{
|
||||
$snapshots = [];
|
||||
|
||||
|
||||
// Split the output into individual snapshot blocks
|
||||
$blocks = preg_split('/\n\s*\n/', $output);
|
||||
|
||||
|
||||
foreach ($blocks as $block) {
|
||||
$block = trim($block);
|
||||
if (empty($block)) continue;
|
||||
|
||||
if (empty($block))
|
||||
continue;
|
||||
|
||||
// Skip header lines and warnings
|
||||
if (strpos($block, 'Name Port User') !== false ||
|
||||
if (
|
||||
strpos($block, 'Name Port User') !== false ||
|
||||
strpos($block, '---- ---- ----') !== false ||
|
||||
strpos($block, 'WARNING:') !== false) {
|
||||
strpos($block, 'WARNING:') !== false
|
||||
) {
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
$snapshot = [];
|
||||
|
||||
|
||||
// Parse each line of the snapshot block
|
||||
$lines = explode("\n", $block);
|
||||
foreach ($lines as $line) {
|
||||
$line = trim($line);
|
||||
if (empty($line)) continue;
|
||||
|
||||
if (empty($line))
|
||||
continue;
|
||||
|
||||
// Parse key-value pairs
|
||||
if (strpos($line, ' : ') !== false) {
|
||||
list($key, $value) = explode(' : ', $line, 2);
|
||||
$key = trim($key);
|
||||
$value = trim($value);
|
||||
|
||||
|
||||
switch ($key) {
|
||||
case 'Name':
|
||||
$snapshot['name'] = $value;
|
||||
@@ -2214,13 +2219,13 @@ class vCenter
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Only add if we have at least a name
|
||||
if (!empty($snapshot['name'])) {
|
||||
$snapshots[] = $snapshot;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return $snapshots;
|
||||
}
|
||||
|
||||
@@ -2230,14 +2235,15 @@ class vCenter
|
||||
private function parseSnapshotOutput($output): array
|
||||
{
|
||||
$snapshots = [];
|
||||
|
||||
|
||||
// Split by lines and look for snapshot information
|
||||
$lines = explode("\n", $output);
|
||||
|
||||
|
||||
foreach ($lines as $line) {
|
||||
$line = trim($line);
|
||||
if (empty($line)) continue;
|
||||
|
||||
if (empty($line))
|
||||
continue;
|
||||
|
||||
// Look for snapshot name patterns
|
||||
if (preg_match('/(\w+_\d{2}_\d{2}_\d{4}_\d{2}_\d{2}_\d{2})/', $line, $matches)) {
|
||||
$snapshots[] = [
|
||||
@@ -2246,7 +2252,7 @@ class vCenter
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return $snapshots;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user