60 lines
1.9 KiB
PHP
60 lines
1.9 KiB
PHP
<?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)
|
|
]);
|
|
}
|
|
}
|