Home » Tutorials » How to Monitor System and GPU Information in Python

How to Monitor System and GPU Information in Python

Today, you’ll become the Sherlock Holmes of your computer world. I’ll guide you through every step to monitor and get your hardware information in Python using the psutil library. We’re also diving into the GPU territory with the GPUtil library.

PS: We’ve already seen this topic in our previous article, we used the wmi library for Windows users. But today’s tutorial works in all systems.

Let’s get started!

Table of Contents

Necessary Libraries

Let’s get everything set up before we jump into the code. So, make sure to install these libraries via the terminal or your command prompt:

$ pip install tk 
$ pip install psutil 
$ pip install gputil

Imports

As programmers, we would want our users to have an easy time navigating our programs so we use the tkinter library, which allows us to create a graphical user interface (GUI). Since our goal is to get hardware information across all systems (Windows, Linux, macOS), we need to import two libraries:

  • psutil: the psutil library retrieves information on running processes and system utilization.
  • GPUtil: retrieves GPU information.
import tkinter as tk
from tkinter import scrolledtext
import psutil
import GPUtil

Defining get_hardware_info Function

Next, we define this function that is responsible for acquiring hardware information and inserting it into the hardware_info widget to be displayed. So, without further ado let’s break down this function:

Clearing Previous Information

def get_hardware_info():
   hardware_info.delete(1.0, tk.END)  # Clear previous info

This line ensures that the hardware_info widget is empty by deleting any previously displayed text or information before triggering this function.

Fetching Disk Drive Information

   # Disk Drives
   hardware_info.insert(tk.END, "Disk Drives:\n")
   for partition in psutil.disk_partitions():
       hardware_info.insert(tk.END, f"Mountpoint: {partition.mountpoint}\n")
       try:
           usage = psutil.disk_usage(partition.mountpoint)
           hardware_info.insert(tk.END, f"Total Size: {usage.total / (1024 ** 3):.2f} GB\n")
           hardware_info.insert(tk.END, f"Used: {usage.used / (1024 ** 3):.2f} GB\n")
           hardware_info.insert(tk.END, f"Free: {usage.free / (1024 ** 3):.2f} GB\n")
           hardware_info.insert(tk.END, f"Percentage: {usage.percent}%\n\n")
       except PermissionError:
           hardware_info.insert(tk.END, "Access denied\n\n")

This part of the code begins with a heading labeled “Disk Drives“, then iterates over each disk drive to gather information, such as mount point, total size, and usage, using the psutil.disk_partitions() command. Afterward, it inserts this information into the hardware_info widget for display.

Fetching Network Adapter Information

   # Network Adapters
   hardware_info.insert(tk.END, "Network Adapters:\n")
   for interface, addrs in psutil.net_if_addrs().items():
       hardware_info.insert(tk.END, f"Interface: {interface}\n")
       for addr in addrs:
           hardware_info.insert(tk.END, f"  Address Family: {addr.family}\n")
           hardware_info.insert(tk.END, f"  Address: {addr.address}\n")
           if addr.netmask:
               hardware_info.insert(tk.END, f"  Netmask: {addr.netmask}\n")
           if addr.broadcast:
               hardware_info.insert(tk.END, f"  Broadcast IP: {addr.broadcast}\n")
           if addr.ptp:
               hardware_info.insert(tk.END, f"  MAC Address: {addr.ptp}\n")
           hardware_info.insert(tk.END, "\n")

Similar to the previous part it starts with a heading that says “Network Adapters“, and then it goes through each Adapter to retrieve information such as interface name, address family, address, netmask, broadcast IP, and MAC address using the command psutil.net_if_addrs().items(). Finally, it inserts this information into the hardware_info widget to be displayed.

Fetching GPU Information

   # Graphics Processing Units (GPUs)
   hardware_info.insert(tk.END, "Graphics Processing Units (GPUs):\n")
   gpus = GPUtil.getGPUs()
   if gpus:
       for gpu in gpus:
           hardware_info.insert(tk.END, f"Name: {gpu.name}\n")
           hardware_info.insert(tk.END, f"GPU ID: {gpu.id}\n")
           hardware_info.insert(tk.END, f"Load: {gpu.load * 100:.1f}%\n")
           hardware_info.insert(tk.END, f"Memory Total: {gpu.memoryTotal} MB\n")
           hardware_info.insert(tk.END, f"Memory Used: {gpu.memoryUsed} MB\n")
           hardware_info.insert(tk.END, f"Memory Free: {gpu.memoryFree} MB\n")
           hardware_info.insert(tk.END, f"Memory Utilization: {gpu.memoryUtil * 100:.1f}%\n")
           hardware_info.insert(tk.END, f"Temperature: {gpu.temperature} °C\n\n")
   else:
       hardware_info.insert(tk.END, "No compatible GPU found.\n")

