feat: enhance firewall log loading in Proxmox component with improved response handling and error logging for better reliability and debugging
This commit is contained in:
@@ -766,16 +766,83 @@ class Firewall extends Component
|
|||||||
public function loadFirewallLogs(): void
|
public function loadFirewallLogs(): void
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
|
// Try the firewall log endpoint
|
||||||
$response = $this->firewallRequest(
|
$response = $this->firewallRequest(
|
||||||
'/nodes/' . $this->server->node->name . '/qemu/' . $this->server->vm_id . '/firewall/log',
|
'/nodes/' . $this->server->node->name . '/qemu/' . $this->server->vm_id . '/firewall/log',
|
||||||
'get',
|
'get',
|
||||||
['limit' => $this->logLimit]
|
['limit' => $this->logLimit]
|
||||||
);
|
);
|
||||||
$rawLogs = $response->json()['data'] ?? [];
|
|
||||||
|
// Check if response is successful
|
||||||
|
if (!$response->successful()) {
|
||||||
|
\Log::warning('Firewall log API returned non-success status', [
|
||||||
|
'status' => $response->status(),
|
||||||
|
'body' => $response->body()
|
||||||
|
]);
|
||||||
|
$this->firewallLogs = [];
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$responseData = $response->json();
|
||||||
|
|
||||||
|
// Proxmox firewall log API can return data in different formats
|
||||||
|
// Try multiple possible response structures
|
||||||
|
$rawLogs = [];
|
||||||
|
|
||||||
|
if (isset($responseData['data']) && is_array($responseData['data'])) {
|
||||||
|
$rawLogs = $responseData['data'];
|
||||||
|
} elseif (is_array($responseData) && !isset($responseData['data'])) {
|
||||||
|
// Response might be the array directly
|
||||||
|
$rawLogs = $responseData;
|
||||||
|
}
|
||||||
|
|
||||||
|
// If still empty, try to get logs from the response body as text
|
||||||
|
if (empty($rawLogs)) {
|
||||||
|
$body = $response->body();
|
||||||
|
if (!empty($body) && is_string($body)) {
|
||||||
|
// Try to parse as JSON first
|
||||||
|
$jsonData = json_decode($body, true);
|
||||||
|
if (json_last_error() === JSON_ERROR_NONE && is_array($jsonData)) {
|
||||||
|
$rawLogs = $jsonData['data'] ?? $jsonData;
|
||||||
|
} else {
|
||||||
|
// If not JSON, try to split by newlines (plain text format)
|
||||||
|
$lines = explode("\n", trim($body));
|
||||||
|
$rawLogs = array_filter($lines, function($line) {
|
||||||
|
return !empty(trim($line));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
\Log::debug('Firewall logs raw response', [
|
||||||
|
'raw_logs_count' => count($rawLogs),
|
||||||
|
'sample_entry' => !empty($rawLogs) ? (is_array($rawLogs[0]) ? $rawLogs[0] : substr($rawLogs[0], 0, 100)) : null
|
||||||
|
]);
|
||||||
|
|
||||||
$this->firewallLogs = [];
|
$this->firewallLogs = [];
|
||||||
|
|
||||||
foreach ($rawLogs as $logEntry) {
|
foreach ($rawLogs as $logEntry) {
|
||||||
$logText = $logEntry['n'] ?? $logEntry['t'] ?? '';
|
$logText = null;
|
||||||
|
|
||||||
|
// Try different possible field names for the log text
|
||||||
|
if (is_string($logEntry)) {
|
||||||
|
// If the entry is a string directly, use it
|
||||||
|
$logText = trim($logEntry);
|
||||||
|
} elseif (is_array($logEntry)) {
|
||||||
|
// Try common field names
|
||||||
|
$logText = $logEntry['n'] ?? $logEntry['t'] ?? $logEntry['text'] ?? $logEntry['log'] ?? $logEntry['message'] ?? $logEntry['line'] ?? '';
|
||||||
|
|
||||||
|
// If still empty, try to get the first string value
|
||||||
|
if (empty($logText)) {
|
||||||
|
foreach ($logEntry as $value) {
|
||||||
|
if (is_string($value) && !empty(trim($value))) {
|
||||||
|
$logText = trim($value);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (empty($logText)) {
|
if (empty($logText)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@@ -786,7 +853,18 @@ class Firewall extends Component
|
|||||||
$this->firewallLogs[] = $parsed;
|
$this->firewallLogs[] = $parsed;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
\Log::debug('Firewall logs parsed', [
|
||||||
|
'parsed_count' => count($this->firewallLogs)
|
||||||
|
]);
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
|
// Log the error for debugging
|
||||||
|
\Log::error('Failed to load firewall logs: ' . $e->getMessage(), [
|
||||||
|
'server_id' => $this->server->id,
|
||||||
|
'vm_id' => $this->server->vm_id,
|
||||||
|
'node' => $this->server->node->name ?? 'unknown',
|
||||||
|
'trace' => $e->getTraceAsString()
|
||||||
|
]);
|
||||||
$this->firewallLogs = [];
|
$this->firewallLogs = [];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user