日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

使用Python統計Linux系統內存使用率

系統 1761 0
          
            #!/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

          
        

更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 瑞安市| 张家港市| 绥德县| 石棉县| 铁岭县| 巴青县| 普洱| 上蔡县| 龙岩市| 西丰县| 大邑县| 偃师市| 双鸭山市| 淳化县| 长武县| 栖霞市| 兰西县| 咸阳市| 怀远县| 来宾市| 朝阳市| 六安市| 灵宝市| 美姑县| 教育| 苏州市| 金坛市| 和龙市| 新野县| 涿州市| 秦皇岛市| 东平县| 祁门县| 青冈县| 甘泉县| 信阳市| 新和县| 平乐县| 苏尼特左旗| 抚顺县| 宜都市|