Skip to content

Commit 233910b

Browse files
committed
Fix cpu and memory flags in Podman
1 parent ca526ba commit 233910b

2 files changed

Lines changed: 8 additions & 1 deletion

File tree

check_docker/check_docker.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,10 @@ def check_memory(container, thresholds):
551551

552552
inspection = get_stats(container)
553553

554-
# Subtracting cache to match what `docker stats` does.
554+
# Workaround for Podman, since it doesn't provide the stats field
555+
if 'stats' not in inspection['memory_stats'].keys(): inspection['memory_stats']['stats'] = {'total_cache': 0}
556+
557+
# # Subtracting cache to match what `docker stats` does.
555558
adjusted_usage = inspection['memory_stats']['usage']
556559
if 'total_cache' in inspection['memory_stats']['stats']:
557560
# CGroups v1 - https://www.kernel.org/doc/Documentation/cgroup-v1/memory.txt
@@ -715,6 +718,9 @@ def calculate_cpu_capacity_precentage(info, stats):
715718
else:
716719
available_limit_ratio = (period * num_cpus) / quota
717720

721+
# Workaround for Podman, since it doesn't provide the field
722+
if 'system_cpu_usage' not in stats['precpu_stats'].keys(): stats['precpu_stats']['system_cpu_usage'] = 0
723+
718724
cpu_delta = stats['cpu_stats']['cpu_usage']['total_usage'] - stats['precpu_stats']['cpu_usage']['total_usage']
719725
system_delta = stats['cpu_stats']['system_cpu_usage'] - stats['precpu_stats']['system_cpu_usage']
720726
usage = (cpu_delta / system_delta) * available_limit_ratio

tests/test_check_docker.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,7 @@ def mock_response(*args, **kwargs):
257257
({'limit': 10, 'usage': 1, 'stats': {'total_cache': 1}}, 20, 30, '%', cd.OK_RC),
258258
({'limit': 10, 'usage': 3, 'stats': {'total_cache': 1}}, 20, 30, '%', cd.WARNING_RC),
259259
({'limit': 10, 'usage': 4, 'stats': {'total_cache': 1}}, 20, 30, '%', cd.CRITICAL_RC),
260+
({'limit': 10, 'usage': 4}, 20, 30, '%', cd.CRITICAL_RC),
260261
({'limit': 10, 'usage': 4, 'stats': {'total_cache': 1}}, 20, 30, 'BAD_UNITS', cd.UNKNOWN_RC),
261262
))
262263
def test_check_memory(check_docker_with_units, memory_stats, warn, crit, units, expected_status):

0 commit comments

Comments
 (0)