Module: check_mk
Branch: master
Commit: cea45be58b4c92f902baf85256574b50f6a92d54
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=cea45be58b4c92…
Author: Moritz Kiemer <mo(a)mathias-kettner.de>
Date: Thu Apr 18 10:50:23 2019 +0200
local: make testable and add test
Change-Id: I6064a90f6abb0c3f3c4a11a54e65ada6016761d1
---
checks/local | 15 ++---
checks/mrpe | 17 +----
checks/parse_nagios.include | 42 ++++++++++++
tests/unit/checks/generictests/datasets/local_1.py | 76 ++++++++++++++++++++++
4 files changed, 125 insertions(+), 25 deletions(-)
diff --git a/checks/local b/checks/local
index 7a64587..ad1317f 100644
--- a/checks/local
+++ b/checks/local
@@ -123,16 +123,10 @@ def _parse_local_line(line):
compute_data = []
if perftxt != "-":
for entry in perftxt.split('|'):
- try:
- varname, valuetxt = entry.split('=')
- values = valuetxt.split(';')
- compute_data.append(tuple([varname] + values))
- # perfdata must not contain values with colons. So we split
- # these values and use the upper levels only.
- upper_values = [v.split(':')[-1] for v in values]
- perfdata.append(tuple([varname] + upper_values))
-
- except ValueError:
+ new_perf = parse_nagios_perfstring(entry)
+ if new_perf:
+ perfdata.append(new_perf)
+ else:
return 3, "Invalid performance data %s in local check output
%s" % \
(perftxt, " ".join(line)), []
@@ -208,4 +202,5 @@ check_info["local"] = {
'has_perfdata': True,
'node_info': True,
'group': 'local',
+ 'includes': ['parse_nagios.include']
}
diff --git a/checks/mrpe b/checks/mrpe
index 2187904..66738ae 100644
--- a/checks/mrpe
+++ b/checks/mrpe
@@ -62,20 +62,6 @@ def parse_mrpe(info):
return parsed
-def mrpe_parse_perfdata(perfinfo):
- if '=' not in perfinfo:
- return None
- varname, valuetxt = perfinfo.split("=", 1)
- values = valuetxt.split(";")
- try:
- return tuple([varname] + [int(v) for v in values])
- except ValueError:
- try:
- return tuple([varname] + [float(v) for v in values])
- except ValueError:
- return tuple([varname] + values)
-
-
@get_parsed_item_data
def check_mrpe(_no_item, _no_params, data):
# This check is cluster-aware. An item might be found
@@ -105,7 +91,7 @@ def check_mrpe(_no_item, _no_params, data):
perf_parsed = []
for perfvalue in perfdata:
- new_perf = mrpe_parse_perfdata(perfvalue)
+ new_perf = parse_nagios_perfstring(perfvalue)
if new_perf:
perf_parsed.append(new_perf)
@@ -121,4 +107,5 @@ check_info["mrpe"] = {
'check_function': check_mrpe,
'service_description': '%s',
'has_perfdata': True,
+ 'includes': ['parse_nagios.include'],
}
diff --git a/checks/parse_nagios.include b/checks/parse_nagios.include
new file mode 100644
index 0000000..c943e09
--- /dev/null
+++ b/checks/parse_nagios.include
@@ -0,0 +1,42 @@
+#!/usr/bin/python
+# -*- encoding: utf-8; py-indent-offset: 4 -*-
+# +------------------------------------------------------------------+
+# | ____ _ _ __ __ _ __ |
+# | / ___| |__ ___ ___| | __ | \/ | |/ / |
+# | | | | '_ \ / _ \/ __| |/ / | |\/| | ' / |
+# | | |___| | | | __/ (__| < | | | | . \ |
+# | \____|_| |_|\___|\___|_|\_\___|_| |_|_|\_\ |
+# | |
+# | Copyright Mathias Kettner 2019 mk(a)mathias-kettner.de |
+# +------------------------------------------------------------------+
+#
+# This file is part of Check_MK.
+# The official homepage is at
http://mathias-kettner.de/check_mk.
+#
+# check_mk is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by
+# the Free Software Foundation in version 2. check_mk is distributed
+# in the hope that it will be useful, but WITHOUT ANY WARRANTY; with-
+# out even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+# PARTICULAR PURPOSE. See the GNU General Public License for more de-
+# tails. You should have received a copy of the GNU General Public
+# License along with GNU Make; see the file COPYING. If not, write
+# to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
+# Boston, MA 02110-1301 USA.
+
+
+def parse_nagios_perfstring(perfinfo):
+ if '=' not in perfinfo:
+ return None
+ varname, valuetxt = perfinfo.split("=", 1)
+ values = valuetxt.split(";")
+ # perfdata must not contain values with colons. So we split
+ # these values and use the upper levels only.
+ values = [v.split(':')[-1] for v in values]
+ try:
+ return tuple([varname] + [int(v) for v in values])
+ except ValueError:
+ try:
+ return tuple([varname] + [float(v) for v in values])
+ except ValueError:
+ return tuple([varname] + values)
diff --git a/tests/unit/checks/generictests/datasets/local_1.py
b/tests/unit/checks/generictests/datasets/local_1.py
new file mode 100644
index 0000000..ca8afd7
--- /dev/null
+++ b/tests/unit/checks/generictests/datasets/local_1.py
@@ -0,0 +1,76 @@
+# yapf: disable
+
+
+checkname = 'local'
+
+
+info = [
+ ['node_1', '0', 'Service_FOO', 'V=1', 'This',
'Check', 'is', 'OK'],
+ ['node_1', '1', 'Bar_Service', '-', 'This',
'is', 'WARNING', 'and', 'has', 'no',
+ 'performance', 'data'],
+ ['node_1', '2', 'NotGood', 'V=120;50;100;0;1000',
'A', 'critical', 'check'],
+ ['node_1', 'P', 'Some_other_Service',
'value1=10;30;50|value2=20;10:20;0:50;0;100',
+ 'Result', 'is', 'computed', 'from', 'two',
'values'],
+ ['node_1', 'P', 'This_is_OK', 'foo=18;20;50'],
+ ['node_1', 'P', 'Some_yet_other_Service',
'temp=40;30;50|humidity=28;50:100;0:50;0;100'],
+ ['node_2', 'P', 'Has-no-var', '-', 'This',
'has', 'no', 'variable'],
+ ['node_2', 'P', 'No-Text', 'hirn=-8;-20'],
+]
+
+
+discovery = {
+ '': [
+ ('Bar_Service', {}),
+ ('Has-no-var', {}),
+ ('No-Text', {}),
+ ('NotGood', {}),
+ ('Service_FOO', {}),
+ ('Some_other_Service', {}),
+ ('Some_yet_other_Service', {}),
+ ('This_is_OK', {}),
+ ],
+}
+
+
+checks = {
+ '': [
+ ('Bar_Service', {}, [
+ (1, 'On node node_1: This is WARNING and has no performance data',
[]),
+ ]),
+ ('Has-no-var', {}, [
+ (0, 'On node node_2: This has no variable', []),
+ ]),
+ ('No-Text', {}, [
+ (0, 'On node node_2: ', [
+ ('hirn', -8, -20, None, None, None),
+ ]),
+ ]),
+ ('NotGood', {}, [
+ (2, 'On node node_1: A critical check', [
+ ('V', 120, 50, 100, 0, 1000),
+ ]),
+ ]),
+ ('Service_FOO', {}, [
+ (0, 'On node node_1: This Check is OK', [
+ ('V', 1, None, None, None, None),
+ ]),
+ ]),
+ ('Some_other_Service', {}, [
+ (0, 'On node node_1: Result is computed from two values', [
+ ('value1', 10, 30, 50, None, None),
+ ('value2', 20, 20, 50, 0, 100),
+ ]),
+ ]),
+ ('Some_yet_other_Service', {}, [
+ (0, 'On node node_1: ', [
+ ('temp', 40, 30, 50, None, None),
+ ('humidity', 28, 100, 50, 0, 100),
+ ]),
+ ]),
+ ('This_is_OK', {}, [
+ (0, 'On node node_1: ', [
+ ('foo', 18, 20, 50, None, None),
+ ]),
+ ]),
+ ],
+}