[checkmk-commits] mrpe: decypher cryptic lines selection

Moritz Kiemer mo at mathias-kettner.de
Thu Apr 18 09:50:22 CEST 2019


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

Author: Moritz Kiemer <mo at mathias-kettner.de>
Date:   Wed Apr 17 20:54:47 2019 +0200

mrpe: decypher cryptic lines selection

When will the result be used?
     best state
     0  1  3  2
 0   -  X  X  X
 1   -  -  X  X
 3   -  -  -  X
 2   -  -  -  X

Meaning: If we have an OK one, take the first.
         Else if we have an WARN one, take the first.
         Else if we have a UNKNOWN one, take the first.
         Else if we have a CRIT one, take the last one (!),

We instead now take the first CRIT one, to simplify.

Change-Id: Ia1d14dae1bd18243ac593f110c9fc4b326cd740c

---

 checks/mrpe | 72 +++++++++++++++++++++++++++----------------------------------
 1 file changed, 32 insertions(+), 40 deletions(-)

diff --git a/checks/mrpe b/checks/mrpe
index f72584e..2187904 100644
--- a/checks/mrpe
+++ b/checks/mrpe
@@ -80,47 +80,39 @@ def mrpe_parse_perfdata(perfinfo):
 def check_mrpe(_no_item, _no_params, data):
     # This check is cluster-aware. An item might be found
     # more than once. In that case we use the best of the
-    # multiple statuses.
-
-    best_state = None
-
-    for dataset in data:
-
-        # First line:  OUTPUT|PERFDATA
-        parts = dataset.info[0].split("|", 1)
-        output = [parts[0].strip()]
-        if len(parts) > 1:
-            perfdata = parts[1].strip().split()
+    # multiple statuses (Where OK < WARN < UNKNOWN < CRIT).
+    dataset = min(data, key=lambda x: (0, 1, 3, 2)[x.state])
+
+    # First line:  OUTPUT|PERFDATA
+    parts = dataset.info[0].split("|", 1)
+    output = [parts[0].strip()]
+    if len(parts) > 1:
+        perfdata = parts[1].strip().split()
+    else:
+        perfdata = []
+
+    # Further lines
+    now_comes_perfdata = False
+    for line in dataset.info[1:]:
+        if now_comes_perfdata:
+            perfdata += line.split()
         else:
-            perfdata = []
-
-        # Further lines
-        now_comes_perfdata = False
-        for line in dataset.info[1:]:
-            if now_comes_perfdata:
-                perfdata += line.split()
-            else:
-                parts = line.split("|", 1)
-                output.append(parts[0].strip())
-                if len(parts) > 1:
-                    perfdata += parts[1].strip().split()
-                    now_comes_perfdata = True
-
-        if best_state in [None, 2] \
-           or (dataset.state < best_state and dataset.state != 2):
-            perf_parsed = []
-            for perfvalue in perfdata:
-                new_perf = mrpe_parse_perfdata(perfvalue)
-                if new_perf:
-                    perf_parsed.append(new_perf)
-
-            # name of check command needed for PNP to choose the correct template
-            if dataset.name:
-                perf_parsed.append(dataset.name)
-            best_result = dataset.state, "\n".join(output), perf_parsed
-            best_state = dataset.state
-
-    return best_result
+            parts = line.split("|", 1)
+            output.append(parts[0].strip())
+            if len(parts) > 1:
+                perfdata += parts[1].strip().split()
+                now_comes_perfdata = True
+
+    perf_parsed = []
+    for perfvalue in perfdata:
+        new_perf = mrpe_parse_perfdata(perfvalue)
+        if new_perf:
+            perf_parsed.append(new_perf)
+
+    # name of check command needed for PNP to choose the correct template
+    if dataset.name:
+        perf_parsed.append(dataset.name)
+    return dataset.state, "\n".join(output), perf_parsed
 
 
 check_info["mrpe"] = {



More information about the checkmk-commits mailing list