Skip to content

add support for zabbix as alternative to nagios #81

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/vsc/__init__.py
Original file line number Diff line number Diff line change
@@ -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),
Expand Down
2 changes: 1 addition & 1 deletion lib/vsc/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -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),
Expand Down
2 changes: 1 addition & 1 deletion lib/vsc/utils/availability.py
Original file line number Diff line number Diff line change
@@ -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),
Expand Down
2 changes: 1 addition & 1 deletion lib/vsc/utils/cache.py
Original file line number Diff line number Diff line change
@@ -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),
Expand Down
2 changes: 1 addition & 1 deletion lib/vsc/utils/fs_store.py
Original file line number Diff line number Diff line change
@@ -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),
Expand Down
2 changes: 1 addition & 1 deletion lib/vsc/utils/lock.py
Original file line number Diff line number Diff line change
@@ -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),
Expand Down
12 changes: 9 additions & 3 deletions lib/vsc/utils/nagios.py
Original file line number Diff line number Diff line change
@@ -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),
Expand Down Expand Up @@ -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"""
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do you not need/use the if/else logic? i would have expected that you only want to override the if block?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for zabbix the if/else logic is skipped: we always report, even if the data is too old, and let zabbix handle that.

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))
Expand Down Expand Up @@ -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"""
Expand All @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion lib/vsc/utils/pickle_files.py
Original file line number Diff line number Diff line change
@@ -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),
Expand Down
2 changes: 1 addition & 1 deletion lib/vsc/utils/rest_oauth.py
Original file line number Diff line number Diff line change
@@ -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),
Expand Down
20 changes: 12 additions & 8 deletions lib/vsc/utils/script_tools.py
Original file line number Diff line number Diff line change
@@ -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),
Expand Down Expand Up @@ -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),
}

Expand Down Expand Up @@ -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.
Expand All @@ -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

Expand All @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion lib/vsc/utils/timestamp.py
Original file line number Diff line number Diff line change
@@ -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),
Expand Down
2 changes: 1 addition & 1 deletion lib/vsc/utils/timestamp_pid_lockfile.py
Original file line number Diff line number Diff line change
@@ -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),
Expand Down
64 changes: 64 additions & 0 deletions lib/vsc/utils/zabbix.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@

# -*- encoding: utf-8 -*-
#
# Copyright 2012-2022 Ghent University
Copy link
Contributor

@wpoely86 wpoely86 Jun 20, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# Copyright 2012-2022 Ghent University
# Copyright 2022-2022 Vrije Universiteit Brussel

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And run find ./{lib,test} -type f -name '*.py' | REPO_BASE_DIR=$PWD xargs -I '{}' python -m vsc.install.headers '{}' in the repo.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this command reverts the change back to 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 <http://www.gnu.org/licenses/>.
#
"""
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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for zabbix, we want a normal exit I think? Will the agent not fail if it's non-zero?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

from Ward:

Checking the exit code is removed from external checks, user parameters and system.run items. These checks will not to become unsupported if the exit code is different than zero. Exit code checks are now only performed in custom alert scripts, remote commands and user scripts executed on Zabbix server and proxy.

in de release notes van 3.4.3

dus this ok



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):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how do you use this in code? i see no easy way to pass this via commandline or config file

Copy link
Author

@smoors smoors May 13, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we use this class directly like this:

opts = ExtendedSimpleOptionZabbix(options)

this similar to how it's used here: https://github.com/vub-hpc/Lmod-UGent/blob/master/run_lmod_cache.py

or did I miss something?

MONITORCLASS = SimpleZabbix
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/00-import.py
Original file line number Diff line number Diff line change
@@ -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),
Expand Down
2 changes: 1 addition & 1 deletion test/__init__.py
Original file line number Diff line number Diff line change
@@ -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),
Expand Down
2 changes: 1 addition & 1 deletion test/cache.py
Original file line number Diff line number Diff line change
@@ -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),
Expand Down
2 changes: 1 addition & 1 deletion test/nagios.py
Original file line number Diff line number Diff line change
@@ -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),
Expand Down
2 changes: 1 addition & 1 deletion test/nagios_results.py
Original file line number Diff line number Diff line change
@@ -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),
Expand Down
2 changes: 1 addition & 1 deletion test/nagios_simple.py
Original file line number Diff line number Diff line change
@@ -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),
Expand Down
2 changes: 1 addition & 1 deletion test/script_tools.py
Original file line number Diff line number Diff line change
@@ -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),
Expand Down
2 changes: 1 addition & 1 deletion test/timestamp.py
Original file line number Diff line number Diff line change
@@ -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),
Expand Down
93 changes: 93 additions & 0 deletions test/zabbix.py
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
#
"""
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)