PythonによるCPU、GPU、Mem、Diskリソース状態のモニタリング(簡易)

3013 ワード

import psutil
import GPUtil

class pcInfo:

    @staticmethod
    def GetGpuInfo():
        gpulist = []
        # GPUtil.showUtilization()

        #     GPU   ,     
        Gpus = GPUtil.getGPUs()
        for gpu in Gpus:
            # print('gpu.id:', gpu.id)
            # print('GPU  :', gpu.memoryTotal)
            # print('GPU   :', gpu.memoryUsed)
            # print('gpu    :', gpu.memoryUtil * 100)
            #  GPU      

            gpu_memoryTotal = round((gpu.memoryTotal) /1024)
            gpu.memoryUsed = round((gpu.memoryUsed) /1024,2)
            gpu_memoryUtil = round((gpu.memoryUtil) * 100 ,2)
            gpulist.append([gpu.id, gpu_memoryTotal, gpu.memoryUsed, gpu_memoryUtil]) #GPU  ,GPU  ,GPU   ,gpu    
        print("GPU  (G):GPU  ,GPU  ,GPU   ,gpu    ")
        return gpulist


    @staticmethod
    def GetCpuInfo():
        cpu_count = psutil.cpu_count(logical=False)  #1    CPU,2    CPU
        xc_count = psutil.cpu_count()                #   ,      
        cpu_slv = round((psutil.cpu_percent(1)), 2)  # cpu   
        list = [cpu_count,xc_count,cpu_slv] #   ,   ,cpu   
        print("CPU  (G):  ,   ,CPU   ")
        return list


    #       
    @staticmethod
    def GetMemoryInfo():
        memory = psutil.virtual_memory()
        total_nc = round((float(memory.total) / 1024 / 1024 / 1024), 2)  #    
        used_nc = round((float(memory.used) / 1024 / 1024 / 1024), 2)  #     
        free_nc = round((float(memory.free) / 1024 / 1024 / 1024), 2)  #     
        syl_nc = round((float(memory.used) / float(memory.total) * 100), 2)  #      

        ret_list = [total_nc, used_nc, free_nc, syl_nc] #    ,      ,     ,     
        print("    (G):   ,    ,    ,     ")
        return ret_list


    #       
    @staticmethod
    def GetDiskInfo():
        list = psutil.disk_partitions()  #     
        ilen = len(list)  #       
        i = 0
        retlist1 = []
        retlist2 = []
        while i < ilen:
            diskinfo = psutil.disk_usage(list[i].device)
            total_disk = round((float(diskinfo.total) / 1024 / 1024 / 1024), 2)  #    
            used_disk = round((float(diskinfo.used) / 1024 / 1024 / 1024), 2)  #     
            free_disk = round((float(diskinfo.free) / 1024 / 1024 / 1024), 2)  #     
            syl_disk = diskinfo.percent #    

            retlist1 = [i, list[i].device, total_disk, used_disk, free_disk, syl_disk]  #   ,    ,   ,     ,    ,   
            retlist2.append(retlist1)
            i = i + 1
        print("    (G):  ,    ,  ,    ,    ,   ")
        return retlist2

    @staticmethod
    def GetCPUTmpInfo():
        retlist3 = psutil.sensors_temperatures()
        print("CPU  :",retlist3)
        return retlist3

try:
   print(pcInfo.GetCpuInfo())
   print(pcInfo.GetGpuInfo())
   print(pcInfo.GetMemoryInfo())
   print(pcInfo.GetDiskInfo())
   print(pcInfo.GetCPUTmpInfo())
except:
    pass