Exemple #1
0
def get_device_stats(device_dir, devices, statistics, other):

    _dict = {}

    for device in devices:
        if not _dict.get(device):
            _dict[device] = {}

        for metric in statistics:
            path = os.path.join(device_dir, device, 'statistics', metric)

            try:
                _dict[device][metric] = fsutil.read_file(
                    path).strip().split('\n')[0]

            except Exception as e:
                logger.error("while get_device_stats: " + repr(e))
                return {}

        for x in other:
            path = os.path.join(device_dir, device, x)

            try:
                _dict[device][x] = fsutil.read_file(
                    path).strip().split('\n')[0]

            except Exception as e:
                logger.error("while get_device_stats: " + repr(e))
                return {}

    return _dict
Exemple #2
0
def account(cgroup_path):
    file_name = os.path.join(cgroup_path, 'blkio.io_service_bytes_recursive')

    # file content format:
    # 8:0 Read 1053712384
    # 8:0 Write 81929383424
    # 8:0 Sync 80865775616
    # 8:0 Async 2117320192
    # 8:0 Total 82983095808
    # 8:16 Read 1053712384
    # 8:16 Write 81929383424
    # ...
    # Total 82983095808

    content = fsutil.read_file(file_name)
    lines = content.split('\n')[:-1]

    r = {}
    for line in lines:
        parts = line.split()
        if parts[1] in ('Read', 'Write'):
            r[parts[0]] = r.get(parts[0]) or {}
            r[parts[0]][parts[1]] = int(parts[2])

    return r
Exemple #3
0
        def _write_wait(cont_write, cont_read, start_after, atomic):

            time.sleep(start_after)

            fsutil.write_file(fn, cont_write, atomic=atomic)

            if cont_read != fsutil.read_file(fn):
                assert_ok['ok'] = False
Exemple #4
0
def account(cgroup_path):
    usage_file = os.path.join(cgroup_path, 'cpuacct.usage')

    usage = int(fsutil.read_file(usage_file).strip())

    return {
        'usage': usage,
    }
Exemple #5
0
def load_progress():

    progress_file = cnf['PROGRESS_FILE']

    if os.path.isfile(progress_file):
        progress = utfjson.load(fsutil.read_file(progress_file))
    else:
        progress = {'marker': '', 'total_n': 0, 'total_size': 0.}

    return progress
Exemple #6
0
    def test_read_write_file(self):

        fn = '/tmp/pykit-ut-rw-file'
        force_remove(fn)

        dd('write/read file')
        fsutil.write_file(fn, '123')
        self.assertEqual('123', fsutil.read_file(fn))

        dd('write/read 3MB file')
        cont = '123' * (1024**2)

        fsutil.write_file(fn, cont)
        self.assertEqual(cont, fsutil.read_file(fn))

        dd('write file with uid/gid')
        fsutil.write_file(fn, '1', uid=1, gid=1)
        stat = os.stat(fn)
        self.assertEqual(1, stat.st_uid)
        self.assertEqual(1, stat.st_gid)

        force_remove(fn)
Exemple #7
0
    def test_config(self):

        force_remove('/tmp/foo')

        # write to default place and then ensure config changes stat path

        rst = fsutil.iostat('/dev/sda1')

        # change stat path

        old = config.iostat_stat_path
        config.iostat_stat_path = '/tmp/foo'

        with ututil.Timer() as t:
            rst = fsutil.iostat('/dev/sda1')
            dd(rst)

            self.assertGreaterEqual(t.spent(), 1.0)

        # should be able to read something.
        fsutil.read_file(config.iostat_stat_path)

        force_remove(config.iostat_stat_path)
        config.iostat_stat_path = old
    def test_config(self):

        force_remove('/tmp/foo')

        # write to default place and then ensure config changes stat path

        rst = fsutil.iostat('/dev/sda1')

        # change stat path

        old = config.iostat_stat_path
        config.iostat_stat_path = '/tmp/foo'

        with ututil.Timer() as t:
            rst = fsutil.iostat('/dev/sda1')
            dd(rst)

            self.assertGreaterEqual(t.spent(), 1.0)

        # should be able to read something.
        fsutil.read_file(config.iostat_stat_path)

        force_remove(config.iostat_stat_path)
        config.iostat_stat_path = old
Exemple #9
0
def get_alive_device(device_dir):

    devices = os.listdir(device_dir)
    devices = [device for device in devices
               if os.path.isdir(os.path.join(device_dir, device))]

    alive_devices = []

    try:

        for device in devices:

            path = os.path.join(device_dir, device, 'operstate')
            status = fsutil.read_file(path).strip().split('\n')[0]

            if "up" == status:
                alive_devices.append(device)

    except Exception as e:

        logger.error("while get_discovery_list: " + repr(e))
        return []

    return alive_devices
Exemple #10
0
def get_pid_from_file(pid_file):
    data = fsutil.read_file(pid_file)
    return int(data)
Exemple #11
0
def collect_data():

    item_params = {}

    try:

        partitions = psutil.disk_partitions(all=False)

        for x in partitions:
            item_params[x.mountpoint] = x.device

    except Exception as e:

        logger.error("set item_params data error: " + repr(e))
        return []

    _dict = {}

    try:

        lines = fsutil.read_file('/tmp/zbx-iostat-data').split('\n')

        for line in lines:
            _line = line.split()
            if not line.startswith('Device') and len(_line) == 14:
                block_device = _line[0]
                _dict[block_device] = _line[1:]

    except Exception as e:

        logger.error("load /tmp/zbx-iostat-data data error: " + repr(e))
        return []

    for k, iostat_values in _dict.items():
        iostat_keys = [
            'rrqm/s',
            'wrqm/s',
            'r/s',
            'w/s',
            'rMB/s',
            'wMB/s',
            'avgrq-sz',
            'avgqu-sz',
            'await',
            'r_await',
            'w_await',
            'svctm',
            '%util',
        ]
        # map value to key
        iostat = dict(zip(iostat_keys, iostat_values))
        _dict[k] = iostat

    try:

        for mountpoint, device in item_params.items():

            if 'mapper' in device:

                _fetch_key = os.readlink(device).split('/')[-1]

            else:

                _fetch_key = device.split('/')[-1]
                # remove digit from string
                _fetch_key = ''.join(
                    [i for i in _fetch_key if not i.isdigit()])

            item_params[mountpoint] = _dict[_fetch_key]

    except Exception as e:

        logger.error("data map failed: " + repr(e))
        return []

    _list = []
    _list.append(item_params)

    logger.info("collect_data: " + repr(_list))

    return _list