Module: check_mk
Branch: master
Commit: f673a852737f23687738bd9e90fdadbcdac4eccd
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=f673a852737f23…
Author: Moritz Kiemer <mo(a)mathias-kettner.de>
Date: Thu Apr 4 14:15:03 2019 +0200
mk_mongodb: refactor even more
Change-Id: I6089fd9253c6f926f8c92b13585ff62d5c06c4d7
---
agents/plugins/mk_mongodb | 106 ++++++++++++++++++++++++----------------------
1 file changed, 56 insertions(+), 50 deletions(-)
diff --git a/agents/plugins/mk_mongodb b/agents/plugins/mk_mongodb
index f4c7fd1..6f04ac4 100755
--- a/agents/plugins/mk_mongodb
+++ b/agents/plugins/mk_mongodb
@@ -19,6 +19,8 @@ from contextlib import contextmanager
import pymongo # pylint: disable=import-error
+MK_VARDIR = os.environ.get("MK_VARDIR")
+
@contextmanager
def piggyback(info):
@@ -123,6 +125,59 @@ def section_by_keys(section_name, keys, server_status,
output_key=False):
sys.stdout.write(fmt % item)
+def section_logwatch(client):
+ # TODO: this produces an empty section if MK_VARDIR is not set. Intended?
+ sys.stdout.write("<<<logwatch>>>\n")
+ sys.stdout.write("[[[MongoDB startupWarnings]]]\n")
+ startup_warnings = client.admin.command({"getLog":
"startupWarnings"})
+
+ if MK_VARDIR:
+ state_file = "%s/mongodb.state" % MK_VARDIR
+ last_timestamp = None
+ output_all = False
+
+ # Supports: Nov 6 13:44:09
+ # 2015-10-17T05:35:24
+ def get_timestamp(text):
+ for pattern in ["%a %b %d %H:%M:%S",
"%Y-%m-%dT%H:%M:%S"]:
+ try:
+ result = time.mktime(time.strptime(text, pattern))
+ return result
+ except Exception: # pylint: disable=broad-except
+ continue
+
+ year_available = False
+ if os.path.exists(state_file):
+ last_timestamp = int(file(state_file).read())
+ if time.localtime(last_timestamp).tm_year >= 2015:
+ year_available = True
+
+ # Note: there is no year information in these loglines
+ # As workaround we look at the creation date (year) of the last statefile
+ # If it differs and there are new messages we start from the beginning
+ if not year_available:
+ statefile_year = time.localtime(os.stat(state_file).st_ctime).tm_year
+ if time.localtime().tm_year != statefile_year:
+ output_all = True
+
+ for line in startup_warnings["log"]:
+ state = "C"
+ state_index = line.find("]") + 2
+ if len(line) == state_index or line[state_index:].startswith("**
"):
+ state = "."
+
+ if "** WARNING:" in line:
+ state = "W"
+
+ if output_all or get_timestamp(line.split(".")[0]) >
last_timestamp:
+ sys.stdout.write("%s %s\n" % (state, line))
+
+ # update state file
+ if startup_warnings["log"]:
+ timestamp =
get_timestamp(startup_warnings["log"][-1].split(".")[0])
+ file(state_file, "w").write("%d" % timestamp)
+
+
def main():
# might be implemented in the future..
host = None
@@ -186,56 +241,7 @@ def potentially_piggybacked_sections(client, server_status):
for what, value in colldata.items():
sys.stdout.write("%s\t%s\t%s\t%s\n" % (dbname, collname, what,
value))
- sys.stdout.write("<<<logwatch>>>\n")
- sys.stdout.write("[[[MongoDB startupWarnings]]]\n")
- startup_warnings = client.admin.command({"getLog":
"startupWarnings"})
-
- var_dir = os.environ.get("MK_VARDIR")
- if var_dir:
- state_file = "%s/mongodb.state" % var_dir
- last_timestamp = None
- output_all = False
-
- # Supports: Nov 6 13:44:09
- # 2015-10-17T05:35:24
- def get_timestamp(text):
- for pattern in ["%a %b %d %H:%M:%S",
"%Y-%m-%dT%H:%M:%S"]:
- try:
- result = time.mktime(time.strptime(text, pattern))
- return result
- except Exception: # pylint: disable=broad-except
- continue
-
- year_available = False
- if os.path.exists(state_file):
- last_timestamp = int(file(state_file).read())
- if time.localtime(last_timestamp).tm_year >= 2015:
- year_available = True
-
- # Note: there is no year information in these loglines
- # As workaround we look at the creation date (year) of the last statefile
- # If it differs and there are new messages we start from the beginning
- if not year_available:
- statefile_year = time.localtime(os.stat(state_file).st_ctime).tm_year
- if time.localtime().tm_year != statefile_year:
- output_all = True
-
- for line in startup_warnings["log"]:
- state = "C"
- state_index = line.find("]") + 2
- if len(line) == state_index or line[state_index:].startswith("**
"):
- state = "."
-
- if "** WARNING:" in line:
- state = "W"
-
- if output_all or get_timestamp(line.split(".")[0]) >
last_timestamp:
- sys.stdout.write("%s %s\n" % (state, line))
-
- # update state file
- if startup_warnings["log"]:
- timestamp =
get_timestamp(startup_warnings["log"][-1].split(".")[0])
- file(state_file, "w").write("%d" % timestamp)
+ section_logwatch(client)
if __name__ == "__main__":