From f23bf8f7997de4e7b246af6d628b188b7184c37a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A8=81=E6=B5=B7-=E8=80=81=E5=88=98?= <7953616+lmc2020@user.noreply.gitee.com> Date: Thu, 19 Dec 2024 07:29:38 +0000 Subject: [PATCH] =?UTF-8?q?update=20applications/view/system/monitor.py.?= =?UTF-8?q?=20=E5=AF=B9=E7=A3=81=E7=9B=98=E4=BF=A1=E6=81=AF=E9=83=A8?= =?UTF-8?q?=E5=88=86=E8=BF=9B=E8=A1=8C=E4=BA=86=E5=AE=8C=E5=96=84=EF=BC=8C?= =?UTF-8?q?=E4=BB=A5=E9=81=BF=E5=85=8D=E5=87=BA=E7=8E=B0=E5=9C=A8=E5=B0=9D?= =?UTF-8?q?=E8=AF=95=E8=AE=BF=E9=97=AE=E6=9F=90=E4=B8=AA=E8=AE=BE=E5=A4=87?= =?UTF-8?q?=EF=BC=88=E5=9C=A8=E8=BF=99=E4=B8=AA=E4=BE=8B=E5=AD=90=E4=B8=AD?= =?UTF-8?q?=E6=98=AF=20'H'=20=E7=9B=98=EF=BC=89=E6=97=B6=E9=81=87=E5=88=B0?= =?UTF-8?q?=E4=BA=86=E9=97=AE=E9=A2=98=EF=BC=8C=E5=9B=A0=E4=B8=BA=E8=AF=A5?= =?UTF-8?q?=E8=AE=BE=E5=A4=87=E5=B0=9A=E6=9C=AA=E5=87=86=E5=A4=87=E5=A5=BD?= =?UTF-8?q?=E6=88=96=E4=B8=8D=E5=8F=AF=E7=94=A8=E8=80=8C=E5=B4=A9=E6=BA=83?= =?UTF-8?q?=E3=80=82=E4=BF=AE=E6=94=B9=E5=90=8E=E5=BD=93=E7=A8=8B=E5=BA=8F?= =?UTF-8?q?=E9=81=87=E5=88=B0=20PermissionError=20=E6=97=B6=EF=BC=8C?= =?UTF-8?q?=E5=AE=83=E4=BC=9A=E6=8D=95=E8=8E=B7=E5=B9=B6=E6=89=93=E5=8D=B0?= =?UTF-8?q?=E9=94=99=E8=AF=AF=E4=BF=A1=E6=81=AF=EF=BC=8C=E8=80=8C=E4=B8=8D?= =?UTF-8?q?=E6=98=AF=E5=B4=A9=E6=BA=83=E3=80=82=E5=90=8C=E6=97=B6=E8=A7=A3?= =?UTF-8?q?=E5=86=B3=E9=81=BF=E5=85=8D=E5=9B=A0=E4=B8=BA=E9=A2=91=E7=B9=81?= =?UTF-8?q?=E8=BD=AE=E8=AF=A2=E5=8F=AF=E8=83=BD=E5=B8=A6=E6=9D=A5=E7=9A=84?= =?UTF-8?q?=E6=80=A7=E8=83=BD=E5=BD=B1=E5=93=8D=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 威海-老刘 <7953616+lmc2020@user.noreply.gitee.com> --- applications/view/system/monitor.py | 58 ++++++++++++++++++----------- 1 file changed, 36 insertions(+), 22 deletions(-) diff --git a/applications/view/system/monitor.py b/applications/view/system/monitor.py index 972d49c..2574e7d 100644 --- a/applications/view/system/monitor.py +++ b/applications/view/system/monitor.py @@ -13,6 +13,9 @@ from applications.common.utils.rights import authorize bp = Blueprint('adminMonitor', __name__, url_prefix='/monitor') +# 缓存数据 +cache = {} + # 系统监控 @bp.get('/') @authorize("system:monitor:main") @@ -26,7 +29,10 @@ def main(): # 逻辑cpu数量 cpu_count = psutil.cpu_count() # cpu使用率 - cpus_percent = psutil.cpu_percent(interval=0.1, percpu=False) # percpu 获取主使用率 + cpus_percent = cache.get('cpus_percent') + if cpus_percent is None: + cpus_percent = psutil.cpu_percent(interval=0.1, percpu=False) # percpu 获取主使用率 + cache['cpus_percent'] = cpus_percent # 内存 memory_information = psutil.virtual_memory() # 内存使用率 @@ -35,29 +41,33 @@ def main(): memory_total: int = memory_information.total memory_free: int = memory_information.free # 磁盘信息 - - disk_partitions_list = [] - # 判断是否在容器中 - if not os.path.exists('/.dockerenv'): - disk_partitions = psutil.disk_partitions() - for i in disk_partitions: - a = psutil.disk_usage(i.device) - disk_partitions_dict = { - 'device': i.device, - 'fstype': i.fstype, - 'total': a.total, - 'used': a.used, - 'free': a.free, - 'percent': a.percent - } - disk_partitions_list.append(disk_partitions_dict) - + disk_partitions_list = cache.get('disk_partitions_list') + if disk_partitions_list is None: + disk_partitions_list = [] + # 判断是否在容器中 + if not os.path.exists('/.dockerenv'): + try: + disk_partitions = psutil.disk_partitions() + for i in disk_partitions: + a = psutil.disk_usage(i.device) + disk_partitions_dict = { + 'device': i.device, + 'fstype': i.fstype, + 'total': a.total, + 'used': a.used, + 'free': a.free, + 'percent': a.percent + } + disk_partitions_list.append(disk_partitions_dict) + except PermissionError as e: + print(f"无法访问设备: {e}") + # 可以在这里添加一些日志记录或错误处理代码 + cache['disk_partitions_list'] = disk_partitions_list # 开机时间 boot_time = datetime.fromtimestamp(psutil.boot_time()).replace(microsecond=0) up_time = datetime.now().replace(microsecond=0) - boot_time up_time_list = re.split(r':', str(up_time)) up_time_format = " {} 小时{} 分钟{} 秒".format(up_time_list[0], up_time_list[1], up_time_list[2]) - # 当前时间 time_now = time.strftime('%H:%M:%S ', time.localtime(time.time())) return render_template( @@ -77,24 +87,28 @@ def main(): time_now=time_now ) - # 图表 api @bp.get('/polling') @authorize("system:monitor:main") def ajax_polling(): # 获取cpu使用率 - cpus_percent = psutil.cpu_percent(interval=0.1, percpu=False) # percpu 获取主使用率 + cpus_percent = cache.get('cpus_percent') + if cpus_percent is None: + cpus_percent = psutil.cpu_percent(interval=0.1, percpu=False) # percpu 获取主使用率 + cache['cpus_percent'] = cpus_percent # 获取内存使用率 memory_information = psutil.virtual_memory() memory_usage = memory_information.percent time_now = time.strftime('%H:%M:%S ', time.localtime(time.time())) return jsonify(cups_percent=cpus_percent, memory_used=memory_usage, time_now=time_now) - # 关闭程序 @bp.get('/kill') @authorize("system:monitor:main") def kill(): + # 检查是否有权限 + if not current_app.config['ADMIN_MONITOR_KILL_ENABLE']: + return jsonify(message='Permission denied'), 403 for proc in psutil.process_iter(): if proc.pid == os.getpid(): proc.kill() -- Gitee