Module: check_mk
Branch: master
Commit: 07555860a9a787f46fdbf54984b14fec6d9c0d58
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=07555860a9a787…
Author: Moritz Kiemer <mo(a)mathias-kettner.de>
Date: Mon Feb 18 09:30:51 2019 +0100
7146 mk_docker.py: Optionally use container names as identifier
You can now configure the plugin mk_docker.py to use the containers
name or long ID as host name. You can do this either using the
agent bakery rule "Docker node and containers", or via configuration
file.
CMK-1707
Change-Id: If1285df52db33d4003dbf9c48052dc43bf915d63
---
.werks/7146 | 13 +++++++++++++
agents/cfg_examples/docker.cfg | 10 ++++++++++
agents/plugins/mk_docker.py | 36 ++++++++++++++++--------------------
tests/unit/plugins/test_mk_docker.py | 2 +-
4 files changed, 40 insertions(+), 21 deletions(-)
diff --git a/.werks/7146 b/.werks/7146
new file mode 100644
index 0000000..843569d
--- /dev/null
+++ b/.werks/7146
@@ -0,0 +1,13 @@
+Title: mk_docker.py: Optionally use container names as identifier
+Level: 1
+Component: checks
+Compatible: compat
+Edition: cre
+Version: 1.6.0i1
+Date: 1550501351
+Class: feature
+
+You can now configure the plugin mk_docker.py to use the containers
+name or long ID as host name. You can do this either using the
+agent bakery rule "Docker node and containers", or via configuration
+file.
diff --git a/agents/cfg_examples/docker.cfg b/agents/cfg_examples/docker.cfg
index a874c94..bf12ab0 100644
--- a/agents/cfg_examples/docker.cfg
+++ b/agents/cfg_examples/docker.cfg
@@ -27,6 +27,16 @@ skip_sections: docker_node_disk_usage,docker_node_images
# * docker_container_agent: retrieve information by running the
# check_mk_agent inside the container
+# CONTAINER ID
+# You can choose what to use as the container identifier. This will
+# affect the name used for the piggyback host corresponding to the
+# container, as well as items for services created on the node for each
+# container.
+# By default, the identifier is assumed to be the first 12 characters
+# of the container UUID. You can choose to use the full ID or the containers
+# name instead. Allowed values are "short" (the default), "long" and
"name".
+container_id: name
+
# BASE URL
# By default we are trying to connect to the docker API engine
# via the unix socket:
diff --git a/agents/plugins/mk_docker.py b/agents/plugins/mk_docker.py
index 04a3fc2..c8661ec 100755
--- a/agents/plugins/mk_docker.py
+++ b/agents/plugins/mk_docker.py
@@ -73,6 +73,7 @@ DEFAULT_CFG_SECTION = {
"base_url": "unix://var/run/docker.sock",
"api_version": "auto",
"skip_sections": "",
+ "container_id": "short",
}
LOGGER = logging.getLogger(__name__)
@@ -159,9 +160,15 @@ def report_exception_to_server(exc):
class MKDockerClient(docker.DockerClient):
'''a docker.DockerClient that caches containers and node
info'''
- def __init__(self, *args, **kwargs):
- super(MKDockerClient, self).__init__(*args, **kwargs)
- self.all_containers = self.containers.list(all=True)
+ def __init__(self, config):
+ super(MKDockerClient, self).__init__(config['base_url'],
version=config['api_version'])
+ all_containers = self.containers.list(all=True)
+ if config['container_id'] == "name":
+ self.all_containers = [(c.attrs["Name"].lstrip('/'), c) for
c in all_containers]
+ elif config['container_id'] == "long":
+ self.all_containers = [(c.attrs["Id"], c) for c in all_containers]
+ else:
+ self.all_containers = [(c.attrs["Id"][:12], c) for c in
all_containers]
self.node_info = self.info()
@@ -242,7 +249,7 @@ class AgentDispatcher(object):
return None
result = container.exec_run(['sh', '-c', 'bash -c echo'],
socket=True)
if not self.get_stdout(result):
- LOGGER.info("failed to run bash in container: %s",
container.short_id)
+ LOGGER.info("failed to run bash in container")
return None
result = container.exec_run(
@@ -279,15 +286,6 @@ def set_version_info(client):
Section.version_info['ApiVersion'] = data.get('ApiVersion')
-def short_id(container):
- '''return a shortened id
-
- We do not use container.short_id for compatibility reasons.
- Also we are dealing with trust issues.
- '''
- return container.attrs["Id"][:12]
-
-
#.
# .--Sections------------------------------------------------------------.
# | ____ _ _ |
@@ -381,7 +379,7 @@ def section_node_images(client, _config):
LOGGER.debug(client.all_containers)
section.append('[[[containers]]]')
- for container in client.all_containers:
+ for __, container in client.all_containers:
section.append(json.dumps(container.attrs))
section.write()
@@ -403,8 +401,7 @@ def section_container_client(client, _config):
node_name = client.node_info.get("Name")
# For the container status, we want information about *all* containers
- for container in client.all_containers:
- container_id = short_id(container)
+ for container_id, container in client.all_containers:
LOGGER.info("container (via client): %s", container_id)
section = Section('container_node_name', piggytarget=container_id)
@@ -422,13 +419,12 @@ def section_container_client(client, _config):
@skippable
def section_container_agent(client, _config):
- running_containers = [c for c in client.all_containers if c.status ==
"running"]
+ running_containers = [c for c in client.all_containers if c[1].status ==
"running"]
if not running_containers:
return
dispatcher = AgentDispatcher()
- for container in running_containers:
- container_id = short_id(container)
+ for container_id, container in running_containers:
LOGGER.info("container(via agent): %s", container_id)
result = dispatcher.check_container(container)
@@ -466,7 +462,7 @@ def main():
config = get_config(args.config_file)
try: # first calls by docker-daemon: report failure
- client = MKDockerClient(config['base_url'],
version=config['api_version'])
+ client = MKDockerClient(config)
except () if DEBUG else Exception as exc:
report_exception_to_server(exc)
sys.exit(1)
diff --git a/tests/unit/plugins/test_mk_docker.py b/tests/unit/plugins/test_mk_docker.py
index af740ad..fdb9cd6 100644
--- a/tests/unit/plugins/test_mk_docker.py
+++ b/tests/unit/plugins/test_mk_docker.py
@@ -11,7 +11,7 @@ sys.path.insert(0, os.path.join(cmk_path(), 'agents',
'plugins'))
import mk_docker # pylint: disable=import-error,wrong-import-position
PLUGIN_CHECKSUMS = {
- '0.1': '609daeed48fda1198701458e9bd4f205',
+ '0.1': 'aec6ebec8565d1326d1b0df52191d594',
}