yandextank/plugins/Telegraf/client.py
def install(self):
        """Create folder and copy agent and metrics scripts to remote host"""
        logger.info(
            "Installing monitoring agent at %s@%s...",
            self.username,
            self.host)

        # create remote temp dir
        cmd = self.python + ' -c "import tempfile; print tempfile.mkdtemp();"'
        logger.info("Creating temp dir on %s", self.host)
        try:
            out, errors, err_code = self.ssh.execute(cmd)
        except Exception:
            logger.error(
                "Failed to install monitoring agent to %s",
                self.host,
                exc_info=True)
            return None, None, None
        if errors:
            logger.error("[%s] error: '%s'", self.host, errors)
            logger.error("Cancelling agent installation on %s", self.host)
            return None, None, None

        if err_code:
            logger.error(
                "Failed to create remote dir via SSH at %s@%s, code %s: %s" %
                (self.username, self.host, err_code, out.strip()))
            return None, None, None

        remote_dir = out.strip()
        if remote_dir:
            self.path['AGENT_REMOTE_FOLDER'] = remote_dir
            self.agent_remote_folder = remote_dir
        logger.debug(
            "Remote dir at %s:%s", self.host, self.path['AGENT_REMOTE_FOLDER'])

        # create collector config
        agent_config = self.config.create_collector_config(
            self.path['AGENT_REMOTE_FOLDER'])
        startup_config = self.config.create_startup_config()
        customs_script = self.config.create_custom_exec_script()

        # trying to detect os version/architecture and get information about telegraf client
        # DO NOT DELETE indices in string format below. Python 2.6 does not
        # support string formatting without indices
        remote_cmd = 'import os; print os.path.isfile("' + self.path[
            'TELEGRAF_REMOTE_PATH'] + '")'
        cmd = self.python + ' -c \'{cmd}\''.format(cmd=remote_cmd)
        remote_telegraf_exists = "False"
        try:
            out, err, err_code = self.ssh.execute(cmd)
        except Exception:
            logger.error(
                "SSH execute error trying to check telegraf availability on host %s",
                self.host,
                exc_info=True)
        else:
            if err:
                logger.error("[%s] error: '%s'", self.host, errors)
            if out.strip():
                remote_telegraf_exists = out.strip()

        try:
            if remote_telegraf_exists in "True":
                logger.debug('Found telegraf client on %s..', self.host)
            else:
                logger.debug(
                    'Not found telegraf client on %s, trying to install from tank. Copying..',
                    self.host)
                if os.path.isfile(self.path['TELEGRAF_LOCAL_PATH']):
                    self.ssh.send_file(
                        self.path['TELEGRAF_LOCAL_PATH'],
                        self.path['TELEGRAF_REMOTE_PATH'])
                elif os.path.isfile("/usr/bin/telegraf"):
                    self.ssh.send_file(
                        '/usr/bin/telegraf', self.path['TELEGRAF_REMOTE_PATH'])
                else:
                    logger.error(
                        'Telegraf binary not found neither on %s nor on localhost at specified path: %s\n'
                        'You can download telegraf binaries here: https://github.com/influxdata/telegraf\n'
                        'or install debian package: `telegraf`', self.host, self.path['TELEGRAF_LOCAL_PATH'])
                    return None, None, None

            self.ssh.send_file(
                os.path.join(
                    self.path['AGENT_LOCAL_FOLDER'],
                    self.AGENT_FILENAME),
                os.path.join(
                    self.path['AGENT_REMOTE_FOLDER'],
                    self.AGENT_FILENAME))
            self.ssh.send_file(
                agent_config,
                os.path.join(
                    self.path['AGENT_REMOTE_FOLDER'],
                    'agent.cfg'))
            self.ssh.send_file(
                startup_config,
                os.path.join(
                    self.path['AGENT_REMOTE_FOLDER'],
                    'agent_startup.cfg'))
            self.ssh.send_file(
                customs_script,
                os.path.join(
                    self.path['AGENT_REMOTE_FOLDER'],
                    'agent_customs.sh'))

        except Exception:
            logger.error(
                "Failed to install agent on %s", self.host, exc_info=True)
            return None, None, None

        return agent_config, startup_config, customs_script
