def check_server_url(url, url_list=[]): """ check if the url provided is a valid server-status page Input: url string: the url provided by the user url_list []string: list of url user has monitored already Output: True if the user provides a valid url False otherwise """ ret_val = False if not p_utils.check_url(url, url_list): return False res = utils.get_command_output('curl -s {url}'.format(url=url)) status = check_apache_server_status(res) if status is None: utils.print_warn( 'The url you have provided ' 'does not seem to be the correct server_status ' 'page. Incorrect server-status will not be ' 'recorded.') utils.cprint() record = utils.ask( 'Would you like to record this url anyway?', 'no') if record: ret_val = True else: utils.cprint( 'Monitoring {}'.format(status)) ret_val = True return ret_val
def check_server_url(url, url_list=[]): """ check if the url provided is a valid server-status page Input: url string: the url provided by the user url_list []string: list of url user has monitored already Output: ret_val bool: True if user provides a valid url False otherwise """ ret_val = False if not p_utils.check_url(url, url_list): return False res = utils.get_command_output('curl -s {url}'.format(url=url)) status = check_nginx_status(res) if not status: utils.print_warn('The url you have provided ' 'does not seem to be the correct server_status ' 'page. Incorrect server-status will not be ' 'recorded.') record = utils.ask('Would you like to record this url anyway?', 'no') if record: ret_val = True else: ret_val = True return ret_val
def check_dependency(): utils.print_step('Checking dependency') utils.print_step(' Checking http_stub_status_module') cmd_res = utils.get_command_output('nginx -V 2>&1 | grep -o ' 'with-http_stub_status_module') if cmd_res is None: utils.print_warn('http_stub_status_module is not enabled.\n' 'This module is required to enable the ' 'nginx-status page.') self.raise_error('http_stub_status_module') utils.print_success()
def main(): """ Provides usage guide and prompt the user for choice of installers """ check_version() parser = argparse.ArgumentParser( prog='python -m python_installer.gather_metrics', description='Python Configuration Installer') # os arg parser.add_argument( '--os', choices=['DEBIAN', 'REDHAT'], required=True, help='Operating system (DEBIAN|REDHAT)', metavar='(DEBIAN|REDHAT)') # agent arg parser.add_argument( '--agent', choices=['COLLECTD', 'TELEGRAF'], required=True, help='Agent (COLLECTD|TELEGRAF)', metavar='(COLLECTD|TELEGRAF)') # app_dir parser.add_argument( '--app_dir', required=True, help='The location of WF-PCInstaller where setup.py resides.') # log_file parser.add_argument( '--log_file', required=True, help='Errors will be logged to this file.') # debug parser.add_argument( '--debug', action='store_true', default=False, help='Turn debug flag on.') # --TEST parser.add_argument( '--TEST', action='store_true', default=False, help='Installs all detected applications with default setting.\n' 'This is for integeration test. Default is false.') args = parser.parse_args() config.OPERATING_SYSTEM = args.os config.AGENT = args.agent config.APP_DIR = args.app_dir config.INSTALL_LOG = args.log_file config.DEBUG = args.debug config.TEST = args.TEST if config.AGENT == config.COLLECTD: conf.check_collectd_exists() conf.check_collectd_path() if config.DEBUG: utils.print_warn('DEBUG IS ON') (app_list, app_dict) = detect_applications() try: if config.TEST: test_installer(app_list, app_dict) else: # if at least one installer is ran to completion, # then exit with success if installer_menu(app_list, app_dict): sys.exit(0) else: sys.exit(1) except KeyboardInterrupt: utils.eprint('\nQuitting the installer via keyboard interrupt.') sys.exit(1)
def installer_menu(app_list, support_dict): """ provide the menu and prompts for the user to interact with the installer Input: app_list []: list of app detected that plugin support support_dict {}: dict that associates with each app in app_list Output: count int: the number of successful installation installer flow: 1. show selections 2. installs a plugin 3. updates install state 4. menu changes with install state Sample menu: Option Name State Date (1) mysql Installed (installation date) (2) apache Incomplete (3) postgres New if resinstall, warn that this will overwrite {conf_file}. """ exit_cmd = [ 'quit', 'q', 'exit'] count = 0 # the format of each row in installing menu menu_rowf = ( '{index:{index_pad}} {name:{name_pad}} ' '{state:{state_pad}} {date}') # padding of each parameter index_pad = 7 name_pad = 30 state_pad = 12 color = utils.BLACK # default color res = None # to begin the prompt loop utils.cprint() utils.cprint( 'We have detected the following applications that are ' 'supported by our configurators.') while res not in exit_cmd: utils.cprint() utils.cprint( 'The following are the available configurators:') utils.cprint( menu_rowf.format( index='Option', index_pad=index_pad, name='Name', name_pad=name_pad, state='State', state_pad=state_pad, date='Date')) # install_state_dict contains other agents' state as well install_state_dict = check_install_state(app_list) install_state = install_state_dict[config.AGENT] for i, app in enumerate(app_list): # formatted string for menu index = '({i})'.format(i=i) app_installer = '{app}'.format(app=app) app_state = install_state[app]['state'] if 'date' in install_state[app]: date = install_state[app]['date'] else: date = '' if app_state == NEW: state = 'New' color = utils.GREEN elif app_state == INCOMPLETE: state = 'Incomplete' color = utils.YELLOW elif app_state == INSTALLED: state = 'Installed' color = utils.BLACK utils.print_color_msg( menu_rowf.format( index=index, index_pad=index_pad, name=app_installer, name_pad=name_pad, state=state, state_pad=state_pad, date=date), color) utils.cprint() utils.cprint( 'To pick a configurator, type in the corresponding number ' 'next to the configurator.\n' 'To quit out of this installer, type "[Q]uit" or "exit".') res = utils.get_input( 'Which installer would you like to run?').lower() if res not in exit_cmd: # check if option is within the array list option = check_option(res, len(app_list)) if option is not None: app = app_list[option] app_dict = support_dict[app] utils.cprint( 'You have selected ({option}) {app} installer'.format( option=option, app=app)) app_state = install_state[app]['state'] if app_state == INSTALLED: utils.print_warn( 'You have previously used this configurator\n' 'Reinstalling will overwrite the old configuration ' 'file, {}.'.format(app_dict['conf_name'])) confirm = utils.ask( 'Would you like to proceed with ' 'the configurator?') if confirm: if run_installer(config.AGENT, app_dict): install_state[app]['state'] = INSTALLED count += 1 else: install_state[app]['state'] = INCOMPLETE install_state[app]['date'] = '{:%c}'.format( datetime.now()) update_install_state(config.AGENT, install_state_dict) else: utils.print_reminder('Invalid option.') return count
def check_collectd_path(): utils.print_step(' Checking if collectd is installed in our specified ' 'directory') res = utils.check_path_exists(config.COLLECTD_HOME) if not res: sys.stderr.write('Collectd was not found at our ' 'default installation folder. ' 'If you need help with configuration, please ' 'contact [email protected]\n') sys.exit(1) utils.print_success() def check_collectd_conf_dir(): """ Check if managed_config directory exists, if not, create one. """ res = utils.check_path_exists(config.COLLECTD_CONF_DIR) if not res: utils.cprint('Creating collectd managed config dir') utils.call_command('mkdir ' + config.COLLECTD_CONF_DIR) if __name__ == '__main__': utils.print_warn('This is for testing conf_collected_plugin.py') utils.cprint(config.COLLECTD_HOME) utils.cprint(config.COLLECTD_CONF_DIR)
def check_collectd_path(): utils.print_step( ' Checking if collectd is installed in our specified ' 'directory') res = utils.check_path_exists(config.COLLECTD_HOME) if not res: sys.stderr.write( 'Collectd was not found at our ' 'default installation folder. ' 'If you need help with configuration, please ' 'contact [email protected]\n') sys.exit(1) utils.print_success() def check_collectd_conf_dir(): """ Check if managed_config directory exists, if not, create one. """ res = utils.check_path_exists(config.COLLECTD_CONF_DIR) if not res: utils.cprint('Creating collectd managed config dir') utils.call_command('mkdir ' + config.COLLECTD_CONF_DIR) if __name__ == '__main__': utils.print_warn('This is for testing conf_collected_plugin.py') utils.cprint(config.COLLECTD_HOME) utils.cprint(config.COLLECTD_CONF_DIR)
def output_config(self, data, out): if not data: return False metrics = (' Verbose false\n' ' # Catch Redis metrics (prefix with Redis_)\n' ' Redis_uptime_in_seconds "gauge"\n' ' Redis_used_cpu_sys "counter"\n' ' Redis_used_cpu_user "counter"\n' ' Redis_used_cpu_sys_children "counter"\n' ' Redis_used_cpu_user_children "counter"\n' ' Redis_uptime_in_days "gauge"\n' ' Redis_lru_clock "counter"\n' ' Redis_connected_clients "gauge"\n' ' Redis_connected_slaves "gauge"\n' ' Redis_client_longest_output_list "gauge"\n' ' Redis_client_biggest_input_buf "gauge"\n' ' Redis_blocked_clients "gauge"\n' ' Redis_expired_keys "counter"\n' ' Redis_evicted_keys "counter"\n' ' Redis_rejected_connections "counter"\n' ' Redis_used_memory "bytes"\n' ' Redis_used_memory_rss "bytes"\n' ' Redis_used_memory_peak "bytes"\n' ' Redis_used_memory_lua "bytes"\n' ' Redis_mem_fragmentation_ratio "gauge"\n' ' Redis_changes_since_last_save "gauge"\n' ' Redis_instantaneous_ops_per_sec "gauge"\n' ' Redis_rdb_bgsave_in_progress "gauge"\n' ' Redis_total_connections_received "counter"\n' ' Redis_total_commands_processed "counter"\n' ' Redis_total_net_input_bytes "counter"\n' ' Redis_total_net_output_bytes "counter"\n' ' Redis_keyspace_hits "derive"\n' ' Redis_keyspace_misses "derive"\n' ' Redis_latest_fork_usec "gauge"\n' ' Redis_connected_slaves "gauge"\n' ' Redis_repl_backlog_first_byte_offset "gauge"\n' ' Redis_master_repl_offset "gauge"\n') slave_metrics = (' #Slave-Only' ' Redis_master_last_io_seconds_ago "gauge"\n' ' Redis_slave_repl_offset "gauge"\n') utils.print_step('Begin writing redis configuration for collectd') # pull comment and append to it if not self.pull_comment(out): utils.print_warn('Failed to pull comment.') out.write('\n\n' '<LoadPlugin python>\n' ' Globals true\n' '</LoadPlugin>\n\n' '<Plugin python>\n' ' ModulePath "{path}"\n' ' Import "redis_info"\n'.format( path=config.COLLECTD_PYTHON_PLUGIN_PATH)) for instance in data: plugin_instance = (' Instance "{iname}"\n' ' Host "{host}"\n' ' Port "{port}"\n').format( iname=instance, host=data[instance]['host'], port=data[instance]['port']) if 'auth' in data[instance]: plugin_instance += (' Auth "{auth}"\n'.format( auth=data[instance]['auth'])) out.write('\n <Module redis_info>\n' '{plugin_instance}' '{metrics}'.format(plugin_instance=plugin_instance, metrics=metrics)) if 'slave' in data[instance]: out.write(slave_metrics) out.write(' </Module>\n') out.write('</Plugin>\n') return True
def output_config(self, data, out): if not data: return False metrics = ( ' Verbose false\n' ' # Catch Redis metrics (prefix with Redis_)\n' ' Redis_uptime_in_seconds "gauge"\n' ' Redis_used_cpu_sys "counter"\n' ' Redis_used_cpu_user "counter"\n' ' Redis_used_cpu_sys_children "counter"\n' ' Redis_used_cpu_user_children "counter"\n' ' Redis_uptime_in_days "gauge"\n' ' Redis_lru_clock "counter"\n' ' Redis_connected_clients "gauge"\n' ' Redis_connected_slaves "gauge"\n' ' Redis_client_longest_output_list "gauge"\n' ' Redis_client_biggest_input_buf "gauge"\n' ' Redis_blocked_clients "gauge"\n' ' Redis_expired_keys "counter"\n' ' Redis_evicted_keys "counter"\n' ' Redis_rejected_connections "counter"\n' ' Redis_used_memory "bytes"\n' ' Redis_used_memory_rss "bytes"\n' ' Redis_used_memory_peak "bytes"\n' ' Redis_used_memory_lua "bytes"\n' ' Redis_mem_fragmentation_ratio "gauge"\n' ' Redis_changes_since_last_save "gauge"\n' ' Redis_instantaneous_ops_per_sec "gauge"\n' ' Redis_rdb_bgsave_in_progress "gauge"\n' ' Redis_total_connections_received "counter"\n' ' Redis_total_commands_processed "counter"\n' ' Redis_total_net_input_bytes "counter"\n' ' Redis_total_net_output_bytes "counter"\n' ' Redis_keyspace_hits "derive"\n' ' Redis_keyspace_misses "derive"\n' ' Redis_latest_fork_usec "gauge"\n' ' Redis_connected_slaves "gauge"\n' ' Redis_repl_backlog_first_byte_offset "gauge"\n' ' Redis_master_repl_offset "gauge"\n') slave_metrics = ( ' #Slave-Only' ' Redis_master_last_io_seconds_ago "gauge"\n' ' Redis_slave_repl_offset "gauge"\n') utils.print_step('Begin writing redis configuration for collectd') # pull comment and append to it if not self.pull_comment(out): utils.print_warn('Failed to pull comment.') out.write( '\n\n' '<LoadPlugin python>\n' ' Globals true\n' '</LoadPlugin>\n\n' '<Plugin python>\n' ' ModulePath "{path}"\n' ' Import "redis_info"\n'.format( path=config.COLLECTD_PYTHON_PLUGIN_PATH)) for instance in data: plugin_instance = ( ' Instance "{iname}"\n' ' Host "{host}"\n' ' Port "{port}"\n').format( iname=instance, host=data[instance]['host'], port=data[instance]['port']) if 'auth' in data[instance]: plugin_instance += ( ' Auth "{auth}"\n'.format( auth=data[instance]['auth'])) out.write( '\n <Module redis_info>\n' '{plugin_instance}' '{metrics}'.format( plugin_instance=plugin_instance, metrics=metrics)) if 'slave' in data[instance]: out.write(slave_metrics) out.write(' </Module>\n') out.write('</Plugin>\n') return True