Skip to content

Commit 86afb52

Browse files
committed
include timestamp into json output for zabbix; add test for zabbix
1 parent 75732bf commit 86afb52

File tree

3 files changed

+106
-9
lines changed

3 files changed

+106
-9
lines changed

lib/vsc/utils/nagios.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -277,18 +277,18 @@ def report_and_exit(self):
277277
unknown_exit("%s nagios gzipped JSON file unavailable (%s)" % (self.header, self.filename))
278278

279279
(timestamp, ((nagios_exit_code, nagios_exit_string), nagios_message)) = nagios_cache.load('nagios')
280+
self.print_report_and_exit(timestamp, nagios_exit_code, nagios_exit_string, nagios_message)
280281

282+
283+
def print_report_and_exit(self, timestamp, nagios_exit_code, nagios_exit_string, nagios_message):
284+
"""Print the nagios report (if the data is not too old) and exit"""
281285
if self.threshold <= 0 or time.time() - timestamp < self.threshold:
282286
self.log.info("Nagios check cache file %s contents delivered: %s", self.filename, nagios_message)
283-
self.print_report(nagios_exit_string, nagios_message)
287+
print("%s %s" % (nagios_exit_string, nagios_message))
284288
sys.exit(nagios_exit_code)
285289
else:
286290
unknown_exit("%s gzipped JSON file too old (timestamp = %s)" % (self.header, time.ctime(timestamp)))
287291

288-
def print_report(self, nagios_exit_string, nagios_message):
289-
"""Print the nagios report"""
290-
print("%s %s" % (nagios_exit_string, nagios_message))
291-
292292
def cache(self, nagios_exit, nagios_message):
293293
"""Store the result in the cache file with a timestamp.
294294

lib/vsc/utils/zabbix.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,14 @@
3333
from __future__ import print_function
3434

3535
import json
36-
36+
import sys
3737

3838
from vsc.utils.nagios import SimpleNagios, NagiosReporter
3939

4040

4141
class SimpleZabbix(SimpleNagios):
42+
"""Class to allow easy interaction with Zabbix related code"""
43+
4244
def __init__(self, **kwargs):
4345
"""Initialise message and perfdata"""
4446
super(SimpleZabbix, self)._init(reporterclass=ZabbixReporter, **kwargs)
@@ -52,7 +54,9 @@ def __str__(self):
5254
class ZabbixReporter(NagiosReporter):
5355
"""Reporting class for Zabbix reports"""
5456

55-
def print_report(self, nagios_exit_string, nagios_message):
56-
"""Print the nagios report"""
57-
print(nagios_message)
57+
def print_report_and_exit(self, timestamp, nagios_exit_code, nagios_exit_string, nagios_message):
58+
"""Print the zabbix report and exit"""
59+
json.dump([timestamp, nagios_exit_string, json.loads(nagios_message)], sys.stdout)
60+
self.log.info("Zabbix check cache file %s contents delivered: %s", self.filename, nagios_message)
61+
sys.exit(nagios_exit_code)
5862

test/zabbix.py

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
#
2+
# Copyright 2012-2022 Ghent University
3+
#
4+
# This file is part of vsc-utils,
5+
# originally created by the HPC team of Ghent University (http://ugent.be/hpc/en),
6+
# with support of Ghent University (http://ugent.be/hpc),
7+
# the Flemish Supercomputer Centre (VSC) (https://www.vscentrum.be),
8+
# the Flemish Research Foundation (FWO) (http://www.fwo.be/en)
9+
# and the Department of Economy, Science and Innovation (EWI) (http://www.ewi-vlaanderen.be/en).
10+
#
11+
# https://github.com/hpcugent/vsc-utils
12+
#
13+
# vsc-utils is free software: you can redistribute it and/or modify
14+
# it under the terms of the GNU Library General Public License as
15+
# published by the Free Software Foundation, either version 2 of
16+
# the License, or (at your option) any later version.
17+
#
18+
# vsc-utils is distributed in the hope that it will be useful,
19+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
20+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21+
# GNU Library General Public License for more details.
22+
#
23+
# You should have received a copy of the GNU Library General Public License
24+
# along with vsc-utils. If not, see <http://www.gnu.org/licenses/>.
25+
#
26+
"""
27+
Tests for the vsc.utils.zabbix module.
28+
29+
@author: Andy Georges (Ghent University)
30+
@author: Samuel Moors (Vrije Universiteit Brussel)
31+
"""
32+
import json
33+
import os
34+
import tempfile
35+
import time
36+
import sys
37+
import random
38+
import string
39+
from pwd import getpwuid
40+
41+
from vsc.install.testing import TestCase
42+
43+
from vsc.utils.zabbix import ZabbixReporter, SimpleZabbix
44+
from vsc.utils.nagios import NAGIOS_EXIT_OK, NAGIOS_EXIT_WARNING, NAGIOS_EXIT_CRITICAL, NAGIOS_EXIT_UNKNOWN
45+
from vsc.utils.py2vs3 import StringIO
46+
47+
48+
class TestZabbix(TestCase):
49+
"""Test for the zabbix reporter class."""
50+
51+
def setUp(self):
52+
user = getpwuid(os.getuid())
53+
self.nagios_user = user.pw_name
54+
super(TestZabbix, self).setUp()
55+
56+
def test_cache(self):
57+
"""Test the caching mechanism in the reporter."""
58+
length = random.randint(1, 30)
59+
exit_code = random.randint(0, 3)
60+
threshold = random.randint(0, 10)
61+
62+
message = ''.join(random.choice(string.printable) for x in range(length))
63+
message = message.rstrip()
64+
message = json.dumps([message])
65+
66+
(handle, filename) = tempfile.mkstemp()
67+
os.unlink(filename)
68+
os.close(handle)
69+
reporter = ZabbixReporter('test_cache', filename, threshold, self.nagios_user)
70+
71+
nagios_exit = [NAGIOS_EXIT_OK, NAGIOS_EXIT_WARNING, NAGIOS_EXIT_CRITICAL, NAGIOS_EXIT_UNKNOWN][exit_code]
72+
73+
reporter.cache(nagios_exit, message)
74+
75+
(handle, output_filename) = tempfile.mkstemp()
76+
os.close(handle)
77+
78+
try:
79+
old_stdout = sys.stdout
80+
buffer = StringIO()
81+
sys.stdout = buffer
82+
reporter_test = ZabbixReporter('test_cache', filename, threshold, self.nagios_user)
83+
reporter_test.report_and_exit()
84+
except SystemExit as err:
85+
line = buffer.getvalue().rstrip()
86+
sys.stdout = old_stdout
87+
buffer.close()
88+
self.assertTrue(err.code == nagios_exit[0])
89+
line = json.loads(line)
90+
self.assertTrue(line[1] == nagios_exit[1])
91+
self.assertTrue(line[2][0] == json.loads(message)[0])
92+
93+
os.unlink(filename)

0 commit comments

Comments
 (0)