Пример #1
0
    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))
Пример #2
0
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()
Пример #3
0
def check_install_state(app_list):
    """
    Given: a list of app name
    return: a list of app_name and their states

    Flow:
     - check file path exists
     - if exists:
           check each app against the fiel
           keep track of app and their states
    """
    file_not_found = False
    empty_state_dict = {}
    for app in app_list:
        empty_state_dict[app] = {}
        empty_state_dict[app]['state'] = NEW

    if config.DEBUG:
        file_path = config.INSTALL_STATE_FILE
    else:
        file_path = config.INSTALL_STATE_FILE_PATH
    file_not_found = not utils.check_path_exists(file_path)

    try:
        install_file = open(file_path)
    except (IOError, OSError) as e:
        file_not_found = True
    except Exception as e:
        utils.exit_with_message('Error: {}'.format(e))

    if file_not_found:
        return empty_state_dict

    try:
        data = json.load(install_file)
    finally:
        install_file.close()
    data = data['data']
    for app in app_list:
        if app not in data:
            data[app] = {}
            data[app]['state'] = NEW
    return data
Пример #4
0
    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()
Пример #5
0
def detect_applications():
    """
    Detect and install appropriate collectd plugin

    Current collectd plugin support:
    apache, cassandra, mysql, nginx, postgresql
    This function uses unix command ps -A and check whether
    the supported application is found.
    """

    if config.DEBUG:
        plugins_file = config.PLUGINS_FILE
    else:
        plugins_file = '{}/{}'.format(config.APP_DIR, config.PLUGINS_FILE)
    utils.print_step('Begin app detection')

    res = utils.get_command_output('ps -A')
    if res is None:
        utils.exit_with_message('Unable to read process off the machine')

    try:
        data_file = open(plugins_file)
    except (IOError, OSError) as e:
        utils.exit_with_message(e)
    except Exception as e:
        utils.exit_with_message('Unexpected error: {}.'.format(e))

    try:
        data = json.load(data_file)
    finally:
        data_file.close()

    support_dict = collections.OrderedDict(data['data'])
    support_list = []

    for app in support_dict:
        app_dict = support_dict[app]
        if check_app(res, app_dict):
            support_list.append(app)

    if len(support_list):
        return (support_list, support_dict)
    else:
        utils.cprint('No supported app plugin is detected')
        sys.exit(0)
Пример #6
0
    def check_dependency(self):
        """
        Apache checklist:
        - check curl
        - mod_status
        - extended status
        """

        utils.print_step('Checking dependency')
        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
        if self.os == config.DEBIAN:
            utils.print_step('  Checking if mod_status is enabled')
            cmd_res = utils.get_command_output('ls /etc/apache2/mods-enabled')
            if cmd_res is None:
                utils.eprint(
                    'Apache2 mods-enabled folder is not '
                    'found /etc/apache2/mods-enabled.')
                utils.print_failure()
            elif '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.print_failure()
                    utils.exit_with_message('a2enmod command was not found')
                utils.print_success()
        elif self.os == config.REDHAT:
            utils.cprint()
            utils.cprint(
                'To enable server status page for the apache web,\n'
                'ensure that mod_status.so module is enabled.\n'
                'This module is often enabled by default.\n'
                '"LoadModule status_module modules/mod_status.so"\n'
                'such line should be included in one of the conf files.\n')
            _ = utils.cinput('Press Enter to continue.')

        utils.cprint()
        utils.cprint(
            'In order to fully utilize the apache plugin with collectd,\n'
            'the ExtendedStatus setting needs be turned on.\n'
            'This setting can be turned on by having "ExtendedStatus on"\n'
            'in one of the .conf file.\n')

        utils.cprint(
            'If you have already enabled this status, '
            'answer "no" to the next question.\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?')

        if res:
            # dir changes depending on the system
            # tested on Ubuntu 14.04, RHEL 7.2
            if self.os == config.DEBIAN:
                conf_dir = '/etc/apache2/conf-enabled'
                app_name = 'apache2'
            elif self.os == config.REDHAT:
                conf_dir = '/etc/httpd/conf.modules.d'
                app_name = 'httpd'

            utils.print_step('Checking if ' + conf_dir + ' exists.')
            if utils.check_path_exists(conf_dir):
                # pull config file here
                utils.print_success()
                self.include_apache_es_conf(conf_dir)
                utils.cprint(
                    'extendedstatus.conf is now included in the '
                    '{0} dir.\n'.format(conf_dir))
                utils.print_step('Restarting apache')
                ret = utils.call_command(
                    'service {app_name} restart >> '
                    '{log} 2>&1'.format(
                        app_name=app_name,
                        log=config.INSTALL_LOG))
                if ret != 0:
                    self.raise_error(
                        'Failed to restart apache service.')
                utils.print_success()
            else:
                exit_with_message(
                    '{cond_dir} dir does not exist. Manual '
                    'set up is required. For help, please '
                    'consule [email protected]'.format(
                        conf_dir=conf_dir))
Пример #7
0
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))
Пример #8
0
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))