diff --git a/src/Orchestration/Adapter/DockerAPI.php b/src/Orchestration/Adapter/DockerAPI.php index e3cac3f..69856a8 100644 --- a/src/Orchestration/Adapter/DockerAPI.php +++ b/src/Orchestration/Adapter/DockerAPI.php @@ -286,6 +286,7 @@ public function getStats(?string $container = null, array $filters = []): array $stats = \json_decode($result['response'], true); + // Calculate CPU usage $cpuDelta = $stats['cpu_stats']['cpu_usage']['total_usage'] - $stats['precpu_stats']['cpu_usage']['total_usage']; $systemCpuDelta = $stats['cpu_stats']['system_cpu_usage'] - $stats['precpu_stats']['system_cpu_usage']; $numberCpus = $stats['cpu_stats']['online_cpus']; @@ -295,6 +296,12 @@ public function getStats(?string $container = null, array $filters = []): array $cpuUsage = 0.0; } + // Calculate memory usage (unsafe div /0) + $memoryUsage = 0.0; + if ($stats['memory_stats']['limit'] > 0 && $stats['memory_stats']['usage'] > 0) { + $memoryUsage = ($stats['memory_stats']['usage'] / $stats['memory_stats']['limit']) * 100.0; + } + // Calculate network I/O $networkIn = 0; $networkOut = 0; @@ -324,7 +331,7 @@ public function getStats(?string $container = null, array $filters = []): array containerId: $stats['id'], containerName: \ltrim($stats['name'], '/'), // Remove '/' prefix cpuUsage: $cpuUsage, - memoryUsage: ($stats['memory_stats']['usage'] / $stats['memory_stats']['limit']) * 100.0, + memoryUsage: $memoryUsage, diskIO: ['in' => $diskRead, 'out' => $diskWrite], memoryIO: ['in' => $memoryIn, 'out' => $memoryOut], networkIO: ['in' => $networkIn, 'out' => $networkOut],