def run_test(config, start_time):
    if (int(time.time()) - start_time) > 60:
        return {}
    result = _do_request(
        config['url'],
        config['useragent'],
        config['connection_timeout'],
        config['referer'],
        config['auth'],
        config['post'],
        config['monitored_phrases'],
    )
    if 'response_code' not in result:
        result.update({
            'response_code': 0,
        })
    if all((
            result.get('response_code') == config['response_code'],
            result.get('response_time') <= config['performance_issues_time'],
    )):
        response_state = RESPONSE_STATE_OK
    elif result.get('response_code') == config['response_code']:
        response_state = RESPONSE_STATE_PERFORMANCE
    else:
        response_state = RESPONSE_STATE_DIE
    result.update({
        'response_state':
        response_state,
        'response_time':
        result.get(
            'response_time',
            config['connection_timeout'],
        ),
    })
    token = create_token(result, config['uuid'], settings.SALT)
    result.update({
        'token': token,
    })
    return result
Exemple #2
0
def run_test(config, start_time):
    if (int(time.time()) - start_time) > 60:
        return {}
    result = _do_request(
        config['url'],
        config['useragent'],
        config['connection_timeout'],
        config['referer'],
        config['auth'],
        config['post'],
        config['monitored_phrases'],
        config['response_code'],
    )
    if 'response_code' not in result:
        result.update({
            'response_code': 0,
        })
    if all((
        result.get('response_code') == config['response_code'],
        result.get('response_time') <= config['performance_issues_time'],
    )):
        response_state = RESPONSE_STATE_OK
    elif result.get('response_code') == config['response_code']:
        response_state = RESPONSE_STATE_PERFORMANCE
    else:
        response_state = RESPONSE_STATE_DIE
    result.update({
        'response_state': response_state,
        'response_time': result.get(
            'response_time',
            config['connection_timeout'],
        ),
    })
    token = create_token(result, config['uuid'], settings.SALT)
    result.update({
        'token': token,
    })
    return result
Exemple #3
0
def _test_service_summary(service_id, task_uuid, start_time, jobs, sensitivity,
                          results={}, cases={}, wordchecks={}):
    if (int(time.time()) - start_time) > 120:
        return
    for job_id, queue_name in jobs:
        if job_id in results:
            continue
        try:
            job = Job.fetch(
                job_id,
                connection=_get_redis_connection(queue_name),
            )
        except NoSuchJobError:
            results[job_id] = None
            continue
        if job.is_failed:
            results[job_id] = None
            job.delete()
        elif job.result is not None:
            results[job_id] = job.result
            job.delete()
        elif (int(time.time()) - start_time) > 105:
            results[job_id] = None
    if len(results) < len(jobs):
        queue = django_rq.get_queue(
            name='monitors' if 'monitors' in settings.RQ_QUEUES else 'default',
        )
        queue.enqueue_call(
            func=_test_service_summary,
            kwargs={
                'service_id': service_id,
                'task_uuid': task_uuid,
                'start_time': start_time,
                'jobs': jobs,
                'sensitivity': sensitivity,
                'results': results,
                'cases': cases,
                'wordchecks': wordchecks,
            },
            timeout=120,
            result_ttl=0,
        )
        return
    main_probe = None
    tick_cases_number = 0
    failed_cases_number = 0
    for job_id, result in results.items():
        if not result:
            result = {
                'response_state': ResponseStateChoices.agent_failed,
                'response_time': 0,
                'response_code': 0,
            }
        agent = _get_agent_by_id(cases[job_id]['agent_id'])
        if not agent:
            continue
        token = result.get('token')
        if all((
            token != create_token(result, task_uuid, agent.salt),
            result.get('response_state') != ResponseStateChoices.agent_failed,
        )):
            continue
        tick_cases_number += 1
        sh = ServiceHistory(
            service_id=service_id,
            response_state=result.get('response_state'),
            response_code=result.get('response_code'),
            response_time=result.get('response_time'),
            namelookup_time=result.get('namelookup_time'),
            connect_time=result.get('connect_time'),
            pretransfer_time=result.get('pretransfer_time'),
            starttransfer_time=result.get('starttransfer_time'),
            redirect_time=result.get('redirect_time'),
            size_download=result.get('size_download'),
            speed_download=result.get('speed_download'),
            redirect_count=result.get('redirect_count'),
            num_connects=result.get('num_connects'),
            agent_id=cases[job_id]['agent_id'],
        )
        if cases[job_id]['arp_id']:
            sh.request_params_id = cases[job_id]['arp_id']
        wordchecks_errors = []
        if 'monitored_phrases' in result and result['monitored_phrases']:
            for wordcheck_id in wordchecks:
                try:
                    if not result['monitored_phrases'][str(wordcheck_id)]:
                        wordchecks_errors.append(wordchecks[wordcheck_id])
                except KeyError:
                    continue
            wordchecks_errors = ", ".join(wordchecks_errors)
            if all((
                sh.response_state == ResponseStateChoices.ok,
                wordchecks_errors,
            )):
                sh.response_state = ResponseStateChoices.wordcheck_error
        if sh.response_state != ResponseStateChoices.ok:
            failed_cases_number += 1
        if main_probe:
            sh.main_probe = main_probe.id
        sh.save()
        if not main_probe:
            main_probe = sh
        effective_url = result.get('effective_url', '')
        service_url = cases[job_id]['url']
        try:
            if effective_url[-1] == "/":
                effective_url = effective_url[0:-1]
            if service_url[-1] == "/":
                service_url = service_url[0:-1]
        except IndexError:
            pass
        extra = None
        if effective_url and service_url != effective_url:
            extra = ServiceHistoryExtra(service_history_id=sh.pk)
            extra.effective_url = effective_url
        error = result.get('error')
        if error:
            if extra is None:
                extra = ServiceHistoryExtra(service_history_id=sh.pk)
            extra.error_msg = error
        if wordchecks_errors:
            if extra is None:
                extra = ServiceHistoryExtra(service_history_id=sh.pk)
            extra.wordchecks_errors = wordchecks_errors
        if extra is not None:
            extra.save()
    if max(int(sensitivity * tick_cases_number), 1) <= failed_cases_number:
        main_probe.tick_failed = True
        main_probe.save()
