用于监控/ proc / diskstats的Python库?

前端之家收集整理的这篇文章主要介绍了用于监控/ proc / diskstats的Python库?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我想从python程序监视系统IO负载,访问类似于linux中/ proc / diskstats中提供的统计信息(尽管显然跨平台库会很棒).是否有一个现有的python库可以用来查询linux上的磁盘IO统计信息?

最佳答案
万一其他人试图用Python解析/ proc / diskstats像Alex建议:

def diskstats_parse(dev=None):
    file_path = '/proc/diskstats'
    result = {}

    # ref: http://lxr.osuosl.org/source/Documentation/iostats.txt
    columns_disk = ['m','mm','dev','reads','rd_mrg','rd_sectors','ms_reading','writes','wr_mrg','wr_sectors','ms_writing','cur_ios','ms_doing_io','ms_weighted']

    columns_partition = ['m','wr_sectors']

    lines = open(file_path,'r').readlines()
    for line in lines:
        if line == '': continue
        split = line.split()
        if len(split) == len(columns_disk):
            columns = columns_disk
        elif len(split) == len(columns_partition):
            columns = columns_partition
        else:
            # No match
            continue

        data = dict(zip(columns,split))
        if dev != None and dev != data['dev']:
            continue
        for key in data:
            if key != 'dev':
                data[key] = int(data[key])
        result[data['dev']] = data

    return result
原文链接:https://www.f2er.com/linux/440648.html

猜你在找的Linux相关文章