Skip to content

Commit 507804c

Browse files
committed
use class constants for monitorclass and reporterclass
1 parent df0cee3 commit 507804c

File tree

3 files changed

+21
-27
lines changed

3 files changed

+21
-27
lines changed

lib/vsc/utils/nagios.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,6 @@ def report_and_exit(self):
279279
(timestamp, ((nagios_exit_code, nagios_exit_string), nagios_message)) = nagios_cache.load('nagios')
280280
self.print_report_and_exit(timestamp, nagios_exit_code, nagios_exit_string, nagios_message)
281281

282-
283282
def print_report_and_exit(self, timestamp, nagios_exit_code, nagios_exit_string, nagios_message):
284283
"""Print the nagios report (if the data is not too old) and exit"""
285284
if self.threshold <= 0 or time.time() - timestamp < self.threshold:
@@ -435,14 +434,10 @@ class SimpleNagios(NagiosResult):
435434
USE_HEADER = True
436435
RESERVED_WORDS = set(['message', 'ok', 'warning', 'critical', 'unknown',
437436
'_exit', '_cache', '_cache_user', '_final', '_final_state', '_report', '_threshold'])
437+
REPORTERCLASS = NagiosReporter
438438

439439
def __init__(self, **kwargs):
440440
"""Initialise message and perfdata"""
441-
self._init(**kwargs)
442-
443-
def _init(self, reporterclass=NagiosReporter, **kwargs):
444-
"""The real init method"""
445-
446441
self.__dict__ = {}
447442
self.message = None # the message
448443

@@ -462,10 +457,10 @@ def _init(self, reporterclass=NagiosReporter, **kwargs):
462457
if self._cache:
463458
# make a NagiosReporter instance that can be used for caching
464459
if self._cache_user:
465-
cache = reporterclass('no header', self._cache, self._threshold, nagios_username=self._cache_user,
460+
cache = self.REPORTERCLASS('no header', self._cache, self._threshold, nagios_username=self._cache_user,
466461
world_readable=self._world_readable)
467462
else:
468-
cache = reporterclass('no header', self._cache, self._threshold, world_readable=self._world_readable)
463+
cache = self.REPORTERCLASS('no header', self._cache, self._threshold, world_readable=self._world_readable)
469464
if self._report_and_exit:
470465
cache.report_and_exit()
471466
else:

lib/vsc/utils/script_tools.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -114,14 +114,13 @@ class ExtendedSimpleOption(SimpleOption):
114114
115115
The prologue should be called at the start of the script; the epilogue at the end.
116116
"""
117+
MONITORCLASS = SimpleNagios
117118

118-
def __init__(self, options, run_prologue=True, excepthook=None, monitorclass=SimpleNagios, **kwargs):
119+
def __init__(self, options, run_prologue=True, excepthook=None, **kwargs):
119120
"""Initialise.
120121
121122
If run_prologue is True (default), we immediately execute the prologue.
122123
123-
- monitorclass: the class to use for interaction with the monitoring software
124-
125124
Note that if taking a lock is requested (default), and the lock cannot be
126125
acquire for some reason, the program will exit,
127126
"""
@@ -131,7 +130,6 @@ def __init__(self, options, run_prologue=True, excepthook=None, monitorclass=Sim
131130

132131
self.nagios_reporter = None
133132
self.lockfile = None
134-
self.monitorclass = monitorclass
135133

136134
if run_prologue:
137135
self.prologue()
@@ -148,13 +146,13 @@ def prologue(self):
148146
149147
See _merge_options for the format.
150148
151-
- if nagios_report is set, creates a monitorclass instance and prints the report.
149+
- if nagios_report is set, creates a SimpleNagios instance and prints the report.
152150
- if ha is set, checks if running on the correct host, set the appropriate nagios message and bail if not.
153151
- if locking_filename is set, take a lock. If the lock fails, bork and set the nagios exit accordingly.
154152
"""
155153

156154
# bail if nagios report is requested
157-
self.nagios_reporter = self.monitorclass(_cache=self.options.nagios_check_filename,
155+
self.nagios_reporter = self.MONITORCLASS(_cache=self.options.nagios_check_filename,
158156
_report_and_exit=self.options.nagios_report,
159157
_threshold=self.options.nagios_check_interval_threshold,
160158
_cache_user=self.options.nagios_user,

lib/vsc/utils/zabbix.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,7 @@
3636
import sys
3737

3838
from vsc.utils.nagios import SimpleNagios, NagiosReporter
39-
40-
41-
class SimpleZabbix(SimpleNagios):
42-
"""Class to allow easy interaction with Zabbix related code"""
43-
44-
def __init__(self, **kwargs):
45-
"""Initialise message and perfdata"""
46-
super(SimpleZabbix, self)._init(reporterclass=ZabbixReporter, **kwargs)
47-
48-
def __str__(self):
49-
"""__str__ determines how the data is written to the cache"""
50-
processed_dict = {key: value for (key, value) in self.__dict__.items() if not key.startswith('_')}
51-
return json.dumps(processed_dict)
39+
from vsc.utils.script_tools import ExtendedSimpleOption
5240

5341

5442
class ZabbixReporter(NagiosReporter):
@@ -60,3 +48,16 @@ def print_report_and_exit(self, timestamp, nagios_exit_code, nagios_exit_string,
6048
self.log.info("Zabbix check cache file %s contents delivered: %s", self.filename, nagios_message)
6149
sys.exit(nagios_exit_code)
6250

51+
52+
class SimpleZabbix(SimpleNagios):
53+
"""Class to allow easy interaction with Zabbix related code"""
54+
REPORTERCLASS = ZabbixReporter
55+
56+
def __str__(self):
57+
"""__str__ determines how the data is written to the cache"""
58+
processed_dict = {key: value for (key, value) in self.__dict__.items() if not key.startswith('_')}
59+
return json.dumps(processed_dict)
60+
61+
62+
class ExtendedSimpleOptionZabbix(ExtendedSimpleOption):
63+
MONITORCLASS = SimpleZabbix

0 commit comments

Comments
 (0)