Beispiel #1
0
    def write_plugin(self, out):
        count = 0  # number of server being monitored

        utils.print_step('Begin writing nginx plugin for collectd')
        out.write('LoadPlugin "nginx"\n')
        out.write('<Plugin "nginx">\n')

        self.plugin_usage()

        while count == 0:  # can only monitor one for this plugin
            url = utils.get_input(
                'Please enter the url that contains your '
                'nginx-status:\n(ex: localhost/nginx-status)\n'
                '(This plugin can only monitor one server)')
            utils.cprint()
            utils.print_step('Checking http response for %s' % url)
            res = utils.get_command_output('curl -s -i '+url)

            if res is None:
                ret = utils.INVALID_URL
            else:
                ret = utils.check_http_response(res)

            if ret == utils.NOT_AUTH:
                # skip for this case for now, ask for user/pass
                utils.eprint(
                    'Authorization server status is required, please '
                    'try again.\n')
            elif ret == utils.NOT_FOUND or ret == utils.INVALID_URL:
                utils.print_failure()
                utils.eprint(
                    'Invalid url was provided, please try '
                    'again.\n')
            elif ret == utils.HTTP_OK:
                utils.print_success()
                res = self.check_nginx_status(res)
                if not res:
                    utils.print_warn(
                        'The url you have provided '
                        'does not seem to be the correct nginx_status '
                        'page.  Incorrect server-status will not be '
                        'recorded by collectd.')
                    res = utils.ask(
                        'Would you like to record this url anyway?', 'no')

                if res:
                    plugin_instance = (
                        '    URL "{url}"\n').format(url=url)
                    utils.cprint('Result from:\n{}'.format(plugin_instance))
                    res = utils.ask(
                        'Is this the correct url to monitor?')
                    if res:
                        count = count + 1
                        out.write(plugin_instance)
        out.write('</Plugin>\n')
        return count
    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()
Beispiel #3
0
    def check_dependency(self):
        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()
Beispiel #4
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)
Beispiel #5
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))
Beispiel #6
0
    def write_plugin(self, out):
        count = 0
        server_list = []
        sv_list = []
        overwrite = True

        utils.print_step('Begin writing apache plugin for collectd')
        out.write('LoadPlugin "apache"\n')
        out.write('<Plugin "apache">\n')
        self.apache_plugin_usage()

        while utils.ask('Would you like to add a server to monitor?'):
            sv_name = utils.get_input(
                'How would you like to name this server? '
                '(space between words will be removed)').replace(" ", "")

            if sv_name in sv_list:
                utils.cprint('You have already used {}.'.format(
                    sv_name))
                continue

            url = utils.get_input(
                'Please enter the url that contains your '
                'server-status (ex: www.apache.org/server_status):')
            utils.cprint()

            if url in server_list:
                utils.eprint(
                    'You have already added this {} server.'.format(url))
                continue

            utils.print_step('Checking http response for %s' % url)
            res = utils.get_command_output('curl -s -i '+url)

            if res is None:
                ret = utils.INVALID_URL
            else:
                ret = utils.check_http_response(res)

            if ret == utils.NOT_AUTH:
                # skip for this case for now, ask for user/pass
                utils.eprint(
                    'Authorization server status is required, please '
                    'try again.\n')
            elif ret == utils.NOT_FOUND or ret == utils.INVALID_URL:
                utils.print_failure()
                utils.eprint(
                    'Invalid url was provided, please try '
                    'again.\n')
            elif ret == utils.HTTP_OK:
                utils.print_success()
                overwrite = True
                status = self.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 by collectd.')
                    overwrite = utils.ask(
                        'Would you like to record this url anyway?', 'no')

                if overwrite:
                    if status:
                        utils.cprint(status)
                    url_auto = url+'?auto'
                    plugin_instance = (
                        '  <Instance "{instance}">\n'
                        '    URL "{url}"\n'
                        '  </Instance>\n').format(instance=sv_name,
                                                  url=url_auto)
                    utils.cprint()
                    utils.cprint(
                        'Your url is appended with ?auto to convert '
                        'the content into machine readable code.\n'
                        '{}'.format(plugin_instance))
                    res = utils.ask(
                        'Is this the correct status to monitor?')
                    if res:
                        utils.print_step('Saving instance')
                        count = count + 1
                        server_list.append(url)
                        sv_list.append(sv_name)
                        out.write(plugin_instance)
                        utils.print_success()
                    else:
                        utils.cprint('Instance is not saved.')
        out.write('</Plugin>\n')
        return count
