diff --git a/lib/vsc/__init__.py b/lib/vsc/__init__.py
index f1c17a1..409eb68 100644
--- a/lib/vsc/__init__.py
+++ b/lib/vsc/__init__.py
@@ -1,5 +1,5 @@
#
-# Copyright 2015-2021 Ghent University
+# Copyright 2015-2022 Ghent University
#
# This file is part of vsc-utils,
# originally created by the HPC team of Ghent University (http://ugent.be/hpc/en),
diff --git a/lib/vsc/utils/__init__.py b/lib/vsc/utils/__init__.py
index f1c17a1..409eb68 100644
--- a/lib/vsc/utils/__init__.py
+++ b/lib/vsc/utils/__init__.py
@@ -1,5 +1,5 @@
#
-# Copyright 2015-2021 Ghent University
+# Copyright 2015-2022 Ghent University
#
# This file is part of vsc-utils,
# originally created by the HPC team of Ghent University (http://ugent.be/hpc/en),
diff --git a/lib/vsc/utils/availability.py b/lib/vsc/utils/availability.py
index ab16205..80fa3cd 100644
--- a/lib/vsc/utils/availability.py
+++ b/lib/vsc/utils/availability.py
@@ -1,5 +1,5 @@
#
-# Copyright 2012-2021 Ghent University
+# Copyright 2012-2022 Ghent University
#
# This file is part of vsc-utils,
# originally created by the HPC team of Ghent University (http://ugent.be/hpc/en),
diff --git a/lib/vsc/utils/cache.py b/lib/vsc/utils/cache.py
index 782fbd4..df278bb 100644
--- a/lib/vsc/utils/cache.py
+++ b/lib/vsc/utils/cache.py
@@ -1,5 +1,5 @@
#
-# Copyright 2012-2021 Ghent University
+# Copyright 2012-2022 Ghent University
#
# This file is part of vsc-utils,
# originally created by the HPC team of Ghent University (http://ugent.be/hpc/en),
diff --git a/lib/vsc/utils/fs_store.py b/lib/vsc/utils/fs_store.py
index 209e793..552250b 100644
--- a/lib/vsc/utils/fs_store.py
+++ b/lib/vsc/utils/fs_store.py
@@ -1,5 +1,5 @@
#
-# Copyright 2012-2021 Ghent University
+# Copyright 2012-2022 Ghent University
#
# This file is part of vsc-utils,
# originally created by the HPC team of Ghent University (http://ugent.be/hpc/en),
diff --git a/lib/vsc/utils/lock.py b/lib/vsc/utils/lock.py
index a8a289f..d827f3f 100644
--- a/lib/vsc/utils/lock.py
+++ b/lib/vsc/utils/lock.py
@@ -1,5 +1,5 @@
#
-# Copyright 2012-2021 Ghent University
+# Copyright 2012-2022 Ghent University
#
# This file is part of vsc-utils,
# originally created by the HPC team of Ghent University (http://ugent.be/hpc/en),
diff --git a/lib/vsc/utils/nagios.py b/lib/vsc/utils/nagios.py
index 77bed77..56417c8 100644
--- a/lib/vsc/utils/nagios.py
+++ b/lib/vsc/utils/nagios.py
@@ -1,7 +1,7 @@
# -*- encoding: utf-8 -*-
#
-# Copyright 2012-2021 Ghent University
+# Copyright 2012-2022 Ghent University
#
# This file is part of vsc-utils,
# originally created by the HPC team of Ghent University (http://ugent.be/hpc/en),
@@ -277,7 +277,10 @@ def report_and_exit(self):
unknown_exit("%s nagios gzipped JSON file unavailable (%s)" % (self.header, self.filename))
(timestamp, ((nagios_exit_code, nagios_exit_string), nagios_message)) = nagios_cache.load('nagios')
+ self.print_report_and_exit(timestamp, nagios_exit_code, nagios_exit_string, nagios_message)
+ def print_report_and_exit(self, timestamp, nagios_exit_code, nagios_exit_string, nagios_message):
+ """Print the nagios report (if the data is not too old) and exit"""
if self.threshold <= 0 or time.time() - timestamp < self.threshold:
self.log.info("Nagios check cache file %s contents delivered: %s", self.filename, nagios_message)
print("%s %s" % (nagios_exit_string, nagios_message))
@@ -431,6 +434,8 @@ class SimpleNagios(NagiosResult):
USE_HEADER = True
RESERVED_WORDS = set(['message', 'ok', 'warning', 'critical', 'unknown',
'_exit', '_cache', '_cache_user', '_final', '_final_state', '_report', '_threshold'])
+ REPORTERCLASS = NagiosReporter
+ DEFAULT_CACHE_USER = 'nrpe'
def __init__(self, **kwargs):
"""Initialise message and perfdata"""
@@ -453,10 +458,11 @@ def __init__(self, **kwargs):
if self._cache:
# make a NagiosReporter instance that can be used for caching
if self._cache_user:
- cache = NagiosReporter('no header', self._cache, self._threshold, nagios_username=self._cache_user,
+ cache = self.REPORTERCLASS('no header', self._cache, self._threshold, nagios_username=self._cache_user,
world_readable=self._world_readable)
else:
- cache = NagiosReporter('no header', self._cache, self._threshold, world_readable=self._world_readable)
+ cache = self.REPORTERCLASS(
+ 'no header', self._cache, self._threshold, world_readable=self._world_readable)
if self._report_and_exit:
cache.report_and_exit()
else:
diff --git a/lib/vsc/utils/pickle_files.py b/lib/vsc/utils/pickle_files.py
index 10d6411..b500e4b 100644
--- a/lib/vsc/utils/pickle_files.py
+++ b/lib/vsc/utils/pickle_files.py
@@ -1,5 +1,5 @@
#
-# Copyright 2012-2021 Ghent University
+# Copyright 2012-2022 Ghent University
#
# This file is part of vsc-utils,
# originally created by the HPC team of Ghent University (http://ugent.be/hpc/en),
diff --git a/lib/vsc/utils/rest_oauth.py b/lib/vsc/utils/rest_oauth.py
index 209311a..65742d5 100644
--- a/lib/vsc/utils/rest_oauth.py
+++ b/lib/vsc/utils/rest_oauth.py
@@ -1,5 +1,5 @@
#
-# Copyright 2012-2021 Ghent University
+# Copyright 2012-2022 Ghent University
#
# This file is part of vsc-utils,
# originally created by the HPC team of Ghent University (http://ugent.be/hpc/en),
diff --git a/lib/vsc/utils/script_tools.py b/lib/vsc/utils/script_tools.py
index ee99d20..277604b 100644
--- a/lib/vsc/utils/script_tools.py
+++ b/lib/vsc/utils/script_tools.py
@@ -1,5 +1,5 @@
#
-# Copyright 2012-2021 Ghent University
+# Copyright 2012-2022 Ghent University
#
# This file is part of vsc-utils,
# originally created by the HPC team of Ghent University (http://ugent.be/hpc/en),
@@ -80,7 +80,7 @@ def _script_name(full_name):
os.path.join(NAGIOS_CACHE_DIR,
NAGIOS_CACHE_FILENAME_TEMPLATE % (_script_name(sys.argv[0]),))),
'nagios-check-interval-threshold': ('threshold of nagios checks timing out', 'int', 'store', 0),
- 'nagios-user': ('user nagios runs as', 'string', 'store', 'nrpe'),
+ 'nagios-user': ('user nagios runs as', 'string', 'store', None),
'nagios-world-readable-check': ('make the nagios check data file world readable', None, 'store_true', False),
}
@@ -114,6 +114,7 @@ class ExtendedSimpleOption(SimpleOption):
The prologue should be called at the start of the script; the epilogue at the end.
"""
+ MONITORCLASS = SimpleNagios
def __init__(self, options, run_prologue=True, excepthook=None, **kwargs):
"""Initialise.
@@ -127,6 +128,9 @@ def __init__(self, options, run_prologue=True, excepthook=None, **kwargs):
options_ = _merge_options(options)
super(ExtendedSimpleOption, self).__init__(options_, **kwargs)
+ if not self.options.nagios_user:
+ self.options.nagios_user = self.MONITORCLASS.DEFAULT_CACHE_USER
+
self.nagios_reporter = None
self.lockfile = None
@@ -151,12 +155,12 @@ def prologue(self):
"""
# bail if nagios report is requested
- self.nagios_reporter = SimpleNagios(_cache=self.options.nagios_check_filename,
- _report_and_exit=self.options.nagios_report,
- _threshold=self.options.nagios_check_interval_threshold,
- _cache_user=self.options.nagios_user,
- _world_readable=self.options.nagios_world_readable_check,
- )
+ self.nagios_reporter = self.MONITORCLASS(_cache=self.options.nagios_check_filename,
+ _report_and_exit=self.options.nagios_report,
+ _threshold=self.options.nagios_check_interval_threshold,
+ _cache_user=self.options.nagios_user,
+ _world_readable=self.options.nagios_world_readable_check,
+ )
# check for HA host
if self.options.ha and not proceed_on_ha_service(self.options.ha):
diff --git a/lib/vsc/utils/timestamp.py b/lib/vsc/utils/timestamp.py
index 25965e1..830293c 100644
--- a/lib/vsc/utils/timestamp.py
+++ b/lib/vsc/utils/timestamp.py
@@ -1,6 +1,6 @@
# -*- coding: latin-1 -*-
#
-# Copyright 2009-2021 Ghent University
+# Copyright 2009-2022 Ghent University
#
# This file is part of vsc-utils,
# originally created by the HPC team of Ghent University (http://ugent.be/hpc/en),
diff --git a/lib/vsc/utils/timestamp_pid_lockfile.py b/lib/vsc/utils/timestamp_pid_lockfile.py
index 53116ce..9c27a16 100644
--- a/lib/vsc/utils/timestamp_pid_lockfile.py
+++ b/lib/vsc/utils/timestamp_pid_lockfile.py
@@ -1,5 +1,5 @@
#
-# Copyright 2012-2021 Ghent University
+# Copyright 2012-2022 Ghent University
#
# This file is part of vsc-utils,
# originally created by the HPC team of Ghent University (http://ugent.be/hpc/en),
diff --git a/lib/vsc/utils/zabbix.py b/lib/vsc/utils/zabbix.py
new file mode 100644
index 0000000..53a698b
--- /dev/null
+++ b/lib/vsc/utils/zabbix.py
@@ -0,0 +1,64 @@
+
+# -*- encoding: utf-8 -*-
+#
+# Copyright 2012-2022 Ghent University
+#
+# This file is part of vsc-utils,
+# originally created by the HPC team of Ghent University (http://ugent.be/hpc/en),
+# with support of Ghent University (http://ugent.be/hpc),
+# the Flemish Supercomputer Centre (VSC) (https://www.vscentrum.be),
+# the Flemish Research Foundation (FWO) (http://www.fwo.be/en)
+# and the Department of Economy, Science and Innovation (EWI) (http://www.ewi-vlaanderen.be/en).
+#
+# https://github.com/hpcugent/vsc-utils
+#
+# vsc-utils is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Library General Public License as
+# published by the Free Software Foundation, either version 2 of
+# the License, or (at your option) any later version.
+#
+# vsc-utils is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU Library General Public License for more details.
+#
+# You should have received a copy of the GNU Library General Public License
+# along with vsc-utils. If not, see .
+#
+"""
+This module adapts the nagios module so its output can be interpreted by Zabbix.
+
+@author: Samuel Moors (Vrije Universiteit Brussel)
+"""
+from __future__ import print_function
+
+import json
+import sys
+
+from vsc.utils.nagios import SimpleNagios, NagiosReporter
+from vsc.utils.script_tools import ExtendedSimpleOption
+
+
+class ZabbixReporter(NagiosReporter):
+ """Reporting class for Zabbix reports"""
+
+ def print_report_and_exit(self, timestamp, nagios_exit_code, nagios_exit_string, nagios_message):
+ """Print the zabbix report and exit"""
+ print('{"timestamp": %f, "exit_string": "%s", "message": %s}' % (timestamp, nagios_exit_string, nagios_message))
+ self.log.info("Zabbix check cache file %s contents delivered: %s", self.filename, nagios_message)
+ sys.exit(nagios_exit_code)
+
+
+class SimpleZabbix(SimpleNagios):
+ """Class to allow easy interaction with Zabbix related code"""
+ REPORTERCLASS = ZabbixReporter
+ DEFAULT_CACHE_USER = 'zabbix'
+
+ def __str__(self):
+ """__str__ determines how the data is written to the cache"""
+ processed_dict = {key: value for (key, value) in self.__dict__.items() if not key.startswith('_')}
+ return json.dumps(processed_dict)
+
+
+class ExtendedSimpleOptionZabbix(ExtendedSimpleOption):
+ MONITORCLASS = SimpleZabbix
diff --git a/setup.py b/setup.py
index eb08065..db13081 100755
--- a/setup.py
+++ b/setup.py
@@ -54,7 +54,7 @@
PACKAGE = {
- 'version': '2.1.10',
+ 'version': '2.1.11',
'author': [ag, sdw],
'maintainer': [ag, sdw],
'excluded_pkgs_rpm': ['vsc', 'vsc.utils'], # vsc is default, vsc.utils is provided by vsc-base
diff --git a/test/00-import.py b/test/00-import.py
index 5433120..753c284 100644
--- a/test/00-import.py
+++ b/test/00-import.py
@@ -1,5 +1,5 @@
#
-# Copyright 2016-2021 Ghent University
+# Copyright 2016-2022 Ghent University
#
# This file is part of vsc-utils,
# originally created by the HPC team of Ghent University (http://ugent.be/hpc/en),
diff --git a/test/__init__.py b/test/__init__.py
index 2c87f0f..fe69a6e 100644
--- a/test/__init__.py
+++ b/test/__init__.py
@@ -1,5 +1,5 @@
#
-# Copyright 2016-2021 Ghent University
+# Copyright 2016-2022 Ghent University
#
# This file is part of vsc-utils,
# originally created by the HPC team of Ghent University (http://ugent.be/hpc/en),
diff --git a/test/cache.py b/test/cache.py
index ea315d4..25b24db 100644
--- a/test/cache.py
+++ b/test/cache.py
@@ -1,5 +1,5 @@
#
-# Copyright 2012-2021 Ghent University
+# Copyright 2012-2022 Ghent University
#
# This file is part of vsc-utils,
# originally created by the HPC team of Ghent University (http://ugent.be/hpc/en),
diff --git a/test/nagios.py b/test/nagios.py
index f04eb48..52bf590 100644
--- a/test/nagios.py
+++ b/test/nagios.py
@@ -1,5 +1,5 @@
#
-# Copyright 2012-2021 Ghent University
+# Copyright 2012-2022 Ghent University
#
# This file is part of vsc-utils,
# originally created by the HPC team of Ghent University (http://ugent.be/hpc/en),
diff --git a/test/nagios_results.py b/test/nagios_results.py
index ae416dc..30c4354 100644
--- a/test/nagios_results.py
+++ b/test/nagios_results.py
@@ -1,6 +1,6 @@
# encoding: utf-8
#
-# Copyright 2012-2021 Ghent University
+# Copyright 2012-2022 Ghent University
#
# This file is part of vsc-utils,
# originally created by the HPC team of Ghent University (http://ugent.be/hpc/en),
diff --git a/test/nagios_simple.py b/test/nagios_simple.py
index f033c21..9f306dc 100644
--- a/test/nagios_simple.py
+++ b/test/nagios_simple.py
@@ -1,5 +1,5 @@
#
-# Copyright 2012-2021 Ghent University
+# Copyright 2012-2022 Ghent University
#
# This file is part of vsc-utils,
# originally created by the HPC team of Ghent University (http://ugent.be/hpc/en),
diff --git a/test/script_tools.py b/test/script_tools.py
index 8bac724..f74b555 100644
--- a/test/script_tools.py
+++ b/test/script_tools.py
@@ -1,5 +1,5 @@
#
-# Copyright 2016-2021 Ghent University
+# Copyright 2016-2022 Ghent University
#
# This file is part of vsc-utils,
# originally created by the HPC team of Ghent University (http://ugent.be/hpc/en),
diff --git a/test/timestamp.py b/test/timestamp.py
index e105130..4dfa7a8 100644
--- a/test/timestamp.py
+++ b/test/timestamp.py
@@ -1,5 +1,5 @@
#
-# Copyright 2018-2021 Ghent University
+# Copyright 2018-2022 Ghent University
#
# This file is part of vsc-utils,
# originally created by the HPC team of Ghent University (http://ugent.be/hpc/en),
diff --git a/test/zabbix.py b/test/zabbix.py
new file mode 100644
index 0000000..6752dbb
--- /dev/null
+++ b/test/zabbix.py
@@ -0,0 +1,93 @@
+#
+# Copyright 2012-2022 Ghent University
+#
+# This file is part of vsc-utils,
+# originally created by the HPC team of Ghent University (http://ugent.be/hpc/en),
+# with support of Ghent University (http://ugent.be/hpc),
+# the Flemish Supercomputer Centre (VSC) (https://www.vscentrum.be),
+# the Flemish Research Foundation (FWO) (http://www.fwo.be/en)
+# and the Department of Economy, Science and Innovation (EWI) (http://www.ewi-vlaanderen.be/en).
+#
+# https://github.com/hpcugent/vsc-utils
+#
+# vsc-utils is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Library General Public License as
+# published by the Free Software Foundation, either version 2 of
+# the License, or (at your option) any later version.
+#
+# vsc-utils is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU Library General Public License for more details.
+#
+# You should have received a copy of the GNU Library General Public License
+# along with vsc-utils. If not, see .
+#
+"""
+Tests for the vsc.utils.zabbix module.
+
+@author: Andy Georges (Ghent University)
+@author: Samuel Moors (Vrije Universiteit Brussel)
+"""
+import json
+import os
+import tempfile
+import time
+import sys
+import random
+import string
+from pwd import getpwuid
+
+from vsc.install.testing import TestCase
+
+from vsc.utils.zabbix import ZabbixReporter, SimpleZabbix
+from vsc.utils.nagios import NAGIOS_EXIT_OK, NAGIOS_EXIT_WARNING, NAGIOS_EXIT_CRITICAL, NAGIOS_EXIT_UNKNOWN
+from vsc.utils.py2vs3 import StringIO
+
+
+class TestZabbix(TestCase):
+ """Test for the zabbix reporter class."""
+
+ def setUp(self):
+ user = getpwuid(os.getuid())
+ self.nagios_user = user.pw_name
+ super(TestZabbix, self).setUp()
+
+ def test_cache(self):
+ """Test the caching mechanism in the reporter."""
+ length = random.randint(1, 30)
+ exit_code = random.randint(0, 3)
+ threshold = random.randint(0, 10)
+
+ message = ''.join(random.choice(string.printable) for x in range(length))
+ message = message.rstrip()
+ message = json.dumps([message])
+
+ (handle, filename) = tempfile.mkstemp()
+ os.unlink(filename)
+ os.close(handle)
+ reporter = ZabbixReporter('test_cache', filename, threshold, self.nagios_user)
+
+ nagios_exit = [NAGIOS_EXIT_OK, NAGIOS_EXIT_WARNING, NAGIOS_EXIT_CRITICAL, NAGIOS_EXIT_UNKNOWN][exit_code]
+
+ reporter.cache(nagios_exit, message)
+
+ (handle, output_filename) = tempfile.mkstemp()
+ os.close(handle)
+
+ try:
+ old_stdout = sys.stdout
+ buffer = StringIO()
+ sys.stdout = buffer
+ reporter_test = ZabbixReporter('test_cache', filename, threshold, self.nagios_user)
+ reporter_test.report_and_exit()
+ except SystemExit as err:
+ line = buffer.getvalue().rstrip()
+ sys.stdout = old_stdout
+ buffer.close()
+ self.assertTrue(err.code == nagios_exit[0])
+ line = json.loads(line)
+ self.assertTrue(line["exit_string"] == nagios_exit[1])
+ self.assertTrue(line["message"][0] == json.loads(message)[0])
+
+ os.unlink(filename)