From 4894486c634b638e3ec938aecf0edde8cd2ae51e Mon Sep 17 00:00:00 2001 From: Renato Covarrubias Date: Sat, 6 Jan 2018 14:21:59 -0300 Subject: [PATCH] Fix KeyError when online_cpus don't exists --- docker_check.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/docker_check.py b/docker_check.py index 73dc500..fe5b702 100755 --- a/docker_check.py +++ b/docker_check.py @@ -47,7 +47,12 @@ def get_cpu_pct(stats): stats['precpu_stats']['cpu_usage']['total_usage'] system_delta = stats['cpu_stats']['system_cpu_usage'] - \ stats['precpu_stats']['system_cpu_usage'] - online_cpus = stats['cpu_stats']['online_cpus'] + try: + online_cpus = stats['cpu_stats']['online_cpus'] + except KeyError: + online_cpus = len([item + for item in stats['cpu_stats']['percpu_usage'] + if item > 0]) if cpu_delta > 0 and system_delta > 0: return (cpu_delta / system_delta) * online_cpus * 100 return 0.0