Exemple #4
0
def _test_service_summary(service_id,
                          task_uuid,
                          start_time,
                          jobs,
                          sensitivity,
                          results={},
                          cases={},
                          wordchecks={}):
    if (int(time.time()) - start_time) > 120:
        return
    for job_id, queue_name in jobs:
        if job_id in results:
            continue
        try:
            job = Job.fetch(
                job_id,
                connection=_get_redis_connection(queue_name),
            )
        except NoSuchJobError:
            results[job_id] = None
            continue
        if job.is_failed:
            results[job_id] = None
            job.delete()
        elif job.result is not None:
            results[job_id] = job.result
            job.delete()
        elif (int(time.time()) - start_time) > 105:
            results[job_id] = None
    if len(results) < len(jobs):
        queue = django_rq.get_queue(name='monitors' if 'monitors'
                                    in settings.RQ_QUEUES else 'default', )
        queue.enqueue_call(
            func=_test_service_summary,
            kwargs={
                'service_id': service_id,
                'task_uuid': task_uuid,
                'start_time': start_time,
                'jobs': jobs,
                'sensitivity': sensitivity,
                'results': results,
                'cases': cases,
                'wordchecks': wordchecks,
            },
            timeout=120,
            result_ttl=0,
        )
        return
    main_probe = None
    tick_cases_number = 0
    failed_cases_number = 0
    for job_id, result in results.items():
        if not result:
            result = {
                'response_state': ResponseStateChoices.agent_failed,
                'response_time': 0,
                'response_code': 0,
            }
        agent = _get_agent_by_id(cases[job_id]['agent_id'])
        if not agent:
            continue
        token = result.get('token')
        if all((
                token != create_token(result, task_uuid, agent.salt),
                result.get('response_state') !=
                ResponseStateChoices.agent_failed,
        )):
            continue
        tick_cases_number += 1
        sh = ServiceHistory(
            service_id=service_id,
            response_state=result.get('response_state'),
            response_code=result.get('response_code'),
            response_time=result.get('response_time'),
            namelookup_time=result.get('namelookup_time'),
            connect_time=result.get('connect_time'),
            pretransfer_time=result.get('pretransfer_time'),
            starttransfer_time=result.get('starttransfer_time'),
            redirect_time=result.get('redirect_time'),
            size_download=result.get('size_download'),
            speed_download=result.get('speed_download'),
            redirect_count=result.get('redirect_count'),
            num_connects=result.get('num_connects'),
            agent_id=cases[job_id]['agent_id'],
        )
        if cases[job_id]['arp_id']:
            sh.request_params_id = cases[job_id]['arp_id']
        wordchecks_errors = []
        if 'monitored_phrases' in result and result['monitored_phrases']:
            for wordcheck_id in wordchecks:
                try:
                    if not result['monitored_phrases'][str(wordcheck_id)]:
                        wordchecks_errors.append(wordchecks[wordcheck_id])
                except KeyError:
                    continue
            wordchecks_errors = ", ".join(wordchecks_errors)
            if all((
                    sh.response_state == ResponseStateChoices.ok,
                    wordchecks_errors,
            )):
                sh.response_state = ResponseStateChoices.wordcheck_error
        if sh.response_state != ResponseStateChoices.ok:
            failed_cases_number += 1
        if main_probe:
            sh.main_probe = main_probe.id
        sh.save()
        if not main_probe:
            main_probe = sh
        effective_url = result.get('effective_url', '')
        service_url = cases[job_id]['url']
        try:
            if effective_url[-1] == "/":
                effective_url = effective_url[0:-1]
            if service_url[-1] == "/":
                service_url = service_url[0:-1]
        except IndexError:
            pass
        extra = None
        if effective_url and service_url != effective_url:
            extra = ServiceHistoryExtra(service_history_id=sh.pk)
            extra.effective_url = effective_url
        error = result.get('error')
        if error:
            if extra is None:
                extra = ServiceHistoryExtra(service_history_id=sh.pk)
            extra.error_msg = error
        if wordchecks_errors:
            if extra is None:
                extra = ServiceHistoryExtra(service_history_id=sh.pk)
            extra.wordchecks_errors = wordchecks_errors
        if extra is not None:
            extra.save()
    if max(int(sensitivity * tick_cases_number), 1) <= failed_cases_number:
        main_probe.tick_failed = True
        main_probe.save()