query->get('SYSLOG_IDENTIFIER'); $host = $request->query->get('host'); $lines = $request->query->getInt('lines', 100); if (!$identifier) { return new JsonResponse(['error' => 'El parámetro SYSLOG_IDENTIFIER es obligatorio.'], Response::HTTP_BAD_REQUEST); } // Sanitización básica $lines = max(1, min(1000, $lines)); // Limitar entre 1 y 1000 líneas por seguridad/rendimiento $args = []; $args[] = sprintf('SYSLOG_IDENTIFIER=%s', escapeshellarg($identifier)); if ($host) { $args[] = sprintf('_HOSTNAME=%s', escapeshellarg($host)); } $command = sprintf('journalctl %s --no-pager -n %d 2>&1', implode(' ', $args), $lines); $output = []; $returnVar = 0; exec($command, $output, $returnVar); if ($returnVar !== 0) { return new JsonResponse([ 'error' => 'Error al ejecutar journalctl', 'command' => $command, 'output' => implode("\n", $output) ], Response::HTTP_INTERNAL_SERVER_ERROR); } return new JsonResponse([ 'identifier' => $identifier, 'host' => $host, 'lines' => $lines, 'content' => implode("\n", $output) ]); } }