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

import ConfigParser
import logging
import psutil

import monasca_setup.agent_config
from monasca_setup.detection import Plugin

log = logging.getLogger(__name__)


class VCenter(Plugin):
    """Configures ESX Cluster monitoring through VCenter"""

    def _detect(self):
        """Method to detect the nova-compute service,
        if found set the nova.conf, the flags under vmware section will be used to
        configure the plugin, else the args are used to configure.
        """
        # Find the nova compute process and locate its conf
        nova_conf = None
        for proc in psutil.process_iter():
            try:
                cmd = proc.cmdline()
                if len(cmd) > 2 and 'python' in cmd[0] and 'nova-compute' in cmd[1]:
                    param = [cmd.index(y) for y in cmd if 'hypervisor.conf' in y][0]
                    if '=' in cmd[param]:
                        nova_conf = cmd[param].split('=')[1]
                    else:
                        nova_conf = cmd[param]
            except IOError:
                # Process has already terminated, ignore
                continue
        # for cases where this plugin and nova-compute service runs separately
        # we will configure the plugin with given args.
        # so, we have to set these below variables
        self.nova_conf = nova_conf
        self.available = True

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

        if self.dependencies_installed():
            nova_cfg = ConfigParser.SafeConfigParser()
            instance = {}
            if self.nova_conf is None:
                log.warn("No nova compute service found.")
                if self.args:
                    # read from arg list
                    instance = self._read_from_args(instance)
                else:
                    # get the default config format
                    instance = self._config_format()
            else:
                log.info("Using nova configuration file {0}".format(self.nova_conf))
                nova_cfg.read(self.nova_conf)
                cfg_section = 'vmware'

                # extract the vmware config from nova.conf and build instances
                if (nova_cfg.has_option(cfg_section, 'host_ip')
                        and nova_cfg.has_option(cfg_section, 'host_username')
                        and nova_cfg.has_option(cfg_section, 'host_password')
                        and nova_cfg.has_option(cfg_section, 'cluster_name')):

                    instance = {
                        'vcenter_ip': nova_cfg.get(cfg_section, 'host_ip'),
                        'username': nova_cfg.get(cfg_section, 'host_username'),
                        'password': nova_cfg.get(cfg_section, 'host_password'),
                        'clusters': [nova_cfg.get(cfg_section, 'cluster_name')]
                    }
                else:
                    # put default format
                    instance = self._config_format()
            config['vcenter'] = {'init_config': {},
                                 'instances': [instance]}
        return config<commit_msg>VCenter config file<commit_after># (C) Copyright 2016 Hewlett Packard Enterprise Development Company LP

import ConfigParser
import logging
import psutil

import monasca_setup.agent_config
from monasca_setup.detection import Plugin
from monasca_setup.detection.utils import find_process_name

log = logging.getLogger(__name__)


class VCenter(Plugin):
    """Configures ESX Cluster monitoring through VCenter"""

    def _detect(self):
        """Method to detect the nova-compute service,
        if found set the nova.conf, the flags under vmware section will be used to
        configure the plugin, else the args are used to configure.
        """
        # Find the nova compute process and locate its conf
        process_exist = find_process_name('nova-compute') is not None
        # for cases where this plugin and nova-compute service runs separately
        # we will configure the plugin with given args.
        # so, we have to set these below variables
        self.nova_conf = self.get_nova_config_file() if process_exist else None
        has_config_file_or_args = (self.nova_conf is not None or
                                   self.args is not None)
        self.available = process_exist and has_config_file_or_args
        if not self.available:
            if not process_exist:
                log.error('Nova-compute process does not exist.')
            elif not has_config_file_or_args:
                log.error(('Nova-compute process exists but '
                           'the configuration file was not detected and no '
                           'arguments were given.'))

    def get_nova_config_file(self):
        nova_conf = None
        for proc in psutil.process_iter():
            try:
                cmd = proc.cmdline()
                if len(cmd) > 2 and 'python' in cmd[0] and 'nova-compute' in cmd[1]:
                    param = [cmd.index(y) for y in cmd if 'hypervisor.conf' in y][0]
                    if '=' in cmd[param]:
                        nova_conf = cmd[param].split('=')[1]
                    else:
                        nova_conf = cmd[param]
            except IOError:
                # Process has already terminated, ignore
                continue
        return nova_conf

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

        if self.dependencies_installed():
            nova_cfg = ConfigParser.SafeConfigParser()
            instance = {}
            if self.nova_conf is None:
                log.warn("Nova compute configuration file was not found.")
                if self.args:
                    # read from arg list
                    instance = self._read_from_args(instance)
                else:
                    # get the default config format
                    instance = self._config_format()
            else:
                log.info("Using nova configuration file {0}".format(self.nova_conf))
                nova_cfg.read(self.nova_conf)
                cfg_section = 'vmware'

                # extract the vmware config from nova.conf and build instances
                if (nova_cfg.has_option(cfg_section, 'host_ip')
                        and nova_cfg.has_option(cfg_section, 'host_username')
                        and nova_cfg.has_option(cfg_section, 'host_password')
                        and nova_cfg.has_option(cfg_section, 'cluster_name')):

                    instance = {
                        'vcenter_ip': nova_cfg.get(cfg_section, 'host_ip'),
                        'username': nova_cfg.get(cfg_section, 'host_username'),
                        'password': nova_cfg.get(cfg_section, 'host_password'),
                        'clusters': [nova_cfg.get(cfg_section, 'cluster_name')]
                    }
                else:
                    # put default format
                    instance = self._config_format()
            config['vcenter'] = {'init_config': {},
                                 'instances': [instance]}
        return config
{'fraction_non_alphanumeric': 0.07830673519069516, 'fraction_numerical': 0.002704895861509332, 'mean_word_length': 1.693008011653314, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6975481', 'n_tokens_mistral': 1954, 'n_tokens_neox': 1846, 'n_words': 557}