python获得linux服务器的内存使用率,虚拟内存使用率,pythonlinux,python获得linu
python获得linux服务器的内存使用率,虚拟内存使用率,pythonlinux,python获得linu
python获得linux服务器的内存使用率,虚拟内存使用率. linux系统的内存占用信息在/proc/meminfo文件中。 您可以打开这个文件会看到更多详细的信息。
def get_mem_usage_percent(): try: f = open('/proc/meminfo', 'r') for line in f: if line.startswith('MemTotal:'): mem_total = int(line.split()[1]) elif line.startswith('MemFree:'): mem_free = int(line.split()[1]) elif line.startswith('Buffers:'): mem_buffer = int(line.split()[1]) elif line.startswith('Cached:'): mem_cache = int(line.split()[1]) elif line.startswith('SwapTotal:'): vmem_total = int(line.split()[1]) elif line.startswith('SwapFree:'): vmem_free = int(line.split()[1]) else: continue f.close() except: return None physical_percent = usage_percent(mem_total - (mem_free + mem_buffer + mem_cache), mem_total) virtual_percent = 0 if vmem_total > 0: virtual_percent = usage_percent((vmem_total - vmem_free), vmem_total) return physical_percent, virtual_percentdef usage_percent(use, total): try: ret = (float(use) / total) * 100 except ZeroDivisionError: raise Exception("ERROR - zero division error") return ret
相关内容
- 获得进程内存使用量的Python脚本,进程python脚本,None
- python操作符重写,实现+操作符,python操作符,import str
- python中的虚函数,python函数,class Super:
- 抓取知乎日报内容在本地另存为txt文档,日报txt,#File
- 最美应用爬虫,应用爬虫,import reque
- 获取网页上图片的下载地址,获取图片下载地址,#!/us
- python和bash统计CPU利用率,pythonbash利用率,#!/usr/bin/p
- python 爬小说我梦见了欲望,,import urlli
- 多任务多线程任务管理类,多任务多线程管理类,#codi
- 每行数据重复N次合并生成新文件,数据重复n合并,""
评论关闭