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