refactor: enhance log processing in Proxmox Firewall component to support both JSON and text/plain formats, improving log entry parsing and ensuring robust handling of structured data
This commit is contained in:
@@ -832,19 +832,50 @@ class Firewall extends Component
|
||||
return;
|
||||
}
|
||||
|
||||
// Proxmox firewall log endpoint returns text/plain format
|
||||
// Each log entry is on a new line
|
||||
$body = $response->body();
|
||||
$rawLogs = [];
|
||||
$contentType = strtolower($response->header('Content-Type', ''));
|
||||
|
||||
if (! empty($body)) {
|
||||
// Split by newlines (handle both \n and \r\n)
|
||||
$lines = preg_split('/\r\n|\r|\n/', $body);
|
||||
$rawLogs = array_filter($lines, static function ($line) {
|
||||
return ! empty(trim($line));
|
||||
});
|
||||
// Re-index array after filtering
|
||||
$rawLogs = array_values($rawLogs);
|
||||
if (str_contains($contentType, 'application/json')) {
|
||||
// When Accept header allows JSON, Proxmox returns structured data
|
||||
$jsonData = $response->json();
|
||||
$entries = [];
|
||||
|
||||
if (is_array($jsonData)) {
|
||||
$entries = $jsonData['data'] ?? $jsonData;
|
||||
}
|
||||
|
||||
if (is_array($entries)) {
|
||||
foreach ($entries as $entry) {
|
||||
$logText = null;
|
||||
|
||||
if (is_array($entry)) {
|
||||
$logText = $entry['log']
|
||||
?? $entry['text']
|
||||
?? $entry['t']
|
||||
?? $entry['n']
|
||||
?? null;
|
||||
} elseif (is_string($entry)) {
|
||||
$logText = $entry;
|
||||
}
|
||||
|
||||
if (is_string($logText) && trim($logText) !== '') {
|
||||
$rawLogs[] = trim($logText);
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Proxmox firewall log endpoint may also return text/plain
|
||||
// Each log entry is on a new line (handle both \n and \r\n)
|
||||
$lines = preg_split('/\r\n|\r|\n/', $body);
|
||||
foreach ($lines as $line) {
|
||||
$line = trim((string) $line);
|
||||
if ($line !== '') {
|
||||
$rawLogs[] = $line;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
\Log::debug('Firewall logs fetched from Proxmox API', [
|
||||
|
||||
Reference in New Issue
Block a user