Hazelnut是一款用来解析proc/meminfo的Python库。
安装教程:
从源文件使用
$ python setup.py install或者从PyPi安装
$ pip install hazelnut文件
基本用法:
>>> from hazelnut import MemInfo>>> mem = MemInfo()>>> memMemTotal: 8092252 kBMemFree: 5444872 kBMemAvailable: 7138880 kBBuffers: 484724 kBCached: 1299716 kBSwapCached: 0 kBActive: 1732744 kBInactive: 671312 kBActive(anon): 509420 kBInactive(anon): 111312 kBActive(file): 1223324 kBInactive(file): 560000 kBUnevictable: 0 kBMlocked: 0 kBSwapTotal: 16776188 kBSwapFree: 16776188 kBDirty: 0 kBWriteback: 0 kBAnonPages: 619632 kBMapped: 96216 kBShmem: 1120 kBSlab: 184200 kBSReclaimable: 159252 kBSUnreclaim: 24948 kBKernelStack: 3856 kBPageTables: 5664 kBNFS_Unstable: 0 kBBounce: 0 kBWritebackTmp: 0 kBCommitLimit: 20822312 kBCommitted_AS: 1755880 kBVmallocTotal: 34359738367 kBVmallocUsed: 362896 kBVmallocChunk: 34359343324 kBHardwareCorrupted: 0 kBAnonHugePages: 0 kBHugePages_Total: 0HugePages_Free: 0HugePages_Rsvd: 0HugePages_Surp: 0Hugepagesize: 2048 kBDirectMap4k: 97980 kBDirectMap2M: 8187904 kB返回输出为dict类型:
>>> mem.dict(){ "Inactive": "671312 kB", "Mlocked": "0 kB", "HardwareCorrupted": "0 kB", "Bounce": "0 kB", "Active": "1746544 kB", "Cached": "1299720 kB", "Unevictable": "0 kB", "SReclaimable": "159256 kB", "DirectMap2M": "8187904 kB", "Buffers": "484736 kB", "Hugepagesize": "2048 kB", "MemAvailable": "7126028 kB", "HugePages_Rsvd": "0", "Inactive(anon)": "111312 kB", "CommitLimit": "20822312 kB", "SUnreclaim": "24940 kB", "Inactive(file)": "560000 kB", "SwapCached": "0 kB", "HugePages_Total": "0", "Active(file)": "1223340 kB", "Writeback": "0 kB", "Dirty": "0 kB", "PageTables": "5700 kB", "Shmem": "1120 kB", "MemFree": "5432004 kB", "Mapped": "96256 kB", "WritebackTmp": "0 kB", "Slab": "184196 kB", "HugePages_Free": "0", "AnonPages": "633396 kB", "HugePages_Surp": "0", "DirectMap4k": "97980 kB", "Committed_AS": "1774860 kB", "VmallocUsed": "362896 kB", "MemTotal": "8092252 kB", "SwapTotal": "16776188 kB", "NFS_Unstable": "0 kB", "VmallocTotal": "34359738367 kB", "Active(anon)": "523204 kB", "KernelStack": "3856 kB", "SwapFree": "16776188 kB", "AnonHugePages": "0 kB", "VmallocChunk": "34359343324 kB"}搜索(不区分大小写):
>>> mem.search('Swap')['SwapCached: 0 kB\n', 'SwapTotal: 16776188 kB\n', 'SwapFree: 16776188 kB\n']获取内存使用为int类型(不区分大小写):
>>> mem.get('Inactive(anon)')111312
评论