Module: check_mk
Branch: master
Commit: f9ba5b21bac80bb254d8960073b5fa83fed695bb
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=f9ba5b21bac80b…
Author: Lars Michelsen <lm(a)mathias-kettner.de>
Date: Thu Nov 16 11:12:32 2017 +0100
Cleaned up single oid cache mechanism implementation a bit and improved verbose output
Change-Id: I2fb83e44ec10b07b59b294b44e2cfd62e6e212f5
---
cmk_base/snmp.py | 45 +++++++++++++++++++++++++++++----------------
1 file changed, 29 insertions(+), 16 deletions(-)
diff --git a/cmk_base/snmp.py b/cmk_base/snmp.py
index 3eaa9a3..8ec8f57 100644
--- a/cmk_base/snmp.py
+++ b/cmk_base/snmp.py
@@ -57,7 +57,7 @@ _enforce_stored_walks = False
# TODO: Replace this by generic caching
_g_single_oid_hostname = None
-_g_single_oid_cache = {}
+_g_single_oid_cache = None
_g_walk_cache = {}
_g_snmp_cache_info_tables = {} # Information about SNMP caching
@@ -74,12 +74,20 @@ _g_snmp_cache_info_tables = {} # Information about SNMP caching
#TODO CACHING
-def initialize_single_oid_cache(hostname):
- global _g_single_oid_cache
- _g_single_oid_cache = _get_single_oid_cache(hostname)
+def initialize_single_oid_cache(hostname, from_disk=False):
+ global _g_single_oid_cache, _g_single_oid_hostname
+ if _g_single_oid_hostname != hostname or _g_single_oid_cache is None:
+ _g_single_oid_hostname = hostname
+ if from_disk:
+ _g_single_oid_cache = _load_single_oid_cache(hostname)
+ else:
+ _g_single_oid_cache = {}
def write_single_oid_cache(hostname):
+ if not _g_single_oid_cache:
+ return
+
cache_dir = cmk.paths.snmp_scan_cache_dir
if not os.path.exists(cache_dir):
os.makedirs(cache_dir)
@@ -88,16 +96,20 @@ def write_single_oid_cache(hostname):
def set_single_oid_cache(hostname, oid, value):
- _clear_other_hosts_oid_cache(hostname)
_g_single_oid_cache[oid] = value
-def _get_single_oid_cache(hostname):
+def _is_in_single_oid_cache(hostname, oid):
+ return oid in _g_single_oid_cache
+
+
+def _get_oid_from_single_oid_cache(hostname, oid):
+ return _g_single_oid_cache.get(oid)
+
+
+def _load_single_oid_cache(hostname):
cache_path = "%s/%s" % (cmk.paths.snmp_scan_cache_dir, hostname)
- cached_single_oids = store.load_data_from_file(cache_path)
- if cached_single_oids is None:
- return {}
- return cached_single_oids
+ return store.load_data_from_file(cache_path, {})
def cleanup_host_caches():
@@ -109,9 +121,9 @@ def cleanup_host_caches():
def _clear_other_hosts_oid_cache(hostname):
- global _g_single_oid_hostname
+ global _g_single_oid_hostname, _g_single_oid_cache
if _g_single_oid_hostname != hostname:
- _g_single_oid_cache.clear()
+ _g_single_oid_cache = None
_g_single_oid_hostname = hostname
@@ -274,11 +286,12 @@ def get_single_oid(hostname, ipaddress, oid, check_type=None,
do_snmp_scan=True)
else:
oid = '.' + oid
- _clear_other_hosts_oid_cache(hostname)
-
# TODO: Use generic cache mechanism
- if not do_snmp_scan or oid in _g_single_oid_cache:
- return _g_single_oid_cache.get(oid)
+ if _is_in_single_oid_cache(hostname, oid):
+ console.vverbose(" Using cached OID %s: " % oid)
+ value = _get_oid_from_single_oid_cache(hostname, oid)
+ console.vverbose("%s%s%s%s\n" % (tty.bold, tty.green, value,
tty.normal))
+ return value
console.vverbose(" Getting OID %s: " % oid)
if _enforce_stored_walks or config.is_usewalk_host(hostname):