The Palos Publishing Company

Follow Us On The X Platform @PalosPublishing
Categories We Write About

Build a Python-powered resource manager

A Python-powered resource manager can help monitor, allocate, and manage system resources like CPU, memory, disk space, and active processes. Below is a basic implementation of a resource manager using Python. It features:

  • CPU and memory usage tracking

  • Process listing and filtering

  • Disk usage monitoring

  • A simple CLI menu interface

We’ll use the following libraries:

  • psutil for system resource tracking

  • os and platform for system information

  • time for periodic updates

python
import psutil import os import platform import time def clear_screen(): os.system('cls' if os.name == 'nt' else 'clear') def get_system_info(): uname = platform.uname() return { "System": uname.system, "Node Name": uname.node, "Release": uname.release, "Version": uname.version, "Machine": uname.machine, "Processor": uname.processor } def show_cpu_info(): print("CPU Info:") print(f"Logical CPUs: {psutil.cpu_count(logical=True)}") print(f"Physical CPUs: {psutil.cpu_count(logical=False)}") print("CPU Usage Per Core:") for i, percentage in enumerate(psutil.cpu_percent(percpu=True, interval=1)): print(f" Core {i + 1}: {percentage}%") print(f"Total CPU Usage: {psutil.cpu_percent()}%") def show_memory_info(): mem = psutil.virtual_memory() print("Memory Info:") print(f"Total: {get_size(mem.total)}") print(f"Available: {get_size(mem.available)}") print(f"Used: {get_size(mem.used)} ({mem.percent}%)") def show_disk_info(): print("Disk Info:") partitions = psutil.disk_partitions() for p in partitions: print(f"Device: {p.device}") try: usage = psutil.disk_usage(p.mountpoint) print(f" Total Size: {get_size(usage.total)}") print(f" Used: {get_size(usage.used)} ({usage.percent}%)") print(f" Free: {get_size(usage.free)}") except PermissionError: print(" [Permission Denied]") def show_processes(limit=10): print(f"Top {limit} Processes by Memory Usage:") processes = [(p.pid, p.info['name'], p.info['memory_info'].rss) for p in psutil.process_iter(['name', 'memory_info'])] processes = sorted(processes, key=lambda x: x[2], reverse=True) for pid, name, mem in processes[:limit]: print(f"PID: {pid}, Name: {name}, Memory: {get_size(mem)}") def get_size(bytes, suffix="B"): factor = 1024 for unit in ["", "K", "M", "G", "T"]: if bytes < factor: return f"{bytes:.2f}{unit}{suffix}" bytes /= factor def menu(): while True: clear_screen() print("Python Resource Manager") print("=======================") print("1. Show System Info") print("2. Show CPU Info") print("3. Show Memory Info") print("4. Show Disk Info") print("5. Show Top Processes") print("6. Exit") choice = input("Enter your choice: ") clear_screen() if choice == '1': for k, v in get_system_info().items(): print(f"{k}: {v}") elif choice == '2': show_cpu_info() elif choice == '3': show_memory_info() elif choice == '4': show_disk_info() elif choice == '5': show_processes() elif choice == '6': print("Exiting Resource Manager...") break else: print("Invalid choice. Please select a valid option.") input("nPress Enter to return to the menu...") if __name__ == "__main__": menu()

Setup Instructions:

  1. Install dependencies:

    bash
    pip install psutil
  2. Run the script:

    bash
    python resource_manager.py

Optional Enhancements:

  • Add logging functionality for long-term monitoring.

  • Export stats to CSV or JSON.

  • Build a GUI using Tkinter, PyQt, or Kivy.

  • Add alert system for thresholds (e.g., notify if memory usage > 90%).

Let me know if you want a web-based or GUI version next.

Share this Page your favorite way: Click any app below to share.

Enter your email below to join The Palos Publishing Company Email List

We respect your email privacy

Categories We Write About