Cleanup config.nodes_of
[check_mk.git] / checks / docker_container_status
blob69649fa9431b8efa0f28b6ab93e840afffebd05b
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_status(info):
29 '''process the first line to a JSON object
31 In case there are multiple lines of output sent by the agent only process the first
32 line. We assume that this a full JSON object. The rest of the section is skipped.
33 When a container got piggyback data from multiple hosts (e.g. a cluster) this results
34 in multiple JSON objects handed over to this check.
35 '''
36 version = docker_get_version(info) # pylint: disable=undefined-variable
38 index = 0 if version is None else 1
39 parsed = docker_json_get_obj(info[index]) or {} if info[index:] else {} # pylint: disable=undefined-variable
41 if version is None:
42 return DeprecatedDict(parsed) # pylint: disable=undefined-variable
43 return parsed
47 # .--Health--------------------------------------------------------------.
48 # | _ _ _ _ _ |
49 # | | | | | ___ __ _| | |_| |__ |
50 # | | |_| |/ _ \/ _` | | __| '_ \ |
51 # | | _ | __/ (_| | | |_| | | | |
52 # | |_| |_|\___|\__,_|_|\__|_| |_| |
53 # | |
54 # +----------------------------------------------------------------------+
55 # | Represents the containers internal status, as implemented within |
56 # | the container itself using Docker's HEALTHCHECK API |
57 # '----------------------------------------------------------------------'
60 def inventory_docker_container_status_health(parsed):
61 if "Health" in parsed:
62 yield None, None
65 def check_docker_container_status_health(_no_item, _no_params, parsed):
66 health_status = parsed.get("Health", {}).get("Status")
68 if health_status == "healthy":
69 return 0, "Healthy"
70 elif health_status == "starting":
71 return 1, "Starting"
72 elif health_status == "unhealthy":
73 failing_streak = parsed.get("Health", {}).get("FailingStreak", "not found")
74 return 2, "Unhealthy - Failing Streak: %d" % failing_streak
75 return 3, "Health Status '%s' unknown" % health_status
78 check_info["docker_container_status.health"] = {
79 "inventory_function": inventory_docker_container_status_health,
80 "check_function": check_docker_container_status_health,
81 "service_description": "Docker container health",
82 'includes': ['docker.include', 'legacy_docker.include'],
86 # .--Status - Main Check-------------------------------------------------.
87 # | ____ _ _ |
88 # | / ___|| |_ __ _| |_ _ _ ___ |
89 # | \___ \| __/ _` | __| | | / __| |
90 # | ___) | || (_| | |_| |_| \__ \ |
91 # | |____/ \__\__,_|\__|\__,_|___/ |
92 # | |
93 # +----------------------------------------------------------------------+
94 # | Represents the status of the docker container "from the outside" |
95 # '----------------------------------------------------------------------'
98 @append_deprecation_warning # pylint: disable=undefined-variable
99 def check_docker_container_status(_no_item, _no_params, parsed):
100 status = parsed.get("Status", "unknown")
101 state = {"running": 0, "unknown": 3}.get(status, 2)
103 yield state, "Status: %s" % status
105 if parsed.get("Error"):
106 yield 2, "Error: %s" % parsed["Error"]
109 check_info['docker_container_status'] = {
110 'parse_function': parse_docker_container_status,
111 'inventory_function': discover_single,
112 'check_function': check_docker_container_status,
113 'service_description': 'Docker container status',
114 'includes': ['docker.include', 'legacy_docker.include'],