Exemplo n.º 1
0
def hostname(hostname_type='host', host_os=detect_host_os()):
    """
    Returns host name.

    :raises: WrongArgumentError if unknown hostname_type is supplied

    :depends on: [host_os.hostname, host_os.AVAILABLE_HOSTNAME_TYPES]
    """
    validate_mode(hostname_type, host_os.AVAILABLE_HOSTNAME_TYPES)

    return host_os.hostname(hostname_type)
Exemplo n.º 2
0
def hostname(hostname_type='host', host_os=detect_host_os()):
    """
    Returns host name.

    :raises: WrongArgumentError if unknown hostname_type is supplied

    :depends on: [host_os.hostname, host_os.AVAILABLE_HOSTNAME_TYPES]
    """
    validate_mode(hostname_type, host_os.AVAILABLE_HOSTNAME_TYPES)

    return host_os.hostname(hostname_type)
Exemplo n.º 3
0
Arquivo: memory.py Projeto: blin/zabby
def size(mode='total', host_os=detect_host_os()):
    """
    Returns an amount of memory of specified mode

    :param mode: Varies with host_os
    :raises: WrongArgument if unsupported mode is supplied

    :depends on: [host_os.AVAILABLE_MEMORY_TYPES, host_os.memory]
    """
    validate_mode(mode, host_os.AVAILABLE_MEMORY_TYPES)

    return host_os.memory()[mode]
Exemplo n.º 4
0
Arquivo: memory.py Projeto: blin/zabby
def size(mode='total', host_os=detect_host_os()):
    """
    Returns an amount of memory of specified mode

    :param mode: Varies with host_os
    :raises: WrongArgument if unsupported mode is supplied

    :depends on: [host_os.AVAILABLE_MEMORY_TYPES, host_os.memory]
    """
    validate_mode(mode, host_os.AVAILABLE_MEMORY_TYPES)

    return host_os.memory()[mode]
Exemplo n.º 5
0
def size(device='all', mode='free', host_os=detect_host_os()):
    """
    Returns free and total swap size converted according to mode

    :param mode: one of free, total, used, pfree, pused

    :raises: WrongArgument if unsupported mode is supplied

    :depends on: [host_os.swap_size]
    """
    validate_mode(mode, SIZE_CONVERSION_MODES)

    free, total = host_os.swap_size(device)

    return convert_size(free, total, mode)
Exemplo n.º 6
0
def size(device="all", mode="free", host_os=detect_host_os()):
    """
    Returns free and total swap size converted according to mode

    :param mode: one of free, total, used, pfree, pused

    :raises: WrongArgument if unsupported mode is supplied

    :depends on: [host_os.swap_size]
    """
    validate_mode(mode, SIZE_CONVERSION_MODES)

    free, total = host_os.swap_size(device)

    return convert_size(free, total, mode)
Exemplo n.º 7
0
def size(filesystem, mode="total", host_os=detect_host_os()):
    """
    Returns free and total fs size converted according to mode

    :param filesystem: what filesystem to check
    :type filesystem: str

    :param mode: one of free, total, used, pfree, pused

    :raises: WrongArgument if unsupported mode is supplied

    :depends on: [host_os.fs_size]
    """
    validate_mode(mode, SIZE_CONVERSION_MODES)

    free, total = host_os.fs_size(filesystem)

    return convert_size(free, total, mode)
Exemplo n.º 8
0
Arquivo: fs.py Projeto: blin/zabby
def size(filesystem, mode="total", host_os=detect_host_os()):
    """
    Returns free and total fs size converted according to mode

    :param filesystem: what filesystem to check
    :type filesystem: str

    :param mode: one of free, total, used, pfree, pused

    :raises: WrongArgument if unsupported mode is supplied

    :depends on: [host_os.fs_size]
    """
    validate_mode(mode, SIZE_CONVERSION_MODES)

    free, total = host_os.fs_size(filesystem)

    return convert_size(free, total, mode)
Exemplo n.º 9
0
def outgoing(interface_name, mode="bytes", host_os=detect_host_os()):
    """
    Returns amount of sent bytes or packets, dropped outgoing packets or
    send errors

    :depends on: [host_os.net_interface_names, host_os.net_interface_info]
    :raises: WrongArgument if unsupported mode is supplied
    :raises: WrongArgument if interface is not present on this host
    :type interface_name: str
    """
    validate_mode(mode, NET_MODES)

    interface_names = host_os.net_interface_names()
    validate_mode(interface_name, interface_names)

    info = host_os.net_interface_info(interface_name)

    return info._asdict()["{direction}_{mode}".format(direction='out',
                                                      mode=mode)]
