sensor-monitor/threshold_alarm_logger: Log sensor name
This patch introduces an optional feature to append the sensor name to
the error message description. This helps in identifying the specific
sensor that triggered the alarm, improving debugging and visibility.
Change-Id: I32942ab74ebdcf0675a35718a4140d221c3aa20a
Signed-off-by: Patrick Rudolph <patrick.rudolph@9elements.com>
Signed-off-by: Naresh Solanki <naresh.solanki@9elements.com>
diff --git a/meson.build b/meson.build
index d2cc75d..5d8e3fd 100644
--- a/meson.build
+++ b/meson.build
@@ -157,6 +157,13 @@
get_option('sensor-monitor-soft-shutdown-delay'),
)
+log_sensor_name_on_error = get_option('log-sensor-name-on-error')
+if log_sensor_name_on_error
+ conf.set('LOG_SENSOR_NAME_ON_ERROR', '1')
+else
+ conf.set('LOG_SENSOR_NAME_ON_ERROR', '0')
+endif
+
# Presence
conf.set('NUM_PRESENCE_LOG_ENTRIES', get_option('num-presence-log-entries'))
conf.set_quoted('PRESENCE_YAML_FILE', get_option('presence-config'))
diff --git a/meson.options b/meson.options
index 1664f04..706da8b 100644
--- a/meson.options
+++ b/meson.options
@@ -156,6 +156,13 @@
description: 'Milliseconds to delay the alarm before soft shutdown.',
)
+option(
+ 'log-sensor-name-on-error',
+ type: 'boolean',
+ value: false,
+ description: 'Append sensor name to error messages.',
+)
+
# Other
option(
diff --git a/sensor-monitor/threshold_alarm_logger.cpp b/sensor-monitor/threshold_alarm_logger.cpp
index 6b20e77..545f01e 100644
--- a/sensor-monitor/threshold_alarm_logger.cpp
+++ b/sensor-monitor/threshold_alarm_logger.cpp
@@ -359,11 +359,30 @@
type.front() = toupper(type.front());
std::string errorName = errorNameBase + type + name + status;
+ if (LOG_SENSOR_NAME_ON_ERROR != 0)
+ {
+ errorName += " on sensor " + getSensorName(sensorPath);
+ }
SDBusPlus::callMethod(loggingService, loggingPath, loggingCreateIface,
"Create", errorName, convertForMessage(severity), ad);
}
+std::string ThresholdAlarmLogger::getSensorName(const std::string& sensorPath)
+{
+ auto pos = sensorPath.find_last_of('/');
+ if ((sensorPath.back() == '/') || (pos == std::string::npos))
+ {
+ log<level::ERR>(
+ std::format("Cannot get sensor name from sensor path {}",
+ sensorPath)
+ .c_str());
+ return "unknown_sensor";
+ }
+
+ return sensorPath.substr(pos + 1);
+}
+
std::string ThresholdAlarmLogger::getSensorType(std::string sensorPath)
{
auto pos = sensorPath.find_last_of('/');
diff --git a/sensor-monitor/threshold_alarm_logger.hpp b/sensor-monitor/threshold_alarm_logger.hpp
index 9524589..8b63360 100644
--- a/sensor-monitor/threshold_alarm_logger.hpp
+++ b/sensor-monitor/threshold_alarm_logger.hpp
@@ -157,6 +157,17 @@
std::string getSensorType(std::string sensorPath);
/**
+ * @brief Returns the name of the sensor using the path segment
+ *
+ * /xyz/openbmc_project/sensors/voltage/vout -> name == vout
+ *
+ * @param[in] sensorPath - The sensor object path name
+ *
+ * @return std::string The name segment
+ */
+ std::string getSensorName(const std::string& sensorPath);
+
+ /**
* @brief Allows for skipping event logs based on the sensor type.
*
* Specifically for the 'utilization' type because its provider