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/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