diff --git a/.vscode/settings.json b/.vscode/settings.json deleted file mode 100644 index a8c20032..00000000 --- a/.vscode/settings.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "python-envs.defaultEnvManager": "ms-python.python:conda", - "python-envs.defaultPackageManager": "ms-python.python:conda", - "python-envs.pythonProjects": [] -} \ No newline at end of file diff --git a/system_temperature_monitor/system_temperature_monitor b/system_temperature_monitor/system_temperature_monitor new file mode 100644 index 00000000..0a237705 --- /dev/null +++ b/system_temperature_monitor/system_temperature_monitor @@ -0,0 +1,87 @@ +import time +import os +from datetime import datetime + +try: + import psutil +except Exception: + psutil = None + +try: + from pynvml import nvmlInit, nvmlDeviceGetHandleByIndex, nvmlDeviceGetTemperature, nvmlShutdown, NVML_TEMPERATURE_GPU + nvmlInit() + NVML_AVAILABLE = True +except Exception: + NVML_AVAILABLE = False + +def clear(): + os.system('cls' if os.name == 'nt' else 'clear') + +def read_cpu_temps(): + if not psutil: + return {} + temps = psutil.sensors_temperatures(fahrenheit=False) + result = {} + for k, entries in temps.items(): + for e in entries: + name = f"{k}:{e.label or 'temp'}" + result[name] = e.current + return result + +def read_gpu_temps(): + if not NVML_AVAILABLE: + return {} + result = {} + try: + i = 0 + while True: + try: + handle = nvmlDeviceGetHandleByIndex(i) + except Exception: + break + temp = nvmlDeviceGetTemperature(handle, NVML_TEMPERATURE_GPU) + result[f"GPU:{i}"] = temp + i += 1 + except Exception: + pass + return result + +def format_table(cpu, gpu): + lines = [] + now = datetime.now().strftime("%Y-%m-%d %H:%M:%S") + lines.append(f"Timestamp: {now}") + lines.append("-" * 40) + if cpu: + lines.append("CPU Temperatures (°C):") + for k, v in cpu.items(): + lines.append(f"{k:<30} {v:6.1f}") + else: + lines.append("CPU temperature data not available via psutil.sensors_temperatures()") + lines.append("-" * 40) + if gpu: + lines.append("GPU Temperatures (°C):") + for k, v in gpu.items(): + lines.append(f"{k:<30} {v:6.1f}") + else: + lines.append("No NVidia GPU data available via pynvml") + return "\n".join(lines) + +def main(interval=1.0): + try: + while True: + cpu = read_cpu_temps() + gpu = read_gpu_temps() + clear() + print(format_table(cpu, gpu)) + time.sleep(interval) + except KeyboardInterrupt: + pass + finally: + if NVML_AVAILABLE: + try: + nvmlShutdown() + except Exception: + pass + +if __name__ == "__main__": + main()