-
Notifications
You must be signed in to change notification settings - Fork 12
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
base: master
Are you sure you want to change the base?
Changes from all commits
72e92ff
3f0e291
7b639b9
70d2fa9
c7595ad
75732bf
86afb52
df0cee3
507804c
2fa7a98
dacb163
4869a7c
a316993
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,64 @@ | ||||||
|
||||||
# -*- encoding: utf-8 -*- | ||||||
# | ||||||
# Copyright 2012-2022 Ghent University | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And run There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this command reverts the change back to |
||||||
# | ||||||
# 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) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. from Ward:
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): | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we use this class directly like this:
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 |
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) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.