Exemplo n.º 10
0
def incoming(interface_name, mode="bytes", host_os=detect_host_os()):
    """
    Returns amount of received bytes or packets, dropped incoming packets or
    receive errors

    :depends on: [host_os.net_interface_names, host_os.net_interface_info]
    :raises: WrongArgument if unsupported mode is supplied
    :raises: WrongArgument if interface is not present on this host
    :type interface_name: str
    """
    validate_mode(mode, NET_MODES)

    interface_names = host_os.net_interface_names()
    validate_mode(interface_name, interface_names)

    info = host_os.net_interface_info(interface_name)

    return info._asdict()[
        "{direction}_{mode}".format(direction='in', mode=mode)
    ]
Exemplo n.º 11
0
def service(service_name, ip='127.0.0.1', port=None, timeout=1.0):
    """
    Returns 1 if service running on port accepts connections and behaves as
    expected, 0 otherwise

    :param service_name: specifies expected behaviour and port
        ssh:
            behavior: should respond with a greeting message upon connection
            port: 22
    :param port: overrides port specified by service_name

    :raises: WrongArgumentError if unsupported service_name is supplied,
        port is not an integer in range [0,65535] or
        timeout is not a positive float
    """
    validate_mode(service_name, SERVICES.keys())
    if port:
        try:
            port = int(port)
            if port < 0 or 65535 < port:
                raise ValueError()
        except ValueError:
            raise WrongArgumentError(
                "Port must be an integer in range [0,65535], got '{0}'".format(
                    port))
    else:
        port = SERVICES[service_name]
    try:
        timeout = float(timeout)
        if timeout < 0.0:
            raise ValueError()
    except:
        raise WrongArgumentError(
            "Timeout must be float greater than 0, got '{0}'".format(timeout))

    if service_name == 'ssh':
        running = _check_ssh(ip, port, timeout)
    else:
        running = False

    return int(running)
Exemplo n.º 12
0
Arquivo: tcp.py Projeto: blin/zabby
def service(service_name, ip='127.0.0.1', port=None, timeout=1.0):
    """
    Returns 1 if service running on port accepts connections and behaves as
    expected, 0 otherwise

    :param service_name: specifies expected behaviour and port
        ssh:
            behavior: should respond with a greeting message upon connection
            port: 22
    :param port: overrides port specified by service_name

    :raises: WrongArgumentError if unsupported service_name is supplied,
        port is not an integer in range [0,65535] or
        timeout is not a positive float
    """
    validate_mode(service_name, SERVICES.keys())
    if port:
        try:
            port = int(port)
            if port < 0 or 65535 < port:
                raise ValueError()
        except ValueError:
            raise WrongArgumentError(
                "Port must be an integer in range [0,65535], got '{0}'".format(
                    port))
    else:
        port = SERVICES[service_name]
    try:
        timeout = float(timeout)
        if timeout < 0.0:
            raise ValueError()
    except:
        raise WrongArgumentError(
            "Timeout must be float greater than 0, got '{0}'".format(timeout))

    if service_name == 'ssh':
        running = _check_ssh(ip, port, timeout)
    else:
        running = False

    return int(running)
Exemplo n.º 13
0
Arquivo: proc.py Projeto: blin/zabby
def num(name=ALL_PROCESSES, user=ALL_USERS, state='all', cmdline=None,
        host_os=detect_host_os()):
    """
    Returns number of userspace processes matching filter

    :depends on: [host_os.process_infos, host_os.uid]
    :raises: WrongArgument if unsupported state is supplied
    :raises: OperatingSystemError if user is invalid
    """
    validate_mode(state, PROC_NUM_MODES)

    uid = None
    if user != ALL_USERS:
        uid = host_os.uid(user)

    number_of_processes = 0
    for process_info in host_os.process_infos():
        if _matches_filter(process_info, name, uid, state, cmdline):
            number_of_processes += 1

    return number_of_processes
Exemplo n.º 14
0
Arquivo: cpu.py Projeto: blin/zabby
def load(cpu='all', mode='avg1', host_os=detect_host_os()):
    """
    Returns average number of processes that are either in a runnable or
    uninterruptable state.

    :raises: WrongArgumentError if unknown cpu is supplied
    :raises: WrongArgumentError if unknown mode is supplied

    :depends on: [host_os.system_load, host_os.cpu_count]
    """
    validate_mode(cpu, ['all', 'percpu'])
    validate_mode(mode, AVERAGE_MODE.keys())

    system_load = host_os.system_load()

    value = system_load._asdict()[mode]

    if cpu == 'percpu':
        value /= host_os.cpu_count()

    return value
