Module: check_mk
Branch: master
Commit: c96503d8a5e08fa4210ace688af66f368453937c
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=c96503d8a5e08f…
Author: Lars Michelsen <lm(a)mathias-kettner.de>
Date: Wed Nov 22 11:03:15 2017 +0100
Refactored SNMP walk cache file IO to use standard store methods
Change-Id: I2822cc4ffd74e4d05e65ad8f0895fa4c2b9c9465
---
cmk_base/automations/check_mk.py | 7 ++++---
cmk_base/snmp.py | 25 ++++++++++++++-----------
2 files changed, 18 insertions(+), 14 deletions(-)
diff --git a/cmk_base/automations/check_mk.py b/cmk_base/automations/check_mk.py
index be553ab..bccaa61 100644
--- a/cmk_base/automations/check_mk.py
+++ b/cmk_base/automations/check_mk.py
@@ -24,6 +24,7 @@
# to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
# Boston, MA 02110-1301 USA.
+import ast
import glob
import os
import subprocess
@@ -159,7 +160,7 @@ class AutomationSetAutochecks(DiscoveryAutomation):
# from python source code to actual values.
def execute(self, args):
hostname = args[0]
- new_items = eval(sys.stdin.read())
+ new_items = ast.literal_eval(sys.stdin.read())
discovery.set_autochecks_of(hostname, new_items)
self._trigger_discovery_check(hostname)
return None
@@ -196,7 +197,7 @@ class AutomationRenameHosts(Automation):
# Python pair-list from stdin:
# [("old1", "new1"), ("old2", "new2")])
def execute(self, args):
- renamings = eval(sys.stdin.read())
+ renamings = ast.literal_eval(sys.stdin.read())
actions = []
@@ -813,7 +814,7 @@ class AutomationGetConfiguration(Automation):
# We read the list of variable names from stdin since
# that could be too much for the command line
- variable_names = eval(sys.stdin.read())
+ variable_names = ast.literal_eval(sys.stdin.read())
missing_variables = [ v for v in variable_names
if not hasattr(config, v) ]
diff --git a/cmk_base/snmp.py b/cmk_base/snmp.py
index 3ea05ec..dcf10e6 100644
--- a/cmk_base/snmp.py
+++ b/cmk_base/snmp.py
@@ -558,26 +558,29 @@ def _is_snmpwalk_cachable(column):
def _get_cached_snmpwalk(hostname, fetchoid):
- path = "%s/snmp_cache/%s/%s" % (cmk.paths.var_dir, hostname, fetchoid)
+ path = _snmpwalk_cache_path(hostname, fetchoid)
try:
console.vverbose(" Loading %s from walk cache %s\n" % (fetchoid,
path))
- # TODO: Use store.load_data_from_file()
- return eval(file(path).read())
- except IOError:
- return None # don't print error when not cached yet
+ return store.load_data_from_file(path)
except:
if cmk.debug.enabled():
raise
- console.verbose("Failed to read cached SNMP walk from %s, ignoring.\n"
% path)
+ console.verbose(" Failed loading walk cache. Continue without it.\n" %
path)
return None
def _save_snmpwalk_cache(hostname, fetchoid, rowinfo):
- base_dir = "%s/snmp_cache/%s/" % (cmk.paths.var_dir, hostname)
- if not os.path.exists(base_dir):
- os.makedirs(base_dir)
- console.vverbose(" Caching walk of %s\n" % fetchoid)
- file(base_dir + fetchoid, "w").write("%r\n" % rowinfo)
+ path = _snmpwalk_cache_path(hostname, fetchoid)
+
+ if not os.path.exists(os.path.dirname(path)):
+ os.makedirs(os.path.dirname(path))
+
+ console.vverbose(" Saving walk of %s to walk cache %s\n" % (fetchoid,
path))
+ store.save_data_to_file(path, rowinfo, pretty=False)
+
+
+def _snmpwalk_cache_path(hostname, fetchoid):
+ return os.path.join(cmk.paths.var_dir, hostname, fetchoid)
def _get_stored_snmpwalk(hostname, oid):