Psutil + Flask开发Linux /Windows系统监控
来自CloudWiki
目录
简介
psutil 是一个跨平台库(http://pythonhosted.org/psutil)能够获取到系统运行的进程和系统利用率(包括CPU、内存、磁盘、网络等)信息。主要用来做系统监控,性能分析,进程管理。支持 Linux、Mac OS、Windows 系统。
本文以 psutil 模块获取系统信息开发一个监控 Windows /linux系统的平台。
技术选择
- 监控的系统是 Windows /linux系统
- 监控系统模块选择 psutil 模块
- Web 框架选择的是 Flask 框架
技术准备
安装 Flask
安装 psutil
windows/centos7:
pip3 install psutil
centos8:
Centos 8 配置yum源、Centos 8安装psutil
获取系统信息
初始化
app.py:
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 = {}
网页模板
templates/list.html:
<!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>
CPU信息
通过 psutil 获取 CPU 信息
@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') #允许任意网址访问本站
内存
@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)
效果:
https://blog.csdn.net/ityouknow/article/details/105985451
磁盘
通过 psutil 获取磁盘大小、分区、使用率和磁盘IO
网卡
通过 psutil 获取网络接口和网络连接的信息
代码
app.py
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') #允许任意网址访问本站
templates/list.html
templates/list.html:
<!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>
效果
参考文档 : https://blog.csdn.net/ityouknow/article/details/105985451