-
Notifications
You must be signed in to change notification settings - Fork 12
WIP: Cleaning up the CLI mess stack #97
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
13ba028
ed9905e
96548ab
696fcd5
d8fa8b7
5424543
c61a7a9
3a2b52b
1e47902
90e0612
b3c59d0
5a72a8a
e06b58f
ccf333c
da653ff
c4c7f32
a19bf48
076ce72
5e465e8
01a07a1
a63831f
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 |
---|---|---|
|
@@ -28,3 +28,31 @@ | |
""" | ||
import pkg_resources | ||
pkg_resources.declare_namespace(__name__) | ||
|
||
import os | ||
import warnings | ||
from functools import wraps | ||
|
||
def _script_name(full_name): | ||
"""Return the script name without .py extension if any. This assumes that the script name does not contain a | ||
dot in case of lacking an extension. | ||
""" | ||
(name, _) = os.path.splitext(full_name) | ||
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. use pathlib |
||
return os.path.basename(name) | ||
|
||
|
||
def deprecated_class(message=None): | ||
def decorator(cls): | ||
class Wrapped(cls): | ||
def __init__(self, *args, **kwargs): | ||
warnings.warn( | ||
f"{cls.__name__} is deprecated. {message or ''}", | ||
category=DeprecationWarning, | ||
stacklevel=2, | ||
) | ||
super().__init__(*args, **kwargs) | ||
Wrapped.__name__ = cls.__name__ | ||
Wrapped.__doc__ = cls.__doc__ | ||
Wrapped.__module__ = cls.__module__ | ||
return Wrapped | ||
return decorator |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,6 +39,7 @@ | |
@author: Luis Fernando Muñoz Mejías (Ghent University) | ||
""" | ||
|
||
import logging | ||
import operator | ||
import os | ||
import pwd | ||
|
@@ -47,12 +48,16 @@ | |
import sys | ||
import time | ||
|
||
from pathlib import Path | ||
|
||
from vsc.utils import _script_name | ||
from vsc.utils.cache import FileCache | ||
from vsc.utils.fancylogger import getLogger | ||
|
||
log = getLogger(__name__) | ||
#log = getLogger(__name__) | ||
|
||
NAGIOS_CACHE_DIR = '/var/cache' | ||
NAGIOS_CACHE_FILENAME = 'cache.nagios.json.gz' | ||
NAGIOS_CACHE_FILENAME_TEMPLATE = '%s.nagios.json.gz' | ||
|
||
NAGIOS_OK = 'OK' | ||
|
@@ -85,7 +90,7 @@ def _real_exit(message, code, metrics=''): | |
metrics = f'|{message[1]}' | ||
if len(msg) > NAGIOS_MAX_MESSAGE_LENGTH: | ||
# log long message but print truncated message | ||
log.info("Nagios report %s: %s%s", exit_text, msg, metrics) | ||
logging.info("Nagios report %s: %s%s", exit_text, msg, metrics) | ||
msg = msg[:NAGIOS_MAX_MESSAGE_LENGTH-3] + '...' | ||
|
||
print(f"{exit_text} {msg}{metrics}") | ||
|
@@ -203,7 +208,7 @@ def parse(self, nrange): | |
self.log.debug("parse: start %s end %s neg %s", start, end, neg) | ||
else: | ||
msg = f"parse: invalid nrange {nrange}." | ||
self.log.Error(msg) | ||
self.log.error(msg) | ||
raise ValueError(nrange) | ||
|
||
def range_fn(test): | ||
|
@@ -241,6 +246,69 @@ def alert(self, test): | |
""" | ||
return not self.range_fn(test) | ||
|
||
class NagiosStatusMixin: | ||
""" | ||
A mixin class providing methods for Nagios status codes. | ||
|
||
Note that these methods do not return, they exit the script. | ||
|
||
Options come from NAGIOS_MIXIN_OPTIONS. | ||
""" | ||
|
||
NAGIOS_MIXIN_OPTIONS = { | ||
'nagios-report': ('print out nagios information', None, 'store_true', False, 'n'), | ||
'nagios-check-filename': | ||
('filename of where the nagios check data is stored', 'str', 'store', | ||
Path(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), | ||
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. I still don't know what this does, thresholds are configured in icinga, not in checks. 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. What do you mean? This has been in NagiosRepoerter for 12 years.
See |
||
'nagios-user': ('user nagios runs as', 'str', 'store', 'nrpe'), | ||
'nagios-world-readable-check': ('make the nagios check data file world readable', None, 'store_true', False), | ||
} | ||
|
||
def nagios_prologue(self): | ||
""" | ||
This will set up the reporter, but exit immediately of the report is requested | ||
""" | ||
# bail if nagios report is requested | ||
self.nagios = 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, | ||
) | ||
|
||
def nagios_epilogue(self, nagios_exit, nagios_message): | ||
""" | ||
This will write the result to the cache file | ||
""" | ||
self.nagios._exit(nagios_exit, nagios_message) | ||
|
||
def ok(self, msg): | ||
""" | ||
Convenience method that exits with Nagios OK exit code. | ||
""" | ||
exit_from_errorcode(0, msg) | ||
|
||
def warning(self, msg): | ||
""" | ||
Convenience method that exits with Nagios WARNING exit code. | ||
""" | ||
exit_from_errorcode(1, msg) | ||
|
||
def critical(self, msg): | ||
""" | ||
Convenience method that exits with Nagios CRITICAL exit code. | ||
""" | ||
exit_from_errorcode(2, msg) | ||
|
||
def unknown(self, msg): | ||
""" | ||
Convenience method that exits with Nagios UNKNOWN exit code. | ||
""" | ||
exit_from_errorcode(3, msg) | ||
|
||
|
||
class NagiosReporter: | ||
"""Reporting class for Nagios/Icinga reports. | ||
|
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.
what does this do?