Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Import the psutil library for system information
- import psutil
- import time
- def system_monitor(interval=5):
- """
- Monitors CPU, memory, and disk usage.
- interval: time in seconds between updates
- """
- while True:
- # CPU usage percentage over 1 second
- cpu = psutil.cpu_percent(interval=1)
- # Memory usage details
- memory = psutil.virtual_memory()
- # Disk usage details for root directory
- disk = psutil.disk_usage('/')
- # Print results in a clean format
- print("="*40)
- print(f"CPU Usage: {cpu}%")
- print(f"Memory Usage: {memory.percent}% "
- f"({memory.used // (1024**2)} MB / {memory.total // (1024**2)} MB)")
- print(f"Disk Usage: {disk.percent}% "
- f"({disk.used // (1024**3)} GB / {disk.total // (1024**3)} GB)")
- print("="*40)
- # Wait before repeating
- time.sleep(interval)
- if __name__ == "__main__":
- # Run the monitor with updates every 5 seconds
- system_monitor(interval=5)
Advertisement
Add Comment
Please, Sign In to add comment