Skip to content

Commit 90e0612

Browse files
committed
fix: move nagios mixin to nagios module
1 parent 1e47902 commit 90e0612

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed

lib/vsc/utils/nagios.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,14 @@
4747
import sys
4848
import time
4949

50+
from vsc.utils import _script_name
5051
from vsc.utils.cache import FileCache
5152
from vsc.utils.fancylogger import getLogger
5253

5354
log = getLogger(__name__)
5455

5556
NAGIOS_CACHE_DIR = '/var/cache'
57+
NAGIOS_CACHE_FILENAME = 'cache.nagios.json.gz'
5658
NAGIOS_CACHE_FILENAME_TEMPLATE = '%s.nagios.json.gz'
5759

5860
NAGIOS_OK = 'OK'
@@ -241,6 +243,72 @@ def alert(self, test):
241243
"""
242244
return not self.range_fn(test)
243245

246+
class NagiosStatusMixin:
247+
"""
248+
A mixin class providing methods for Nagios status codes.
249+
250+
Note that these methods do not return, they exit the script.
251+
252+
Options come from NAGIOS_MIXIN_OPTIONS.
253+
"""
254+
255+
NAGIOS_MIXIN_OPTIONS = {
256+
'nagios-report': ('print out nagios information', None, 'store_true', False, 'n'),
257+
'nagios-check-filename':
258+
('filename of where the nagios check data is stored', 'str', 'store',
259+
os.path.join(
260+
NAGIOS_CACHE_DIR,
261+
NAGIOS_CACHE_FILENAME_TEMPLATE % (_script_name(sys.argv[0]),)
262+
)
263+
),
264+
'nagios-check-interval-threshold': ('threshold of nagios checks timing out', 'int', 'store', 0),
265+
'nagios-user': ('user nagios runs as', 'str', 'store', 'nrpe'),
266+
'nagios-world-readable-check': ('make the nagios check data file world readable', None, 'store_true', False),
267+
}
268+
269+
def nagios_prologue(self):
270+
"""
271+
This will set up the reporter, but exit immediately of the report is requested
272+
"""
273+
# bail if nagios report is requested
274+
self.nagios = SimpleNagios(
275+
_cache=self.options.nagios_check_filename,
276+
_report_and_exit=self.options.nagios_report,
277+
_threshold=self.options.nagios_check_interval_threshold,
278+
_cache_user=self.options.nagios_user,
279+
_world_readable=self.options.nagios_world_readable_check,
280+
)
281+
282+
def nagios_epilogue(self, nagios_exit, nagios_message):
283+
"""
284+
This will write the result to the cache file
285+
"""
286+
self.nagios._exit(nagios_exit, nagios_message)
287+
288+
def ok(self, msg):
289+
"""
290+
Convenience method that exits with Nagios OK exit code.
291+
"""
292+
exit_from_errorcode(0, msg)
293+
294+
def warning(self, msg):
295+
"""
296+
Convenience method that exits with Nagios WARNING exit code.
297+
"""
298+
exit_from_errorcode(1, msg)
299+
300+
def critical(self, msg):
301+
"""
302+
Convenience method that exits with Nagios CRITICAL exit code.
303+
"""
304+
exit_from_errorcode(2, msg)
305+
306+
def unknown(self, msg):
307+
"""
308+
Convenience method that exits with Nagios UNKNOWN exit code.
309+
"""
310+
exit_from_errorcode(3, msg)
311+
244312

245313
class NagiosReporter:
246314
"""Reporting class for Nagios/Icinga reports.

0 commit comments

Comments
 (0)