Merge pull request 'New endpoint. Disk logs.' (#104) from develop into main
testing/ogcore-api/pipeline/head There was a failure building this commit Details
ogcore-debian-package/pipeline/head This commit looks good Details

Reviewed-on: #104
pull/105/head
Manuel Aranda Rosales 2026-05-05 18:22:18 +02:00
commit 2860161d93
3 changed files with 84 additions and 5 deletions

View File

@ -128,6 +128,14 @@ resources:
uriTemplate: /clients/{uuid}/delete-lease
controller: App\Controller\OgDhcp\Client\DeleteClientLeaseAction
get_disk_logs:
class: ApiPlatform\Metadata\Get
method: GET
input: false
output: false
uriTemplate: /clients/{uuid}/disk-logs
controller: App\Controller\OgAgent\GetDiskLogsAction
properties:
App\Entity\Client:

View File

@ -10,17 +10,17 @@ when@dev:
level: info
path: "%kernel.logs_dir%/%kernel.environment%.log"
formatter: App\Formatter\CustomLineFormatter
channels: ["!event"]
channels: ["!event", "!deprecation"]
console:
type: console
process_psr_3_messages: false
channels: ["!event", "!doctrine", "!console"]
channels: ["!event", "!doctrine", "!console", "!deprecation"]
syslog:
type: syslog
ident: "ogcore"
level: info
formatter: App\Formatter\CustomLineFormatter
channels: ["!event"]
channels: ["!event", "!deprecation"]
when@test:
monolog:
@ -43,11 +43,11 @@ when@prod:
path: "%kernel.logs_dir%/%kernel.environment%.log"
level: error
formatter: App\Formatter\CustomLineFormatter
channels: ["!event"]
channels: ["!event", "!deprecation"]
syslog:
type: syslog
ident: "ogcore"
level: info
formatter: App\Formatter\CustomLineFormatter
channels: ["!event"]
channels: ["!event", "!deprecation"]

View File

@ -0,0 +1,71 @@
<?php
namespace App\Controller\OgAgent;
use App\Entity\Client;
use Psr\Log\LoggerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Attribute\AsController;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
#[AsController]
class GetDiskLogsAction extends AbstractController
{
private const LOG_DIR = '/var/log/ogClients/';
public function __construct(
private readonly LoggerInterface $logger,
) {
}
public function __invoke(Client $client, Request $request): JsonResponse
{
if (!$client->getIp()) {
throw new BadRequestHttpException('Client has no IP address assigned.');
}
$ip = $client->getIp();
$logPath = self::LOG_DIR . $ip . '/clieng.log';
if (!file_exists($logPath)) {
throw new NotFoundHttpException(sprintf('No disk log file found for client %s.', $ip));
}
$lines = $request->query->getInt('lines', 0);
$content = $this->readLogFile($logPath, $lines);
$this->logger->info('Disk logs read for client', ['client' => $client->getId(), 'ip' => $ip]);
return new JsonResponse([
'ip' => $ip,
'path' => $logPath,
'content' => $content,
], Response::HTTP_OK);
}
private function readLogFile(string $path, int $lines): string
{
if ($lines <= 0) {
return file_get_contents($path);
}
$file = new \SplFileObject($path, 'r');
$file->seek(PHP_INT_MAX);
$total = $file->key();
$start = max(0, $total - $lines);
$result = [];
$file->seek($start);
while (!$file->eof()) {
$result[] = $file->current();
$file->next();
}
return implode('', $result);
}
}