[checkmk-commits] mk_mongodb: refactor reading of state file

Moritz Kiemer mo at mathias-kettner.de
Fri Apr 5 10:20:29 CEST 2019


Module: check_mk
Branch: master
Commit: 659b58ca1b5733e4c885ac186154e37e25312759
URL:    http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=659b58ca1b5733e4c885ac186154e37e25312759

Author: Moritz Kiemer <mo at mathias-kettner.de>
Date:   Fri Apr  5 08:06:34 2019 +0200

mk_mongodb: refactor reading of state file

Change-Id: Ifd1d69c0537a51d596e3fdafbf0cd89a0d9374d3

---

 agents/plugins/mk_mongodb | 97 +++++++++++++++++++++++++----------------------
 1 file changed, 51 insertions(+), 46 deletions(-)

diff --git a/agents/plugins/mk_mongodb b/agents/plugins/mk_mongodb
index 6f04ac4..c4dfdeb 100755
--- a/agents/plugins/mk_mongodb
+++ b/agents/plugins/mk_mongodb
@@ -125,57 +125,62 @@ def section_by_keys(section_name, keys, server_status, output_key=False):
             sys.stdout.write(fmt % item)
 
 
+def get_timestamp(text):
+    """parse timestamps like 'Nov  6 13:44:09' or '2015-10-17T05:35:24'"""
+    for pattern in ["%a %b %d %H:%M:%S", "%Y-%m-%dT%H:%M:%S"]:
+        try:
+            return time.mktime(time.strptime(text, pattern))
+        except ValueError:
+            pass
+
+
+def read_statefile(state_file):
+    last_timestamp = None
+    output_all = False
+    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
+    return last_timestamp, output_all
+
+
 def section_logwatch(client):
-    # TODO: this produces an empty section if MK_VARDIR is not set. Intended?
+    if not MK_VARDIR:
+        return
+
     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)
+    state_file = "%s/mongodb.state" % MK_VARDIR
+
+    last_timestamp, output_all = read_statefile(state_file)
+
+    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():



More information about the checkmk-commits mailing list