From 4d8b4d611445b43bde78a8bb91232308bcf45e4c Mon Sep 17 00:00:00 2001 From: Spencer Date: Mon, 18 Aug 2025 13:01:11 -0500 Subject: [PATCH] fix: Handle missing sensors command gracefully --- monitor_agent.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/monitor_agent.py b/monitor_agent.py index 951c290..b28d3cf 100644 --- a/monitor_agent.py +++ b/monitor_agent.py @@ -45,18 +45,28 @@ def get_network_metrics(): def get_cpu_temperature(): """Gets the CPU temperature using the sensors command.""" + try: + subprocess.check_output(["sensors"], text=True) + except (subprocess.CalledProcessError, FileNotFoundError): + print("Error: 'sensors' command not found. Please install lm-sensors.") + return {"cpu_temperature": "N/A"} try: sensors_output = subprocess.check_output(["sensors"], text=True) parsed_sensors = jc.parse('sensors', sensors_output) # This is a simplified example, you may need to adjust the parsing logic based on your specific hardware cpu_temp = parsed_sensors[0]['values'][0]['input'] return {"cpu_temperature": cpu_temp} - except (subprocess.CalledProcessError, FileNotFoundError, KeyError, IndexError) as e: + except (subprocess.CalledProcessError, FileNotFoundError, KeyError, IndexError, jc.exceptions.ParseError) as e: print(f"Error getting CPU temperature: {e}") - return None + return {"cpu_temperature": "N/A"} def get_gpu_temperature(): """Gets the GPU temperature using the sensors command.""" + try: + subprocess.check_output(["sensors"], text=True) + except (subprocess.CalledProcessError, FileNotFoundError): + print("Error: 'sensors' command not found. Please install lm-sensors.") + return {"gpu_temperature": "N/A"} try: sensors_output = subprocess.check_output(["sensors"], text=True) parsed_sensors = jc.parse('sensors', sensors_output) @@ -67,7 +77,7 @@ def get_gpu_temperature(): gpu_temp = adapter['values'][0]['input'] return {"gpu_temperature": gpu_temp} return {"gpu_temperature": "N/A"} - except (subprocess.CalledProcessError, FileNotFoundError, KeyError, IndexError) as e: + except (subprocess.CalledProcessError, FileNotFoundError, KeyError, IndexError, jc.exceptions.ParseError) as e: print(f"Error getting GPU temperature: {e}") return {"gpu_temperature": "N/A"}