From 124b08d1e1ed02f3505bf896417375d6f8246bf3 Mon Sep 17 00:00:00 2001 From: RETR0-OS Date: Fri, 11 Apr 2025 21:03:16 -0700 Subject: [PATCH 1/2] Build a hardware detector --- Hardware Detector/README.md | 54 ++++++++++++++++++ Hardware Detector/hardware_detector.py | 76 ++++++++++++++++++++++++++ Hardware Detector/requirements.txt | 3 + 3 files changed, 133 insertions(+) create mode 100644 Hardware Detector/README.md create mode 100644 Hardware Detector/hardware_detector.py create mode 100644 Hardware Detector/requirements.txt diff --git a/Hardware Detector/README.md b/Hardware Detector/README.md new file mode 100644 index 00000000..cf63726d --- /dev/null +++ b/Hardware Detector/README.md @@ -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. diff --git a/Hardware Detector/hardware_detector.py b/Hardware Detector/hardware_detector.py new file mode 100644 index 00000000..3086f962 --- /dev/null +++ b/Hardware Detector/hardware_detector.py @@ -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) \ No newline at end of file diff --git a/Hardware Detector/requirements.txt b/Hardware Detector/requirements.txt new file mode 100644 index 00000000..f767002e --- /dev/null +++ b/Hardware Detector/requirements.txt @@ -0,0 +1,3 @@ +psutil +pynvml +typing \ No newline at end of file From 02cadf3a6c27262fd5fe45484d13668d33d78ba6 Mon Sep 17 00:00:00 2001 From: RETR0-OS Date: Mon, 12 May 2025 12:34:52 -0700 Subject: [PATCH 2/2] update docs --- README.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index de76a6e9..68a9457a 100644 --- a/README.md +++ b/README.md @@ -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. | @@ -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. | @@ -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! |