import psutil,socket,platform,os,re

cpu = {'user': 0, 'system': 0, 'idle': 0, 'percent': 0}
mem = {'total': 0, 'avaiable': 0, 'percent': 0, 'used': 0, 'free': 0}

#磁盘名称
disk_id = []
#将每个磁盘的total used free percent 分别存入到相应的list
disk_total = []
disk_used = []
disk_free = []
disk_percent = []

#判断操作系统,获取IP
day = platform.system()
def selsys():
    if day == 'Linux':
        def valid_ip(ip):
            if ("255" in ip) or ( ip == "127.0.0.1") or ( ip == "0.0.0.0" ):
                return False
            else:
                return True

        def get_ip(valid_ip):
            ipss = ''.join(os.popen("ifconfig").readlines())
            match = "\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}"
            ips = re.findall(match, ipss, flags=re.M)
            ip = filter(valid_ip, ips)
            return ''.join(ip)


        ip_addr = get_ip(valid_ip)
        return ip_addr

    else:
        localIP = socket.gethostbyname(socket.gethostname())#得到本地ip
        return localIP

# 获取CPU信息
def get_cpu_info():
    cpu_times = psutil.cpu_times()
    cpu['user'] = cpu_times.user
    cpu['system'] = cpu_times.system
    cpu['idle'] = cpu_times.idle
    cpu['percent'] = psutil.cpu_percent(interval=2)

# 获取内存信息
def get_mem_info():
    mem_info = psutil.virtual_memory()
    mem['total'] = mem_info.total
    mem['available'] = mem_info.available
    mem['percent'] = mem_info.percent
    mem['used'] = mem_info.used
    mem['free'] = mem_info.free

#获取磁盘
def get_disk_info():
    for id in psutil.disk_partitions():
        if 'cdrom' in id.opts or id.fstype == '':
            continue
        disk_name = id.device.split(':')
        s = disk_name[0]
        disk_id.append(s)
        disk_info = psutil.disk_usage(id.device)
        disk_total.append(disk_info.total)
        disk_used.append(disk_info.used)
        disk_free.append(disk_info.free)
        disk_percent.append(disk_info.percent)

if __name__ == '__main__':
    print(day)
    print(selsys())
    get_cpu_info()
    cpu_status = cpu['percent']
    print("CPU为:%d%%" % cpu_status)
    get_mem_info()
    mem_status = mem['percent']
    print("内存为:%d%%" % mem_status)
    get_disk_info()
    for i in range(len(disk_id)):
        print(u'%s盘空闲率: %s %%' % (disk_id[i], 100 - disk_percent[i]))
最后修改:2019 年 12 月 05 日 04 : 30 PM
如果觉得我的文章对你有用,请随意赞赏