From 124b08d1e1ed02f3505bf896417375d6f8246bf3 Mon Sep 17 00:00:00 2001 From: RETR0-OS Date: Fri, 11 Apr 2025 21:03:16 -0700 Subject: [PATCH 1/3] 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 0000000..cf63726 --- /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 0000000..3086f96 --- /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 0000000..f767002 --- /dev/null +++ b/Hardware Detector/requirements.txt @@ -0,0 +1,3 @@ +psutil +pynvml +typing \ No newline at end of file From 16bc15e43f07e57213e3d78007e1358c026b0340 Mon Sep 17 00:00:00 2001 From: Aaditya Jindal <74290459+RETR0-OS@users.noreply.github.com> Date: Fri, 11 Apr 2025 21:22:39 -0700 Subject: [PATCH 2/3] Update requirements.txt --- Hardware Detector/requirements.txt | 1 - 1 file changed, 1 deletion(-) diff --git a/Hardware Detector/requirements.txt b/Hardware Detector/requirements.txt index f767002..63340d4 100644 --- a/Hardware Detector/requirements.txt +++ b/Hardware Detector/requirements.txt @@ -1,3 +1,2 @@ psutil pynvml -typing \ No newline at end of file From 23f53b6800014166d70356b1d760726516ba8d29 Mon Sep 17 00:00:00 2001 From: Aaditya Jindal <74290459+RETR0-OS@users.noreply.github.com> Date: Fri, 11 Apr 2025 21:23:00 -0700 Subject: [PATCH 3/3] Update README.md --- Hardware Detector/README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/Hardware Detector/README.md b/Hardware Detector/README.md index cf63726..937bf7a 100644 --- a/Hardware Detector/README.md +++ b/Hardware Detector/README.md @@ -24,7 +24,6 @@ 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: