Skip to content
Permalink
Browse files

8911 FIX Fix CRIT criterion for k8s_pod_container

Cleanly terminated containers would erroneously mark the
check CRIT.  Now, the check is only CRIT when a container
exited with a non-zero error code, otherwise it remains OK.
The check now reports more information: the number of
running containers, the number of waiting containers, and
the number of terminated containers.

This fixes CMK-2683.

Change-Id: I8b76b5e71f1d5ca2f4539407a64735bde298d789
  • Loading branch information...
Synss committed Sep 9, 2019
1 parent a03cfc0 commit a323a3a97e94092eeb3b261d124572d3cd6a954d
@@ -0,0 +1,15 @@
Title: Fix CRIT criterion for k8s_pod_container
Level: 1
Component: checks
Compatible: incomp
Edition: cre
Version: 1.7.0i1
Date: 1568041019
Class: fix

Cleanly terminated containers would erroneously mark the
check CRIT. Now, the check is only CRIT when a container
exited with a non-zero error code, otherwise it remains OK.
The check now reports more information: the number of
running containers, the number of waiting containers, and
the number of terminated containers.
@@ -4,9 +4,13 @@ catalog: containerization/kubernetes
license: GPL
distribution: check_mk
description:
This check monitors the total number and the number of
ready containers of a pod. A container is ready if it
passes its readiness probe.
This check monitors the number of containers of a pod.
If containers terminated with a non-zero exit code, then
the check is {CRIT}, otherwise is is OK.

The check returns the number of ready over total
containers, and the number of running, waiting, and
terminated containers.

To account for the initial setup of the container or the
switch of a Pod from one to another the ruleset
@@ -30,15 +30,29 @@ def inventory_k8s_pod_container(parsed):


def check_k8s_pod_container(_no_item, params, parsed):
container = len(parsed)
total = len(parsed)
ready = sum(container['ready'] for container in parsed.itervalues())

state = 2 if ready != container else 0
containers_state = collections.defaultdict(int)
states = [0]
for container in parsed.itervalues():
containers_state[container["state"]] += 1
states.append({
"running": 0,
"waiting": 1,
"terminated": 0 if int(container["state_exit_code"]) == 0 else 2,
}[container["state"]])
perfdata = [
('docker_all_containers', container, None, None, 0, container),
('ready_containers', ready, None, None, 0, container),
('docker_all_containers', total, None, None, 0, total),
('ready_containers', ready, None, None, 0, total),
]
yield state, "Ready: %d/%d" % (ready, container), perfdata
infotext = ", ".join((
"Ready: %d/%d" % (ready, total),
"Running: %d" % containers_state["running"],
"Waiting: %d" % containers_state["waiting"],
"Terminated: %d" % containers_state["terminated"],
))

yield max(states), infotext, perfdata


check_info['k8s_pod_container'] = {
@@ -427,12 +427,26 @@ def containers(self):
'image_pull_policy': container.image_pull_policy,
'ready': False,
'restart_count': 0,
'state': None,
'state_reason': None,
'state_exit_code': None,
'container_id': None,
'image_id': None,
} for container in self._containers
}
for container_status in self._container_statuses:
data = view[container_status.name]
state = container_status.state
if state:
if state.running:
data["state"] = "running"
elif state.terminated:
data["state"] = "terminated"
data["state_exit_code"] = state.terminated.exit_code
data["state_reason"] = state.terminated.reason
elif state.waiting:
data["state"] = "waiting"
data["state_reason"] = state.waiting.reason
data['ready'] = container_status.ready
data['restart_count'] = container_status.restart_count
data['container_id'] = (container_status.container_id.replace('docker://', '')
@@ -1,16 +1,34 @@
# yapf: disable


checkname = 'k8s_pod_container'


info = [[u'{"nginx": {"image_pull_policy": "IfNotPresent", "image": "nginx:1.7.9", "container_id": "87cc3642854cc8810e24ca49b4c9ce5a03aee5076284d0945524c229289740a2", "restart_count": 1, "image_id": "docker-pullable://nginx@sha256:e3456c851a152494c3e4ff5fcc26f240206abac0c9d794affb40e0714846c451", "ready": true}}']]


discovery = {'': [(None, {})]}


checks = {'': [(None,
{},
[(0, 'Ready: 1/1', [('docker_all_containers', 1, None, None, 0, 1),
('ready_containers', 1, None, None, 0, 1)])])]}
checkname = "k8s_pod_container"

parsed = {
u"pi": {
u"image_pull_policy": u"Always",
u"state_reason": u"Completed",
u"image": u"perl",
u"container_id": u"94d698838e88b72fdaf7b48dd7c227f5d36915c3279af6b1da33d397cef0c276",
u"restart_count": 0,
u"image_id": u"docker-pullable://perl@sha256:5cada8a3709c245b0256a4d986801e598abf95576eb01767bde94d567e23104e",
u"state": u"terminated",
u"ready": False,
u"state_exit_code": 0,
}
}

discovery = {"": [(None, {})]}

checks = {
"": [(
None,
{},
[(
0,
"Ready: 0/1, Running: 0, Waiting: 0, Terminated: 1",
[
("docker_all_containers", 1, None, None, 0, 1),
("ready_containers", 0, None, None, 0, 1),
],
)],
)]
}

0 comments on commit a323a3a

Please sign in to comment.
You can’t perform that action at this time.