|
| 1 | +""" |
| 2 | +System Resource Monitor |
| 3 | +Author: Shaurya Shah |
| 4 | +
|
| 5 | +A lightweight CLI tool to monitor real-time CPU, Memory, and Disk usage |
| 6 | +with warning thresholds and formatted metrics. |
| 7 | +""" |
| 8 | + |
| 9 | +import os |
| 10 | +import shutil |
| 11 | +import sys |
| 12 | +import time |
| 13 | + |
| 14 | +try: |
| 15 | + import psutil |
| 16 | +except ImportError: |
| 17 | + psutil = None |
| 18 | + |
| 19 | + |
| 20 | +def format_bytes(bytes_num: int) -> str: |
| 21 | + """Format bytes to human-readable strings (MB, GB, TB).""" |
| 22 | + for unit in ['B', 'KB', 'MB', 'GB', 'TB']: |
| 23 | + if bytes_num < 1024: |
| 24 | + return f"{bytes_num:.2f} {unit}" |
| 25 | + bytes_num /= 1024 |
| 26 | + return f"{bytes_num:.2f} PB" |
| 27 | + |
| 28 | + |
| 29 | +def get_disk_usage(path: str = "/"): |
| 30 | + """Fetch disk usage statistics across platforms.""" |
| 31 | + try: |
| 32 | + total, used, free = shutil.disk_usage(path) |
| 33 | + percent = (used / total) * 100 |
| 34 | + return { |
| 35 | + "total": format_bytes(total), |
| 36 | + "used": format_bytes(used), |
| 37 | + "free": format_bytes(free), |
| 38 | + "percent": f"{percent:.1f}%", |
| 39 | + "warning": percent > 85.0 |
| 40 | + } |
| 41 | + except Exception as e: |
| 42 | + return { |
| 43 | + "total": "N/A", |
| 44 | + "used": "N/A", |
| 45 | + "free": "N/A", |
| 46 | + "percent": "N/A", |
| 47 | + "warning": False |
| 48 | + } |
| 49 | + |
| 50 | + |
| 51 | +def display_metrics(): |
| 52 | + """Print current system health metrics.""" |
| 53 | + print("=" * 55) |
| 54 | + print(" REAL-TIME SYSTEM RESOURCE MONITOR ") |
| 55 | + print("=" * 55) |
| 56 | + |
| 57 | + # Disk Info |
| 58 | + root_path = os.path.abspath(os.sep) |
| 59 | + disk = get_disk_usage(root_path) |
| 60 | + print(f"\n[Disk Usage - Root Drive]") |
| 61 | + print(f" Total Space: {disk['total']}") |
| 62 | + print(f" Used Space: {disk['used']} ({disk['percent']})") |
| 63 | + print(f" Free Space: {disk['free']}") |
| 64 | + if disk["warning"]: |
| 65 | + print(" [!] WARNING: Low disk space detected!") |
| 66 | + |
| 67 | + # CPU & Memory (if psutil is available) |
| 68 | + if psutil: |
| 69 | + cpu_pct = psutil.cpu_percent(interval=1) |
| 70 | + mem = psutil.virtual_memory() |
| 71 | + print(f"\n[CPU & Memory Usage]") |
| 72 | + print(f" CPU Utilization: {cpu_pct}%") |
| 73 | + print(f" RAM Used: {format_bytes(mem.used)} / {format_bytes(mem.total)} ({mem.percent}%)") |
| 74 | + print(f" RAM Available: {format_bytes(mem.available)}") |
| 75 | + if cpu_pct > 90.0: |
| 76 | + print(" [!] WARNING: High CPU utilization!") |
| 77 | + if mem.percent > 90.0: |
| 78 | + print(" [!] WARNING: Critical memory usage!") |
| 79 | + else: |
| 80 | + print("\n[CPU & Memory Usage]") |
| 81 | + print(" Tip: Install 'psutil' (pip install psutil) to display live CPU and RAM stats.") |
| 82 | + |
| 83 | + print("\n" + "=" * 55) |
| 84 | + |
| 85 | + |
| 86 | +def main(): |
| 87 | + display_metrics() |
| 88 | + |
| 89 | + |
| 90 | +if __name__ == "__main__": |
| 91 | + main() |
0 commit comments