插件窝 干货文章 在不到 30 分钟内构建一个树莓派监控仪表盘

在不到 30 分钟内构建一个树莓派监控仪表盘

monitoring 树莓 data div 969    来源:    2025-04-14

30分钟内构建树莓派监控仪表板

所需材料

  • 树莓派(已安装Raspberry Pi OS)
  • 网络连接
  • 约30分钟时间

步骤概览

1. 系统更新 (5分钟)

sudo apt update && sudo apt upgrade -y

2. 安装必要软件 (5分钟)

sudo apt install -y python3-pip python3-venv nginx

3. 设置Python虚拟环境 (3分钟)

mkdir ~/monitoring && cd ~/monitoring
python3 -m venv venv
source venv/bin/activate

4. 安装监控工具 (5分钟)

pip install psutil gpiozero flask

5. 创建Flask应用 (7分钟)

创建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)

6. 创建HTML模板 (5分钟)

创建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>

7. 运行应用 (2分钟)

python app.py

8. 访问仪表板

在浏览器中访问: http://<你的树莓派IP>:5000

可选进阶步骤

使用Nginx作为反向代理 (额外10分钟)

  1. 配置Nginx:
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;
    }
}
  1. 启用配置:
sudo ln -s /etc/nginx/sites-available/monitoring /etc/nginx/sites-enabled
sudo nginx -t
sudo systemctl restart nginx

设置开机自启动 (额外5分钟)

创建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秒自动刷新显示关键系统指标!