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
54 changes: 54 additions & 0 deletions Hardware Detector/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# 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`
- `typing` (built-in for Python 3.5+)

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)
3 changes: 3 additions & 0 deletions Hardware Detector/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
psutil
pynvml
typing
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ More information on contributing and the general code of conduct for discussion
## List of Scripts in Repo

| Script | Link | Description |
| ---------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| ---------------------------------------- |----------------------------------------------------------------------------------------------------------------------------------------------------------| ----------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Arrange It | [Arrange It](https://github.com/DhanushNehru/Python-Scripts/tree/master/Arrange%20It) | A Python script that can automatically move files into corresponding folders based on their extensions. |
| Auto WiFi Check | [Auto WiFi Check](https://github.com/DhanushNehru/Python-Scripts/tree/master/Auto%20WiFi%20Check) | A Python script to monitor if the WiFi connection is active or not |
| AutoCert | [AutoCert](https://github.com/DhanushNehru/Python-Scripts/tree/master/AutoCert) | A Python script to auto-generate e-certificates in bulk. |
Expand Down Expand Up @@ -73,6 +73,7 @@ More information on contributing and the general code of conduct for discussion
| Get Hexcodes From Websites | [Get Hexcodes From Websites](https://github.com/DhanushNehru/Python-Scripts/tree/master/Get%20Hexcodes%20From%20Websites) | Generates a Python list containing Hexcodes from a website. |
| Hand_Volume | [Hand_Volume](https://github.com/DhanushNehru/Python-Scripts/tree/master/Hand%20Volume) | Detects and tracks hand movements to control volume. |
| Hangman Game | [Hangman](https://github.com/DhanushNehru/Python-Scripts/tree/master/Hangman%20Game) | A classic word-guessing game where players guess letters to find the hidden word |
| Hardware Detector | [Hardware Detector](https://github.com/DhanushNehru/Python-Scripts/tree/master/Hardware%20Detector) | A Python script that detects and displays hardware specifications and resource usage, including CPU, RAM, disk space, and GPU information. |
| Harvest Predictor | [Harvest Predictor](https://github.com/DhanushNehru/Python-Scripts/tree/master/Harvest%20Predictor) | Takes some necessary input parameters and predicts harvest according to it. |
| Html-to-images | [html-to-images](https://github.com/DhanushNehru/Python-Scripts/tree/master/HTML%20to%20Images) | Converts HTML documents to image files. |
| Image Capture | [Image Capture](https://github.com/DhanushNehru/Python-Scripts/tree/master/Image%20Capture) | Captures image from your webcam and saves it on your local device. |
Expand Down Expand Up @@ -110,7 +111,7 @@ More information on contributing and the general code of conduct for discussion
| PDF to Audio | [PDF to Audio](https://github.com/DhanushNehru/Python-Scripts/tree/master/PDF%20to%20Audio) | Converts PDF to audio. |
| PDF to Text | [PDF to text](https://github.com/DhanushNehru/Python-Scripts/tree/master/PDF%20to%20text) | Converts PDF to text. |
| PDF merger and splitter | [PDF Merger and Splitter](https://github.com/AbhijitMotekar99/Python-Scripts/blob/master/PDF%20Merger%20and%20Splitter/PDF%20Merger%20and%20Splitter.py) | Create a tool that can merge multiple PDF files into one or split a single PDF into separate pages. |
| Pizza Order | [Pizza Order](https://github.com/DhanushNehru/Python-Scripts/tree/master/Pizza%20Order) | An algorithm designed to handle pizza orders from customers with accurate receipts and calculations. |
| Pizza Order | [Pizza Order](https://github.com/DhanushNehru/Python-Scripts/tree/master/Pizza%20Order) | An algorithm designed to handle pizza orders from customers with accurate receipts and calculations. |
| Planet Simulation | [Planet Simulation](https://github.com/DhanushNehru/Python-Scripts/tree/master/Planet%20Simulation) | A simulation of several planets rotating around the sun. |
| Playlist Exchange | [Playlist Exchange](https://github.com/DhanushNehru/Python-Scripts/tree/master/Playlist%20Exchange) | A Python script to exchange songs and playlists between Spotify and Python. |
| Pigeonhole Sort | [Algorithm](https://github.com/DhanushNehru/Python-Scripts/tree/master/PigeonHole) | The pigeonhole sort algorithm to sort your arrays efficiently! |
Expand Down