Refactoring: Changed remaining check parameters starting with an 's' to the new rules...
[check_mk.git] / checks / docker_container_mem
blob9541f55bc566fea92d4a7cea729c9ecaea7c8bde
1 #!/usr/bin/python
2 # -*- encoding: utf-8; py-indent-offset: 4 -*-
3 # +------------------------------------------------------------------+
4 # | ____ _ _ __ __ _ __ |
5 # | / ___| |__ ___ ___| | __ | \/ | |/ / |
6 # | | | | '_ \ / _ \/ __| |/ / | |\/| | ' / |
7 # | | |___| | | | __/ (__| < | | | | . \ |
8 # | \____|_| |_|\___|\___|_|\_\___|_| |_|_|\_\ |
9 # | |
10 # | Copyright Mathias Kettner 2018 mk@mathias-kettner.de |
11 # +------------------------------------------------------------------+
13 # This file is part of Check_MK.
14 # The official homepage is at http://mathias-kettner.de/check_mk.
16 # check_mk is free software; you can redistribute it and/or modify it
17 # under the terms of the GNU General Public License as published by
18 # the Free Software Foundation in version 2. check_mk is distributed
19 # in the hope that it will be useful, but WITHOUT ANY WARRANTY; with-
20 # out even the implied warranty of MERCHANTABILITY or FITNESS FOR A
21 # PARTICULAR PURPOSE. See the GNU General Public License for more de-
22 # tails. You should have received a copy of the GNU General Public
23 # License along with GNU Make; see the file COPYING. If not, write
24 # to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
25 # Boston, MA 02110-1301 USA.
28 def parse_docker_container_mem(info):
29 # parsed contains memory usages in bytes
30 parsed = {}
31 for line in info:
32 if line[0] == "MemTotal:" and line[2] == "kB":
33 parsed["MemTotal"] = int(line[1]) * 1024
34 else:
35 parsed[line[0]] = int(line[1])
37 # Populate a dictionary in the format check_memory() form mem.include expects.
38 # The values are scaled to kB
39 mem = {
40 "SwapTotal": 0,
41 "SwapFree": 0,
44 # Calculate used memory like docker does (https://github.com/moby/moby/issues/10824)
45 usage_kb = (parsed["usage_in_bytes"] - parsed["cache"]) / 1024.0
47 # Extract the real memory limit for the container. There is either the
48 # maximum amount of memory available or a configured limit for the
49 # container (cgroup).
50 mem["MemTotal"] = min(parsed["MemTotal"], parsed["limit_in_bytes"]) / 1024.0
51 mem["MemFree"] = mem["MemTotal"] - usage_kb
52 mem["Caches"] = parsed["cache"] / 1024.0
54 return mem
57 def check_docker_container_mem(_no_item, params, parsed):
58 return check_memory(params, parsed)
61 check_info["docker_container_mem"] = {
62 "parse_function": parse_docker_container_mem,
63 "inventory_function": discover_single,
64 "check_function": check_docker_container_mem,
65 "service_description": "Memory used",
66 "has_perfdata": True,
67 "group": "memory",
68 "default_levels_variable": "memory_default_levels",
69 "includes": ["mem.include"],