def check_collectd_conf_dir():
    """
    Check if managed_config directory exists, if not,
    create one.
    """
    res = utils.check_path_exists(COLLECTD_CONF_DIR)
    if not res:
        print 'Creating collectd managed config dir'
        utils.call_command('mkdir ' + COLLECTD_CONF_DIR)
def check_collectd_conf_dir():
    """
    Check if managed_config directory exists, if not,
    create one.
    """
    res = utils.check_path_exists(COLLECTD_CONF_DIR)
    if not res:
        print 'Creating collectd managed config dir'
        utils.call_command('mkdir ' + COLLECTD_CONF_DIR)
Example #3
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))
Example #4
0
    def check_dependency(self):
        utils.print_step('Checking dependency')

        ldd_out = utils.get_command_output(
            'ldd {}/java.so'.format(self.plugin_dir))
        for line in ldd_out.split('\n'):
            libjvm_re = re.search(r'libjvm.so(.*?)not found', line)
            if libjvm_re is not None:
                utils.call_command(
                    'echo Missing libjvm dependency for collectd java plugin '
                    '>>{log}'.format(log=config.INSTALL_LOG))
                self.raise_error(
                    'Missing libjvm dependency for collectd java plugin.')
        utils.print_success()
Example #5
0
    def write_plugin(self, out):
        """
        Flow:
        create /opt/collectd/plugins/python directory
        mv the redis_info.py into there
        ask for instance name
        then ask for hostname, port
        ask for auth
        """
        count = 0  # number of server being monitored
        iname_list = []
        server_list = []
        first_prompt = True
        python_plugin_path = '/opt/collectd/plugins/python'
        if config.DEBUG:
            plugin_src = '{}/{}'.format(
                config.PLUGIN_EXTENSION_DIR, 'redis_info.py')
        else:
            plugin_src = '{}/{}/{}'.format(
                config.APP_DIR,
                config.PLUGIN_EXTENSION_DIR,
                'redis_info.py')

        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 configuring Redis python plugin for collectd')

        # create directory if it doesn't exist
        if not utils.check_path_exists(python_plugin_path):
            utils.print_step(
                '  Creating directory {} '
                'for collectd python plugin'.format(python_plugin_path))
            res = utils.call_command(
                'mkdir -p {}'.format(python_plugin_path))
            if res == 0:
                utils.print_success()
            else:
                utils.print_failure()
                raise Exception('Unable to create directory.')
                
        utils.print_step(
            '  Moving python plugin')
        res = utils.call_command(
            'cp {src} {dst}'.format(
                src=plugin_src, dst=python_plugin_path))
        if res == 0:
            utils.print_success()
        else:
            utils.print_failure()
            raise Exception('Failed to move the plugin.')
            
        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')
        out.write(
            '<LoadPlugin python>\n'
            '  Globals true\n'
            '</LoadPlugin>\n\n'
            '<Plugin python>\n'
            '  ModulePath "{path}"\n'
            '  Import "redis_info"\n'.format(path=python_plugin_path))

        while utils.ask('Would you like to add a server to monitor?'):
            iname = utils.prompt_and_check_input(
                prompt=(
                    '\nHow would you like to name this monitoring instance?\n'
                    '(How it should appear on your wavefront metric page, \n'
                    'space between words will be removed)'),
                check_func=(
                    lambda x: x.replace(" ", "") not in iname_list),
                usage=(
                    '{} has already been used.'.format),
                usage_fmt=True).replace(" ", "")

            host = utils.prompt_and_check_input(
                prompt=(
                    'Please enter the hostname that connects to your\n'
                    'redis server: (ex: localhost)'),
                check_func=utils.hostname_resolves,
                usage='{} does not resolve.'.format,
                usage_fmt=True)

            port = utils.prompt_and_check_input(
                prompt=(
                    'What is the TCP-port used to connect to the host? '),
                check_func=utils.check_valid_port,
                usage=(
                    'A valid port is a number '
                    'between (0, 65535) inclusive.\n'),
                default='6379')

            if (host, port) in server_list:
                utils.eprint(
                    'You have already monitored {host}:{port}.'.format(
                        host=host, port=port))
                continue

            plugin_instance = (
                '    Instance "{iname}"\n'
                '    Host "{host}"\n'
                '    Port "{port}"\n').format(
                    iname=iname,
                    host=host, port=port)

            if utils.ask(
                    'Is there an authorization password set up for\n'
                    '{host}:{port}'.format(host=host, port=port),
                    default=None):
                auth = utils.get_input(
                    'What is the authorization password?')
                plugin_instance += (
                    '    Auth "{auth}"\n'.format(auth=auth))

            slave = utils.ask(
                'Is this a slave server?', default='no')

            utils.cprint()
            if slave:
                utils.cprint('(slave server)')
            utils.cprint('Result: \n{}'.format(plugin_instance))
            res = utils.ask('Is the above information correct?')

            if res:
                utils.print_step('Saving instance')
                count = count + 1
                iname_list.append(iname)
                server_list.append((host, port))
                out.write(
                    '\n  <Module redis_info>\n'
                    '{plugin_instance}'
                    '{metrics}'.format(
                        plugin_instance=plugin_instance,
                        metrics=metrics))
                if slave:
                    out.write(slave_metrics)
                out.write('  </Module>\n')
                utils.print_success()
            else:
                utils.cprint('This instance is not saved.')
        out.write('</Plugin>\n')
        return count
Example #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))
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))