【Python】獲取機器使用資訊

CS青雀發表於2018-12-13
獲取機器使用資訊
# -*- 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)

 

相關文章