Skip to content

Build a hardware detector #419

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions Hardware Detector/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Hardware Resource Monitor in Python

## Description

This project is a Python-based hardware resource monitoring tool that provides real-time information about the system's hardware specifications and resource usage. It uses the `psutil` and `pynvml` libraries to gather data about the CPU, RAM, disk space, and GPU.

## Features

- Detects and displays GPU specifications, including:
- GPU name
- Total memory (in GB)
- Used memory (in GB)
- Detects and displays system specifications, including:
- Total and used RAM (in GB)
- Available disk space (in GB)
- Number of CPU cores
- CPU usage percentage
- Continuously monitors hardware resources with a customizable update interval.
- Displays data in a clean and user-friendly format in the console.

## Requirements

The following Python libraries are required to run the project:

- `psutil`
- `pynvml`

You can install the required dependencies using the following command:

```bash
pip install -r requirements.txt
```

## Usage

1. Clone the repository or download the project files.
2. Install the required dependencies using the `requirements.txt` file.
3. Run the `hardware_detector.py` script to start monitoring hardware resources:

```bash
python hardware_detector.py
```

4. Press `Ctrl+C` to stop the monitoring process.

## Notes

- Ensure that your system has a CUDA-enabled GPU with the correct drivers installed to retrieve GPU information.
- The script clears the console output on each update for a clean display. This behavior may vary depending on the operating system.

## License

This project is licensed under the MIT License. See the `LICENSE` file for more details.
76 changes: 76 additions & 0 deletions Hardware Detector/hardware_detector.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import psutil
import pynvml
import time
import os


class HardwareDetector:
"""
This class detects the hardware specifications of the computer and monitors them continuously.
"""

def __init__(self):
self.hardware_profile = {}

def get_gpu_specs(self) -> None:
"""
Detects the GPU specifications of the computer.
:return: None
"""
pynvml.nvmlInit()
device_count = pynvml.nvmlDeviceGetCount()
if device_count == 0:
self.hardware_profile['gpu_name'] = "None available"
self.hardware_profile['gpu_total_memory_gb'] = 0
self.hardware_profile['gpu_used_memory_gb'] = 0
return
gpu_handle = pynvml.nvmlDeviceGetHandleByIndex(0)
gpu_name = pynvml.nvmlDeviceGetName(gpu_handle)
gpu_mem_info = pynvml.nvmlDeviceGetMemoryInfo(gpu_handle)
gpu_total_mem = gpu_mem_info.total / (1024 ** 3)
gpu_used_mem = gpu_mem_info.used / (1024 ** 3)
pynvml.nvmlShutdown()
self.hardware_profile['gpu_name'] = gpu_name
self.hardware_profile['gpu_total_memory_gb'] = round(gpu_total_mem, 2)
self.hardware_profile['gpu_used_memory_gb'] = round(gpu_used_mem, 2)

def get_computer_specs(self) -> None:
"""
Detects the computer specifications including RAM, available disk space, and CPU cores.
:return: None
"""
memory = psutil.virtual_memory()
ram_total = memory.total
ram_used = memory.used
available_diskspace = psutil.disk_usage('/').free / (1024 ** 3)
cpu_cores = psutil.cpu_count(logical=True)
cpu_usage = psutil.cpu_percent(interval=0.1)
self.hardware_profile['ram_total_gb'] = round(ram_total / (1024 ** 3), 2)
self.hardware_profile['ram_used_gb'] = round(ram_used / (1024 ** 3), 2)
self.hardware_profile['available_diskspace_gb'] = round(available_diskspace, 2)
self.hardware_profile['cpu_cores'] = cpu_cores
self.hardware_profile['cpu_usage_percent'] = cpu_usage

def monitor_resources(self, interval: int = 1) -> None:
"""
Continuously monitors and displays hardware resources.
:param interval: Time in seconds between updates.
:return: None
"""
try:
while True:
self.get_computer_specs()
self.get_gpu_specs()
os.system('cls' if os.name == 'nt' else 'clear') # Clear the console for a clean display
print("Hardware Resource Monitor")
print("==========================")
for key, value in self.hardware_profile.items():
print(f"{key}: {value}")
time.sleep(interval)
except KeyboardInterrupt:
print("\nMonitoring stopped.")


# Run the continuous monitor
hardware = HardwareDetector()
hardware.monitor_resources(interval=0.5)
2 changes: 2 additions & 0 deletions Hardware Detector/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
psutil
pynvml