Module: check_mk
Branch: master
Commit: b77e51cb8a52ccd0194e55017024f46882d49ebe
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=b77e51cb8a52cc…
Author: Lars Michelsen <lm(a)mathias-kettner.de>
Date: Thu Oct 20 11:44:35 2016 +0200
Started moving in-memory cache handling to dedicated module
---
cmk_base/__init__.py | 4 ++++
cmk_base/caching.py | 58 ++++++++++++++++++++++++++++++++++++++++++++++++
modules/check_mk.py | 10 +++++----
modules/check_mk_base.py | 17 +++++++-------
4 files changed, 76 insertions(+), 13 deletions(-)
diff --git a/cmk_base/__init__.py b/cmk_base/__init__.py
index 66c865c..a0fd6ad 100644
--- a/cmk_base/__init__.py
+++ b/cmk_base/__init__.py
@@ -27,3 +27,7 @@
"""This is the python module hierarchy used by Check_MK's core
components, former called modules. This hosts the checking,
discovery and a lot of other functionality."""
+
+import cmk_base.caching
+
+cache = cmk_base.caching.CacheManager()
diff --git a/cmk_base/caching.py b/cmk_base/caching.py
new file mode 100644
index 0000000..dea4964
--- /dev/null
+++ b/cmk_base/caching.py
@@ -0,0 +1,58 @@
+#!/usr/bin/env python
+# -*- encoding: utf-8; py-indent-offset: 4 -*-
+# +------------------------------------------------------------------+
+# | ____ _ _ __ __ _ __ |
+# | / ___| |__ ___ ___| | __ | \/ | |/ / |
+# | | | | '_ \ / _ \/ __| |/ / | |\/| | ' / |
+# | | |___| | | | __/ (__| < | | | | . \ |
+# | \____|_| |_|\___|\___|_|\_\___|_| |_|_|\_\ |
+# | |
+# | Copyright Mathias Kettner 2014 mk(a)mathias-kettner.de |
+# +------------------------------------------------------------------+
+#
+# This file is part of Check_MK.
+# The official homepage is at
http://mathias-kettner.de/check_mk.
+#
+# check_mk is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by
+# the Free Software Foundation in version 2. check_mk is distributed
+# in the hope that it will be useful, but WITHOUT ANY WARRANTY; with-
+# out even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+# PARTICULAR PURPOSE. See the GNU General Public License for more de-
+# tails. You should have received a copy of the GNU General Public
+# 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.
+
+"""Managing in-memory caches through the execution time of
cmk"""
+
+from cmk.exceptions import MKGeneralException
+
+
+class CacheManager(object):
+ def __init__(self):
+ self._caches = {}
+
+
+ def register(self, name):
+ if name in self._caches:
+ raise MKGeneralException("The cache \"%s\" is already
registered" % name)
+
+ self._caches[name] = Cache()
+ return self._caches[name]
+
+
+ def clear_all():
+ for cache in self._caches.values():
+ cache.clear()
+
+
+ def get(self, name):
+ return self._caches[name]
+
+
+
+# Just a small wrapper round a dict to get some caching specific functionality
+# for analysis etc.
+class Cache(dict):
+ pass
diff --git a/modules/check_mk.py b/modules/check_mk.py
index f03ac5d..3849925 100755
--- a/modules/check_mk.py
+++ b/modules/check_mk.py
@@ -58,6 +58,7 @@ import cmk.render as render
import cmk.man_pages as man_pages
import cmk_base.console as console
+import cmk_base.cache
# .--Prelude-------------------------------------------------------------.
# | ____ _ _ |
@@ -1951,11 +1952,12 @@ def get_check_table(hostname, remove_duplicates=False,
use_cache=True, world='co
skip_autochecks = True
# speed up multiple lookup of same host
- if not skip_autochecks and use_cache and hostname in g_check_table_cache:
+ check_table_cache = cmk_base.cache.get("check_tables")
+ if not skip_autochecks and use_cache and hostname in check_table_cache:
if remove_duplicates and is_dual_host(hostname):
- return remove_duplicate_checks(g_check_table_cache[hostname])
+ return remove_duplicate_checks(check_table_cache[hostname])
else:
- return g_check_table_cache[hostname]
+ return check_table_cache[hostname]
check_table = {}
@@ -2078,7 +2080,7 @@ def get_check_table(hostname, remove_duplicates=False,
use_cache=True, world='co
deps.append(d)
if not skip_autochecks and use_cache:
- g_check_table_cache[hostname] = check_table
+ check_table_cache[hostname] = check_table
if remove_duplicates:
return remove_duplicate_checks(check_table)
diff --git a/modules/check_mk_base.py b/modules/check_mk_base.py
index 3b81110..983faad 100644
--- a/modules/check_mk_base.py
+++ b/modules/check_mk_base.py
@@ -64,6 +64,7 @@ import cmk_base.agent_simulator
import cmk_base.utils
import cmk_base.prediction
import cmk_base.console as console
+import cmk_base.cache
# PLANNED CLEANUP:
# - central functions for outputting verbose information and bailing
@@ -99,8 +100,7 @@ def reset_global_caches():
global g_ip_lookup_cache
g_ip_lookup_cache = None # permanently cached ipaddresses from ipaddresses.cache
- for cachevar_name in g_global_caches:
- globals()[cachevar_name] = {}
+ cmk_base.cache.clear_all()
# global variables used to cache temporary values that do not need
@@ -125,10 +125,8 @@ g_global_caches = []
# global variables used to cache temporary values that need to
# be reset after a configuration change.
-g_check_table_cache = {} # per-host-checktables
-g_global_caches.append('g_check_table_cache')
-g_nodesof_cache = {} # Nodes of cluster hosts
-g_global_caches.append('g_nodesof_cache')
+cmk_base.cache.register("check_tables") # Check table of a host
+cmk_base.cache.register("nodes_of") # Nodes of cluster hosts
reset_global_caches()
@@ -1973,16 +1971,17 @@ def i_am_root():
# Returns the nodes of a cluster, or None if hostname is
# not a cluster
def nodes_of(hostname):
- nodes = g_nodesof_cache.get(hostname, False)
+ nodes_of_cache = cmk_base.cache.get("nodes_of")
+ nodes = nodes_of_cache.get(hostname, False)
if nodes != False:
return nodes
for tagged_hostname, nodes in clusters.items():
if hostname == tagged_hostname.split("|")[0]:
- g_nodesof_cache[hostname] = nodes
+ nodes_of_cache[hostname] = nodes
return nodes
- g_nodesof_cache[hostname] = None
+ nodes_of_cache[hostname] = None
return None
def check_uses_snmp(check_type):