sudo apt update && sudo apt upgrade -y
sudo apt install -y python3-pip python3-venv nginx
mkdir ~/monitoring && cd ~/monitoring
python3 -m venv venv
source venv/bin/activate
pip install psutil gpiozero flask
创建app.py
文件:
from flask import Flask, render_template
import psutil
import datetime
app = Flask(__name__)
def get_system_info():
cpu = psutil.cpu_percent(interval=1)
mem = psutil.virtual_memory()
disk = psutil.disk_usage('/')
temp = psutil.sensors_temperatures().get('cpu_thermal', [None])[0].current
return {
'cpu': cpu,
'mem_total': mem.total / (1024 ** 3),
'mem_used': mem.used / (1024 ** 3),
'mem_percent': mem.percent,
'disk_total': disk.total / (1024 ** 3),
'disk_used': disk.used / (1024 ** 3),
'disk_percent': disk.percent,
'temp': temp,
'time': datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
}
@app.route('/')
def dashboard():
return render_template('dashboard.html', data=get_system_info())
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)
创建templates/dashboard.html
:
<!DOCTYPE html>
<html>
<head>
<title>树莓派监控</title>
<meta http-equiv="refresh" content="5">
<style>
body { font-family: Arial, sans-serif; margin: 20px; }
.metric { margin-bottom: 20px; }
.progress-container { width: 100%; background-color: #ddd; }
.progress-bar { height: 30px; background-color: #4CAF50; text-align: center; line-height: 30px; color: white; }
</style>
</head>
<body>
<h1>树莓派系统监控</h1>
<p>更新时间: {{ data.time }}</p>
<div class="metric">
<h3>CPU 使用率: {{ data.cpu }}%</h3>
<div class="progress-container">
<div class="progress-bar" style="width: {{ data.cpu }}%">{{ data.cpu }}%</div>
</div>
</div>
<div class="metric">
<h3>内存使用: {{ "%.2f"|format(data.mem_used) }} GB / {{ "%.2f"|format(data.mem_total) }} GB ({{ data.mem_percent }}%)</h3>
<div class="progress-container">
<div class="progress-bar" style="width: {{ data.mem_percent }}%">{{ data.mem_percent }}%</div>
</div>
</div>
<div class="metric">
<h3>磁盘使用: {{ "%.2f"|format(data.disk_used) }} GB / {{ "%.2f"|format(data.disk_total) }} GB ({{ data.disk_percent }}%)</h3>
<div class="progress-container">
<div class="progress-bar" style="width: {{ data.disk_percent }}%">{{ data.disk_percent }}%</div>
</div>
</div>
<div class="metric">
<h3>CPU 温度: {{ data.temp }}°C</h3>
</div>
</body>
</html>
python app.py
在浏览器中访问: http://<你的树莓派IP>:5000
sudo nano /etc/nginx/sites-available/monitoring
添加以下内容:
server {
listen 80;
server_name your_domain_or_ip;
location / {
proxy_pass http://127.0.0.1:5000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}
sudo ln -s /etc/nginx/sites-available/monitoring /etc/nginx/sites-enabled
sudo nginx -t
sudo systemctl restart nginx
创建systemd服务:
sudo nano /etc/systemd/system/monitoring.service
添加:
[Unit]
Description=Raspberry Pi Monitoring Service
After=network.target
[Service]
User=pi
WorkingDirectory=/home/pi/monitoring
ExecStart=/home/pi/monitoring/venv/bin/python /home/pi/monitoring/app.py
Restart=always
[Install]
WantedBy=multi-user.target
启用服务:
sudo systemctl daemon-reload
sudo systemctl enable monitoring.service
sudo systemctl start monitoring.service
现在你有了一个功能完善的树莓派监控仪表板,每5秒自动刷新显示关键系统指标!