Skip to content

Commit 124b08d

Browse files
committed
Build a hardware detector
1 parent f552b18 commit 124b08d

File tree

3 files changed

+133
-0
lines changed

3 files changed

+133
-0
lines changed

Hardware Detector/README.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Hardware Resource Monitor in Python
2+
3+
## Description
4+
5+
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.
6+
7+
## Features
8+
9+
- Detects and displays GPU specifications, including:
10+
- GPU name
11+
- Total memory (in GB)
12+
- Used memory (in GB)
13+
- Detects and displays system specifications, including:
14+
- Total and used RAM (in GB)
15+
- Available disk space (in GB)
16+
- Number of CPU cores
17+
- CPU usage percentage
18+
- Continuously monitors hardware resources with a customizable update interval.
19+
- Displays data in a clean and user-friendly format in the console.
20+
21+
## Requirements
22+
23+
The following Python libraries are required to run the project:
24+
25+
- `psutil`
26+
- `pynvml`
27+
- `typing` (built-in for Python 3.5+)
28+
29+
You can install the required dependencies using the following command:
30+
31+
```bash
32+
pip install -r requirements.txt
33+
```
34+
35+
## Usage
36+
37+
1. Clone the repository or download the project files.
38+
2. Install the required dependencies using the `requirements.txt` file.
39+
3. Run the `hardware_detector.py` script to start monitoring hardware resources:
40+
41+
```bash
42+
python hardware_detector.py
43+
```
44+
45+
4. Press `Ctrl+C` to stop the monitoring process.
46+
47+
## Notes
48+
49+
- Ensure that your system has a CUDA-enabled GPU with the correct drivers installed to retrieve GPU information.
50+
- The script clears the console output on each update for a clean display. This behavior may vary depending on the operating system.
51+
52+
## License
53+
54+
This project is licensed under the MIT License. See the `LICENSE` file for more details.
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import psutil
2+
import pynvml
3+
import time
4+
import os
5+
6+
7+
class HardwareDetector:
8+
"""
9+
This class detects the hardware specifications of the computer and monitors them continuously.
10+
"""
11+
12+
def __init__(self):
13+
self.hardware_profile = {}
14+
15+
def get_gpu_specs(self) -> None:
16+
"""
17+
Detects the GPU specifications of the computer.
18+
:return: None
19+
"""
20+
pynvml.nvmlInit()
21+
device_count = pynvml.nvmlDeviceGetCount()
22+
if device_count == 0:
23+
self.hardware_profile['gpu_name'] = "None available"
24+
self.hardware_profile['gpu_total_memory_gb'] = 0
25+
self.hardware_profile['gpu_used_memory_gb'] = 0
26+
return
27+
gpu_handle = pynvml.nvmlDeviceGetHandleByIndex(0)
28+
gpu_name = pynvml.nvmlDeviceGetName(gpu_handle)
29+
gpu_mem_info = pynvml.nvmlDeviceGetMemoryInfo(gpu_handle)
30+
gpu_total_mem = gpu_mem_info.total / (1024 ** 3)
31+
gpu_used_mem = gpu_mem_info.used / (1024 ** 3)
32+
pynvml.nvmlShutdown()
33+
self.hardware_profile['gpu_name'] = gpu_name
34+
self.hardware_profile['gpu_total_memory_gb'] = round(gpu_total_mem, 2)
35+
self.hardware_profile['gpu_used_memory_gb'] = round(gpu_used_mem, 2)
36+
37+
def get_computer_specs(self) -> None:
38+
"""
39+
Detects the computer specifications including RAM, available disk space, and CPU cores.
40+
:return: None
41+
"""
42+
memory = psutil.virtual_memory()
43+
ram_total = memory.total
44+
ram_used = memory.used
45+
available_diskspace = psutil.disk_usage('/').free / (1024 ** 3)
46+
cpu_cores = psutil.cpu_count(logical=True)
47+
cpu_usage = psutil.cpu_percent(interval=0.1)
48+
self.hardware_profile['ram_total_gb'] = round(ram_total / (1024 ** 3), 2)
49+
self.hardware_profile['ram_used_gb'] = round(ram_used / (1024 ** 3), 2)
50+
self.hardware_profile['available_diskspace_gb'] = round(available_diskspace, 2)
51+
self.hardware_profile['cpu_cores'] = cpu_cores
52+
self.hardware_profile['cpu_usage_percent'] = cpu_usage
53+
54+
def monitor_resources(self, interval: int = 1) -> None:
55+
"""
56+
Continuously monitors and displays hardware resources.
57+
:param interval: Time in seconds between updates.
58+
:return: None
59+
"""
60+
try:
61+
while True:
62+
self.get_computer_specs()
63+
self.get_gpu_specs()
64+
os.system('cls' if os.name == 'nt' else 'clear') # Clear the console for a clean display
65+
print("Hardware Resource Monitor")
66+
print("==========================")
67+
for key, value in self.hardware_profile.items():
68+
print(f"{key}: {value}")
69+
time.sleep(interval)
70+
except KeyboardInterrupt:
71+
print("\nMonitoring stopped.")
72+
73+
74+
# Run the continuous monitor
75+
hardware = HardwareDetector()
76+
hardware.monitor_resources(interval=0.5)

Hardware Detector/requirements.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
psutil
2+
pynvml
3+
typing

0 commit comments

Comments
 (0)