2025-01-24 00:01:29 +08:00
|
|
|
import psutil
|
|
|
|
class SystemStatus:
|
2025-01-24 19:19:17 +08:00
|
|
|
def __init__(self):
|
|
|
|
self.CPU = CPU()
|
|
|
|
self.MemoryVirtual = Memory('virtual')
|
|
|
|
self.MemorySwap = Memory('swap')
|
|
|
|
self.Disks = Disks()
|
|
|
|
self.NetworkInterfaces = NetworkInterfaces()
|
|
|
|
self.Processes = Processes()
|
|
|
|
def toJson(self):
|
|
|
|
return {
|
|
|
|
"CPU": self.CPU,
|
|
|
|
"Memory": {
|
|
|
|
"VirtualMemory": self.MemoryVirtual,
|
|
|
|
"SwapMemory": self.MemorySwap
|
|
|
|
},
|
|
|
|
"Disks": self.Disks,
|
|
|
|
"NetworkInterfaces": self.NetworkInterfaces,
|
|
|
|
"Processes": self.Processes
|
|
|
|
}
|
|
|
|
|
2025-01-24 00:01:29 +08:00
|
|
|
|
|
|
|
class CPU:
|
|
|
|
def __init__(self):
|
|
|
|
self.cpu_percent: float = 0
|
|
|
|
self.cpu_percent_per_cpu: list[float] = []
|
|
|
|
def getData(self):
|
|
|
|
try:
|
2025-01-24 19:19:17 +08:00
|
|
|
self.cpu_percent_per_cpu = psutil.cpu_percent(interval=0.5, percpu=True)
|
|
|
|
self.cpu_percent = psutil.cpu_percent(interval=0.5)
|
2025-01-24 00:01:29 +08:00
|
|
|
except Exception as e:
|
|
|
|
pass
|
|
|
|
def toJson(self):
|
|
|
|
self.getData()
|
|
|
|
return self.__dict__
|
|
|
|
|
|
|
|
class Memory:
|
|
|
|
def __init__(self, memoryType: str):
|
2025-01-24 19:19:17 +08:00
|
|
|
self.__memoryType__ = memoryType
|
2025-01-24 00:01:29 +08:00
|
|
|
self.total = 0
|
|
|
|
self.available = 0
|
|
|
|
self.percent = 0
|
|
|
|
def getData(self):
|
2025-01-24 19:19:17 +08:00
|
|
|
try:
|
|
|
|
if self.__memoryType__ == "virtual":
|
|
|
|
memory = psutil.virtual_memory()
|
|
|
|
else:
|
|
|
|
memory = psutil.swap_memory()
|
|
|
|
self.total = memory.total
|
|
|
|
self.available = memory.available
|
|
|
|
self.percent = memory.percent
|
|
|
|
except Exception as e:
|
|
|
|
pass
|
2025-01-24 00:01:29 +08:00
|
|
|
def toJson(self):
|
|
|
|
self.getData()
|
|
|
|
return self.__dict__
|
2025-01-24 19:19:17 +08:00
|
|
|
|
|
|
|
class Disks:
|
|
|
|
def __init__(self):
|
|
|
|
self.disks : list[Disk] = []
|
|
|
|
def getData(self):
|
|
|
|
try:
|
|
|
|
self.disks = list(map(lambda x : Disk(x.mountpoint), psutil.disk_partitions()))
|
|
|
|
except Exception as e:
|
|
|
|
pass
|
|
|
|
def toJson(self):
|
|
|
|
self.getData()
|
|
|
|
return self.disks
|
|
|
|
|
2025-01-24 00:01:29 +08:00
|
|
|
class Disk:
|
|
|
|
def __init__(self, mountPoint: str):
|
|
|
|
self.total = 0
|
|
|
|
self.used = 0
|
|
|
|
self.free = 0
|
|
|
|
self.percent = 0
|
|
|
|
self.mountPoint = mountPoint
|
|
|
|
def getData(self):
|
2025-01-24 19:19:17 +08:00
|
|
|
try:
|
|
|
|
disk = psutil.disk_usage(self.mountPoint)
|
|
|
|
self.total = disk.total
|
|
|
|
self.free = disk.free
|
|
|
|
self.used = disk.used
|
|
|
|
self.percent = disk.percent
|
|
|
|
except Exception as e:
|
|
|
|
pass
|
2025-01-24 00:01:29 +08:00
|
|
|
def toJson(self):
|
|
|
|
self.getData()
|
|
|
|
return self.__dict__
|
|
|
|
|
|
|
|
class NetworkInterfaces:
|
|
|
|
def __init__(self):
|
|
|
|
self.interfaces = {}
|
|
|
|
def getData(self):
|
2025-01-24 19:19:17 +08:00
|
|
|
try:
|
|
|
|
network = psutil.net_io_counters(pernic=True, nowrap=True)
|
|
|
|
for i in network.keys():
|
|
|
|
self.interfaces[i] = network[i]._asdict()
|
|
|
|
except Exception as e:
|
|
|
|
pass
|
2025-01-24 00:01:29 +08:00
|
|
|
def toJson(self):
|
|
|
|
self.getData()
|
|
|
|
return self.interfaces
|
|
|
|
|
|
|
|
class Processes:
|
|
|
|
class Process:
|
|
|
|
def __init__(self, name, command, pid, percent):
|
|
|
|
self.name = name
|
|
|
|
self.command = command
|
|
|
|
self.pid = pid
|
|
|
|
self.percent = percent
|
|
|
|
def toJson(self):
|
|
|
|
return self.__dict__
|
|
|
|
def __init__(self):
|
|
|
|
self.CPU_Top_10_Processes: list[Processes.Process] = []
|
|
|
|
self.Memory_Top_10_Processes: list[Processes.Process] = []
|
|
|
|
def getData(self):
|
2025-01-24 19:19:17 +08:00
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
processes = list(psutil.process_iter())
|
|
|
|
self.CPU_Top_10_Processes = sorted(
|
|
|
|
list(map(lambda x : Processes.Process(x.name(), " ".join(x.cmdline()), x.pid, x.cpu_percent()), processes)),
|
|
|
|
key=lambda x : x.percent, reverse=True)[:20]
|
|
|
|
self.Memory_Top_10_Processes = sorted(
|
|
|
|
list(map(lambda x : Processes.Process(x.name(), " ".join(x.cmdline()), x.pid, x.memory_percent()), processes)),
|
|
|
|
key=lambda x : x.percent, reverse=True)[:20]
|
|
|
|
break
|
|
|
|
except Exception as e:
|
|
|
|
continue
|
2025-01-24 00:01:29 +08:00
|
|
|
def toJson(self):
|
|
|
|
self.getData()
|
|
|
|
return {
|
|
|
|
"cpu_top_10": self.CPU_Top_10_Processes,
|
|
|
|
"memory_top_10": self.Memory_Top_10_Processes
|
2025-01-24 19:19:17 +08:00
|
|
|
}
|