Creado endpoint para mostrar logs desde disco de los distintos componentes de OG
testing/ogcore-api/pipeline/head There was a failure building this commit Details

disk-logs^2
Juan Manuel Bardallo 2026-05-14 13:18:10 +02:00
parent fb6e3c0e53
commit d2b7b0265a
1 changed files with 59 additions and 0 deletions

View File

@ -0,0 +1,59 @@
<?php
declare(strict_types=1);
namespace App\Controller\System;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Http\Attribute\IsGranted;
class DiskLogsAction extends AbstractController
{
#[Route('/opengnsys/system/disk-logs', name: 'api_system_disk_logs', methods: ['GET'])]
#[IsGranted('ROLE_ADMIN')]
public function __invoke(Request $request): JsonResponse
{
$identifier = $request->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)
]);
}
}