Just like the two previous parts, this section begins with a heading labeled “Graphics Processing Unit“. Using the GPUtil.getGPUs() command, it searches for GPUs. If any are found, it then iterates over each one, retrieving information such as name, memory load, memory usage, etc.

This information is then inserted into the hardware_info widget to be displayed. However, if no compatible GPU is found, a message stating ‘No Compatible GPU found’ will be inserted into the hardware_info widget for display.

Creating the Main Window

# Create the main application window
root = tk.Tk()
root.title("Hardware Information - The Pycodes")

Following that, we create the main window and give it a title.

Creating a Scrolled Text Widget

# Create a scrolled text widget to display hardware info
hardware_info = scrolledtext.ScrolledText(root, width=80, height=30, wrap=tk.WORD)
hardware_info.pack(expand=True, fill="both", padx=10, pady=10)

Now, we create the hardware_info widget which will display the hardware information acquired by our function, we give it a specific height and width as well as specify how the text should be wrapped, and to make it easier for the user we make it scrollable.

Creating a Button

# Create a button to fetch hardware info
fetch_button = tk.Button(root, text="Fetch Hardware Info", command=get_hardware_info)
fetch_button.pack(pady=10)

After that, we create the “Fetch Hardware Info” button which will call the get_hardware_info Function when clicked.

Main Loop

# Run the Tkinter event loop
root.mainloop()

This part is the one responsible for keeping the main window running and responsive to the user until he exits willingly.

Example

Full Code

import tkinter as tk
from tkinter import scrolledtext
import psutil
import GPUtil


def get_hardware_info():
   hardware_info.delete(1.0, tk.END)  # Clear previous info


   # Disk Drives
   hardware_info.insert(tk.END, "Disk Drives:\n")
   for partition in psutil.disk_partitions():
       hardware_info.insert(tk.END, f"Mountpoint: {partition.mountpoint}\n")
       try:
           usage = psutil.disk_usage(partition.mountpoint)
           hardware_info.insert(tk.END, f"Total Size: {usage.total / (1024 ** 3):.2f} GB\n")
           hardware_info.insert(tk.END, f"Used: {usage.used / (1024 ** 3):.2f} GB\n")
           hardware_info.insert(tk.END, f"Free: {usage.free / (1024 ** 3):.2f} GB\n")
           hardware_info.insert(tk.END, f"Percentage: {usage.percent}%\n\n")
       except PermissionError:
           hardware_info.insert(tk.END, "Access denied\n\n")


   # Network Adapters
   hardware_info.insert(tk.END, "Network Adapters:\n")
   for interface, addrs in psutil.net_if_addrs().items():
       hardware_info.insert(tk.END, f"Interface: {interface}\n")
       for addr in addrs:
           hardware_info.insert(tk.END, f"  Address Family: {addr.family}\n")
           hardware_info.insert(tk.END, f"  Address: {addr.address}\n")
           if addr.netmask:
               hardware_info.insert(tk.END, f"  Netmask: {addr.netmask}\n")
           if addr.broadcast:
               hardware_info.insert(tk.END, f"  Broadcast IP: {addr.broadcast}\n")
           if addr.ptp:
               hardware_info.insert(tk.END, f"  MAC Address: {addr.ptp}\n")
           hardware_info.insert(tk.END, "\n")


   # Graphics Processing Units (GPUs)
   hardware_info.insert(tk.END, "Graphics Processing Units (GPUs):\n")
   gpus = GPUtil.getGPUs()
   if gpus:
       for gpu in gpus:
           hardware_info.insert(tk.END, f"Name: {gpu.name}\n")
           hardware_info.insert(tk.END, f"GPU ID: {gpu.id}\n")
           hardware_info.insert(tk.END, f"Load: {gpu.load * 100:.1f}%\n")
           hardware_info.insert(tk.END, f"Memory Total: {gpu.memoryTotal} MB\n")
           hardware_info.insert(tk.END, f"Memory Used: {gpu.memoryUsed} MB\n")
           hardware_info.insert(tk.END, f"Memory Free: {gpu.memoryFree} MB\n")
           hardware_info.insert(tk.END, f"Memory Utilization: {gpu.memoryUtil * 100:.1f}%\n")
           hardware_info.insert(tk.END, f"Temperature: {gpu.temperature} °C\n\n")
   else:
       hardware_info.insert(tk.END, "No compatible GPU found.\n")



# Create the main application window
root = tk.Tk()
root.title("Hardware Information - The Pycodes")


# Create a scrolled text widget to display hardware info
hardware_info = scrolledtext.ScrolledText(root, width=80, height=30, wrap=tk.WORD)
hardware_info.pack(expand=True, fill="both", padx=10, pady=10)


# Create a button to fetch hardware info
fetch_button = tk.Button(root, text="Fetch Hardware Info", command=get_hardware_info)
fetch_button.pack(pady=10)


# Run the Tkinter event loop
root.mainloop()

Happy Coding!

Subscribe for Top Free Python Tutorials!

Receive the best directly.  Elevate Your Coding Journey!

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top
×