11"""
2- 该文件存放的是option插件类
2+ 该文件存放的是option扩展功能类
33"""
44
5- from .jm_option import *
5+ from .jm_option import JmOption , JmModuleConfig , jm_debug
66
77
88class JmOptionPlugin :
@@ -28,11 +28,11 @@ def build(cls, option: JmOption) -> 'JmOptionPlugin':
2828
2929
3030"""
31- 插件功能 :登录禁漫,并保存登录后的cookies,让所有client都带上此cookies
31+ 扩展功能 :登录禁漫,并保存登录后的cookies,让所有client都带上此cookies
3232"""
3333
3434
35- class LoginPlugin (JmOptionPlugin ):
35+ class JmLoginPlugin (JmOptionPlugin ):
3636 plugin_key = 'login'
3737
3838 def invoke (self , username , password ) -> None :
@@ -50,4 +50,92 @@ def invoke(self, username, password) -> None:
5050 jm_debug ('plugin.login' , '登录成功' )
5151
5252
53- JmModuleConfig .register_plugin (LoginPlugin )
53+ class UsageLogPlugin (JmOptionPlugin ):
54+ plugin_key = 'usage-log'
55+
56+ def invoke (self , ** kwargs ) -> None :
57+ import threading
58+ threading .Thread (
59+ target = self .monitor_resource_usage ,
60+ kwargs = kwargs ,
61+ daemon = True ,
62+ ).start ()
63+
64+ def monitor_resource_usage (
65+ self ,
66+ interval = 1 ,
67+ enable_warning = True ,
68+ warning_cpu_percent = 70 ,
69+ warning_mem_percent = 50 ,
70+ warning_thread_count = 100 ,
71+ ):
72+ try :
73+ import psutil
74+ except ImportError :
75+ msg = (f'插件`{ self .plugin_key } `依赖psutil库,请先安装psutil再使用。'
76+ f'安装命令: [pip install psutil]' )
77+ import warnings
78+ warnings .warn (msg )
79+ # import sys
80+ # print(msg, file=sys.stderr)
81+ return
82+
83+ from time import sleep
84+ from threading import active_count
85+ # 获取当前进程
86+ process = psutil .Process ()
87+
88+ cpu_percent = None
89+ # noinspection PyUnusedLocal
90+ thread_count = None
91+ # noinspection PyUnusedLocal
92+ mem_usage = None
93+
94+ def warning ():
95+ warning_msg_list = []
96+ if cpu_percent >= warning_cpu_percent :
97+ warning_msg_list .append (f'进程占用cpu过高 ({ cpu_percent } % >= { warning_cpu_percent } %)' )
98+
99+ mem_percent = psutil .virtual_memory ().percent
100+ if mem_percent >= warning_mem_percent :
101+ warning_msg_list .append (f'系统内存占用过高 ({ mem_percent } % >= { warning_mem_percent } %)' )
102+
103+ if thread_count >= warning_thread_count :
104+ warning_msg_list .append (f'线程数过多 ({ thread_count } >= { warning_thread_count } )' )
105+
106+ if len (warning_msg_list ) != 0 :
107+ warning_msg_list .insert (0 , '硬件占用告警,占用过高可能导致系统卡死!' )
108+ warning_msg_list .append ('' )
109+ jm_debug ('plugin.psutil.warning' , '\n ' .join (warning_msg_list ))
110+
111+ while True :
112+ # 获取CPU占用率(0~100)
113+ cpu_percent = process .cpu_percent ()
114+ # 获取内存占用(MB)
115+ mem_usage = round (process .memory_info ().rss / 1024 / 1024 , 2 )
116+ thread_count = active_count ()
117+ # 获取网络占用情况
118+ # network_info = psutil.net_io_counters()
119+ # network_bytes_sent = network_info.bytes_sent
120+ # network_bytes_received = network_info.bytes_recv
121+
122+ # 打印信息
123+ msg = ', ' .join ([
124+ f'线程数: { thread_count } ' ,
125+ f'CPU占用: { cpu_percent } %' ,
126+ f'内存占用: { mem_usage } MB' ,
127+ # f"发送的字节数: {network_bytes_sent}",
128+ # f"接收的字节数: {network_bytes_received}",
129+ ])
130+ jm_debug ('plugin.psutil.log' , msg )
131+
132+ if enable_warning is True :
133+ # 警告
134+ warning ()
135+
136+ # 等待一段时间
137+ sleep (interval )
138+
139+
140+ JmModuleConfig .register_plugin (JmLoginPlugin )
141+ JmModuleConfig .register_plugin (UsageLogPlugin )
0 commit comments