#!/usr/bin/python
#*-*coding:utf8*-*
"""适用于在/proc/meminfo中提供了可用内存MemAvailable的系统使用"""
from optparse import OptionParser
import sys
parser = OptionParser()
parser.add_option("-k",
dest="k",
action="store_true",
default=True,
help="Display Meminfo KB")
parser.add_option("-m",
dest="m",
action="store_true",
default=False,
help="Display Meminfo MB")
parser.add_option("-g",
dest="g",
action="store_true",
default=False,
help="Display Meminfo GB")
option, args = parser.parse_args()
with open("/proc/meminfo") as meminfo:
for i in meminfo:
if i.startswith("MemTotal"):
total_mem = i.split()
total_mem = total_mem[1]
continue
elif i.startswith("MemAvailable"):
free_mem = i.split()
free_mem = free_mem[1]
break
else:
pass
total_mem, free_mem = float(total_mem), float(free_mem)
x = free_mem/total_mem
if option.k and (not option.m) and (not option.g):
print("总内存为:%d" %total_mem + " kB")
print("剩余可用内存为:%d" %free_mem + " kB")
print("剩余内存可用比例为:%d" %(x*100) + "%" )
elif option.k and option.m and (not option.g):
print("总内存为:%d" %(total_mem/1024) + " MB")
print("剩余可用内存为:%d" %(free_mem/1024) + " MB")
print("剩余内存可用比例为:%d" %(x*100) + "%" )
elif option.k and (not option.m) and option.g:
print("总内存为:%.2f" %(total_mem/1024/1024) + " GB")
print("剩余可用内存为:%.2f" %(free_mem/1024/1024) + " GB")
print("剩余内存可用比例为:%d" %(x*100) + "%" )
else:
pass