1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
|
import psutil
import platform
from pydantic import BaseModel
from sys import platform as pf
from .apscheduler import scheduler
if pf == "win32":
import wmi
from win32com.client import GetObject
class PlatformInfo(BaseModel):
name: str
struct: str
type: str
class CpuInfo(BaseModel):
name: str
count: int
max_freq: str
current_freq: str
percent: float
process: int
class MemInfo(BaseModel):
total: int
available: int
percent: float
used: int
free: int
swap_total: int
swap_used: int
swap_free: int
swap_percent: float
swap_sin: int
swap_sout: int
class DiskInfo(BaseModel):
name: str
total: int
used: int
free: int
drive: int
speed: list[int]
class NetInfo(BaseModel):
speed: list[int]
sent_total: int
recv_total: int
package_sent: int
package_recv: int
def get_platform_info() -> PlatformInfo:
return PlatformInfo(
name=platform.platform(), struct=platform.architecture()[0], type=pf
)
def get_cpu_info() -> CpuInfo:
cpu_name = platform.processor()
if pf == "win32":
winm = GetObject("winmgmts:root\cimv2")
cpus = winm.ExecQuery("SELECT * FROM Win32_Processor")
cpu_name = cpus[0].Name.strip()
cpu_count = psutil.cpu_count(False)
_freq = psutil.cpu_freq()
cpu_max_freq = f"{'%.2f'%(_freq.max / 1000)}"
cpu_current_freq = f"{'%.2f'%(_freq.current / 1000)}"
cpu_percent = psutil.cpu_percent(interval=0.1)
process = len(psutil.pids())
return CpuInfo(
name=cpu_name,
count=cpu_count,
max_freq=cpu_max_freq,
current_freq=cpu_current_freq,
percent=cpu_percent,
process=process,
)
def get_mem_info() -> MemInfo:
vm = psutil.virtual_memory()
sm = psutil.swap_memory()
return MemInfo(
total=vm.total,
available=vm.available,
percent=vm.percent,
used=vm.used,
free=vm.free,
swap_total=sm.total,
swap_used=sm.used,
swap_free=sm.free,
swap_percent=sm.percent,
swap_sin=sm.sin,
swap_sout=sm.sout,
)
last_disk_io = [0, 0]
now_disk_io = [0, 0]
@scheduler.scheduled_job("interval", seconds=1, misfire_grace_time=15)
async def _():
global last_disk_io, now_disk_io
disk_counters = psutil.disk_io_counters()
now_disk_io = [
disk_counters.write_bytes - last_disk_io[0], # type: ignore
disk_counters.read_bytes - last_disk_io[1], # type: ignore
]
last_disk_io = [disk_counters.write_bytes, disk_counters.read_bytes] # type: ignore
def get_disk_info():
disk_name = "ATRI Disk Pro Plus"
disk_total = int()
disk_used = int()
disk_free = int()
disk_drive = 1
if pf == "win32":
w = wmi.WMI()
disk_list = [d.DeviceID for d in w.Win32_LogicalDisk()]
for i in disk_list:
disk = psutil.disk_usage(i)
disk_total += disk.total
disk_used += disk.used
disk_free += disk.free
disk_name = w.Win32_DiskDrive()[0].Model
disk_drive = len(w.Win32_DiskDrive())
else:
disk = psutil.disk_usage("/")
disk_total = disk.total
disk_used = disk.used
disk_free = disk.free
return DiskInfo(
name=disk_name,
total=disk_total,
used=disk_used,
free=disk_free,
drive=disk_drive,
speed=now_disk_io,
)
last_net_io = [0, 0]
now_net_io = [0, 0]
@scheduler.scheduled_job("interval", seconds=1, misfire_grace_time=15)
async def _():
global last_net_io, now_net_io
net_counters = psutil.net_io_counters()
now_net_io = [
net_counters.bytes_sent - last_net_io[0],
net_counters.bytes_recv - last_net_io[1],
]
last_net_io = [net_counters.bytes_sent, net_counters.bytes_recv]
def get_net_info():
net = psutil.net_io_counters()
return NetInfo(
speed=now_net_io,
sent_total=net.bytes_sent,
recv_total=net.bytes_recv,
package_sent=net.packets_sent,
package_recv=net.packets_recv,
)
|