How to Check CPU and Disk Health Using Python
Monitoring your computer's performance is an essential part of keeping it running smoothly. In this tutorial, I'll share a simple Python script that helps you check your system's CPU usage and basic disk health information in just a few seconds.
Whether you're a beginner learning Python or someone interested in system monitoring and automation, this script is easy to understand and customize. You'll learn how Python can be used to retrieve real-time system information and build practical utilities for everyday use.
Feel free to copy the script, experiment with it, and modify it to suit your own needs. Happy coding!
import psutil
def check_cpu(threshold=85):
usage = psutil.cpu_percent(interval=1)
status = "CRITICAL" if usage >= threshold else "OK"
print(f"CPU Usage : {usage}% [{status}]")
return usage
def check_disk(path="/", threshold=85):
disk = psutil.disk_usage(path)
usage_pct = disk.percent
status = "CRITICAL" if usage_pct >= threshold else "OK"
print(f"Disk Usage : {usage_pct}% [{status}]")
print(f" Total : {disk.total / (1024**3):.2f} GB")
print(f" Used : {disk.used / (1024**3):.2f} GB")
print(f" Free : {disk.free / (1024**3):.2f} GB")
return usage_pct
def system_health(cpu_threshold=85, disk_threshold=85, disk_path="C:\\"):
print("=" * 40)
print(" SYSTEM HEALTH REPORT")
print("=" * 40)
cpu = check_cpu(threshold=cpu_threshold)
print("-" * 40)
disk = check_disk(path=disk_path, threshold=disk_threshold)
print("=" * 40)
if cpu >= cpu_threshold or disk >= disk_threshold:
print("⚠ WARNING: One or more metrics are critical!")
else:
print("✓ All systems healthy.")
if __name__ == "__main__":
system_health()
Comments
Post a Comment