Module: check_mk
Branch: master
Commit: de7fba5fadb09434a735594c804c164fe717cfed
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=de7fba5fadb094…
Author: Mathias Kettner <mk(a)mathias-kettner.de>
Date: Thu Mar 20 10:39:56 2014 +0100
INV: Fix exception on empty paths, add RH to OS discovery
---
inventory/lnx_distro | 24 ++++++++++++++++++++++--
modules/inventory.py | 26 ++++++++++++++++++++++++++
web/plugins/views/inventory.py | 26 +++++++++++++++-----------
3 files changed, 63 insertions(+), 13 deletions(-)
diff --git a/inventory/lnx_distro b/inventory/lnx_distro
index a73d904..9d522ff 100644
--- a/inventory/lnx_distro
+++ b/inventory/lnx_distro
@@ -24,11 +24,17 @@
# to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
# Boston, MA 02110-1301 USA.
-# Example output (Ubuntu 12.10)
+# Example outputs from various systems:
# <<<lnx_distro:sep(124)>>>
# /etc/debian_version|wheezy/sid
+
+# <<<lnx_distro:sep(124)>>>
#
/etc/lsb-release|DISTRIB_ID=Ubuntu|DISTRIB_RELEASE=12.10|DISTRIB_CODENAME=quantal|DISTRIB_DESCRIPTION="Ubuntu
12.10"
+# <<<lnx_distro:sep(124)>>>
+# /etc/redhat-release|Red Hat Enterprise Linux Server release 6.5 (Santiago)
+
+
def inv_lnx_distro(info):
node = inv_tree("software.os.")
@@ -37,10 +43,24 @@ def inv_lnx_distro(info):
inv_lnx_parse_lsb_release(node, line[1:])
elif line[0] == '/etc/debian_version':
inv_lnx_parse_debian(node, line[1])
+ elif line[0] == '/etc/redhat-release':
+ inv_lnx_parse_redhat_release(node, line[1])
+
+def inv_lnx_parse_redhat_release(node, line):
+ node["type"] = "linux"
+ parts = line.split("(")
+ left = parts[0].strip()
+ node["code_name"] = parts[1].rstrip(")")
+ name, _release, version = left.rsplit(None, 2)
+ if name.startswith("Red Hat"):
+ node["vendor"] = "Red Hat"
+ node["version"] = version
+ node["name"] = left
+
def inv_lnx_parse_lsb_release(node, lines):
+ node["type"] = "linux"
for line in lines:
- node["type"] = "linux"
varname, value = line.split("=", 1)
value = value.strip("'").strip('"')
if varname == "DISTRIB_ID":
diff --git a/modules/inventory.py b/modules/inventory.py
index 1fbb780..ee83580 100644
--- a/modules/inventory.py
+++ b/modules/inventory.py
@@ -116,6 +116,27 @@ def inv_tree(path):
return node
+# Removes empty nodes from a (sub)-tree. Returns
+# True if the tree itself is empty
+def inv_cleanup_tree(tree):
+
+ if type(tree) == dict:
+ for key, value in tree.items():
+ if inv_cleanup_tree(value):
+ del tree[key]
+ return not tree
+
+ elif type(tree) == list:
+ to_delete = []
+ for nr, entry in enumerate(tree):
+ if inv_cleanup_tree(entry):
+ to_delete.append(nr)
+ for nr in to_delete[::-1]:
+ del tree[nr]
+ return not tree
+
+ else:
+ return False # cannot clean non-container nodes
#.
# .--Inventory-----------------------------------------------------------.
@@ -217,6 +238,9 @@ def do_inv_for(hostname, ipaddress):
plugin["inv_function"](info)
+ # Remove empty paths
+ inv_cleanup_tree(g_inv_tree)
+
if inventory_pprint_output:
r = pprint.pformat(g_inv_tree)
else:
@@ -247,3 +271,5 @@ def run_inv_export_hooks(hostname, tree):
raise
raise MKGeneralException("Failed to execute export hook %s: %s"
% (
hookname, e))
+
+
diff --git a/web/plugins/views/inventory.py b/web/plugins/views/inventory.py
index 50531e8..d94c989 100644
--- a/web/plugins/views/inventory.py
+++ b/web/plugins/views/inventory.py
@@ -28,6 +28,9 @@ import inventory
def paint_host_inventory(row, invpath):
invdata = inventory.get(row["host_inventory"], invpath)
+ if not invdata:
+ return "", _("No inventory data available")
+
hint = inv_display_hint(invpath)
if "paint_function" in hint:
return hint["paint_function"](invdata)
@@ -300,17 +303,18 @@ def render_inv_subtree(hostname, invpath, node):
render_inv_subtree_leaf(hostname, invpath, node)
def render_inv_subtree_foldable(hostname, invpath, node):
- icon, title = inv_titleinfo(invpath, node)
-
- if "%d" in title: # Replace with list index
- list_index = int(invpath.split(":")[-1].rstrip(".")) + 1
- title = title % list_index
-
- fetch_url = html.makeuri_contextless([("host", hostname),
("path", invpath)], "ajax_inv_render_tree.py")
- if html.begin_foldable_container("inv_" + hostname, invpath, False, title,
icon=icon, fetch_url=fetch_url):
- # Render only if it is open. We'll get the stuff via ajax later if it's
closed
- render_inv_subtree_container(hostname, invpath, node)
- html.end_foldable_container()
+ if node: # omit empty nodes completely
+ icon, title = inv_titleinfo(invpath, node)
+
+ if "%d" in title: # Replace with list index
+ list_index = int(invpath.split(":")[-1].rstrip(".")) + 1
+ title = title % list_index
+
+ fetch_url = html.makeuri_contextless([("host", hostname),
("path", invpath)], "ajax_inv_render_tree.py")
+ if html.begin_foldable_container("inv_" + hostname, invpath, False,
title, icon=icon, fetch_url=fetch_url):
+ # Render only if it is open. We'll get the stuff via ajax later if
it's closed
+ render_inv_subtree_container(hostname, invpath, node)
+ html.end_foldable_container()
def render_inv_subtree_container(hostname, invpath, node):
hint = inv_display_hint(invpath)