feat : add DockerService for container status and stats
This commit is contained in:
100
src/Service/DockerService.php
Normal file
100
src/Service/DockerService.php
Normal file
@@ -0,0 +1,100 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\Service;
|
||||||
|
|
||||||
|
use Symfony\Component\Process\Process;
|
||||||
|
|
||||||
|
final class DockerService
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @return array{status: string, image: string, version: string, startedAt: string}
|
||||||
|
*/
|
||||||
|
public function getContainerStatus(string $containerName): array
|
||||||
|
{
|
||||||
|
$process = new Process([
|
||||||
|
'docker', 'inspect',
|
||||||
|
'--format', '{{.State.Status}}||{{.Config.Image}}||{{.State.StartedAt}}',
|
||||||
|
$containerName,
|
||||||
|
]);
|
||||||
|
$process->setTimeout(10);
|
||||||
|
$process->run();
|
||||||
|
|
||||||
|
if (!$process->isSuccessful()) {
|
||||||
|
return [
|
||||||
|
'status' => 'not_found',
|
||||||
|
'image' => '',
|
||||||
|
'version' => '',
|
||||||
|
'startedAt' => '',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
$parts = explode('||', trim($process->getOutput()));
|
||||||
|
|
||||||
|
if (\count($parts) < 3) {
|
||||||
|
return [
|
||||||
|
'status' => 'not_found',
|
||||||
|
'image' => '',
|
||||||
|
'version' => '',
|
||||||
|
'startedAt' => '',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
$image = $parts[1];
|
||||||
|
$version = 'latest';
|
||||||
|
if (str_contains($image, ':')) {
|
||||||
|
$version = substr($image, strrpos($image, ':') + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
return [
|
||||||
|
'status' => $parts[0],
|
||||||
|
'image' => $image,
|
||||||
|
'version' => $version,
|
||||||
|
'startedAt' => $parts[2],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array{cpuPercent: float, memoryUsage: string, memoryLimit: string, memoryPercent: float}
|
||||||
|
*/
|
||||||
|
public function getContainerStats(string $containerName): array
|
||||||
|
{
|
||||||
|
$process = new Process([
|
||||||
|
'docker', 'stats', '--no-stream',
|
||||||
|
'--format', '{{.CPUPerc}}||{{.MemUsage}}||{{.MemPerc}}',
|
||||||
|
$containerName,
|
||||||
|
]);
|
||||||
|
$process->setTimeout(10);
|
||||||
|
$process->run();
|
||||||
|
|
||||||
|
if (!$process->isSuccessful()) {
|
||||||
|
return [
|
||||||
|
'cpuPercent' => 0.0,
|
||||||
|
'memoryUsage' => '',
|
||||||
|
'memoryLimit' => '',
|
||||||
|
'memoryPercent' => 0.0,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
$parts = explode('||', trim($process->getOutput()));
|
||||||
|
|
||||||
|
if (\count($parts) < 3) {
|
||||||
|
return [
|
||||||
|
'cpuPercent' => 0.0,
|
||||||
|
'memoryUsage' => '',
|
||||||
|
'memoryLimit' => '',
|
||||||
|
'memoryPercent' => 0.0,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
$memParts = explode(' / ', $parts[1]);
|
||||||
|
|
||||||
|
return [
|
||||||
|
'cpuPercent' => (float) rtrim($parts[0], '%'),
|
||||||
|
'memoryUsage' => $memParts[0] ?? '',
|
||||||
|
'memoryLimit' => $memParts[1] ?? '',
|
||||||
|
'memoryPercent' => (float) rtrim($parts[2], '%'),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user