def _apply_default_config(self): if self.config.has_option('postgres', 'interval'): interval = self.fetch('postgres', 'interval') else: interval = None for plugin in Plugin.only_child_subclasses(): plugin.set_default_config(self.config, interval)
def _check_interval(self): for plugin in Plugin.only_child_subclasses(): if not self.config.has_option(plugin.__name__.lower(), 'interval'): self.config.set(plugin.__name__.lower(), 'interval', '{0}'.format(plugin.Interval))
def _apply_default_config(self): for plugin in Plugin.only_child_subclasses(): plugin.set_default_config(self.config)
def start(): def quit_handler(_signo=None, _stack_frame=None): logging.info("Bye bye!") sys.exit(0) signal.signal(signal.SIGTERM, quit_handler) if platform.LINUX: signal.signal(signal.SIGQUIT, quit_handler) commands = sys.argv[1:] if len(commands) > 0: tool = commands[0] if tool == '-h' or tool == '--help': print_total_help() elif tool == 'bootstrap': from mamonsu.tools.bootstrap.start import run_deploy sys.argv.remove('bootstrap') return run_deploy() elif tool == 'report': from mamonsu.tools.report.start import run_report sys.argv.remove('report') return run_report() elif tool == 'tune': from mamonsu.tools.tune.start import run_tune sys.argv.remove('tune') return run_tune() elif tool == 'zabbix': from mamonsu.tools.zabbix_cli.start import run_zabbix sys.argv.remove('zabbix') return run_zabbix() elif tool == 'agent': from mamonsu.tools.agent.start import run_agent sys.argv.remove('agent') return run_agent() elif tool == 'upload': args, commands = parse_args() if not args.zabbix_address: print('Option --zabbix-address is missing') sys.exit(2) if not os.path.isfile(args.zabbix_file): print('Cannot find zabbix file with metric to upload. Check path in --zabbix-file option.') sys.exit(2) cfg = Config(args.config_file, args.plugins_dirs) cfg.config.set('zabbix', 'address', args.zabbix_address) cfg.config.set('zabbix', 'port', args.zabbix_port) cfg.config.set('zabbix', 'client', args.zabbix_client) cfg.config.set('log', 'level', args.zabbix_log_level) supervisor = Supervisor(cfg) supervisor.send_file_zabbix(cfg, args.zabbix_file) sys.exit(0) elif tool == 'export': args, commands = parse_args() # get PG version Plugin.VersionPG = define_pg_version(args.pg_version) cfg = Config(args.config_file, args.plugins_dirs) if args.old_zabbix: Plugin.old_zabbix = True if not len(commands) == 2 and not len(commands) == 3: print_total_help() if commands[1] == 'zabbix-parameters': # zabbix agent keys generation Plugin.Type = 'agent' # change plugin type for template generator plugins = [] if len(commands) == 2: commands.append('postgrespro_agent.conf') for klass in Plugin.only_child_subclasses(): if klass.__name__ != "PgWaitSampling" and klass.__name__ != "Cfs": plugins.append(klass(cfg)) args.plugin_type = correct_plugin_type(args.plugin_type) if args.plugin_type == 'pg' or args.plugin_type == 'sys' or args.plugin_type == 'all': template = GetKeys() # write conf file try: fd = codecs.open(commands[2], 'w', 'utf-8') fd.write(template.txt(args.plugin_type, plugins)) # pass command type print("Configuration file for zabbix-agent has been saved as {0}".format(commands[2])) except (IOError, EOFError) as e: print(" {0} ".format(e)) sys.exit(2) # write bash scripts for zabbix - agent to a file # check if conf file has a path len_path = commands[2].rfind("/") # get path for conf file and scripts if len_path != -1: path = commands[2][:len_path] + "/scripts" Plugin.PATH = path else: path = os.getcwd() + "/scripts" Plugin.PATH = path # create directory for scripts along the path of conf file if needed if not os.path.exists(path): os.makedirs(path) for key in Scripts.Bash: with codecs.open(path + "/" + key + ".sh", 'w+', 'utf-8') as f: # configuration file for zabbix-agent is generated for selected plugin-type f.write(Scripts.Bash[key]) # pass script itself os.chmod(path + "/" + key + ".sh", 0o755) print("Bash scripts for native zabbix-agent have been saved to {0}".format(path)) else: print("Got wrong plugin types. See help 'mamonsu -- help' ") sys.exit(2) sys.exit(0) elif commands[1] == 'config': # if no name for conf, save to postgrespro.conf if len(commands) == 2: commands.append('postgrespro.conf') try: fd = open(commands[2], 'w') cfg.config.write(fd) print("Configuration file for mamonsu has been saved as {0}".format(commands[2])) sys.exit(0) except (IOError, EOFError) as e: print(" {0} ".format(e)) sys.exit(2) elif commands[1] == 'template': plugins = [] if len(commands) == 2: commands.append('postgrespro.xml') for klass in Plugin.only_child_subclasses(): plugins.append(klass(cfg)) template = ZbxTemplate(args.template, args.application) try: fd = codecs.open(commands[2], 'w', 'utf-8') fd.write(template.xml("all", plugins)) # set type to 'all' for mamonsu print('Template for mamonsu has been saved as {file}'.format(file=commands[2])) sys.exit(0) except (IOError, EOFError) as e: print(" {0} ".format(e)) sys.exit(2) elif commands[1] == 'zabbix-template': Plugin.Type = 'agent' # change plugin type for template generator if len(commands) == 2: commands.append('postgrespro_agent.xml') plugins = [] args.plugin_type = correct_plugin_type(args.plugin_type) if args.plugin_type == 'pg' or args.plugin_type == 'sys' or args.plugin_type == 'all': for klass in Plugin.only_child_subclasses(): if klass.__name__ != "PgWaitSampling" and klass.__name__ != "Cfs": # check if plugin is for EE plugins.append(klass(cfg)) template = ZbxTemplate(args.template, args.application) try: fd = codecs.open(commands[2], 'w', 'utf-8') fd.write(template.xml(args.plugin_type, plugins)) print('Template for zabbix-agent has been saved as {file}'.format(file=commands[2])) sys.exit(0) except (IOError, EOFError) as e: print(" {0} ".format(e)) sys.exit(2) else: print("Got wrong plugin types. See help 'mamonsu -- help' ") sys.exit(2) else: print_total_help() args, commands = parse_args() if len(commands) > 0: print_total_help() cfg = Config(args.config_file, args.plugins_dirs) # simple daemon if args.daemon: try: pid = os.fork() if pid > 0: sys.exit(0) except Exception as e: sys.stderr.write('Can\'t fork: {0}\n'.format(e)) sys.exit(2) # write pid-file if args.pid is not None: try: with open(args.pid, 'w') as pidfile: pidfile.write(str(os.getpid())) except Exception as e: sys.stderr.write('Can\'t write pid file, error: %s\n'.format(e)) sys.exit(2) supervisor = Supervisor(cfg) try: logging.info("Start mamonsu") supervisor.start() except KeyboardInterrupt: quit_handler()
def start(): def quit_handler(_signo=None, _stack_frame=None): logging.info("Bye bye!") sys.exit(0) signal.signal(signal.SIGTERM, quit_handler) if platform.LINUX: signal.signal(signal.SIGQUIT, quit_handler) commands = sys.argv[1:] if len(commands) > 0: tool = commands[0] if tool == "-h" or tool == "--help": print_total_help() elif tool == "bootstrap": from mamonsu.tools.bootstrap.start import run_deploy sys.argv.remove("bootstrap") return run_deploy() elif tool == "report": from mamonsu.tools.report.start import run_report sys.argv.remove("report") return run_report() elif tool == "tune": from mamonsu.tools.tune.start import run_tune sys.argv.remove("tune") return run_tune() elif tool == "zabbix": from mamonsu.tools.zabbix_cli.start import run_zabbix sys.argv.remove("zabbix") return run_zabbix() elif tool == "agent": from mamonsu.tools.agent.start import run_agent sys.argv.remove("agent") return run_agent() elif tool == "export": args, commands = parse_args() cfg = Config(args.config_file, args.plugins_dirs) if not len(commands) == 3: print_total_help() elif commands[1] == "config": with open(commands[2], "w") as fd: cfg.config.write(fd) sys.exit(0) elif commands[1] == "template": plugins = [] for klass in Plugin.only_child_subclasses(): plugins.append(klass(cfg)) template = ZbxTemplate(args.template, args.application) with codecs.open(commands[2], "w", "utf-8") as f: f.write(template.xml(plugins)) sys.exit(0) else: print_total_help() args, commands = parse_args() if len(commands) > 0: print_total_help() cfg = Config(args.config_file, args.plugins_dirs) # simple daemon if args.daemon: try: pid = os.fork() if pid > 0: sys.exit(0) except Exception as e: sys.stderr.write("Can't fork: {0}\n".format(e)) sys.exit(2) # write pid-file if args.pid is not None: try: with open(args.pid, "w") as pidfile: pidfile.write(str(os.getpid())) except Exception as e: sys.stderr.write("Can't write pid file, error: %s\n".format(e)) sys.exit(2) supervisor = Supervisor(cfg) try: logging.info("Start mamonsu") supervisor.start() except KeyboardInterrupt: quit_handler()
def __init__(self): parser = optparse.OptionParser( usage='%prog [-c] [-p]', version='%prog {0}'.format(__version__)) parser.add_option( '-c', '--config', dest='config', default=None, help='Path to config file') parser.add_option( '-p', '--pid', dest='pid', default=None, help='Path to pid file') parser.add_option( '-w', '--write-config-file', dest='write_config_file', default=None, help='Write default config to file and exit') parser.add_option( '-e', '--export-template-file', dest='write_template_file', default=None, help='Write template to file and exit') parser.add_option( '-t', '--template', dest='template', default='PostgresPro-{0}'.format(sys.platform.title()), help='Generated template name') parser.add_option( '-a', '--application', dest='application', default='App-PostgresPro-{0}'.format(sys.platform.title()), help='Application for generated template') if platform.WINDOWS: SERVICE_NAME = 'mamonsu' SERVICE_DESC = 'Zabbix active agent mamonsu' parser.add_option( '-r', '--register', dest='register', default=None, help='Register as windows service with params') parser.add_option( '-u', '--unregister', dest='unregister', action='store_true', default=False, help='Unregister windows service') args, _ = parser.parse_args() config = configparser.ConfigParser() config.add_section('template') config.set('template', 'name', args.template) config.set('template', 'application', args.application) config.add_section('postgres') config.set('postgres', 'user', Config.default_user()) config.set('postgres', 'password', str(Config.default_pgpassword())) config.set('postgres', 'database', Config.default_db()) config.set('postgres', 'host', Config.default_host()) config.set('postgres', 'port', str(Config.default_port())) config.set('postgres', 'query_timeout', '10') config.add_section('log') config.set('log', 'file', str(None)) config.set('log', 'level', 'INFO') config.set( 'log', 'format', '[%(levelname)s] %(asctime)s - %(name)s\t-\t%(message)s') config.add_section('zabbix') config.set('zabbix', 'client', socket.gethostname()) config.set('zabbix', 'address', '127.0.0.1') config.set('zabbix', 'port', str(10051)) config.add_section('sender') config.set('sender', 'queue', str(300)) config.add_section('health') config.set('health', 'uptime', str(60 * 10 * 100)) config.set('health', 'cache', str(80)) config.add_section('bgwriter') config.set('bgwriter', 'max_checkpoints_req', '5') if args.config and not os.path.isfile(args.config): print('Config file {0} not found'.format(args.config)) sys.exit(1) else: if args.config is not None: config.read(args.config) self.config = config logging.basicConfig( format=self.fetch('log', 'format', raw=True), filename=self.fetch('log', 'file'), level=self.get_logger_level(self.fetch('log', 'level'))) if args.write_config_file is not None: with open(args.write_config_file, 'w') as configfile: config.write(configfile) sys.exit(0) if args.write_template_file is not None: plugins = [] PluginLoader.load() for klass in Plugin.__subclasses__(): plugins.append(klass(self)) template = ZbxTemplate(args.template, args.application) with open(args.write_template_file, 'w') as templatefile: templatefile.write(template.xml(plugins)) sys.exit(0) if platform.WINDOWS: from mamonsu.lib.win32service import ServiceControlManagerContext from mamonsu.lib.win32service import ServiceType, ServiceStartType if args.register: with ServiceControlManagerContext() as scm: scm.create_service( SERVICE_NAME, SERVICE_DESC, ServiceType.WIN32_OWN_PROCESS, ServiceStartType.AUTO, '{0} {1}'.format(__file__, args.register), ) sys.exit(0) if args.unregister: with ServiceControlManagerContext() as scm: scm.delete_service(SERVICE_NAME) sys.exit(0) if args.pid is not None: with open(args.pid, 'w') as pidfile: pidfile.write(str(os.getpid())) os.environ['PGUSER'] = self.fetch('postgres', 'user') if self.fetch('postgres', 'password'): os.environ['PGPASSWORD'] = self.fetch('postgres', 'password') os.environ['PGHOST'] = self.fetch('postgres', 'host') os.environ['PGPORT'] = str(self.fetch('postgres', 'port')) os.environ['PGDATABASE'] = self.fetch('postgres', 'database') os.environ['PGTIMEOUT'] = self.fetch('postgres', 'query_timeout')
def _load_plugins(self): PluginLoader.load() for klass in Plugin.__subclasses__(): plugin = klass(self.config) self.Plugins.append(plugin)
def start(): def quit_handler(_signo=None, _stack_frame=None): logging.info("Bye bye!") sys.exit(0) signal.signal(signal.SIGTERM, quit_handler) if platform.LINUX: signal.signal(signal.SIGQUIT, quit_handler) commands = sys.argv[1:] if len(commands) > 0: tool = commands[0] if tool == '-h' or tool == '--help': print_total_help() elif tool == 'bootstrap': from mamonsu.tools.bootstrap.start import run_deploy sys.argv.remove('bootstrap') return run_deploy() elif tool == 'report': from mamonsu.tools.report.start import run_report sys.argv.remove('report') return run_report() elif tool == 'tune': from mamonsu.tools.tune.start import run_tune sys.argv.remove('tune') return run_tune() elif tool == 'zabbix': from mamonsu.tools.zabbix_cli.start import run_zabbix sys.argv.remove('zabbix') return run_zabbix() elif tool == 'agent': from mamonsu.tools.agent.start import run_agent sys.argv.remove('agent') return run_agent() elif tool == 'export': args, commands = parse_args() cfg = Config(args.config_file, args.plugins_dirs) if not len(commands) == 3: print_total_help() elif commands[1] == 'config': with open(commands[2], 'w') as fd: cfg.config.write(fd) sys.exit(0) elif commands[1] == 'template': plugins = [] for klass in Plugin.only_child_subclasses(): plugins.append(klass(cfg)) template = ZbxTemplate(args.template, args.application) with codecs.open(commands[2], 'w', 'utf-8') as f: f.write(template.xml(plugins)) sys.exit(0) else: print_total_help() args, commands = parse_args() if len(commands) > 0: print_total_help() cfg = Config(args.config_file, args.plugins_dirs) # simple daemon if args.daemon: try: pid = os.fork() if pid > 0: sys.exit(0) except Exception as e: sys.stderr.write('Can\'t fork: {0}\n'.format(e)) sys.exit(2) # write pid-file if args.pid is not None: try: with open(args.pid, 'w') as pidfile: pidfile.write(str(os.getpid())) except Exception as e: sys.stderr.write('Can\'t write pid file, error: %s\n'.format(e)) sys.exit(2) supervisor = Supervisor(cfg) try: logging.info("Start mamonsu") supervisor.start() except KeyboardInterrupt: quit_handler()
def start(): def quit_handler(_signo=None, _stack_frame=None): logging.info("Bye bye!") sys.exit(0) signal.signal(signal.SIGTERM, quit_handler) if platform.LINUX: signal.signal(signal.SIGQUIT, quit_handler) commands = sys.argv[1:] if len(commands) > 0: tool = commands[0] if tool == '-h' or tool == '--help': print_total_help() elif tool == 'bootstrap': from mamonsu.tools.bootstrap.start import run_deploy sys.argv.remove('bootstrap') return run_deploy() elif tool == 'report': from mamonsu.tools.report.start import run_report sys.argv.remove('report') return run_report() elif tool == 'tune': from mamonsu.tools.tune.start import run_tune sys.argv.remove('tune') return run_tune() elif tool == 'zabbix': from mamonsu.tools.zabbix_cli.start import run_zabbix sys.argv.remove('zabbix') return run_zabbix() elif tool == 'agent': from mamonsu.tools.agent.start import run_agent sys.argv.remove('agent') return run_agent() elif tool == 'export': args, commands = parse_args() # get PG version version_args = args.pg_version.split('_') define_pg_version(version_args) cfg = Config(args.config_file, args.plugins_dirs) if not len(commands) == 2 and not len(commands) == 3: print_total_help() if commands[1] == 'zabbix-parameters': # zabbix agent keys generation Plugin.Type = 'agent' # change plugin type for template generator plugins = [] if len(commands) == 2: commands.append('postgrespro_agent.conf') print('Configuration file for zabbix-agent have been saved in postgrespro_agent.conf file') for klass in Plugin.only_child_subclasses(): if klass.__name__ == "PgWaitSampling": # check if plugin is for EE if Plugin.VersionPG['type'] == 'PGEE': plugins.append(klass(cfg)) else: if klass.__name__ != "Cfs": plugins.append(klass(cfg)) args.plugin_type = correct_plugin_type(args.plugin_type) if args.plugin_type == 'pg' or args.plugin_type == 'sys' or args.plugin_type == 'all': # check if conf file has a path len_path = commands[2].rfind("/") # get path for conf file and scripts if len_path != -1: path = commands[2][:len_path] + "/scripts" Plugin.PATH = path else: path = os.getcwd() + "/scripts" Plugin.PATH = path # create directory for scripts along the path of conf file if needed if not os.path.exists(path): os.makedirs(path) print("Directory for scripts has created") template = GetKeys() # if no name for zabbix-parameters save to postgrespro.conf if commands[2].rfind("/") == len(commands[2]) - 1: commands[2] = commands[2][:-1] + "/postgrespro.conf" # write conf file with codecs.open(commands[2], 'w', 'utf-8') as f: f.write(template.txt(args.plugin_type, plugins)) # pass command type # write bash scripts for zabbix - agent to a file for key in Scripts.Bash: with codecs.open(path + "/" + key + ".sh", 'w+', 'utf-8') as f: # configuration file for zabbix-agent is generated for selected plugin-type f.write(Scripts.Bash[key]) # pass script itself os.chmod(path + "/" + key + ".sh", 0o755) print("Bash scripts for native zabbix-agent have been saved to {0}".format(path)) else: print("Got wrong plugin types. For help, see the message below") print_total_help() sys.exit(0) elif commands[1] == 'config': if len(commands) == 2: commands.append('postgrespro.conf') print('Configuration file for mamonsu have been saved in postgrespro.conf file') # if no name for conf, save to mamonsu.conf if commands[2].rfind("/") == len(commands[2]) - 1: commands[2] = commands[2][:-1] + "/mamonsu.conf" with open(commands[2], 'w') as fd: cfg.config.write(fd) sys.exit(0) elif commands[1] == 'template': plugins = [] if len(commands) == 2: commands.append('postgrespro.xml') print('Template for mamonsu have been saved in postgrespro.conf file') for klass in Plugin.only_child_subclasses(): if klass.__name__ == "PgWaitSampling": # check if plugin is for EE if Plugin.VersionPG['type'] == 'PGEE': plugins.append(klass(cfg)) else: plugins.append(klass(cfg)) template = ZbxTemplate(args.template, args.application) # if no name for template save to postgrespro.xml if commands[2].rfind("/") == len(commands[2]) - 1: commands[2] = commands[2][:-1] + "/postgrespro.xml" with codecs.open(commands[2], 'w', 'utf-8') as f: # template for mamonsu (zabbix-trapper) is generated for all available plugins f.write(template.xml("all", plugins)) # set type to 'all' for mamonsu sys.exit(0) elif commands[1] == 'zabbix-template': Plugin.Type = 'agent' # change plugin type for template generator if len(commands) == 2: commands.append('postgrespro_agent.xml') print('Template for zabbix-agent have been saved in postgrespro_agent.xml file') plugins = [] args.plugin_type = correct_plugin_type(args.plugin_type) if args.plugin_type == 'pg' or args.plugin_type == 'sys' or args.plugin_type == 'all': for klass in Plugin.only_child_subclasses(): if klass.__name__ == "PgWaitSampling": # check if plugin is for EE if Plugin.VersionPG['type'] == 'PGEE': plugins.append(klass(cfg)) else: if klass.__name__ != "Cfs": plugins.append(klass(cfg)) template = ZbxTemplate(args.template, args.application) # if no name for template save to postgrespro.xml if commands[2].rfind("/") == len(commands[2]) - 1: commands[2] = commands[2][:-1] + "/postgrespro.xml" with codecs.open(commands[2], 'w', 'utf-8') as f: # template for zabbix-agent is generated for selected plugin-type f.write(template.xml(args.plugin_type, plugins)) sys.exit(0) else: print("Got wrong plugin types. For help, see the message below") print_total_help() else: print_total_help() args, commands = parse_args() if len(commands) > 0: print_total_help() cfg = Config(args.config_file, args.plugins_dirs) # simple daemon if args.daemon: try: pid = os.fork() if pid > 0: sys.exit(0) except Exception as e: sys.stderr.write('Can\'t fork: {0}\n'.format(e)) sys.exit(2) # write pid-file if args.pid is not None: try: with open(args.pid, 'w') as pidfile: pidfile.write(str(os.getpid())) except Exception as e: sys.stderr.write('Can\'t write pid file, error: %s\n'.format(e)) sys.exit(2) supervisor = Supervisor(cfg) try: logging.info("Start mamonsu") supervisor.start() except KeyboardInterrupt: quit_handler()