Set version to 1.5.0b9
[check_mk.git] / inventory / docker_node_images
blobfcb99b75a467236cde843def59f4027f8e34a86f
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_node_images(info):
29 import json
30 parsed = {}
31 section_name = None
32 for line in info:
33 if line[0].startswith("[[[") and line[0].endswith("]]]"):
34 section_name = line[0][3:-3]
35 continue
37 data = json.loads(" ".join(line))
39 if section_name == "images" and data:
40 parsed.setdefault(data["ID"], data)
42 elif section_name == "images_labels":
43 if len(data) == 2:
44 image_id, labels = data
45 image = parsed.get(image_id)
46 if image is not None and labels:
47 image.setdefault("__labels__", labels)
49 elif section_name == "containers":
50 image_name = data["Image"]
51 if ":" in image_name:
52 repository, tag = image_name.split(":", 1)
53 else:
54 repository, tag = image_name, "latest"
56 image = parsed.get((repository, tag))
57 if image is not None:
58 image.setdefault("__containers__", []).append(data)
59 return parsed
62 def inv_docker_node_images(info, inventory_tree, status_data_tree):
63 parsed = _parse_docker_node_images(info)
64 path = "software.applications.docker.images:"
65 inv_node = inventory_tree.get_list(path)
66 status_node = status_data_tree.get_list(path)
68 for image_id, image in sorted(parsed.iteritems()):
69 inv_node.append({
70 "repository" : image["Repository"],
71 "tag" : image["Tag"],
72 "id" : image_id,
73 "creation" : image["CreatedAt"],
74 "size" : image["VirtualSize"],
75 "labels" : _format_labels(image.get("__labels__", {}).items()),
78 status_node.append({
79 "repository" : image["Repository"],
80 "tag" : image["Tag"],
81 "id" : image_id,
82 "amount_containers" : len(image.get("__containers__", [])),
86 inv_info['docker_node_images'] = {
87 'inv_function' : inv_docker_node_images,
88 'has_status_data' : True,
92 def _parse_docker_node_containers(info):
93 import json
94 parsed = {}
95 section_name = None
96 for line in info:
97 if line[0].startswith("[[[") and line[0].endswith("]]]"):
98 section_name = line[0][3:-3]
99 continue
101 if section_name != "containers":
102 continue
104 data = json.loads(" ".join(line))
106 image_name = data["Image"]
107 if ":" in image_name:
108 data["Repository"], data["Tag"] = image_name.split(":", 1)
109 else:
110 data["Repository"], data["Tag"] = image_name, "latest"
112 data["Labels"] = [ p.split("=", 1) for p in data["Labels"].split(",") ]
114 parsed[data["ID"]] = data
117 return parsed
120 def inv_docker_node_images_containers(info, inventory_tree, status_data_tree):
121 parsed = _parse_docker_node_containers(info)
122 status_node = status_data_tree.get_list("software.applications.docker.containers:")
124 for id_, container in sorted(parsed.iteritems()):
125 status_node.append({
126 "id" : id_,
127 "repository" : container["Repository"],
128 "tag" : container["Tag"],
129 "name" : container["Names"],
130 "creation" : container["CreatedAt"],
131 "labels" : _format_labels(container["Labels"]),
132 "status" : container["Status"],
136 def _format_labels(labels):
137 return ", ".join([ ": ".join(p) for p in labels ])
139 # TODO: This section contains also information about the containers. We should
140 # better split it into multiple sections and add them to the different plugins
141 # using the extra_sections feature. In case this is not possible for the inventory
142 # plugins at the moment we should make it possible.
143 inv_info['docker_node_images.containers'] = {
144 'inv_function' : inv_docker_node_images_containers,
145 'has_status_data' : True,