Skip to content

Lightweight server monitoring with historical data, docker stats, and alerts.

fix(agent): keep LHM helper alive after sensor failures (#2624)

Jimmy Liu Jiae84eb25e1c6d
diff --git a/agent/lhm/beszel_lhm.cs b/agent/lhm/beszel_lhm.cs
index 638128795..9dc3ce052 100644
--- a/agent/lhm/beszel_lhm.cs
+++ b/agent/lhm/beszel_lhm.cs
@@ -51,8 +51,7 @@ class Program
var updated = false;
foreach (var sensor in hardware.Sensors)
{
- var validTemp = sensor.SensorType == SensorType.Temperature && sensor.Value.HasValue;
- if (!validTemp ||
+ if (sensor.SensorType != SensorType.Temperature ||
sensor.Name.IndexOf("Distance", StringComparison.OrdinalIgnoreCase) >= 0 ||
sensor.Name.IndexOf("Limit", StringComparison.OrdinalIgnoreCase) >= 0 ||
sensor.Name.IndexOf("Critical", StringComparison.OrdinalIgnoreCase) >= 0 ||
@@ -64,10 +63,25 @@ class Program
if (!updated)
{
- hardware.Update();
+ try
+ {
+ hardware.Update();
+ }
+ catch (Exception ex)
+ {
+ Console.Error.WriteLine($"Could not update {hardware.HardwareType} {hardware.Name}: {ex.Message}");
+ return;
+ }
updated = true;
}
+ // Read the value once after Update(), which may reset it to null.
+ var temperature = sensor.Value;
+ if (!temperature.HasValue)
+ {
+ continue;
+ }
+
var name = sensor.Name;
// if sensor.Name starts with "Temperature" replace with hardware.Identifier but retain the rest of the name.
// usually this is a number like Temperature 3
@@ -77,7 +91,7 @@ class Program
}
// invariant culture assures the value is parsable as a float
- var value = sensor.Value.Value.ToString("0.##", CultureInfo.InvariantCulture);
+ var value = temperature.Value.ToString("0.##", CultureInfo.InvariantCulture);
// write the name and value to the writer
writer.WriteLine($"{name}|{value}");
}