From 0a8793e44b891f98bdf3ab4d4f07778290e227d2 Mon Sep 17 00:00:00 2001 From: Sven Velt Date: Wed, 20 Nov 2024 16:22:55 +0100 Subject: [PATCH] Catch cgroup errors, return no stats --- lxc_container_info.py | 36 ++++++++++++++++++++++++------------ 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/lxc_container_info.py b/lxc_container_info.py index 5d8f6c6..03098c7 100644 --- a/lxc_container_info.py +++ b/lxc_container_info.py @@ -150,18 +150,30 @@ def run_module(): res_stat = {} if ct.running: - res_stat['mem'] = { - 'usage': int(ct.get_cgroup_item("memory.usage_in_bytes")), - 'max_usage': int(ct.get_cgroup_item("memory.max_usage_in_bytes")), - 'usage_mb': int(ct.get_cgroup_item("memory.usage_in_bytes")) // 1048576, - 'max_usage_mb': int(ct.get_cgroup_item("memory.max_usage_in_bytes")) // 1048576, - } - res_stat['kmem'] = { - 'usage': int(ct.get_cgroup_item("memory.kmem.usage_in_bytes")), - 'max_usage': int(ct.get_cgroup_item("memory.kmem.max_usage_in_bytes")), - 'usage_mb': int(ct.get_cgroup_item("memory.kmem.usage_in_bytes")) // 1048576, - 'max_usage_mb': int(ct.get_cgroup_item("memory.kmem.max_usage_in_bytes")) // 1048576, - } + res_stat['mem'] = {} + for (k, v) in [ ('usage', 'memory.usage_in_bytes'), ('max_usage', 'memory.max_usage_in_bytes') ]: + try: + res_stat['mem'][k] = int(ct.get_cgroup_item(v)) + except KeyError: + pass + for (k, v) in [ ('usage_mb', 'memory.usage_in_bytes'), ('max_usage_mb', 'memory.max_usage_in_bytes') ]: + try: + res_stat['mem'][k] = int(ct.get_cgroup_item(v)) // 1048576 + except KeyError: + pass + + res_stat['kmem'] = {} + for (k, v) in [ ('usage', 'memory.kmem.usage_in_bytes'), ('max_usage', 'memory.kmem.max_usage_in_bytes') ]: + try: + res_stat['kmem'][k] = int(ct.get_cgroup_item(v)) + except KeyError: + pass + for (k, v) in [ ('usage_mb', 'memory.kmem.usage_in_bytes'), ('max_usage_mb', 'memory.kmem.max_usage_in_bytes') ]: + try: + res_stat['kmem'][k] = int(ct.get_cgroup_item(v)) // 1048576 + except KeyError: + pass + res_ct['stats'] = res_stat result['containers'][ctname] = res_ct