【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)

 

©️2020 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值