笔记:Python 检查windows的CPU 内存 磁盘空间

import wmi

IP = "192.168.0.1"
username = r".\administrator"
password = r"*********"

# for cls in wmi.WMI().classes:
#    print(cls)
wmiServer = wmi.WMI(IP, user=username, password=password)

# MemInfo
total_mem = wmiServer.Win32_ComputerSystem()[0].TotalPhysicalMemory
free_mem = wmiServer.Win32_OperatingSystem()[0].FreePhysicalMemory
print('total mem:', int(total_mem) / 1024 / 1024 / 1024, 'GB')
print('free_mem:', int(free_mem) / 1024 / 1024, 'GB')

# cpuInfo
CPU_data = []
for cpu in wmiServer.Win32_Processor():
    # print('Utilization: %s: %d %%' % (cpu.DeviceID, cpu.LoadPercentage))
    TmpDict = {}
    TmpDict["CPU"] = cpu.DeviceID
    TmpDict["CPU状态"] = cpu.CpuStatus
    TmpDict["CPU最大速度(MHz)"] = cpu.MaxClockSpeed
    TmpDict["CPU当前速度(MHz)"] = cpu.CurrentClockSpeed
    TmpDict["CPU使用率"] = cpu.LoadPercentage
    CPU_data.append(TmpDict)
print(CPU_data)

# 磁盘空间
GB = 1024 ** 3
Disk_data = []
for Physical_Disk in wmiServer.Win32_DiskDrive():
    for Partition in Physical_Disk.associators("Win32_DiskDriveToDiskPartition"):
        for Logical_Disk in Partition.associators("Win32_LogicalDiskToPartition"):
            TmpDict = {}
            TmpDict["盘符"] = Logical_Disk.Caption
            TmpDict["总量"] = format(int(Logical_Disk.Size) / GB, '.2f')
            TmpDict["使用量"] = format((int(Logical_Disk.Size) - int(Logical_Disk.FreeSpace)) / GB, '.2f')
            TmpDict["空闲量"] = format(int(Logical_Disk.FreeSpace) / GB, '.2f')
            TmpDict["使用率"] = format(int(100.0 * (int(Logical_Disk.Size) - int(Logical_Disk.FreeSpace)) / int(Logical_Disk.Size)), '.2f') + "%"
            Disk_data.append(TmpDict)
print(Disk_data)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值