Raspberry Pi System Stats

Live system stats are the "hello world" of Raspberry Pi networking. In this guide you'll learn about a couple of Python modules:

  • flask - for serving web pages
  • psutil - for getting system stats

By combining these, you will create a simple HTTP server that delivers your system stats in a RESTful fashion. You can jazz this up with javascript to create realtime stats, or just merge everything into one page for a quick overview of your Pi!

About the modules

Flask is a simple and expressive framework for serving web pages from Python. These can be either static or dynamic, we'll just be using simple routes to serve CPU, Memory and Disk information to three different URLs.

psutil ( Python System & Process Utilities ) is a great little cross-platform module for retrieving various snippets of system information. This will supply the CPU percentage and Memory/Disk utilisation stats that we'll be serving with Flask.


Before we start running any scripts, you'll need to install both psutil and Flask, this can be done using the Python Package Manager; pip.

First you'll need to install pip itself ( skip this step if you've already done so ):

sudo apt-get install python-pip

Then you'll need to install flask and psutil:

sudo pip install psutil flask

The Code

Glueing Flask and psutil together into a simple system-stats web service is simple.

The following is a barebones example which will serve up CPU percentage at /cpu and available/total memory at /memory.

Download Code

Right-click and download: system-stats.py

import psutil

from flask import Flask

app = Flask(__name__)
#app.debug = True # Uncomment to debug

def home():
    return 'System Stats!'

def cpu():
    return str(psutil.cpu_percent()) + '%'

def memory():
    memory = psutil.virtual_memory()
    # Divide from Bytes -> KB -> MB
    available = round(memory.available/1024.0/1024.0,1)
    total = round(memory.total/1024.0/1024.0,1)
    return str(available) + 'MB free / ' + str(total) + 'MB total ( ' + str(memory.percent) + '% )'

def disk():
    disk = psutil.disk_usage('/')
    # Divide from Bytes -> KB -> MB -> GB
    free = round(disk.free/1024.0/1024.0/1024.0,1)
    total = round(disk.total/1024.0/1024.0/1024.0,1)
    return str(free) + 'GB free / ' + str(total) + 'GB total ( ' + str(disk.percent) + '% )'

if __name__ == '__main__':

Download this code to your Raspberry Pi and run with:

python system-stats.py

Shopping basket

Need something for this project? You can use the links below to add products to your Pimoroni Shop basket for easy checkout.

Raspberry Pi 3 with Pibow Case
Rainbow £44.00
Raspberry Pi 3 with Pibow Case
Ninja £44.00
Raspberry Pi 3 with Pibow Case
Ninja Coupé £40.00
Raspberry Pi 3 with Pibow Case
Coupé £40.00
Want to checkout or change something? Click here to view your cart.

Raspberry Pi, Python

Phil Howard

Phil is Pimoroni's software guru, instantly recognisable by his somewhat pirate-themed moustache growing attempts. Usually found buried neck deep in Python libraries, he's also been known to escape on occasion and turn out crazy new products. If you need a helping hand, he's a prolific Twitter user and rampages around the forums like a T-Rex. Ask him if you need help with Pimoroni's software libraries, or Propeller HAT.