Module: check_mk
Branch: master
Commit: 110cd6b42743bc5470cb32f8fa2baf5ab6c40367
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=110cd6b42743bc…
Author: Lars Michelsen <lm(a)mathias-kettner.de>
Date: Tue Nov 25 10:32:32 2014 +0100
#1532 FIX mk_sap: Cleaning up old state information from sap.state file
The mk_sap agent plugin writes a state file which contains information
for each SAP alert log file which is monitored. Old log files are now
cleaned up from this file, when they are not monitored anymore.
---
.werks/1532 | 11 +++++++++++
ChangeLog | 1 +
agents/plugins/mk_sap | 46 +++++++++++++++++++++++++++++++++++++---------
3 files changed, 49 insertions(+), 9 deletions(-)
diff --git a/.werks/1532 b/.werks/1532
new file mode 100644
index 0000000..bf6feb4
--- /dev/null
+++ b/.werks/1532
@@ -0,0 +1,11 @@
+Title: mk_sap: Cleaning up old state information from sap.state file
+Level: 1
+Component: checks
+Compatible: compat
+Version: 1.2.5i7
+Date: 1416907854
+Class: fix
+
+The mk_sap agent plugin writes a state file which contains information
+for each SAP alert log file which is monitored. Old log files are now
+cleaned up from this file, when they are not monitored anymore.
diff --git a/ChangeLog b/ChangeLog
index a5092a2..bc3f9df 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -38,6 +38,7 @@
* 1503 FIX: Fixed monitoring of multiple SAP instances with one mk_sap plugin...
* 1515 FIX: cisco_secure: fix service description, fix OK state in case of no
violation
* 1449 FIX: nginx_status: agent plugin no longer honours "http(s)_proxy"
env variables of root user
+ * 1532 FIX: mk_sap: Cleaning up old state information from sap.state file...
Multisite:
* 1508 Allow input of plugin output and perfdata when faking check results...
diff --git a/agents/plugins/mk_sap b/agents/plugins/mk_sap
index 8f867df..747f9ae 100755
--- a/agents/plugins/mk_sap
+++ b/agents/plugins/mk_sap
@@ -109,6 +109,7 @@ SKIP_MTCLASSES = [
]
STATE_FILE = '/etc/check_mk/sap.state'
+state_file_changed = False
# #############################################################################
@@ -139,7 +140,6 @@ monitor_paths = [
# 'SAP CCMS Monitor Templates/Dialog Overview/*',
]
monitor_types = []
-# henri
config_file = os.getenv('MK_CONFDIR', '/etc/check_mk') +
'/sap.cfg'
cfg = {}
@@ -156,6 +156,10 @@ try:
except IOError:
states = {}
+# index of all logfiles which have been found in a run. This is used to
+# remove logfiles which are not available anymore from the states dict.
+logfiles = []
+
# #############################################################################
#
@@ -322,6 +326,8 @@ def alert_details(cfg, alert):
return state, msg
def process_alerts(cfg, logs, ms_name, mon_name, node, alerts):
+ global state_file_changed
+
sid = node["MTSYSID"].strip() or 'Other'
context = node["MTMCNAME"].strip() or 'Other'
path = node["PATH"]
@@ -330,6 +336,8 @@ def process_alerts(cfg, logs, ms_name, mon_name, node, alerts):
hostname = sid
logfile = context + "/" + path
+ logfiles.append((hostname, logfile))
+
logs.setdefault(sid, {})
logs[hostname][logfile] = []
newest_log_dt = None
@@ -350,7 +358,7 @@ def process_alerts(cfg, logs, ms_name, mon_name, node, alerts):
if newest_log_dt:
# Write newest log age to cache to prevent double processing of logs
states[(hostname, logfile)] = newest_log_dt
- file(STATE_FILE, 'w').write(repr(states))
+ state_file_changed = True
return logs
@@ -452,12 +460,32 @@ def check(cfg):
# It is possible to configure multiple SAP instances to monitor. Loop them all, but
# do not terminate when one connection failed
-for entry in cfg:
- try:
- check(entry)
- except sapnwrfc.RFCCommunicationError, e:
- sys.stderr.write('ERROR: Unable to connect (%s)\n' % e)
- except:
- sys.stderr.write('ERROR: Unhandled exception (%s)\n' % e)
+processed_all = True
+try:
+ for entry in cfg:
+ try:
+ check(entry)
+ except sapnwrfc.RFCCommunicationError, e:
+ sys.stderr.write('ERROR: Unable to connect (%s)\n' % e)
+ processed_all = False
+ except Exception, e:
+ sys.stderr.write('ERROR: Unhandled exception (%s)\n' % e)
+ processed_all = False
+
+ # Now check whether or not an old logfile needs to be removed. This can only
+ # be done this way, when all hosts have been reached. Otherwise the cleanup
+ # is skipped.
+ if processed_all:
+ for key in states.keys():
+ if key not in logfiles:
+ state_file_changed = True
+ del states[key]
+
+ # Only write the state file once per run. And only when it has been changed
+ if state_file_changed:
+ file(STATE_FILE, 'w').write(repr(states))
+
+except Exception, e:
+ sys.stderr.write('ERROR: Unhandled exception (%s)\n' % e)
sys.exit(0)