def install(self):
        """Create folder and copy agent and metrics scripts to remote host"""
        logger.info(
            "Installing monitoring agent at %s@%s...",
            self.username,
            self.host)

        # create remote temp dir
        cmd = self.python + ' -c "import tempfile; print tempfile.mkdtemp();"'
        logger.info("Creating temp dir on %s", self.host)
        try:
            out, errors, err_code = self.ssh.execute(cmd)
        except Exception:
            logger.error(
                "Failed to install monitoring agent to %s",
                self.host,
                exc_info=True)
            return None, None, None
        if errors:
            logger.error("[%s] error: '%s'", self.host, errors)
            logger.error("Cancelling agent installation on %s", self.host)
            return None, None, None

        if err_code:
            logger.error(
                "Failed to create remote dir via SSH at %s@%s, code %s: %s" %
                (self.username, self.host, err_code, out.strip()))
            return None, None, None

        remote_dir = out.strip()
        if remote_dir:
            self.path['AGENT_REMOTE_FOLDER'] = remote_dir
            self.agent_remote_folder = remote_dir
        logger.debug(
            "Remote dir at %s:%s", self.host, self.path['AGENT_REMOTE_FOLDER'])

        # create collector config
        agent_config = self.config.create_collector_config(
            self.path['AGENT_REMOTE_FOLDER'])
        startup_config = self.config.create_startup_config()
        customs_script = self.config.create_custom_exec_script()

        # trying to detect os version/architecture and get information about telegraf client
        # DO NOT DELETE indices in string format below. Python 2.6 does not
        # support string formatting without indices
        remote_cmd = 'import os; print os.path.isfile("' + self.path[
            'TELEGRAF_REMOTE_PATH'] + '")'
        cmd = self.python + ' -c \'{cmd}\''.format(cmd=remote_cmd)
        remote_telegraf_exists = "False"
        try:
            out, err, err_code = self.ssh.execute(cmd)
        except Exception:
            logger.error(
                "SSH execute error trying to check telegraf availability on host %s",
                self.host,
                exc_info=True)
        else:
            if err:
                logger.error("[%s] error: '%s'", self.host, errors)
            if out.strip():
                remote_telegraf_exists = out.strip()

        try:
            if remote_telegraf_exists in "True":
                logger.debug('Found telegraf client on %s..', self.host)
            else:
                logger.debug(
                    'Not found telegraf client on %s, trying to install from tank. Copying..',
                    self.host)
                if os.path.isfile(self.path['TELEGRAF_LOCAL_PATH']):
                    self.ssh.send_file(
                        self.path['TELEGRAF_LOCAL_PATH'],
                        self.path['TELEGRAF_REMOTE_PATH'])
                elif os.path.isfile("/usr/bin/telegraf"):
                    self.ssh.send_file(
                        '/usr/bin/telegraf', self.path['TELEGRAF_REMOTE_PATH'])
                else:
                    logger.error(
                        'Telegraf binary not found neither on %s nor on localhost at specified path: %s\n'
                        'You can download telegraf binaries here: https://github.com/influxdata/telegraf\n'
                        'or install debian package: `telegraf`', self.host, self.path['TELEGRAF_LOCAL_PATH'])
                    return None, None, None

            self.ssh.send_file(
                os.path.join(
                    self.path['AGENT_LOCAL_FOLDER'],
                    self.AGENT_FILENAME),
                os.path.join(
                    self.path['AGENT_REMOTE_FOLDER'],
                    self.AGENT_FILENAME))
            self.ssh.send_file(
                agent_config,
                os.path.join(
                    self.path['AGENT_REMOTE_FOLDER'],
                    'agent.cfg'))
            self.ssh.send_file(
                startup_config,
                os.path.join(
                    self.path['AGENT_REMOTE_FOLDER'],
                    'agent_startup.cfg'))
            self.ssh.send_file(
                customs_script,
                os.path.join(
                    self.path['AGENT_REMOTE_FOLDER'],
                    'agent_customs.sh'))

        except Exception:
            logger.error(
                "Failed to install agent on %s", self.host, exc_info=True)
            return None, None, None

        return agent_config, startup_config, customs_script
Create folder and copy agent and metrics scripts to remote host