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

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

系統 1788 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條評論
主站蜘蛛池模板: 浏阳市| 锦州市| 元氏县| 日照市| 平泉县| 鹤岗市| 门头沟区| 沙田区| 肃宁县| 孝义市| 淮安市| 肇源县| 遵义市| 石台县| 平凉市| 屏南县| 旬邑县| 遂平县| 盐津县| 沙洋县| 壶关县| 炎陵县| 阿克陶县| 朝阳市| 新昌县| 怀柔区| 耒阳市| 阳朔县| 涟水县| 洛浦县| 宜良县| 武冈市| 建昌县| 武隆县| 大同市| 福清市| 无棣县| 绍兴市| 海兴县| 天峨县| 南郑县|