Python Project: A System Information Report
This project combines the ideas from the Python series into one useful command-line script. It gathers basic information about the machine, prints a readable report, and can save that report for later comparison.
The Script
Save this as system_report.py:
#!/usr/bin/env python3
import argparse
import platform
import shutil
import socket
from datetime import datetime, timezone
from pathlib import Path
def make_report():
total, used, free = shutil.disk_usage("/")
gigabyte = 1024 ** 3
lines = [
"System information report",
"==========================",
f"Collected: {datetime.now(timezone.utc).isoformat()}",
f"Hostname: {socket.gethostname()}",
f"System: {platform.system()} {platform.release()}",
f"Machine: {platform.machine()}",
f"Python: {platform.python_version()}",
f"Disk used: {used / gigabyte:.1f} GB "
f"of {total / gigabyte:.1f} GB",
f"Disk free: {free / gigabyte:.1f} GB",
]
return "\n".join(lines) + "\n"
def main():
parser = argparse.ArgumentParser(
description="Write a basic system information report."
)
parser.add_argument(
"-o", "--output",
type=Path,
help="also save the report to this file",
)
args = parser.parse_args()
report = make_report()
print(report, end="")
if args.output:
args.output.write_text(report, encoding="utf-8")
print(f"Saved report to {args.output}")
if __name__ == "__main__":
main()
Run It
$ python3 system_report.py System information report ========================== Collected: 2025-... Hostname: pi-gateway System: Linux ... Machine: aarch64 Python: 3.13.2 Disk used: 12.4 GB of 59.6 GB Disk free: 47.2 GB $ python3 system_report.py --output reports/pi-gateway.txt
What This Project Teaches
argparsemakes the script useful from a terminal.pathlib.Pathhandles the optional output file.- Functions keep data collection separate from command-line behavior.
- Standard-library modules provide useful information without extra packages.
- A saved report gives you something to compare after maintenance or a deployment.
From here, the report could be extended with memory usage, network interfaces, sensor readings, or a scheduled upload to a central machine. Add one measured feature at a time and keep the command useful when the network is unavailable.
Adding Raspberry Pi Memory Information
Linux exposes useful runtime information through virtual files under /proc. This is a simple way to add memory information without installing a package:
def memory_summary():
values = {}
for line in Path("/proc/meminfo").read_text().splitlines():
name, value = line.split(":", 1)
if name in {"MemTotal", "MemAvailable"}:
values[name] = value.strip()
return values
Keep Linux-specific code behind a platform check if the script should also run on macOS or Windows. A report should say when a value is unavailable rather than pretending every machine has the same files.
Scheduling a Report
For a report that runs unattended, first make sure it works interactively and writes to a directory the service user can access. A simple cron entry might run it every hour:
# Edit the user's crontab with: crontab -e 0 * * * * /home/pi/node-report/.venv/bin/python \ /home/pi/node-report/system_report.py \ --output /home/pi/reports/latest.txt
Use an absolute path, record errors somewhere you will inspect, and avoid placing passwords in the command line. For a long-running service, systemd provides better restart and logging controls.
Next Improvements
- Add a JSON output option for another program to consume.
- Record the report in a dated file so changes can be compared over time.
- Add a warning when free disk space falls below a chosen percentage.
- Read a node name from a configuration file instead of hard-coding it.
dispelled