The following program helps you to monitor the status of a raspberry pi. The common parameters that we monitor are CPU, Memory & Disk. This program can be used to get the info of any operating system. This is not limited to raspberry pi.
Here I am providing a simple program that provides the details of all these three parameters. You can enhance this program further and make it part of your application.
import psutil | |
# Get cpu statistics | |
cpu = str(psutil.cpu_percent()) + '%' | |
# Calculate memory information | |
memory = psutil.virtual_memory() | |
# Convert Bytes to MB (Bytes -> KB -> MB) | |
available = round(memory.available/1024.0/1024.0,1) | |
total = round(memory.total/1024.0/1024.0,1) | |
mem_info = str(available) + 'MB free / ' + str(total) + 'MB total ( ' + str(memory.percent) + '% )' | |
# Calculate disk information | |
disk = psutil.disk_usage('/') | |
# Convert Bytes to GB (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) | |
disk_info = str(free) + 'GB free / ' + str(total) + 'GB total ( ' + str(disk.percent) + '% )' | |
print("CPU Info–> ", cpu) | |
print("Memory Info–>", mem_info) | |
print("Disk Info–>", disk_info) |