class SensuPlugin(object): def __init__(self): self.plugin_info = { 'check_name': None, 'message': None, 'status': None } self._hook = ExitHook() self._hook.hook() self.exit_code = ExitCode(0, 1, 2, 3) for field in self.exit_code._fields: self.__make_dynamic(field) atexit.register(self.__exitfunction) self.parser = argparse.ArgumentParser() if hasattr(self, 'setup'): self.setup() (self.options, self.remain) = self.parser.parse_known_args() self.settings = SensuUtils.settings() self.run() def output(self, args): print("SensuPlugin: %s" % ' '.join(str(a) for a in args)) def __make_dynamic(self, method): def dynamic(*args): self.plugin_info['status'] = method if len(args) == 0: args = None self.output(args) sys.exit(getattr(self.exit_code, method)) method_lc = method.lower() dynamic.__doc__ = "%s method" % method_lc dynamic.__name__ = method_lc setattr(self, dynamic.__name__, dynamic) def run(self): self.warning("Not implemented! You should override SensuPlugin.run()") def __exitfunction(self): if self._hook.exit_code is None and self._hook.exception is None: print("Check did not exit! You should call an exit code method.") sys.stdout.flush() os._exit(1) elif self._hook.exception: print("Check failed to run: %s, %s" % (sys.last_type, traceback.format_tb(sys.last_traceback))) sys.stdout.flush() os._exit(2)
class SensuPlugin(object): def __init__(self): self.plugin_info = { 'check_name': None, 'message': None, 'status': None } self._hook = ExitHook() self._hook.hook() self.exit_code = ExitCode(0, 1, 2, 3) for field in self.exit_code._fields: self.__make_dynamic(field) atexit.register(self.__exitfunction) self.parser = argparse.ArgumentParser() if hasattr(self, 'setup'): self.setup() (self.options, self.remain) = self.parser.parse_known_args() self.run() def output(self, args): print("SensuPlugin: %s" % ' '.join(str(a) for a in args)) def __make_dynamic(self, method): def dynamic(*args): self.plugin_info['status'] = method if len(args) == 0: args = None self.output(args) sys.exit(getattr(self.exit_code, method)) method_lc = method.lower() dynamic.__doc__ = "%s method" % method_lc dynamic.__name__ = method_lc setattr(self, dynamic.__name__, dynamic) def run(self): self.warning("Not implemented! You should override SensuPlugin.run()") def __exitfunction(self): if self._hook.exit_code is None and self._hook.exception is None: print("Check did not exit! You should call an exit code method.") sys.stdout.flush() os._exit(1) elif self._hook.exception: print("Check failed to run: %s, %s" % (sys.last_type, traceback.format_tb(sys.last_traceback))) sys.stdout.flush() os._exit(2)
class SensuPlugin(object): def __init__(self, args=None): self.plugin_info = {"check_name": None, "message": None, "status": None} self._hook = ExitHook() self._hook.hook() atexit.register(self.__exitfunction) self.exit_code = ExitCode(0, 1, 2, 3) for field in self.exit_code._fields: self.__make_dynamic(field) self.parser = argparse.ArgumentParser() if hasattr(self, "setup"): self.setup() (self.options, self.remain) = self.parser.parse_known_args(args) ret_value = self.run() if hasattr(ret_value, "__call__"): getattr(self, ret_value()) def output(self, args): print("SensuPlugin: %s" % " ".join(str(a) for a in args)) def __make_dynamic(self, method): def dynamic(*args): self.plugin_info["status"] = method if len(args) == 0: args = None self.output(args) sys.exit(getattr(self.exit_code, method)) method_lc = method.lower() dynamic.__doc__ = "%s method" % method_lc dynamic.__name__ = method_lc setattr(self, dynamic.__name__, dynamic) def run(self): self.warning("Not implemented! You should override SensuPlugin.run()") def __exitfunction(self): if self._hook.exit_code is None and self._hook.exception is None: print("Check did not exit! You should call an exit code method.", file=sys.stderr) sys.stderr.flush() sys.exit(1) elif self._hook.exception: print("Check failed to run", file=sys.stderr) traceback.print_last() sys.stdout.flush() sys.exit(2)
class SensuPlugin(object): def __init__(self): self._check_name = None self._message = None self.status = None self.hook = ExitHook() self.hook.hook() for method in dir(ExitCode): if not (method.startswith('__') and method.endswith('__')): self.__make_dynamic(method) atexit.register(self.__exitfunction) self.parser = argparse.ArgumentParser() if hasattr(self, 'setup'): self.setup() self.options = self.parser.parse_args() self.run() def output(self, args): print("SensuPlugin: %s" % ' '.join(str(a) for a in args)) def __make_dynamic(self, method): ec = ExitCode() def dynamic(*args): self.status = method if len(args) == 0: args = None self.output(args) sys.exit(getattr(ec, method)) method_lc = method.lower() dynamic.__doc__ = "%s method" % method_lc dynamic.__name__ = method_lc setattr(self, dynamic.__name__, dynamic) def run(self): self.warning("Not implemented! You should override SensuPlugin.run()") def __exitfunction(self): if self.hook.exit_code is None and self.hook.exception is None: print("Check did not exit! You should call an exit code method.") elif self.hook.exception: print("Check failed to run: %s" % self.hook.exception)
class SensuPlugin(object): ''' Base class used by both checks and metrics plugins. ''' def __init__(self, autorun=True): self.plugin_info = { 'check_name': None, 'message': None, 'status': None } # create a method for each of the exit codes # and register as exiy functions self._hook = ExitHook() self._hook.hook() self.exit_code = ExitCode(0, 1, 2, 3) for field in self.exit_code._fields: self.__make_dynamic(field) atexit.register(self.__exitfunction) # Prepare command line arguments self.parser = argparse.ArgumentParser() if hasattr(self, 'setup'): self.setup() (self.options, self.remain) = self.parser.parse_known_args() if autorun: self.run() def output(self, args): ''' Print the output message. ''' print("SensuPlugin: {}".format(' '.join(str(a) for a in args))) def __make_dynamic(self, method): ''' Create a method for each of the exit codes. ''' def dynamic(*args): self.plugin_info['status'] = method if not args: args = None self.output(args) sys.exit(getattr(self.exit_code, method)) method_lc = method.lower() dynamic.__doc__ = "%s method" % method_lc dynamic.__name__ = method_lc setattr(self, dynamic.__name__, dynamic) def run(self): ''' Method should be overwritten by inherited classes. ''' self.warning("Not implemented! You should override SensuPlugin.run()") def __exitfunction(self): ''' Method called by exit hook, ensures that both an exit code and output is supplied, also catches errors. ''' if self._hook.exit_code is None and self._hook.exception is None: print("Check did not exit! You should call an exit code method.") sys.stdout.flush() os._exit(1) elif self._hook.exception: print("Check failed to run: %s, %s" % (sys.last_type, traceback.format_tb(sys.last_traceback))) sys.stdout.flush() os._exit(2)
class SensuPlugin(object): def __init__(self): self.settings = {} self.config_files = [] self.get_settings() self.plugin_info = { 'check_name': None, 'message': None, 'status': None } self._hook = ExitHook() self._hook.hook() self.exit_code = ExitCode(0, 1, 2, 3) for field in self.exit_code._fields: self.__make_dynamic(field) atexit.register(self.__exitfunction) self.parser = argparse.ArgumentParser() if hasattr(self, 'setup'): self.setup() (self.options, self.remain) = self.parser.parse_known_args() self.run() def get_json(self, file_handler): all_data = json.load(file_handler) for key in all_data.iteritems(): self.settings[key[0]] = key[1] def get_settings(self): if 'SENSU_CONFIG_FILE' in os.environ: env_var = os.environ['SENSU_CONFIG_FILE'] self.config_files.append(env_var) else: self.config_files.append('/etc/sensu/config.json') self.config_files.append('/etc/sensu/conf.d/') for config_file in self.config_files: if os.path.isfile(config_file): with open(config_file) as f_handler: self.get_json(f_handler) elif os.path.isdir(config_file): for _, _, files in os.walk(config_file): for f_file in files: f_path = config_file+f_file if f_path.endswith('.json'): with open(f_path) as f_handler: self.get_json(f_handler) def output(self, args): print("SensuPlugin: %s" % ' '.join(str(a) for a in args)) def __make_dynamic(self, method): def dynamic(*args): self.plugin_info['status'] = method if len(args) == 0: args = None self.output(args) sys.exit(getattr(self.exit_code, method)) method_lc = method.lower() dynamic.__doc__ = "%s method" % method_lc dynamic.__name__ = method_lc setattr(self, dynamic.__name__, dynamic) def run(self): self.warning("Not implemented! You should override SensuPlugin.run()") def __exitfunction(self): if self._hook.exit_code is None and self._hook.exception is None: print("Check did not exit! You should call an exit code method.") sys.stdout.flush() os._exit(1) elif self._hook.exception: print("Check failed to run: %s, %s" % (sys.last_type, traceback.format_tb(sys.last_traceback))) sys.stdout.flush() os._exit(2)