Collecting Live Disk Space, CPU, and Process Data
๐ท๏ธ Final Capstone Engineer Script project / Project: System Health Reporter
๐ง Context Introduction
As you build toward your final capstone project, one of the most practical skills you'll need is the ability to gather live system data. Engineers often need to check disk usage, CPU load, and running processes to troubleshoot issues or monitor system health. This section teaches you how to collect that live data using Python, so you can build scripts that report on system status in real time.
โ๏ธ Why Collect Live System Data?
- Disk Space Monitoring โ Prevents storage outages by alerting when disks are near capacity.
- CPU Usage Tracking โ Helps identify performance bottlenecks or runaway processes.
- Process Inspection โ Allows you to see what's running, find memory hogs, or kill stuck tasks.
- Automation Ready โ Live data collection is the foundation for automated health reports and alerting systems.
๐ Key Python Libraries for System Data
| Library | Purpose | Example Use |
|---|---|---|
| psutil | Cross-platform system monitoring | Get CPU %, disk usage, process list |
| os | Operating system interface | Check file paths, environment variables |
| platform | System information | Get OS name, architecture |
| time | Timing and delays | Add sleep intervals between checks |
๐ ๏ธ Collecting Live Disk Space Data
- Use psutil.disk_usage('/') to get disk statistics for the root partition.
- The result includes total, used, free, and percent values.
- To check multiple mount points, loop through psutil.disk_partitions().
- Convert bytes to human-readable format (GB or MB) by dividing by 1024 twice.
- Example: disk_usage = psutil.disk_usage('/') then access disk_usage.percent for usage percentage.
๐ต๏ธ Collecting Live CPU Data
- Use psutil.cpu_percent(interval=1) to get overall CPU usage over a 1-second sample.
- For per-core data, use psutil.cpu_percent(interval=1, percpu=True).
- To get CPU frequency, use psutil.cpu_freq() which returns current, min, and max MHz.
- For load averages (Unix/Linux), use psutil.getloadavg() which returns 1, 5, and 15-minute averages.
- Example: cpu_usage = psutil.cpu_percent(interval=1) gives a single float percentage.
๐ Collecting Live Process Data
- Use psutil.process_iter(['pid', 'name', 'cpu_percent', 'memory_percent']) to iterate over running processes.
- Each process object has attributes like pid, name, cpu_percent(), and memory_percent().
- To find a specific process by name, loop through and check proc.info['name'].
- For detailed memory info on a single process, use psutil.Process(pid).memory_info().
- Example: for proc in psutil.process_iter(['pid', 'name']): print(proc.info) prints all running processes.
๐ Combining Data into a Health Snapshot
- Create a dictionary to store all collected data in one place.
- Structure it like: {'disk': disk_data, 'cpu': cpu_data, 'processes': top_processes}
- Filter processes to show only the top 5 by CPU or memory usage using sorted() with a key function.
- Add timestamps using time.strftime('%Y-%m-%d %H:%M:%S') for each snapshot.
- Example: snapshot = {'timestamp': now, 'cpu_percent': cpu, 'disk_percent': disk.percent}
๐งช Practical Tips for Live Data Collection
- Always use a short interval parameter (like 0.5 or 1 second) for CPU readings to get accurate results.
- Handle permission errors gracefully with try/except blocks when accessing process details.
- Use sleep() from the time module to space out data collection in monitoring loops.
- Store historical data in a list to track changes over time for trend analysis.
- Remember that psutil needs to be installed via pip install psutil before importing.
๐ Next Steps for Your Capstone Project
- Build a function that returns a formatted string of current system health.
- Extend your script to log data to a file with timestamps.
- Add threshold alerts that print warnings when disk usage exceeds 90% or CPU stays above 80%.
- Experiment with filtering processes to show only those using more than 5% CPU or memory.
- Consider adding network I/O stats using psutil.net_io_counters() for a complete picture.
โ Summary
Collecting live disk space, CPU, and process data is a foundational skill for any engineer building system monitoring tools. With psutil, you can access all major system metrics in a clean, cross-platform way. By combining these data points into a structured snapshot, you create the core logic for your final capstone health reporter project. Start small with one metric, then layer in additional data as you become comfortable with the library.
This file shows how to collect live system data about disk space, CPU usage, and running processes using Python's psutil library.
๐ฅ๏ธ Example 1: Get total disk space on the root drive
Shows how to retrieve the total disk space in bytes for the root partition.
import psutil
disk_usage = psutil.disk_usage('/')
total_bytes = disk_usage.total
print(total_bytes)
๐ค Output: 500107862016
๐ Example 2: Get disk usage percentage
Shows how to get the percentage of disk space currently used on the root drive.
import psutil
disk_usage = psutil.disk_usage('/')
percent_used = disk_usage.percent
print(percent_used)
๐ค Output: 45.2
๐ Example 3: Get current CPU usage percentage
Shows how to capture the current CPU usage as a percentage over a 1-second interval.
import psutil
cpu_percent = psutil.cpu_percent(interval=1)
print(cpu_percent)
๐ค Output: 23.7
๐ Example 4: Count the number of running processes
Shows how to get the total count of active processes on the system.
import psutil
process_count = len(psutil.pids())
print(process_count)
๐ค Output: 187
๐งฉ Example 5: Collect disk, CPU, and process data into a single report
Shows how to combine all three metrics into one readable summary for an engineer.
import psutil
disk = psutil.disk_usage('/')
cpu = psutil.cpu_percent(interval=1)
processes = len(psutil.pids())
print("Disk Used: " + str(disk.percent) + "%")
print("CPU Used: " + str(cpu) + "%")
print("Processes Running: " + str(processes))
๐ค Output: Disk Used: 45.2%
๐ค Output: CPU Used: 23.7%
๐ค Output: Processes Running: 187
๐ Quick Reference Table
| Metric | Function Used | Returns |
|---|---|---|
| Disk space | psutil.disk_usage('/') |
Usage statistics object |
| Disk percentage | .percent on disk object |
Float (0โ100) |
| CPU usage | psutil.cpu_percent(interval=1) |
Float (0โ100) |
| Process count | len(psutil.pids()) |
Integer |
๐ง Context Introduction
As you build toward your final capstone project, one of the most practical skills you'll need is the ability to gather live system data. Engineers often need to check disk usage, CPU load, and running processes to troubleshoot issues or monitor system health. This section teaches you how to collect that live data using Python, so you can build scripts that report on system status in real time.
โ๏ธ Why Collect Live System Data?
- Disk Space Monitoring โ Prevents storage outages by alerting when disks are near capacity.
- CPU Usage Tracking โ Helps identify performance bottlenecks or runaway processes.
- Process Inspection โ Allows you to see what's running, find memory hogs, or kill stuck tasks.
- Automation Ready โ Live data collection is the foundation for automated health reports and alerting systems.
๐ Key Python Libraries for System Data
| Library | Purpose | Example Use |
|---|---|---|
| psutil | Cross-platform system monitoring | Get CPU %, disk usage, process list |
| os | Operating system interface | Check file paths, environment variables |
| platform | System information | Get OS name, architecture |
| time | Timing and delays | Add sleep intervals between checks |
๐ ๏ธ Collecting Live Disk Space Data
- Use psutil.disk_usage('/') to get disk statistics for the root partition.
- The result includes total, used, free, and percent values.
- To check multiple mount points, loop through psutil.disk_partitions().
- Convert bytes to human-readable format (GB or MB) by dividing by 1024 twice.
- Example: disk_usage = psutil.disk_usage('/') then access disk_usage.percent for usage percentage.
๐ต๏ธ Collecting Live CPU Data
- Use psutil.cpu_percent(interval=1) to get overall CPU usage over a 1-second sample.
- For per-core data, use psutil.cpu_percent(interval=1, percpu=True).
- To get CPU frequency, use psutil.cpu_freq() which returns current, min, and max MHz.
- For load averages (Unix/Linux), use psutil.getloadavg() which returns 1, 5, and 15-minute averages.
- Example: cpu_usage = psutil.cpu_percent(interval=1) gives a single float percentage.
๐ Collecting Live Process Data
- Use psutil.process_iter(['pid', 'name', 'cpu_percent', 'memory_percent']) to iterate over running processes.
- Each process object has attributes like pid, name, cpu_percent(), and memory_percent().
- To find a specific process by name, loop through and check proc.info['name'].
- For detailed memory info on a single process, use psutil.Process(pid).memory_info().
- Example: for proc in psutil.process_iter(['pid', 'name']): print(proc.info) prints all running processes.
๐ Combining Data into a Health Snapshot
- Create a dictionary to store all collected data in one place.
- Structure it like: {'disk': disk_data, 'cpu': cpu_data, 'processes': top_processes}
- Filter processes to show only the top 5 by CPU or memory usage using sorted() with a key function.
- Add timestamps using time.strftime('%Y-%m-%d %H:%M:%S') for each snapshot.
- Example: snapshot = {'timestamp': now, 'cpu_percent': cpu, 'disk_percent': disk.percent}
๐งช Practical Tips for Live Data Collection
- Always use a short interval parameter (like 0.5 or 1 second) for CPU readings to get accurate results.
- Handle permission errors gracefully with try/except blocks when accessing process details.
- Use sleep() from the time module to space out data collection in monitoring loops.
- Store historical data in a list to track changes over time for trend analysis.
- Remember that psutil needs to be installed via pip install psutil before importing.
๐ Next Steps for Your Capstone Project
- Build a function that returns a formatted string of current system health.
- Extend your script to log data to a file with timestamps.
- Add threshold alerts that print warnings when disk usage exceeds 90% or CPU stays above 80%.
- Experiment with filtering processes to show only those using more than 5% CPU or memory.
- Consider adding network I/O stats using psutil.net_io_counters() for a complete picture.
โ Summary
Collecting live disk space, CPU, and process data is a foundational skill for any engineer building system monitoring tools. With psutil, you can access all major system metrics in a clean, cross-platform way. By combining these data points into a structured snapshot, you create the core logic for your final capstone health reporter project. Start small with one metric, then layer in additional data as you become comfortable with the library.
Interactive Views
You are currently in ๐ All-in-One mode. Use the tabs at the top to switch to ๐ Theory Only or ๐ป Code Only views.
This file shows how to collect live system data about disk space, CPU usage, and running processes using Python's psutil library.
๐ฅ๏ธ Example 1: Get total disk space on the root drive
Shows how to retrieve the total disk space in bytes for the root partition.
import psutil
disk_usage = psutil.disk_usage('/')
total_bytes = disk_usage.total
print(total_bytes)
๐ค Output: 500107862016
๐ Example 2: Get disk usage percentage
Shows how to get the percentage of disk space currently used on the root drive.
import psutil
disk_usage = psutil.disk_usage('/')
percent_used = disk_usage.percent
print(percent_used)
๐ค Output: 45.2
๐ Example 3: Get current CPU usage percentage
Shows how to capture the current CPU usage as a percentage over a 1-second interval.
import psutil
cpu_percent = psutil.cpu_percent(interval=1)
print(cpu_percent)
๐ค Output: 23.7
๐ Example 4: Count the number of running processes
Shows how to get the total count of active processes on the system.
import psutil
process_count = len(psutil.pids())
print(process_count)
๐ค Output: 187
๐งฉ Example 5: Collect disk, CPU, and process data into a single report
Shows how to combine all three metrics into one readable summary for an engineer.
import psutil
disk = psutil.disk_usage('/')
cpu = psutil.cpu_percent(interval=1)
processes = len(psutil.pids())
print("Disk Used: " + str(disk.percent) + "%")
print("CPU Used: " + str(cpu) + "%")
print("Processes Running: " + str(processes))
๐ค Output: Disk Used: 45.2%
๐ค Output: CPU Used: 23.7%
๐ค Output: Processes Running: 187
๐ Quick Reference Table
| Metric | Function Used | Returns |
|---|---|---|
| Disk space | psutil.disk_usage('/') |
Usage statistics object |
| Disk percentage | .percent on disk object |
Float (0โ100) |
| CPU usage | psutil.cpu_percent(interval=1) |
Float (0โ100) |
| Process count | len(psutil.pids()) |
Integer |