<commit_before># (C) Copyright 2016 Hewlett Packard Enterprise Development Company LP

import logging

from monasca_agent.common.util import timeout_command
import monasca_setup.agent_config
import monasca_setup.detection
from monasca_setup.detection.utils import find_process_name
from monasca_setup.detection.utils import watch_process
from monasca_setup.detection.utils import watch_process_by_username

log = logging.getLogger(__name__)

VERTICA_CONF = '/root/.vertica.cnf'
VSQL_PATH = '/opt/vertica/bin/vsql'
VERTICA_SERVICE = 'vertica'
CONNECTION_TIMEOUT = 3


class Vertica(monasca_setup.detection.Plugin):

    """Detect Vertica process running and DB connection status

        This plugin needs the Vertica username, password.
        The other arguments are optional.
        There are two ways to provide this, either by a file placed in
        /root/.vertica.cnf or by passing the following arguments:
            - user
            - password
            - service   (optional)
            - timeout   (optional - timeout for connection attempt in seconds)
        /root/.vertica.cnf in a format such as
        [client]
            user = user1
            password = <PASSWORD>
            service = monitoring
            timeout = 3
    """

    def _detect(self):
        """Run detection, set self.available True if the service is detected.

        """
        if (find_process_name('vertica') is not None and find_process_name(
                'spread') is not None):
            self.available = True

    def _get_config(self):
        """Set the configuration to be used for connecting to Vertica
        :return:
        """
        # Set defaults and read config or use arguments
        if self.args is None:
            self.user = 'mon_api'
            self.password = 'password'
            self.service = VERTICA_SERVICE
            self.timeout = CONNECTION_TIMEOUT

            self._read_config(VERTICA_CONF)
        else:
            self.user = self.args.get('user', 'mon_api')
            self.password = self.args.get('password', 'password')
            self.service = self.args.get('service', VERTICA_SERVICE)
            self.timeout = self.args.get('timeout', CONNECTION_TIMEOUT)

    def _connection_test(self):
        """Attempt to connect to Vertica DB to verify credentials.
        :return: bool status of the test
        """
        log.info("\tVertica connection test.")
        output = timeout_command(
            [VSQL_PATH, "-U", self.user, "-w", self.password, "-c", "select version();"], self.timeout)
        if (output is not None) and ('Vertica Analytic Database' in output):
            return True
        else:
            return False

    def _read_config(self, config_file):
        """Read the configuration setting member variables as appropriate.
        :param config_file: The filename of the configuration to read and parse
        """
        # Read the Vertica config file to extract the needed variables.
        client_section = False
        try:
            with open(config_file, "r") as conf:
                for row in conf:
                    if "[client]" in row:
                        client_section = True
                        log.info("\tUsing client credentials from {:s}".format(config_file))
                        continue
                    if client_section:
                        if "user" in row:
                            self.user = row.split("=")[1].strip()
                        if "password" in row:
                            self.password = row.split("=")[1].strip()
                        if "vsql_path" in row:
                            self.vsql_path = row.split("=")[1].strip()
                        if "service" in row:
                            self.service = row.split("=")[1].strip()
                        if "timeout" in row:
                            self.timeout = int(row.split("=")[1].strip())
        except IOError:
            log.warn('Unable to open Vertica config file {0}. '
                     'Using default credentials to try to connect.'.format(VERTICA_CONF))

    def build_config(self):
        """Build the config as a Plugins object and return.

        """
        config = monasca_setup.agent_config.Plugins()
        try:
            self._get_config()
            for process in ['vertica', 'spread']:
                config.merge(watch_process([process], self.service, process,
                             exact_match=False, detailed=False))
            config.merge(watch_process_by_username('dbadmin', 'vertica', self.service))
            log.info("\tWatching the vertica process.")
            if self._connection_test():
                log.info("\tBuilding vertica config.")
                instance_config = {'name': 'localhost',
                                   'user': self.user,
                                   'password': <PASSWORD>.password,
                                   'service': self.service,
                                   'timeout': self.timeout}<commit_msg>remove extra process watch for vertica<commit_after># (C) Copyright 2016 Hewlett Packard Enterprise Development Company LP

