Module: check_mk
Branch: master
Commit: 82e4393e8e7191b16a786fcad675f86afacfbbbf
URL: http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=82e4393e8e7191…
Author: Mathias Kettner <mk(a)mathias-kettner.de>
Date: Fri Mar 11 11:03:36 2011 +0100
new agent_simulation mode
---
ChangeLog | 1 +
modules/agent_simulator.py | 29 +++++++++++++++++++++++++++++
modules/check_mk.py | 15 ++++++++++++---
modules/check_mk_base.py | 4 +++-
modules/snmp.py | 2 ++
5 files changed, 47 insertions(+), 4 deletions(-)
diff --git a/ChangeLog b/ChangeLog
index 49f4f1f..642f167 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -5,6 +5,7 @@
* New config option: Set check_submission = "file" in order to write
check result files instead of using Nagios command pipe (safes
CPU ressources)
+ * Agent simulation mode (for internal use and check development)
Multisite:
* Added snmp_uptime and uptime perfometers
diff --git a/modules/agent_simulator.py b/modules/agent_simulator.py
new file mode 100644
index 0000000..ad27303
--- /dev/null
+++ b/modules/agent_simulator.py
@@ -0,0 +1,29 @@
+#!/usr/bin/python
+
+def our_uptime():
+ return int(float((file("/proc/uptime").read().split()[0])))
+
+# replace simulator tags in output
+def agent_simulator_process(output):
+ try:
+ while True:
+ i = output.find('%{')
+ if i == -1:
+ break
+ e = output.find('}', i)
+ if e == -1:
+ break
+ simfunc = output[i+2 : e]
+ replacement = str(eval("agentsim_" + simfunc))
+ output = output[:i] + replacement + output[e+1:]
+ except:
+ pass
+
+ return output
+
+def agentsim_uptime(factor = 1.0):
+ return our_uptime() * factor
+
+def agentsim_enum(values, period = 1): # period is in seconds
+ hit = our_uptime() / period % len(values)
+ return values[hit]
diff --git a/modules/check_mk.py b/modules/check_mk.py
index 9bd86ee..46b1d50 100755
--- a/modules/check_mk.py
+++ b/modules/check_mk.py
@@ -207,6 +207,7 @@ agent_min_version = 0 # warn, if plugin has not at least versio
check_max_cachefile_age = 0 # per default do not use cache files when checking
cluster_max_cachefile_age = 90 # secs.
simulation_mode = False
+agent_simulator = False
perfdata_format = "pnp" # also possible: "standard"
debug_log = None
monitoring_host = "localhost" # your Nagios host
@@ -305,7 +306,8 @@ snmp_scan_functions = {} # SNMP autodetection
# Now include the other modules. They contain everything that is needed
# at check time (and many of that is also needed at administration time).
try:
- for module in [ 'check_mk_base', 'snmp' ]:
+ modules = [ 'check_mk_base', 'snmp' ]
+ for module in modules:
filename = modules_dir + "/" + module + ".py"
execfile(filename)
@@ -2014,7 +2016,7 @@ no_inventory_possible = None
'var_dir', 'counters_directory', 'tcp_cache_dir',
'snmpwalks_dir', 'check_mk_basedir', 'nagios_user',
'www_group', 'cluster_max_cachefile_age', 'check_max_cachefile_age',
- 'simulation_mode', 'aggregate_check_mk', 'debug_log',
+ 'simulation_mode', 'agent_simulator', 'aggregate_check_mk', 'debug_log',
]:
output.write("%s = %r\n" % (var, globals()[var]))
@@ -2039,6 +2041,9 @@ no_inventory_possible = None
if need_snmp_module:
output.write(stripped_python_file(modules_dir + "/snmp.py"))
+ if agent_simulator:
+ output.write(stripped_python_file(modules_dir + "/simulator.py"))
+
# check info table
# We need to include all those plugins that are referenced in the host's
# check table
@@ -3863,11 +3868,15 @@ if do_rrd_update:
try:
import rrdtool
except:
- sys.stdout.write("ERROR: Cannot do direct rrd updates since the Python module\n"+
+ sys.stderr.write("ERROR: Cannot do direct rrd updates since the Python module\n"+
"'rrdtool' could not be loaded. Please install python-rrdtool\n"+
"or set do_rrd_update to False in main.mk.\n")
sys.exit(3)
+# Load agent simulator if enabled
+if agent_simulator:
+ execfile(modules_dir + "/agent_simulator.py")
+
# read automatically generated checks. They are prepended to the check
# table: explicit user defined checks override automatically generated
# ones. Do not read in autochecks, if check_mk is called as module.
diff --git a/modules/check_mk_base.py b/modules/check_mk_base.py
index cbcab5b..0cdfa75 100755
--- a/modules/check_mk_base.py
+++ b/modules/check_mk_base.py
@@ -33,7 +33,6 @@ try:
except NameError:
from sets import Set as set
-
# colored output, if stdout is a tty
if sys.stdout.isatty():
tty_red = '\033[31m'
@@ -359,6 +358,9 @@ def get_realhost_info(hostname, ipaddress, checkname, max_cache_age):
elif len(output) < 16:
raise MKAgentError("Too short output from agent: '%s'" % output)
+ if agent_simulator:
+ output = agent_simulator_process(output)
+
lines = [ l.strip() for l in output.split('\n') ]
info = parse_info(lines)
store_cached_hostinfo(hostname, info)
diff --git a/modules/snmp.py b/modules/snmp.py
index f6bc49a..227f9f7 100644
--- a/modules/snmp.py
+++ b/modules/snmp.py
@@ -362,6 +362,8 @@ def get_stored_snmpwalk(hostname, oid):
if o == oid or o.startswith(oid_prefix + "."):
if len(parts) > 1:
value = parts[1]
+ if agent_simulator:
+ value = agent_simulator_process(value)
else:
value = ""
rowinfo.append((o, strip_snmp_value(value))) # return pair of OID and value