Esempio n. 1
0
    def check_socket_path(self, path):
        if path is None:
            return False
        # if it begins with ~ then do expand user

        if self.os == config.DEBIAN:
            socket_re = re.match(
                r'mysqld.sock$|(~/|/)(.*/)*mysqld.sock$', path)
        if self.os == config.REDHAT:
            socket_re = re.match(
                r'mysql.sock$|(~/|/)(.*/)*mysql.sock$', path)

        if socket_re is None:
            utils.eprint(
                'Invalid path was given.\n'
                ' -filename has to end with .sock.\n'
                ' -Debian uses mysqld.sock, Redhat uses mysql.sock\n'
                ' -If relative path is used, please add ~/ '
                'to the beginning')
            return False

        if socket_re.group(1) == '~/':
            res = utils.check_path_exists(path, expand=True, debug=True)
        else:
            res = utils.check_path_exists(path, debug=True)

        if not res:
            utils.eprint(
                'Invalid path was given. '
                'Could not find {}.'.format(path))

        return res
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)
def check_collectd_path():
    utils.print_step('  Checking if collectd is installed in our specified '
                     'directory')
    res = utils.check_path_exists(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 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))