コード例 #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)
コード例 #2
0
ファイル: __init__.py プロジェクト: blin/zabby
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)
コード例 #3
0
ファイル: memory.py プロジェクト: 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]
コード例 #4
0
ファイル: memory.py プロジェクト: 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]
コード例 #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)
コード例 #6
0
ファイル: swap.py プロジェクト: timchenxiaoyu/zabby
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)
コード例 #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)
コード例 #8
0
ファイル: fs.py プロジェクト: 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)
コード例 #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)]
コード例 #10
0
ファイル: interface.py プロジェクト: blin/zabby
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)
    ]
コード例 #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)
コード例 #12
0
ファイル: tcp.py プロジェクト: 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)
コード例 #13
0
ファイル: proc.py プロジェクト: 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
コード例 #14
0
ファイル: cpu.py プロジェクト: 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
コード例 #15
0
ファイル: cpu.py プロジェクト: 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
コード例 #16
0
ファイル: dev.py プロジェクト: 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
コード例 #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
コード例 #18
0
ファイル: dev.py プロジェクト: 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
コード例 #19
0
ファイル: cpu.py プロジェクト: 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)
コード例 #20
0
ファイル: cpu.py プロジェクト: 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)
コード例 #21
0
ファイル: swap.py プロジェクト: timchenxiaoyu/zabby
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
コード例 #22
0
ファイル: test_utils.py プロジェクト: Bregor/zabby
def test_validate_mode_does_not_raise_exception_if_mode_is_available():
    validate_mode('mode', ['mode'])
コード例 #23
0
ファイル: test_utils.py プロジェクト: blin/zabby
def test_validate_mode_does_not_raise_exception_if_mode_is_available():
    validate_mode('mode', ['mode'])