Exemplo n.º 15
0
Arquivo: cpu.py Projeto: blin/zabby
def load(cpu='all', mode='avg1', host_os=detect_host_os()):
    """
    Returns average number of processes that are either in a runnable or
    uninterruptable state.

    :raises: WrongArgumentError if unknown cpu is supplied
    :raises: WrongArgumentError if unknown mode is supplied

    :depends on: [host_os.system_load, host_os.cpu_count]
    """
    validate_mode(cpu, ['all', 'percpu'])
    validate_mode(mode, AVERAGE_MODE.keys())

    system_load = host_os.system_load()

    value = system_load._asdict()[mode]

    if cpu == 'percpu':
        value /= host_os.cpu_count()

    return value
Exemplo n.º 16
0
Arquivo: dev.py Projeto: blin/zabby
def read_write(direction, device, stat_type, mode, host_os):
    type_without_per_second = (
        stat_type
        if stat_type not in stat_type_per_second
        else stat_type_per_second[stat_type]
    )
    validate_mode(type_without_per_second,
                  host_os.AVAILABLE_DISK_DEVICE_STATS_TYPES)
    validate_mode(mode, AVERAGE_MODE.keys())
    shift = AVERAGE_MODE[mode]
    device_names = host_os.disk_device_names()
    if device != 'all':
        validate_mode(device, device_names)
        devices = set([device])
    else:
        devices = device_names
    stat_name = '{0}_{1}'.format(direction, type_without_per_second)
    now = int(time())
    result = 0.0
    for device_name in devices:
        current_stats = host_os.disk_device_stats(device_name)
        if not stat_type in stat_type_per_second.keys():
            result += current_stats._asdict()[stat_name]
        else:
            shifted_stats, shifted_timestamp = (
                host_os.disk_device_stats_shifted(device_name, shift, now))

            if shifted_stats is not None:
                stat_delta = (current_stats._asdict()[stat_name] -
                              shifted_stats._asdict()[stat_name])
                time_delta = now - shifted_timestamp

                result += stat_delta / time_delta
    return result
Exemplo n.º 17
0
def into_out_of(direction, device, mode, host_os):
    if mode == 'pages':
        if device != 'all':
            raise WrongArgumentError(
                'Swapped pages info per device is not available')
        swap_info = host_os.swap_info()
        result = swap_info._asdict()[direction]
    else:
        validate_mode(mode, SWAP_TO_DISK_DEVICE_STAT.keys())
        disk_device_stat_type = SWAP_TO_DISK_DEVICE_STAT[mode]
        validate_mode(disk_device_stat_type,
                      host_os.AVAILABLE_DISK_DEVICE_STATS_TYPES)
        disk_device_stat = "{0}_{1}".format(direction, disk_device_stat_type)
        device_names = host_os.swap_device_names()
        if device != 'all':
            validate_mode(device, device_names)
            devices = set([device])
        else:
            devices = device_names

        result = 0
        for device_name in devices:
            stats = host_os.disk_device_stats(device_name)
            result += stats._asdict()[disk_device_stat]

    return result
Exemplo n.º 18
0
Arquivo: dev.py Projeto: blin/zabby
def read_write(direction, device, stat_type, mode, host_os):
    type_without_per_second = (stat_type
                               if stat_type not in stat_type_per_second else
                               stat_type_per_second[stat_type])
    validate_mode(type_without_per_second,
                  host_os.AVAILABLE_DISK_DEVICE_STATS_TYPES)
    validate_mode(mode, AVERAGE_MODE.keys())
    shift = AVERAGE_MODE[mode]
    device_names = host_os.disk_device_names()
    if device != 'all':
        validate_mode(device, device_names)
        devices = set([device])
    else:
        devices = device_names
    stat_name = '{0}_{1}'.format(direction, type_without_per_second)
    now = int(time())
    result = 0.0
    for device_name in devices:
        current_stats = host_os.disk_device_stats(device_name)
        if not stat_type in stat_type_per_second.keys():
            result += current_stats._asdict()[stat_name]
        else:
            shifted_stats, shifted_timestamp = (
                host_os.disk_device_stats_shifted(device_name, shift, now))

            if shifted_stats is not None:
                stat_delta = (current_stats._asdict()[stat_name] -
                              shifted_stats._asdict()[stat_name])
                time_delta = now - shifted_timestamp

                result += stat_delta / time_delta
    return result
