示例#1
0
def ship(path):
    """
    Ship gathered metrics via the Insights agent
    """
    try:
        agent = 'insights-client'
        if shutil.which(agent) is None:
            logger.error('could not find {} on PATH'.format(agent))
            return
        logger.debug('shipping analytics file: {}'.format(path))
        try:
            cmd = [
                agent, '--payload', path, '--content-type',
                settings.INSIGHTS_AGENT_MIME
            ]
            output = smart_str(subprocess.check_output(cmd, timeout=60 * 5))
            logger.debug(output)
            # reset the `last_run` when data is shipped
            run_now = now()
            state = TowerAnalyticsState.get_solo()
            state.last_run = run_now
            state.save()

        except subprocess.CalledProcessError:
            logger.exception('{} failure:'.format(cmd))
        except subprocess.TimeoutExpired:
            logger.exception('{} timeout:'.format(cmd))
    finally:
        # cleanup tar.gz
        os.remove(path)
示例#2
0
def gather(dest=None, module=None):
    """
    Gather all defined metrics and write them as JSON files in a .tgz

    :param dest:    the (optional) absolute path to write a compressed tarball
    :pararm module: the module to search for registered analytic collector
                    functions; defaults to awx.main.analytics.collectors
    """

    run_now = now()
    state = TowerAnalyticsState.get_solo()
    last_run = state.last_run
    logger.debug("Last analytics run was: {}".format(last_run))
    
    max_interval = now() - timedelta(days=7)
    if last_run < max_interval or not last_run:
        last_run = max_interval


    if _valid_license() is False:
        logger.exception("Invalid License provided, or No License Provided")
        return "Error: Invalid License provided, or No License Provided"
    
    if not settings.INSIGHTS_TRACKING_STATE:
        logger.error("Insights analytics not enabled")
        return

    if module is None:
        from awx.main.analytics import collectors
        module = collectors

    dest = dest or tempfile.mkdtemp(prefix='awx_analytics')
    for name, func in inspect.getmembers(module):
        if inspect.isfunction(func) and hasattr(func, '__awx_analytics_key__'):
            key = func.__awx_analytics_key__
            path = '{}.json'.format(os.path.join(dest, key))
            with open(path, 'w', encoding='utf-8') as f:
                try:
                    json.dump(func(last_run), f)
                except Exception:
                    logger.exception("Could not generate metric {}.json".format(key))
                    f.close()
                    os.remove(f.name)
    try:
        collectors.copy_tables(since=last_run, full_path=dest)
    except Exception:
        logger.exception("Could not copy tables")
        
    # can't use isoformat() since it has colons, which GNU tar doesn't like
    tarname = '_'.join([
        settings.SYSTEM_UUID,
        run_now.strftime('%Y-%m-%d-%H%M%S%z')
    ])
    tgz = shutil.make_archive(
        os.path.join(os.path.dirname(dest), tarname),
        'gztar',
        dest
    )
    shutil.rmtree(dest)
    return tgz
示例#3
0
def ship(path):
    """
    Ship gathered metrics to the Insights API
    """
    if not path:
        logger.error('Automation Analytics TAR not found')
        return
    if "Error:" in str(path):
        return
    try:
        logger.debug('shipping analytics file: {}'.format(path))
        url = getattr(settings, 'AUTOMATION_ANALYTICS_URL', None)
        if not url:
            logger.error('AUTOMATION_ANALYTICS_URL is not set')
            return
        rh_user = getattr(settings, 'REDHAT_USERNAME', None)
        rh_password = getattr(settings, 'REDHAT_PASSWORD', None)
        if not rh_user:
            return logger.error('REDHAT_USERNAME is not set')
        if not rh_password:
            return logger.error('REDHAT_PASSWORD is not set')
        with open(path, 'rb') as f:
            files = {
                'file':
                (os.path.basename(path), f, settings.INSIGHTS_AGENT_MIME)
            }
            s = requests.Session()
            s.headers = get_awx_http_client_headers()
            s.headers.pop('Content-Type')
            with set_environ(**settings.AWX_TASK_ENV):
                response = s.post(
                    url,
                    files=files,
                    verify="/etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem",
                    auth=(rh_user, rh_password),
                    headers=s.headers,
                    timeout=(31, 31))
            # Accept 2XX status_codes
            if response.status_code >= 300:
                return logger.exception(
                    'Upload failed with status {}, {}'.format(
                        response.status_code, response.text))
        run_now = now()
        state = TowerAnalyticsState.get_solo()
        state.last_run = run_now
        state.save()
    finally:
        # cleanup tar.gz
        os.remove(path)
示例#4
0
def ship(path):
    """
    Ship gathered metrics to the Insights API
    """
    if not path:
        logger.error('Automation Analytics TAR not found')
        return
    if "Error:" in str(path):
        return
    try:
        logger.debug('shipping analytics file: {}'.format(path))
        url = getattr(settings, 'AUTOMATION_ANALYTICS_URL', None)
        if not url:
            logger.error('AUTOMATION_ANALYTICS_URL is not set')
            return
        rh_user = getattr(settings, 'REDHAT_USERNAME', None)
        rh_password = getattr(settings, 'REDHAT_PASSWORD', None)
        if not rh_user:
            return logger.error('REDHAT_USERNAME is not set')
        if not rh_password:
            return logger.error('REDHAT_PASSWORD is not set')
        with open(path, 'rb') as f:
            files = {
                'file':
                (os.path.basename(path), f, settings.INSIGHTS_AGENT_MIME)
            }
            response = requests.post(url,
                                     files=files,
                                     verify=True,
                                     auth=(rh_user, rh_password),
                                     timeout=(31, 31))
            if response.status_code != 202:
                return logger.exception(
                    'Upload failed with status {}, {}'.format(
                        response.status_code, response.text))
        run_now = now()
        state = TowerAnalyticsState.get_solo()
        state.last_run = run_now
        state.save()
    finally:
        # cleanup tar.gz
        os.remove(path)