def write_apache_plugin(out):
    out.write('LoadPlugin "apache"\n')
    out.write('<Plugin "apache">\n')

    count = 0
    server_list = []

    apache_plugin_usage()
    sys.stdout.write(
        'To check whether the server-status page is working, please visit\n'
        '\tyour-server-name/server-status\n'
        'It should look similar to\n'
        '\tapache.org/server-status\n')

    while utils.ask('Would you like to add a server to monitor?'):
        url = utils.get_input(
            'Please enter the url that contains your ' +
            'server-status (ex: www.apache.org/server_status):')
        print
        utils.print_step('Checking http response for %s' % url)
        res = utils.get_command_output('curl -s -i '+url)

        if res is None:
            ret = INVALID_URL
        else:
            ret = check_http_response(res)

        if ret == NOT_AUTH:
            # skip for this case for now, ask for user/pass
            sys.stderr.write(
                'Authorization server status is required, please '
                'try again.\n')
        elif ret == NOT_FOUND or ret == INVALID_URL:
            utils.print_failure()
            sys.stderr.write(
                'Invalid url was provided, please try '
                'again.\n')
        elif ret == HTTP_OK:
            utils.print_success()
            if url in server_list:
                utils.print_warn(
                    'You have already added this server instance.')
            else:
                res = check_apache_server_status(res)
                if res 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 '
                        'by collectd.')
                    utils.ask(
                        'Would you like to record this url anyway?', 'no')
                else:
                    print res
                    res = utils.ask('Is this the correct status to monitor?')
                    print
                    if res:
                        count = count + 1
                        server_list.append(url)
                        instance = 'apache%d' % count
                        url_auto = url+'?auto'
                        plugin_instance = (
                            '  <Instance "{instance}">\n'
                            '    URL "{url}"\n'
                            '  </Instance>\n').format(instance=instance,
                                                      url=url_auto)
                        out.write(plugin_instance)

    out.write('</Plugin>\n')
    return count
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 write_apache_plugin(out):
    out.write('LoadPlugin "apache"\n')
    out.write('<Plugin "apache">\n')

    count = 0
    server_list = []

    apache_plugin_usage()
    sys.stdout.write(
        'To check whether the server-status page is working, please visit\n'
        '\tyour-server-name/server-status\n'
        'It should look similar to\n'
        '\tapache.org/server-status\n')

    while utils.ask('Would you like to add a server to monitor?'):
        url = utils.get_input(
            'Please enter the url that contains your ' +
            'server-status (ex: www.apache.org/server_status):')
        print
        utils.print_step('Checking http response for %s' % url)
        res = utils.get_command_output('curl -s -i ' + url)

        if res is None:
            ret = INVALID_URL
        else:
            ret = check_http_response(res)

        if ret == NOT_AUTH:
            # skip for this case for now, ask for user/pass
            sys.stderr.write('Authorization server status is required, please '
                             'try again.\n')
        elif ret == NOT_FOUND or ret == INVALID_URL:
            utils.print_failure()
            sys.stderr.write('Invalid url was provided, please try '
                             'again.\n')
        elif ret == HTTP_OK:
            utils.print_success()
            if url in server_list:
                utils.print_warn(
                    'You have already added this server instance.')
            else:
                res = check_apache_server_status(res)
                if res 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 '
                        'by collectd.')
                    utils.ask('Would you like to record this url anyway?',
                              'no')
                else:
                    print res
                    res = utils.ask('Is this the correct status to monitor?')
                    print
                    if res:
                        count = count + 1
                        server_list.append(url)
                        instance = 'apache%d' % count
                        url_auto = url + '?auto'
                        plugin_instance = ('  <Instance "{instance}">\n'
                                           '    URL "{url}"\n'
                                           '  </Instance>\n').format(
                                               instance=instance, url=url_auto)
                        out.write(plugin_instance)

    out.write('</Plugin>\n')
    return count
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))