Exemplo n.º 19
0
Arquivo: cpu.py Projeto: blin/zabby
def util(cpu='all', state='user', mode='avg1', host_os=detect_host_os()):
    """
    Returns average percentage of time spent by cpu in a state over a period
    of time

    :raises: WrongArgumentError if unknown cpu is supplied
    :raises: WrongArgumentError if unknown state is supplied
    :raises: WrongArgumentError if unknown mode is supplied

    :depends on: [host_os.cpu_count, host_os.cpu_times_shifted,
                  host_os.cpu_times]
    """
    validate_mode(state, CPU_TIMES)

    available_cpus = list(range(host_os.cpu_count()))
    if cpu == 'all':
        cpus = available_cpus
    else:
        cpu = int(cpu)
        validate_mode(cpu, available_cpus)
        cpus = [cpu]

    validate_mode(mode, AVERAGE_MODE.keys())

    time_in_state = 0
    time_total = 0
    for cpu in cpus:
        shifted_cpu_times = host_os.cpu_times_shifted(cpu, AVERAGE_MODE[mode])
        if shifted_cpu_times is not None:
            current_cpu_times = host_os.cpu_times(cpu)

            cpu_time_in_state = (current_cpu_times._asdict()[state] -
                                 shifted_cpu_times._asdict()[state])
            cpu_time_total = (sum(current_cpu_times) - sum(shifted_cpu_times))

            time_in_state += cpu_time_in_state
            time_total += cpu_time_total
    return (((time_in_state * 100) / time_total)
            if time_total != 0
            else 0.0)
Exemplo n.º 20
0
Arquivo: cpu.py Projeto: blin/zabby
def util(cpu='all', state='user', mode='avg1', host_os=detect_host_os()):
    """
    Returns average percentage of time spent by cpu in a state over a period
    of time

    :raises: WrongArgumentError if unknown cpu is supplied
    :raises: WrongArgumentError if unknown state is supplied
    :raises: WrongArgumentError if unknown mode is supplied

    :depends on: [host_os.cpu_count, host_os.cpu_times_shifted,
                  host_os.cpu_times]
    """
    validate_mode(state, CPU_TIMES)

    available_cpus = list(range(host_os.cpu_count()))
    if cpu == 'all':
        cpus = available_cpus
    else:
        cpu = int(cpu)
        validate_mode(cpu, available_cpus)
        cpus = [cpu]

    validate_mode(mode, AVERAGE_MODE.keys())

    time_in_state = 0
    time_total = 0
    for cpu in cpus:
        shifted_cpu_times = host_os.cpu_times_shifted(cpu, AVERAGE_MODE[mode])
        if shifted_cpu_times is not None:
            current_cpu_times = host_os.cpu_times(cpu)

            cpu_time_in_state = (current_cpu_times._asdict()[state] -
                                 shifted_cpu_times._asdict()[state])
            cpu_time_total = (sum(current_cpu_times) - sum(shifted_cpu_times))

            time_in_state += cpu_time_in_state
            time_total += cpu_time_total
    return (((time_in_state * 100) / time_total) if time_total != 0 else 0.0)
Exemplo n.º 21
0
def into_out_of(direction, device, mode, host_os):
    if mode == "pages":
        if device != "all":
            raise WrongArgumentError("Swapped pages info per device is not available")
        swap_info = host_os.swap_info()
        result = swap_info._asdict()[direction]
    else:
        validate_mode(mode, SWAP_TO_DISK_DEVICE_STAT.keys())
        disk_device_stat_type = SWAP_TO_DISK_DEVICE_STAT[mode]
        validate_mode(disk_device_stat_type, host_os.AVAILABLE_DISK_DEVICE_STATS_TYPES)
        disk_device_stat = "{0}_{1}".format(direction, disk_device_stat_type)
        device_names = host_os.swap_device_names()
        if device != "all":
            validate_mode(device, device_names)
            devices = set([device])
        else:
            devices = device_names

        result = 0
        for device_name in devices:
            stats = host_os.disk_device_stats(device_name)
            result += stats._asdict()[disk_device_stat]

    return result
Exemplo n.º 22
0
def test_validate_mode_does_not_raise_exception_if_mode_is_available():
    validate_mode('mode', ['mode'])
Exemplo n.º 23
0
def test_validate_mode_does_not_raise_exception_if_mode_is_available():
    validate_mode('mode', ['mode'])