zoukankan      html  css  js  c++  java
  • 使用Python统计Linux系统内存使用率

    #!/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
    
    
  • 相关阅读:
    SpringMVC的文件上传
    关于SringMvc的参数的传递
    Myeclipse配置mybatis的xml自动提示
    SSM配置
    sql中truncate 、delete与drop区别
    局部变量和全局变量的调用
    java中 引用类型 和 基本类型 有何区别?
    mysql--select
    网站建设注意要点
    输入域名之后默认首页跳转到其他链接去
  • 原文地址:https://www.cnblogs.com/MeiCheng/p/11555455.html
Copyright © 2011-2022 走看看