【Python】获取机器使用信息

获取机器使用信息
# -*- coding:utf-8 -*-
import socket
import os, psutil
#!/usr/bin/env python


# def getInfo():
#     cores = "Physical Cores:%s" % psutil.cpu_count(logical=False) + "  "
#     cpu = "CPU usage:%0.2f"%psutil.cpu_percent(interval=1)+"%" + "  "
#     memory =  "Memory usage:%d"%(int(round(psutil.virtual_memory().percent)))+"%"+"  "
#     swap = "Swap usage:%0.2f"%(psutil.swap_memory().percent) + "%" + "  "
#     disk = "Disk usage:%0.2f"%(psutil.disk_usage('/').percent) + "%" + "  "
#     loadavg = "Load average:%s"%(str(os.getloadavg())) + "  "
#     return cores+cpu+memory+swap+disk+loadavg

def getInfo():
    hostname = socket.gethostname() + "\n"
    cores = "物理内核数:%s" % psutil.cpu_count(logical=False) + "  "
    swap = "Swap分区使用率:%0.2f"%(psutil.swap_memory().percent) + "%" + "  "
    disk = "硬盘使用率:%0.2f"%(psutil.disk_usage('/').percent) + "%" + "  "
    cpu = "CPU使用率:%0.2f"%psutil.cpu_percent(interval=1)+"%" + "  "
    memory =  "内存使用率:%d"%(int(round(psutil.virtual_memory().percent)))+"%"+"  "
    loadavg = "系统负载:%s"%(str(os.getloadavg())) + "  "
    return hostname+cores+swap+disk+cpu+memory+loadavg


if __name__ == "__main__":
    info = getInfo()
    print(info)

 

发布了392 篇原创文章 · 获赞 492 · 访问量 241万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览