Exemplo n.º 1
0
def disk_usage(dir=None):
    if dir is None:
        dir = IRONIO_PARTITION

    if not os.path.exists(dir):
        dir = '/'

    usage = psutil.disk_usage(dir)
    return {
        'Disk:': {
            'path': dir,
            'total': bytes_to_human(usage.total),
            'used': bytes_to_human(usage.used),
            'free': bytes_to_human(usage.free),
            'percent': int(usage.percent)
        }
    }
Exemplo n.º 2
0
def mem_usage():
    virt = psutil.virtual_memory()
    return {
        'Mem': {
            'total': bytes_to_human(virt.total),
            'used': bytes_to_human(virt.used),
            'free': bytes_to_human(virt.free / 1024),
            'shared': bytes_to_human(getattr(virt, 'shared', 0)),
            'buffers': bytes_to_human(getattr(virt, 'buffers', 0)),
            'cached': bytes_to_human(getattr(virt, 'cached', 0))
        },
    }
def test_bytes_to_human_1_K():
    assert bytes_to_human(1024) == "1.00 KB"
def test_bytes_to_human_1_B():
    assert bytes_to_human(1) == "1 B"
Exemplo n.º 5
0
def validate_response(
    response,
    request_curl,
    request_label=None,
):
    """Validate response

    Args:
        response:
        request_curl:
        request_label:

    Returns:

    """
    response_extra = {}
    if request_label is None:
        request_label = 'Validate Response'

    if not response:
        error_message = f'{request_label}: Failed: None'
        log.error(error_message, extra=response_extra)

        raise TuneRequestModuleError(
            error_message=error_message,
            error_request_curl=request_curl,
            error_code=TuneRequestErrorCodes.REQ_ERR_SOFTWARE)

    log.debug(f'{request_label}: Defined', extra=response_extra)

    response_extra.update({'http_status_code': response.status_code})

    # Not using hasattr on purpose
    # Assuming positive approach will give us speedup since when attribute exists
    # hasattr takes double the time.
    # Anyway we use text attribute here to logging purpose only
    try:
        response_extra.update({'response_text_length': len(response.text)})
    except AttributeError:
        pass

    if response.headers:
        if 'Content-Type' in response.headers:
            response_headers_content_type = \
                safe_str(response.headers['Content-Type'])
            response_extra.update(
                {'Content-Type': response_headers_content_type})

        if 'Content-Length' in response.headers:
            response_headers_content_length = \
                safe_int(response.headers['Content-Length'])
            response_extra.update({
                'Content-Length':
                bytes_to_human(response_headers_content_length)
            })

        if 'Content-Encoding' in response.headers:
            response_content_encoding = \
                safe_str(response.headers['Content-Encoding'])
            response_extra.update(
                {'Content-Encoding': response_content_encoding})

        if 'Transfer-Encoding' in response.headers:
            response_transfer_encoding = \
                safe_str(response.headers['Transfer-Encoding'])
            response_extra.update(
                {'Transfer-Encoding': response_transfer_encoding})

    if not is_http_status_successful(http_status_code=response.status_code):
        error_message = f'{request_label}: Failed'
        log.error(error_message, extra=response_extra)

        raise TuneRequestModuleError(
            error_message=error_message,
            error_request_curl=request_curl,
            error_code=TuneRequestErrorCodes.REQ_ERR_SOFTWARE)

    log.debug(f'{request_label}: Success', extra=response_extra)