Module: check_mk
Branch: master
Commit: bc246c0d12bd01f81dc7dbc1974908c5b4bac2d7
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=bc246c0d12bd01…
Author: Moritz Kiemer <mo(a)mathias-kettner.de>
Date: Wed Oct 10 08:16:57 2018 +0200
docker: refactoring (move to docker.include)
Change-Id: I7cc2dda075e12ebd39e77ba947c43971027c5af9
---
checks/docker.include | 75 ++++++++++++++++++++++++++++++++++++++++++
inventory/docker_node_images | 78 ++------------------------------------------
2 files changed, 77 insertions(+), 76 deletions(-)
diff --git a/checks/docker.include b/checks/docker.include
index b0cdf66..6fe5fe6 100644
--- a/checks/docker.include
+++ b/checks/docker.include
@@ -97,3 +97,78 @@ def parse_docker_node_disk_usage(info):
return dict(map(_parse_docker_node_disk_usage_dict, data_dicts))
+
+def _split_subsections(info):
+ subname = ''
+ subsections = {}
+ for row in info:
+ if not row:
+ continue
+ if row[0].startswith('[[[') and row[0].endswith(']]]'):
+ subname = row[0].strip('[]')
+ continue
+ subsections.setdefault(subname, []).append(row)
+ return subsections
+
+
+def _parse_subsection_images(subinfo):
+ '''parse output of "docker images"'''
+ parsed = {}
+ for row in subinfo:
+ data = json.loads(" ".join(row))
+ parsed.setdefault(data["ID"], data)
+ return parsed
+
+
+def _parse_subsection_labels(subinfo):
+ return [json.loads(" ".join(row)) for row in subinfo]
+
+
+def _parse_subsection_containers(subinfo):
+ parsed = {}
+ for row in subinfo:
+ data = json.loads(" ".join(row))
+
+ image_name = data["Image"]
+ if ":" in image_name:
+ data["Repository"], data["Tag"] =
image_name.split(":", 1)
+ else:
+ data["Repository"], data["Tag"] = image_name,
"latest"
+ parsed[data["ID"]] = data
+ return parsed
+
+
+def parse_docker_node_images(info):
+ '''parse output of <<<docker_node_images>>>
+
+ $ echo "[[[images]]]"
+ $ docker images
+ $ echo "[[[image_labels]]]"
+ $ IMAGE_IDS=$(docker images ls | awk 'BEGIN {ORF=" "} NR>1 {print
$3}')
+ $ docker image inspect "$IMAGE_IDS"
+ $ echo "[[[containers]]]"
+ $ docker ps --all
+ '''
+ subsections = _split_subsections(info)
+
+ images = _parse_subsection_images(subsections.get("images", []))
+ label_data = _parse_subsection_labels(subsections.get("images_labels",
[]))
+ containers = _parse_subsection_containers(subsections.get("containers",
[]))
+
+ for image_id, labels in label_data:
+ image = images.get(image_id)
+ if None not in (image_id, labels, image):
+ image.setdefault("__labels__", {}).update(labels)
+
+ mapping = {(i['Repository'], i['Tag']): i['ID'] for i in
images}
+ for c in containers:
+ image_id = mapping.get((c["Repository"], c["Tag"]))
+ image = images.get(image_id)
+ if image is not None:
+ image.setdefault("__containers__", []).append(c)
+
+ lab = (p.split("=", 1) for p in c.get("Labels",
[]).split(","))
+ c["Labels"] = list(lab)
+
+ return {"images": images, "containers": containers}
+
diff --git a/inventory/docker_node_images b/inventory/docker_node_images
index d254884..ae8d6cc 100644
--- a/inventory/docker_node_images
+++ b/inventory/docker_node_images
@@ -23,82 +23,6 @@
# License along with GNU Make; see the file COPYING. If not, write
# to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
# Boston, MA 02110-1301 USA.
-import json
-
-
-def _split_subsections(info):
- subname = ''
- subsections = {}
- for row in info:
- if not row:
- continue
- if row[0].startswith('[[[') and row[0].endswith(']]]'):
- subname = row[0].strip('[]')
- continue
- subsections.setdefault(subname, []).append(row)
- return subsections
-
-
-def _parse_subsection_images(subinfo):
- '''parse output of "docker images"'''
- parsed = {}
- for row in subinfo:
- data = json.loads(" ".join(row))
- parsed.setdefault(data["ID"], data)
- return parsed
-
-
-def _parse_subsection_labels(subinfo):
- return [json.loads(" ".join(row)) for row in subinfo]
-
-
-def _parse_subsection_containers(subinfo):
- parsed = {}
- for row in subinfo:
- data = json.loads(" ".join(row))
-
- image_name = data["Image"]
- if ":" in image_name:
- data["Repository"], data["Tag"] =
image_name.split(":", 1)
- else:
- data["Repository"], data["Tag"] = image_name,
"latest"
- parsed[data["ID"]] = data
- return parsed
-
-
-def parse_docker_node_images(info):
- '''parse output of <<<docker_node_images>>>
-
- $ echo "[[[images]]]"
- $ docker images
- $ echo "[[[image_labels]]]"
- $ IMAGE_IDS=$(docker images ls | awk 'BEGIN {ORF=" "} NR>1 {print
$3}')
- $ docker image inspect "$IMAGE_IDS"
- $ echo "[[[containers]]]"
- $ docker ps --all
- '''
- subsections = _split_subsections(info)
-
- images = _parse_subsection_images(subsections.get("images", []))
- label_data = _parse_subsection_labels(subsections.get("images_labels",
[]))
- containers = _parse_subsection_containers(subsections.get("containers",
[]))
-
- for image_id, labels in label_data:
- image = images.get(image_id)
- if None not in (image_id, labels, image):
- image.setdefault("__labels__", {}).update(labels)
-
- mapping = {(i['Repository'], i['Tag']): i['ID'] for i in
images}
- for c in containers:
- image_id = mapping.get((c["Repository"], c["Tag"]))
- image = images.get(image_id)
- if image is not None:
- image.setdefault("__containers__", []).append(c)
-
- lab = (p.split("=", 1) for p in c.get("Labels",
[]).split(","))
- c["Labels"] = list(lab)
-
- return {"images": images, "containers": containers}
def inv_docker_node_images(info, inventory_tree, status_data_tree):
@@ -126,6 +50,7 @@ def inv_docker_node_images(info, inventory_tree, status_data_tree):
inv_info['docker_node_images'] = {
+ 'includes' : ['docker.include'],
'inv_function' : inv_docker_node_images,
'has_status_data' : True,
}
@@ -155,6 +80,7 @@ def _format_labels(labels):
# using the extra_sections feature. In case this is not possible for the inventory
# plugins at the moment we should make it possible.
inv_info['docker_node_images.containers'] = {
+ 'includes' : ['docker.include'],
'inv_function' : inv_docker_node_images_containers,
'has_status_data' : True,
}