diff --git a/config/api_platform/Client.yaml b/config/api_platform/Client.yaml index fd1dbe1..84bb7d5 100644 --- a/config/api_platform/Client.yaml +++ b/config/api_platform/Client.yaml @@ -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: diff --git a/config/packages/monolog.yaml b/config/packages/monolog.yaml index 10e6cc3..d631b97 100644 --- a/config/packages/monolog.yaml +++ b/config/packages/monolog.yaml @@ -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"] diff --git a/src/Controller/OgAgent/GetDiskLogsAction.php b/src/Controller/OgAgent/GetDiskLogsAction.php new file mode 100644 index 0000000..d25491b --- /dev/null +++ b/src/Controller/OgAgent/GetDiskLogsAction.php @@ -0,0 +1,71 @@ +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); + } +}