From d417306ac583c81cc9cb097685050d8d078bd097 Mon Sep 17 00:00:00 2001 From: Sergey Babkevych Date: Mon, 24 Nov 2025 09:40:44 +0200 Subject: [PATCH] Update check_docker.py The fix: Changed line 523-524 from: pythonadjusted_usage = inspection['memory_stats']['usage'] - inspection['memory_stats']['stats']['total_cache'] To: pythoncache = inspection['memory_stats']['stats'].get('total_cache', 0) adjusted_usage = inspection['memory_stats']['usage'] - cach --- Root Cause: Some Docker versions don't include the total_cache field in memory_stats['stats']. The script attempts to access this field directly without checking if it exists first. Affected Docker/System: AlmaLinux 9 / Python 3.12 Docker versions that don't provide total_cache in memory stats Original Code (Lines 523-524): python# Subtracting cache to match what `docker stats` does. adjusted_usage = inspection['memory_stats']['usage'] - inspection['memory_stats']['stats']['total_cache'] Fixed Code (Lines 523-525): python# Subtracting cache to match what `docker stats` does. # Handle missing total_cache gracefully (some Docker versions don't provide it) cache = inspection['memory_stats']['stats'].get('total_cache', 0) adjusted_usage = inspection['memory_stats']['usage'] - cache Change Summary: Used .get('total_cache', 0) instead of direct dictionary access Returns 0 if total_cache doesn't exist (no cache to subtract) Maintains backward compatibility with Docker versions that do provide the field --- check_docker/check_docker.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/check_docker/check_docker.py b/check_docker/check_docker.py index 1a9d754..69767ef 100755 --- a/check_docker/check_docker.py +++ b/check_docker/check_docker.py @@ -520,7 +520,10 @@ def check_memory(container, thresholds): inspection = get_stats(container) # Subtracting cache to match what `docker stats` does. - adjusted_usage = inspection['memory_stats']['usage'] - inspection['memory_stats']['stats']['total_cache'] + # Handle missing total_cache gracefully (some Docker versions don't provide it) + cache = inspection['memory_stats']['stats'].get('total_cache', 0) + adjusted_usage = inspection['memory_stats']['usage'] - cache + if thresholds.units == '%': max = 100 usage = int(100 * adjusted_usage / inspection['memory_stats']['limit'])