Skip to main content

Monitor and manage Raspberry Pi system metrics via a Flask server

Project description

RPi Metrics

Github top language Github language count Repository size License

Welcome to the RPi Metrics project! This project allows you to monitor and manage your Raspberry Pi's system metrics such as CPU usage, memory usage, and more via a Flask server and a client application (optional but highly recommended).

[!TIP] You can also remote poweroff, remote reboot, and remote update your Pi, from anywhere in the world, provided that the server is reachable from the wider internet. (Check out https://pi5-monitor.qincai.xyz to see it yourself!)

Prerequisites

Ensure you have the following installed on your Raspberry Pi: If you don't have everything, no worries! Just use the installation script below! It will handle everything for you!

  • Python 3
  • Pip
  • Git
  • Curl

However, ensure that curl is already installed on your system before using the Quick Installer.

Quick Installation

Use the installation script by running:

sudo su
bash <(curl -sSL https://qincai.xyz/rpi-metrics-installer.sh)

Alternatively, you can run:

curl -sSL https://qincai.xyz/rpi-metrics-installer.sh | sudo bash -s - -y

to accept all defaults.

Or:

curl -sSL https://qincai.xyz/rpi-metrics-installer.sh | bash -s - --no-check-root

to bypass root check.

Setting Up the Server

[!NOTE] You can skip this if you used the Quick Installer script.

  1. Clone this repository

    sudo git -c http.followRedirects=true clone https://github.com/QinCai-rui/RPi-Metrics.git /usr/share/rpi-metrics
    
  2. Navigate to the server directory:

    cd /usr/share/rpi-metrics/Server
    
  3. Activate the virtual environment:

    source venv/bin/activate
    
  4. Run the server:

    sudo python3 rpi_metrics_server.py
    
  5. Alternatively, start it as a systemd service (Recommended):

    sudo cp /usr/share/rpi-metrics/Server/rpi-metricsd.service /etc/systemd/system/
    sudo systemctl enable --now rpi-metricsd
    

Setting Up the Client

The client should be a Raspberry Pi Pico W or Pico 2 W with 2 buttons and an SSD1306 128x64 screen. The Pico is optional. You can use this API on any device.

  1. Create a .env file in the client directory with the following content:

    SSID = "your_wifi_ssid"
    PSK = "your_wifi_password"
    SERVER_URL = "http://your_server_url"
    API_KEY = "your_api_key_here"
    
  2. Download the client code to your Pi Pico. The code is here.

  3. Ensure your Raspberry Pi Pico is connected to the internet.

  4. Run the client script (or save it as main.py to run at startup).

Available API Endpoints

  • GET /api/time: Retrieve the current system time. Example Output:

    {
      "Current Time":"Jan 01 00:00:00"
    }
    

  • GET /api/mem: Retrieve memory statistics. Example Output:

    {
      "Total RAM": "465MiB",
      "Total Swap": "2048MiB",
      "Used RAM": "244",
      "Used Swap": "72"
    }
    

  • GET /api/cpu: Retrieve CPU usage. Example Output:

    {
      "CPU Usage": "31%",
      "SoC Temperature": "48.9C"
    }
    

  • GET /api/disk: Retrieve disk usage statistics. Example Output:

    {
      "Total Space": "32G",
      "Used Space": "12G",
      "Available Space": "18G",
      "Usage Percentage": "41%"
    }
    

  • GET /api/system: Retrieve detailed system information. Example Output:

    {
      "Model": "Raspberry Pi 5 Model B Rev 1.0",
      "Kernel Version": "6.6.74+rpt-rpi-2712",
      "OS": "Debian GNU/Linux trixie/sid"
    }
    

  • POST /api/shutdown: Shutdown the system (requires API key in the header). Header name should be x-api-key. The server returns HTTP 200 and the following if a valid API key is provided:

    {
        "message": "System shutting down"
    }
    

    If a valid API key is not provided, the server returns HTTP 401 and the following:

    {
        "error": "Unauthorized"
    }   
    

    Example Usage:

    curl -L -X POST http://your_server_url/api/shutdown -H "x-api-key: your_api_key_here"
    

  • POST /api/reboot: Reboot the system (requires API key in the header). The server returns HTTP 200 and the following if a valid API key is provided:

    {
        "message": "System rebooting now"
    }
    

    If a valid API key is not provided, the server returns HTTP 401.

    Example Usage:

    curl -L -X POST http://your_server_url/api/reboot -H "x-api-key: your_api_key_here"
    

  • POST /api/update: Update the system (requires API key in the header). Header name should be x-api-key. The server returns HTTP 200 and the following if a valid API key is provided, after a update:

    {
        "message": "System update complete!"
    }
    

    If a valid API key is not provided, the server returns HTTP 401 and the following:

    {
        "error": "Unauthorized"
    }   
    

    Example Usage:

    curl -L -X POST http://your_server_url/api/update -H "x-api-key: your_api_key_here"
    

  • GET /api/all: Retrieve comprehensive system statistics. Example Output:

    {
      "CPU Usage": "31%",
      "Current Time": "Jan 1 00:00:00",
      "IP Address": "192.168.2.123 100.93.81.48 fd7a:115c:a1e0::8c01:5130",
      "SoC Temperature": "48.9C",
      "Total RAM": "465MiB",
      "Total Swap": "2048MiB",
      "Used RAM": "244",
      "Used Swap": "72"
    }
    

  • GET /: Access the user-friendly GUI. Example Output: root GUI output image

Uninstallation

If for any reason, you want to uninstall the RPi Metrics server from your Raspberry Pi, use rpi-metrics-uninstall to uninstall it. If that command is not found, try this if you only want to uninstall this project, Python excluded:

curl -sSL https://qincai.xyz/rpi-metrics-uninstaller.sh | sudo bash -s - -wet

Use this if you want to remove Python and other packages installed as well:

curl -sSL https://qincai.xyz/rpi-metrics-uninstaller.sh | sudo bash -s - -extra-wet

[!WARNING] Passing the --extra-wet flag will remove python3, python3-pip, and python3-venv from your system. USE WITH CAUTION!!

Contributing

Contributions are welcome! Please create an issue or fork the repository and submit a pull request for review.

License

This project is licensed under the GPLv3 License. See the LICENSE file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

rpi_metrics_server-1.0.0.tar.gz (25.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

rpi_metrics_server-1.0.0-py3-none-any.whl (24.1 kB view details)

Uploaded Python 3

File details

Details for the file rpi_metrics_server-1.0.0.tar.gz.

File metadata

  • Download URL: rpi_metrics_server-1.0.0.tar.gz
  • Upload date:
  • Size: 25.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for rpi_metrics_server-1.0.0.tar.gz
Algorithm Hash digest
SHA256 3253c4dd325f33f907582e989d3a83455594312c32bda91efb954e9357ccb1c3
MD5 d9404d7633cefecb28985dabea2824a8
BLAKE2b-256 903a11b9b313a5f97ea615430d5d437606f3b7dba84d9e37758e5731a9967bef

See more details on using hashes here.

Provenance

The following attestation bundles were made for rpi_metrics_server-1.0.0.tar.gz:

Publisher: publish-pypi.yml on QinCai-rui/RPi-Metrics

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file rpi_metrics_server-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for rpi_metrics_server-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 252a0a99c2758341c80cf682f8ca1ef1b2f22edee1f28e83b1062cedc54095f5
MD5 f00e26777306fea0b929c3236e518db1
BLAKE2b-256 fc35e640c8378879a459608abf68d21c07382406b3112bf496f8499c787db5c1

See more details on using hashes here.

Provenance

The following attestation bundles were made for rpi_metrics_server-1.0.0-py3-none-any.whl:

Publisher: publish-pypi.yml on QinCai-rui/RPi-Metrics

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page