import logging

from monasca_agent.common.util import timeout_command
import monasca_setup.agent_config
import monasca_setup.detection
from monasca_setup.detection.utils import find_process_name
from monasca_setup.detection.utils import watch_process_by_username

log = logging.getLogger(__name__)

VERTICA_CONF = '/root/.vertica.cnf'
VSQL_PATH = '/opt/vertica/bin/vsql'
VERTICA_SERVICE = 'vertica'
CONNECTION_TIMEOUT = 3


class Vertica(monasca_setup.detection.Plugin):

    """Detect Vertica process running and DB connection status

        This plugin needs the Vertica username, password.
        The other arguments are optional.
        There are two ways to provide this, either by a file placed in
        /root/.vertica.cnf or by passing the following arguments:
            - user
            - password
            - service   (optional)
            - timeout   (optional - timeout for connection attempt in seconds)
        /root/.vertica.cnf in a format such as
        [client]
            user = user1
            password = <PASSWORD>
            service = monitoring
            timeout = 3
    """

    def _detect(self):
        """Run detection, set self.available True if the service is detected.

        """
        if (find_process_name('vertica') is not None and find_process_name(
                'spread') is not None):
            self.available = True

    def _get_config(self):
        """Set the configuration to be used for connecting to Vertica
        :return:
        """
        # Set defaults and read config or use arguments
        if self.args is None:
            self.user = 'mon_api'
            self.password = 'password'
            self.service = VERTICA_SERVICE
            self.timeout = CONNECTION_TIMEOUT

            self._read_config(VERTICA_CONF)
        else:
            self.user = self.args.get('user', 'mon_api')
            self.password = self.args.get('password', 'password')
            self.service = self.args.get('service', VERTICA_SERVICE)
            self.timeout = self.args.get('timeout', CONNECTION_TIMEOUT)

    def _connection_test(self):
        """Attempt to connect to Vertica DB to verify credentials.
        :return: bool status of the test
        """
        log.info("\tVertica connection test.")
        output = timeout_command(
            [VSQL_PATH, "-U", self.user, "-w", self.password, "-c", "select version();"], self.timeout)
        if (output is not None) and ('Vertica Analytic Database' in output):
            return True
        else:
            return False

    def _read_config(self, config_file):
        """Read the configuration setting member variables as appropriate.
        :param config_file: The filename of the configuration to read and parse
        """
        # Read the Vertica config file to extract the needed variables.
        client_section = False
        try:
            with open(config_file, "r") as conf:
                for row in conf:
                    if "[client]" in row:
                        client_section = True
                        log.info("\tUsing client credentials from {:s}".format(config_file))
                        continue
                    if client_section:
                        if "user" in row:
                            self.user = row.split("=")[1].strip()
                        if "password" in row:
                            self.password = row.split("=")[1].strip()
                        if "vsql_path" in row:
                            self.vsql_path = row.split("=")[1].strip()
                        if "service" in row:
                            self.service = row.split("=")[1].strip()
                        if "timeout" in row:
                            self.timeout = int(row.split("=")[1].strip())
        except IOError:
            log.warn('Unable to open Vertica config file {0}. '
                     'Using default credentials to try to connect.'.format(VERTICA_CONF))

    def build_config(self):
        """Build the config as a Plugins object and return.

        """
        config = monasca_setup.agent_config.Plugins()
        try:
            self._get_config()
            config.merge(watch_process_by_username('dbadmin', 'vertica', self.service, 'vertica'))
            log.info("\tWatching the vertica processes.")
            if self._connection_test():
                log.info("\tBuilding vertica config.")
                instance_config = {'name': 'localhost',
                                   'user': self.user,
                                   'password': <PASSWORD>,
                                   'service': self.service,
                                   'timeout': self.timeout}
{'fraction_non_alphanumeric': 0.09303266255435332, 'fraction_numerical': 0.0026291839417534636, 'mean_word_length': 1.7648867766284595, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5552230', 'n_tokens_mistral': 2549, 'n_tokens_neox': 2446, 'n_words': 755}