def clean_plugin_write(self): """ Prevent unfinished config file from being written into the directory Create and write to a temporary file. Use try catch block to call write_plugin function. If the configuration file is written successfully, then it will be copied over to the correct place. """ temp_file = 'wavefront_temp_{0}.conf'.format(utils.random_string(7)) out = utils.write_file(temp_file) error = False if out is None: utils.exit_with_message('') try: res = self.write_plugin(out) except KeyboardInterrupt as k: error = True raise k except Exception as e: utils.eprint( 'Error: {}\n' 'Unexpected flow.'.format(e)) error = True finally: out.close() if error: utils.eprint('\nClosing and removing temp file.\n') utils.call_command('rm ' + temp_file) raise KeyboardInterrupt # if there was at least one instance being monitor if res: utils.print_step('Copying the plugin file to the correct place') cp_cmd = ( 'cp {infile} {conf_dir}/{outfile}').format( infile=temp_file, conf_dir=config.COLLECTD_CONF_DIR, outfile=self.conf_name) ret = utils.call_command(cp_cmd) if ret == 0: utils.print_success() utils.cprint( '{} plugin has been written successfully.'.format( self.plugin_name)) utils.cprint( '{0} can be found at {1}.'.format( self.conf_name, config.COLLECTD_CONF_DIR)) else: utils.call_command('rm {}'.format(temp_file)) utils.exit_with_message('Failed to copy the plugin file.\n') else: utils.call_command('rm {}'.format(temp_file)) raise Exception('You did not provide any instance to monitor.\n') utils.call_command('rm {}'.format(temp_file))
def include_apache_es_conf(conf_dir): """ Assume extendedstatus.conf is unique to wavefront and writing over it. """ filename = 'extendedstatus.conf' out = utils.write_file('{conf_dir}/{filename}'.format(conf_dir=conf_dir, filename=filename)) if out is None: utils.exit_with_message('Unexpected error!') out.write('# ExtendedStatus controls whether Apache will generate ' '"full" status\n' '# information (ExtendedStatus On) ' 'or just basic information (ExtendedStatus\n' '# Off) when the "server-status" handler is called. ' 'The default is Off.\n' 'ExtendedStatus on') out.close()
def include_apache_es_conf(self, conf_dir): """ Assume extendedstatus.conf is unique to wavefront and writing over it. """ filename = 'extendedstatus.conf' out = utils.write_file('{conf_dir}/{filename}'.format( conf_dir=conf_dir, filename=filename)) if out is None: utils.exit_with_message('Unexpected error!') out.write( '# ExtendedStatus controls whether Apache will generate ' '"full" status\n' '# information (ExtendedStatus On) ' 'or just basic information (ExtendedStatus\n' '# Off) when the "server-status" handler is called. ' 'The default is Off.\n' 'ExtendedStatus on') out.close()
def install_apache_plugin(): install = check_install_state('Apache') print if not install: sys.stdout.write( 'This script has detected that you have apache installed and ' 'running.') res = utils.ask( 'Would you like to run the apache plugin installer to ' 'enable collectd to collect data from apache?') else: print 'You have previously installed this plugin.' res = utils.ask( 'Would you like to reinstall this plugin?', default='no') if not res: return apache_title() print sys.stdout.write( 'To enable collectd plugin with Apache, the following ' 'steps need to be taken:\n' '1. mod_status for apache needs to be enabled. (Default is enabled)\n' '2. ExtendedStatus needs to be turned on. (Default is off)\n' '3. Enable the server-status handler for each virtual host.\n') _ = raw_input('Press Enter to continue') utils.print_step('Begin collectd Apache plugin installer') # check point # - check dependency # - change system file # - check mod status # - TODO: pull template # - prompt for user information if not utils.command_exists('curl'): utils.exit_with_failure('Curl is needed for this plugin.') # ubuntu check # Assumption: # -latest apache2 is installed and the installation # -directory is in the default place utils.print_step(' Checking if mod_status is enabled') cmd_res = utils.get_command_output('ls /etc/apache2/mods-enabled') if 'status.conf' not in cmd_res or 'status.load' not in cmd_res: utils.print_step('Enabling apache2 mod_status module.') ret = utils.call_command('sudo a2enmod status') if ret != 0: utils.exit_with_message('a2enmod command was not found') utils.print_success() print sys.stdout.write( 'In order to enable the apache plugin with collectd, the ' 'ExtendedStatus setting must be turned on.\n' 'This setting can be turned on by having "ExtendedStatus on" ' 'in one of the .conf file.\n') sys.stdout.write( 'If you have already enabled this status, ' 'answer "no" to the next question ' 'and ignore the following warning.\n' 'If you would like us to enable this status, answer "yes" and we will ' 'include a extendedstatus.conf file in your apache folder.\n') res = utils.ask( 'Would you like us to enable ' 'the ExtendedStatus?') # missing the flow where user wants to turn on the setting themselves. if res: # include the config file in /apache2/conf-enabled conf_dir = '/etc/apache2/conf-enabled' utils.print_step('Checking if ' + conf_dir + ' exists.') if utils.check_path_exists(conf_dir): # pull config file here utils.print_success() include_apache_es_conf(conf_dir) sys.stdout.write( '\nextendedstatus.conf is now included in the ' + conf_dir + ' dir.\n') utils.print_step('Restarting apache') ret = utils.call_command( 'service apache2 restart >> ' + config.INSTALL_LOG + ' 2>&1') if ret != 0: utils.exit_with_message('Failed to restart apache service.') utils.print_success() else: exit_with_message(conf_dir + ' dir does not exist, ' + 'please consult [email protected]' + 'for help.') else: utils.print_warn( 'Collectd plugin will not work with apache if the ' 'ExtendedStatus is not turned on.') # Begin writing apache plugin print utils.print_step('Begin writing apache plugin for collectd') plugin_file = 'wavefront_temp_apache.conf' out = utils.write_file(plugin_file) error = False if out is None: utils.exit_with_message('') try: res = write_apache_plugin(out) except: sys.stderr.write('Unexpected flow.\n') error = True finally: out.close() if error: sys.stderr.write('Closing and removing temp file.\n') utils.call_command('rm ' + plugin_file) sys.exit(1) # if there was at least one instance being monitor if res: utils.print_step('Copying the plugin file to the correct place') outfile = 'wavefront_apache.conf' cp_cmd = ( 'cp {infile} {conf_dir}/{outfile}').format( infile=plugin_file, conf_dir=COLLECTD_CONF_DIR, outfile=outfile) ret = utils.call_command(cp_cmd) if ret == 0: utils.print_success() print 'Apache_plugin has been written successfully.' sys.stdout.write( 'wavefront_apache.conf can be found at %s.\n' % COLLECTD_CONF_DIR) else: exit_with_message('Failed to copy the plugin file.\n') else: sys.stdout.write('You did not provide any instance to monitor.\n') utils.call_command('rm {}'.format(plugin_file))
def install_apache_plugin(): install = check_install_state('Apache') print if not install: sys.stdout.write( 'This script has detected that you have apache installed and ' 'running.') res = utils.ask('Would you like to run the apache plugin installer to ' 'enable collectd to collect data from apache?') else: print 'You have previously installed this plugin.' res = utils.ask('Would you like to reinstall this plugin?', default='no') if not res: return apache_title() print sys.stdout.write( 'To enable collectd plugin with Apache, the following ' 'steps need to be taken:\n' '1. mod_status for apache needs to be enabled. (Default is enabled)\n' '2. ExtendedStatus needs to be turned on. (Default is off)\n' '3. Enable the server-status handler for each virtual host.\n') _ = raw_input('Press Enter to continue') utils.print_step('Begin collectd Apache plugin installer') # check point # - check dependency # - change system file # - check mod status # - TODO: pull template # - prompt for user information if not utils.command_exists('curl'): utils.exit_with_failure('Curl is needed for this plugin.') # ubuntu check # Assumption: # -latest apache2 is installed and the installation # -directory is in the default place utils.print_step(' Checking if mod_status is enabled') cmd_res = utils.get_command_output('ls /etc/apache2/mods-enabled') if 'status.conf' not in cmd_res or 'status.load' not in cmd_res: utils.print_step('Enabling apache2 mod_status module.') ret = utils.call_command('sudo a2enmod status') if ret != 0: utils.exit_with_message('a2enmod command was not found') utils.print_success() print sys.stdout.write( 'In order to enable the apache plugin with collectd, the ' 'ExtendedStatus setting must be turned on.\n' 'This setting can be turned on by having "ExtendedStatus on" ' 'in one of the .conf file.\n') sys.stdout.write( 'If you have already enabled this status, ' 'answer "no" to the next question ' 'and ignore the following warning.\n' 'If you would like us to enable this status, answer "yes" and we will ' 'include a extendedstatus.conf file in your apache folder.\n') res = utils.ask('Would you like us to enable ' 'the ExtendedStatus?') # missing the flow where user wants to turn on the setting themselves. if res: # include the config file in /apache2/conf-enabled conf_dir = '/etc/apache2/conf-enabled' utils.print_step('Checking if ' + conf_dir + ' exists.') if utils.check_path_exists(conf_dir): # pull config file here utils.print_success() include_apache_es_conf(conf_dir) sys.stdout.write('\nextendedstatus.conf is now included in the ' + conf_dir + ' dir.\n') utils.print_step('Restarting apache') ret = utils.call_command('service apache2 restart >> ' + config.INSTALL_LOG + ' 2>&1') if ret != 0: utils.exit_with_message('Failed to restart apache service.') utils.print_success() else: exit_with_message(conf_dir + ' dir does not exist, ' + 'please consult [email protected]' + 'for help.') else: utils.print_warn('Collectd plugin will not work with apache if the ' 'ExtendedStatus is not turned on.') # Begin writing apache plugin print utils.print_step('Begin writing apache plugin for collectd') plugin_file = 'wavefront_temp_apache.conf' out = utils.write_file(plugin_file) error = False if out is None: utils.exit_with_message('') try: res = write_apache_plugin(out) except: sys.stderr.write('Unexpected flow.\n') error = True finally: out.close() if error: sys.stderr.write('Closing and removing temp file.\n') utils.call_command('rm ' + plugin_file) sys.exit(1) # if there was at least one instance being monitor if res: utils.print_step('Copying the plugin file to the correct place') outfile = 'wavefront_apache.conf' cp_cmd = ('cp {infile} {conf_dir}/{outfile}').format( infile=plugin_file, conf_dir=COLLECTD_CONF_DIR, outfile=outfile) ret = utils.call_command(cp_cmd) if ret == 0: utils.print_success() print 'Apache_plugin has been written successfully.' sys.stdout.write('wavefront_apache.conf can be found at %s.\n' % COLLECTD_CONF_DIR) else: exit_with_message('Failed to copy the plugin file.\n') else: sys.stdout.write('You did not provide any instance to monitor.\n') utils.call_command('rm {}'.format(plugin_file))