查看“Psutil + Flask开发Linux /Windows系统监控”的源代码
←
Psutil + Flask开发Linux /Windows系统监控
跳转至:
导航
,
搜索
因为以下原因,您没有权限编辑本页:
您所请求的操作仅限于该用户组的用户使用:
用户
您可以查看与复制此页面的源代码。
==简介== psutil 是一个跨平台库(http://pythonhosted.org/psutil)能够获取到系统运行的进程和系统利用率(包括CPU、内存、磁盘、网络等)信息。主要用来做系统监控,性能分析,进程管理。支持 Linux、Mac OS、Windows 系统。 本文以 psutil 模块获取系统信息开发一个监控 Windows /linux系统的平台。 ==技术选择== *监控的系统是 Windows /linux系统 *监控系统模块选择 psutil 模块 *Web 框架选择的是 Flask 框架 ==技术准备== ===安装 Flask=== [[Python:安装Flask]] ===安装 psutil=== windows/centos7: pip3 install psutil centos8: [[Centos 8 配置yum源]]、[[Centos 8安装psutil]] ==获取系统信息== ===初始化=== app.py: <nowiki> import psutil import os, signal from random import randrange from flask import Flask, render_template, jsonify, request import time app = Flask(__name__, static_folder="templates") cpu_percent_dict = {} net_io_dict = {'net_io_time':[], 'net_io_sent': [], 'net_io_recv': [], 'pre_sent': 0, 'pre_recv': 0, 'len': -1} disk_dict = {'disk_time':[], 'write_bytes': [], 'read_bytes': [], 'pre_write_bytes': 0, 'pre_read_bytes': 0, 'len': -1} cpu_percent_dict = {}</nowiki> ===网页模板=== templates/list.html: <nowiki> <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>基于psutil的系统监控工具</title> <link href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css" rel="stylesheet"> </head> <body> <h1>{{title}}</h1> <table id="customers" class="table table-striped"> <tr> <th>监测项</th> <th>数值</th> </tr> {% for key, value in my_dict.items() %} <tr> <td>{{ key }}</td> <td>{{ value}}</td> </tr> {% endfor %} </table> </body> </html></nowiki> ===CPU信息=== 通过 psutil 获取 CPU 信息 <nowiki> @app.route("/cpu") def cpu(): for i in range(1,6): # 当前时间 now = time.strftime('%H:%M:%S', time.localtime(time.time())) # CPU 负载 cpu_percent = psutil.cpu_percent() cpu_percent_dict[now] = cpu_percent time.sleep(0.3) # 保持在图表中 10 个数据 if len(cpu_percent_dict.keys()) == 11: cpu_percent_dict.pop(list(cpu_percent_dict.keys())[0]) return render_template('list.html', title="CPU使用情况",my_dict=cpu_percent_dict) if __name__ == '__main__':#程序入口 #app.run()#让应用运行在本地服务器上。 app.run(debug=True,host='0.0.0.0') #允许任意网址访问本站 </nowiki> [[文件:python2022052702.png|600px]] ===内存=== <nowiki> @app.route("/memory") def memory(): memory = psutil.virtual_memory() swap = psutil.swap_memory() sys={} sys['mem_total']= memory.total sys['mem_used']= memory.used sys['mem_free']= memory.free sys['swap_total']=swap.total sys['swap_used']=swap.used sys['swap_free']=swap.free sys['memory_percent']= memory.percent return render_template('list.html', title="内存使用情况",my_dict=sys) </nowiki> 效果: [[文件:python2022052701.png|600px]] https://blog.csdn.net/ityouknow/article/details/105985451 ===磁盘=== 通过 psutil 获取磁盘大小、分区、使用率和磁盘IO ===网卡=== 通过 psutil 获取网络接口和网络连接的信息 ==代码== ===app.py=== <nowiki> import psutil import os, signal from random import randrange from flask import Flask, render_template, jsonify, request import time app = Flask(__name__, static_folder="templates") cpu_percent_dict = {} net_io_dict = {'net_io_time':[], 'net_io_sent': [], 'net_io_recv': [], 'pre_sent': 0, 'pre_recv': 0, 'len': -1} disk_dict = {'disk_time':[], 'write_bytes': [], 'read_bytes': [], 'pre_write_bytes': 0, 'pre_read_bytes': 0, 'len': -1} cpu_percent_dict = {} @app.route("/cpu") def cpu(): for i in range(1,6): # 当前时间 now = time.strftime('%H:%M:%S', time.localtime(time.time())) # CPU 负载 cpu_percent = psutil.cpu_percent() cpu_percent_dict[now] = cpu_percent time.sleep(0.3) # 保持在图表中 10 个数据 if len(cpu_percent_dict.keys()) == 11: cpu_percent_dict.pop(list(cpu_percent_dict.keys())[0]) return render_template('list.html', title="CPU使用情况",my_dict=cpu_percent_dict) @app.route("/memory") def memory(): memory = psutil.virtual_memory() swap = psutil.swap_memory() sys={} sys['mem_total']= memory.total sys['mem_used']= memory.used sys['mem_free']= memory.free sys['swap_total']=swap.total sys['swap_used']=swap.used sys['swap_free']=swap.free sys['memory_percent']= memory.percent return render_template('list.html', title="内存使用情况",my_dict=sys) if __name__ == '__main__':#程序入口 #app.run()#让应用运行在本地服务器上。 app.run(debug=True,host='0.0.0.0') #允许任意网址访问本站 </nowiki> ===templates/list.html=== templates/list.html: <nowiki> <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>基于psutil的系统监控工具</title> <link href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css" rel="stylesheet"> </head> <body> <h1>{{title}}</h1> <table id="customers" class="table table-striped"> <tr> <th>监测项</th> <th>数值</th> </tr> {% for key, value in my_dict.items() %} <tr> <td>{{ key }}</td> <td>{{ value}}</td> </tr> {% endfor %} </table> </body></nowiki> ==效果== [[文件:python2022052702.png|600px]] [[文件:python2022052701.png|600px]] 参考文档 : https://blog.csdn.net/ityouknow/article/details/105985451
返回至
Psutil + Flask开发Linux /Windows系统监控
。
导航菜单
个人工具
登录
命名空间
页面
讨论
变种
视图
阅读
查看源代码
查看历史
更多
搜索
导航
首页
最近更改
随机页面
帮助
工具
链入页面
相关更改
特殊页面
页面信息