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
相关内容
- 64位系统编译Python,64位编译python,如果在64位系统下默认
- python计算文件的md5值,python计算md5,from hashlib
- python通过telnet连接华为设备控制防火墙,python华为,最近
- python使用bmemcached操作memcached示例代码,,python使用bmem
- 爬取千万淘宝商品的python脚本,淘宝商品python脚本,分享
- python打开url下载url内容,python打开url内容,import urlli
- python定义类的操作符__plus__示例,python__plus_,class adder
- python的类和对象,python对象,# Demonstrat
- __getattr__ demo,__getattr_demo,class empty:
- python实现连接两个url,python实现url,import urlpa
评论关闭