def process_args(self): if not self.name: raise CodingError("didn't name check, please set self.name in __init__()") self.no_args() self.host = self.get_opt('host') self.port = self.get_opt('port') validate_host(self.host) validate_port(self.port) self.key = self.get_opt('key') self.regex = self.get_opt('regex') if not self.key: self.usage('--key not defined') self.key = self.key.lstrip('/') validate_chars(self.key, 'key', r'\w\/-') if self.regex: validate_regex(self.regex, 'key') self.validate_thresholds(optional=True)
def validate_threshold(self, name, threshold=None, optional=False, **kwargs): if not isStr(name): raise CodingError('non-string name passed to validate_threshold()') name = re.sub('[^A-Za-z0-9]', '_', name).lower() if threshold is None: # log.debug("using threshold option '%s'", name) threshold = self.get_opt(name) # log.debug("got threshold '%s'", threshold) if optional and threshold is None: # pylint: disable=no-else-return return None else: try: self.__thresholds[name] = Threshold(threshold, name=name, **kwargs) except InvalidThresholdException as _: self.usage(_) log_option(name, threshold) return None
def validate_thresholds(self, name='', warning=None, critical=None, **kwargs): if not isStr(name): raise CodingError( 'non-string name passed to validate_thresholds()') if name: name += '_' if 'percent' in kwargs and kwargs['percent']: del kwargs['percent'] kwargs['min'] = 0 kwargs['max'] = 100 self.validate_threshold(name='{0}{1}'.format(name, 'warning'), threshold=warning, **kwargs) self.validate_threshold(name='{0}{1}'.format(name, 'critical'), threshold=critical, **kwargs)
def validate_threshold(self, name, threshold=None, optional=False, **kwargs): if not isStr(name): raise CodingError('non-string name passed to validate_threshold()') if threshold is None: # log.debug("using threshold option '%s'", name) threshold = self.get_opt(name) # log.debug("got threshold '%s'", threshold) if optional and threshold is None: return None else: try: self.__thresholds[name] = Threshold(threshold, name=name, **kwargs) except InvalidThresholdException as _: self.usage(_) log_option(name, threshold)
def status(self, status): if status not in ERRORS: raise CodingError("invalid status '{0}' passed to harisekhon.NagiosPlugin.status()".format(status)) self.__status = status
def set_threshold(self, name, threshold): if not isinstance(threshold, Threshold): raise CodingError('passed a non-threshold to NagiosPlugin.set_threshold()') self.__thresholds[name] = threshold
def run(self): # pragma: no cover raise CodingError('running HariSekhon.CLI().run() - this should be abstract and non-runnable!' ' You should have overridden this run() method in the client code')
def verbose_default(self, arg): if not isInt(arg): raise CodingError('invalid verbose level passed to verbose_default(), must be an integer') log.debug('setting default verbose to %s', arg) self.__verbose_default = int(arg)
def get_opt(self, name): if not isStr(name): raise CodingError('passed non-string as arg to CLI.get_opt()') if not self.is_option_defined(name): raise CodingError('{0} option not defined'.format(name)) return getattr(self.options, name)
def add_options(self): if not self.name: raise CodingError("didn't name check, please set self.name in __init__()") self.add_hostoption(self.name, default_host=self.default_host, default_port=self.default_port) self.add_thresholds(default_warning=self.warning_threshold_default, default_critical=self.critical_threshold_default)
def status(self, status): if not ERRORS.has_key(status): raise CodingError( "invalid status '%(status)s' passed to harisekhon.NagiosPlugin.status()" ) self.__status = status
def timeout(self, secs): if not isInt(secs): raise CodingError('invalid timeout passed to set_timeout(), must be an integer representing seconds') # pylint: disable=line-too-long validate_int(secs, 'timeout', 0, self.timeout_max) log.debug('setting timeout to %s secs', secs) self.__timeout = int(secs)
def add_options(self): if not self.name: raise CodingError("didn't name check, please set self.name in __init__()") self.add_hostoption(self.name, default_host=self.default_host, default_port=self.default_port) self._add_options()
def add_options(self): super(RestVersionNagiosPlugin, self).add_options() if self.name is None: raise CodingError('self.name is not defined in inheriting class') self.add_expected_version_option()