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
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
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
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)
ProcessInfo = namedtuple( 'ProcessInfo', ['name', 'uid', 'state', 'command_line', 'used_memory', ] ) DISK_DEVICE_STATS_FIELDS = [ 'read_sectors', 'read_operations', 'read_bytes', 'write_sectors', 'write_operations', 'write_bytes', ] DiskDeviceStats = namedtuple('DiskDeviceStats', DISK_DEVICE_STATS_FIELDS) CPU_TIMES = ['user', 'nice', 'system', 'idle', 'iowait', 'irq', 'softirq', ] CpuTimes = namedtuple('CpuTimes', CPU_TIMES) SystemLoad = namedtuple('SystemLoad', list(AVERAGE_MODE.keys())) SwapInfo = namedtuple('SwapInfo', ['read', 'write', ]) class HostOS(object): """ Represents abstract operating system It contains abstract methods that concrete operating systems may provide Different data extraction operations may be enabled by implementing these methods """ def __init__(self): self._collectors = list()