Matt Spinler | 403d1f5 | 2021-02-01 15:35:25 -0600 | [diff] [blame] | 1 | /** |
| 2 | * Copyright © 2021 IBM Corporation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | #include "threshold_alarm_logger.hpp" |
| 17 | |
Matt Spinler | 50bf816 | 2021-02-01 16:24:01 -0600 | [diff] [blame] | 18 | #include "sdbusplus.hpp" |
| 19 | |
Matt Spinler | 3efec61 | 2021-05-11 15:26:17 -0500 | [diff] [blame] | 20 | #include <unistd.h> |
Matt Spinler | 50bf816 | 2021-02-01 16:24:01 -0600 | [diff] [blame] | 21 | |
| 22 | #include <phosphor-logging/log.hpp> |
| 23 | #include <xyz/openbmc_project/Logging/Entry/server.hpp> |
| 24 | |
Patrick Williams | fbf4703 | 2023-07-17 12:27:34 -0500 | [diff] [blame] | 25 | #include <format> |
| 26 | |
Matt Spinler | 403d1f5 | 2021-02-01 15:35:25 -0600 | [diff] [blame] | 27 | namespace sensor::monitor |
| 28 | { |
| 29 | |
Matt Spinler | 50bf816 | 2021-02-01 16:24:01 -0600 | [diff] [blame] | 30 | using namespace sdbusplus::xyz::openbmc_project::Logging::server; |
| 31 | using namespace phosphor::logging; |
Matt Spinler | 66e75a7 | 2021-05-14 10:32:47 -0500 | [diff] [blame] | 32 | using namespace phosphor::fan; |
Matt Spinler | 50bf816 | 2021-02-01 16:24:01 -0600 | [diff] [blame] | 33 | using namespace phosphor::fan::util; |
| 34 | |
Matt Spinler | 403d1f5 | 2021-02-01 15:35:25 -0600 | [diff] [blame] | 35 | const std::string warningInterface = |
| 36 | "xyz.openbmc_project.Sensor.Threshold.Warning"; |
| 37 | const std::string criticalInterface = |
| 38 | "xyz.openbmc_project.Sensor.Threshold.Critical"; |
| 39 | const std::string perfLossInterface = |
| 40 | "xyz.openbmc_project.Sensor.Threshold.PerformanceLoss"; |
Matt Spinler | 2f18267 | 2021-02-01 16:51:38 -0600 | [diff] [blame] | 41 | constexpr auto loggingService = "xyz.openbmc_project.Logging"; |
| 42 | constexpr auto loggingPath = "/xyz/openbmc_project/logging"; |
| 43 | constexpr auto loggingCreateIface = "xyz.openbmc_project.Logging.Create"; |
| 44 | constexpr auto errorNameBase = "xyz.openbmc_project.Sensor.Threshold.Error."; |
| 45 | constexpr auto valueInterface = "xyz.openbmc_project.Sensor.Value"; |
| 46 | constexpr auto assocInterface = "xyz.openbmc_project.Association"; |
Matt Spinler | 403d1f5 | 2021-02-01 15:35:25 -0600 | [diff] [blame] | 47 | |
Matt Spinler | 8ce6507 | 2022-11-03 15:15:55 -0400 | [diff] [blame] | 48 | const std::vector<std::string> thresholdIfaceNames{ |
| 49 | warningInterface, criticalInterface, perfLossInterface}; |
| 50 | |
Delphine CC Chiu | 99914e5 | 2024-05-21 17:38:58 +0800 | [diff] [blame] | 51 | using ErrorData = std::tuple<ErrorName, ErrorStatus, Entry::Level>; |
Matt Spinler | 50bf816 | 2021-02-01 16:24:01 -0600 | [diff] [blame] | 52 | |
| 53 | /** |
| 54 | * Map of threshold interfaces and alarm properties and values to error data. |
| 55 | */ |
| 56 | const std::map<InterfaceName, std::map<PropertyName, std::map<bool, ErrorData>>> |
| 57 | thresholdData{ |
| 58 | |
| 59 | {warningInterface, |
| 60 | {{"WarningAlarmHigh", |
Delphine CC Chiu | 99914e5 | 2024-05-21 17:38:58 +0800 | [diff] [blame] | 61 | {{true, ErrorData{"WarningHigh", "", Entry::Level::Warning}}, |
Matt Spinler | 50bf816 | 2021-02-01 16:24:01 -0600 | [diff] [blame] | 62 | {false, |
Delphine CC Chiu | 99914e5 | 2024-05-21 17:38:58 +0800 | [diff] [blame] | 63 | ErrorData{"WarningHigh", "Clear", Entry::Level::Informational}}}}, |
Matt Spinler | 50bf816 | 2021-02-01 16:24:01 -0600 | [diff] [blame] | 64 | {"WarningAlarmLow", |
Delphine CC Chiu | 99914e5 | 2024-05-21 17:38:58 +0800 | [diff] [blame] | 65 | {{true, ErrorData{"WarningLow", "", Entry::Level::Warning}}, |
Matt Spinler | 50bf816 | 2021-02-01 16:24:01 -0600 | [diff] [blame] | 66 | {false, |
Delphine CC Chiu | 99914e5 | 2024-05-21 17:38:58 +0800 | [diff] [blame] | 67 | ErrorData{"WarningLow", "Clear", Entry::Level::Informational}}}}}}, |
Matt Spinler | 50bf816 | 2021-02-01 16:24:01 -0600 | [diff] [blame] | 68 | |
| 69 | {criticalInterface, |
| 70 | {{"CriticalAlarmHigh", |
Delphine CC Chiu | 99914e5 | 2024-05-21 17:38:58 +0800 | [diff] [blame] | 71 | {{true, ErrorData{"CriticalHigh", "", Entry::Level::Critical}}, |
Matt Spinler | 50bf816 | 2021-02-01 16:24:01 -0600 | [diff] [blame] | 72 | {false, |
Delphine CC Chiu | 99914e5 | 2024-05-21 17:38:58 +0800 | [diff] [blame] | 73 | ErrorData{"CriticalHigh", "Clear", Entry::Level::Informational}}}}, |
Matt Spinler | 50bf816 | 2021-02-01 16:24:01 -0600 | [diff] [blame] | 74 | {"CriticalAlarmLow", |
Delphine CC Chiu | 99914e5 | 2024-05-21 17:38:58 +0800 | [diff] [blame] | 75 | {{true, ErrorData{"CriticalLow", "", Entry::Level::Critical}}, |
| 76 | {false, ErrorData{"CriticalLow", "Clear", |
| 77 | Entry::Level::Informational}}}}}}, |
Matt Spinler | 50bf816 | 2021-02-01 16:24:01 -0600 | [diff] [blame] | 78 | |
| 79 | {perfLossInterface, |
| 80 | {{"PerfLossAlarmHigh", |
Delphine CC Chiu | 99914e5 | 2024-05-21 17:38:58 +0800 | [diff] [blame] | 81 | {{true, ErrorData{"PerformanceLossHigh", "", Entry::Level::Warning}}, |
| 82 | {false, ErrorData{"PerformanceLossHigh", "Clear", |
| 83 | Entry::Level::Informational}}}}, |
Matt Spinler | 50bf816 | 2021-02-01 16:24:01 -0600 | [diff] [blame] | 84 | {"PerfLossAlarmLow", |
Delphine CC Chiu | 99914e5 | 2024-05-21 17:38:58 +0800 | [diff] [blame] | 85 | {{true, ErrorData{"PerformanceLossLow", "", Entry::Level::Warning}}, |
| 86 | {false, ErrorData{"PerformanceLossLow", "Clear", |
| 87 | Entry::Level::Informational}}}}}}}; |
Matt Spinler | 50bf816 | 2021-02-01 16:24:01 -0600 | [diff] [blame] | 88 | |
Matt Spinler | 7f6946b | 2021-05-14 12:43:50 -0500 | [diff] [blame] | 89 | ThresholdAlarmLogger::ThresholdAlarmLogger( |
Patrick Williams | cb356d4 | 2022-07-22 19:26:53 -0500 | [diff] [blame] | 90 | sdbusplus::bus_t& bus, sdeventplus::Event& event, |
Matt Spinler | 7f6946b | 2021-05-14 12:43:50 -0500 | [diff] [blame] | 91 | std::shared_ptr<PowerState> powerState) : |
Patrick Williams | dfddd64 | 2024-08-16 15:21:51 -0400 | [diff] [blame^] | 92 | bus(bus), event(event), _powerState(std::move(powerState)), |
Matt Spinler | 403d1f5 | 2021-02-01 15:35:25 -0600 | [diff] [blame] | 93 | warningMatch(bus, |
| 94 | "type='signal',member='PropertiesChanged'," |
| 95 | "path_namespace='/xyz/openbmc_project/sensors'," |
| 96 | "arg0='" + |
| 97 | warningInterface + "'", |
| 98 | std::bind(&ThresholdAlarmLogger::propertiesChanged, this, |
| 99 | std::placeholders::_1)), |
| 100 | criticalMatch(bus, |
| 101 | "type='signal',member='PropertiesChanged'," |
| 102 | "path_namespace='/xyz/openbmc_project/sensors'," |
| 103 | "arg0='" + |
| 104 | criticalInterface + "'", |
| 105 | std::bind(&ThresholdAlarmLogger::propertiesChanged, this, |
| 106 | std::placeholders::_1)), |
| 107 | perfLossMatch(bus, |
| 108 | "type='signal',member='PropertiesChanged'," |
| 109 | "path_namespace='/xyz/openbmc_project/sensors'," |
| 110 | "arg0='" + |
| 111 | perfLossInterface + "'", |
| 112 | std::bind(&ThresholdAlarmLogger::propertiesChanged, this, |
Matt Spinler | b7a5540 | 2021-10-11 13:45:35 -0500 | [diff] [blame] | 113 | std::placeholders::_1)), |
| 114 | ifacesRemovedMatch(bus, |
| 115 | "type='signal',member='InterfacesRemoved',arg0path=" |
| 116 | "'/xyz/openbmc_project/sensors/'", |
| 117 | std::bind(&ThresholdAlarmLogger::interfacesRemoved, this, |
Matt Spinler | 8ce6507 | 2022-11-03 15:15:55 -0400 | [diff] [blame] | 118 | std::placeholders::_1)), |
| 119 | ifacesAddedMatch(bus, |
| 120 | "type='signal',member='InterfacesAdded',arg0path=" |
| 121 | "'/xyz/openbmc_project/sensors/'", |
| 122 | std::bind(&ThresholdAlarmLogger::interfacesAdded, this, |
| 123 | std::placeholders::_1)) |
Matt Spinler | 50bf816 | 2021-02-01 16:24:01 -0600 | [diff] [blame] | 124 | { |
Matt Spinler | 7f6946b | 2021-05-14 12:43:50 -0500 | [diff] [blame] | 125 | _powerState->addCallback("thresholdMon", |
| 126 | std::bind(&ThresholdAlarmLogger::powerStateChanged, |
| 127 | this, std::placeholders::_1)); |
| 128 | |
Matt Spinler | 50bf816 | 2021-02-01 16:24:01 -0600 | [diff] [blame] | 129 | // check for any currently asserted threshold alarms |
Patrick Williams | dfddd64 | 2024-08-16 15:21:51 -0400 | [diff] [blame^] | 130 | std::for_each( |
| 131 | thresholdData.begin(), thresholdData.end(), |
| 132 | [this](const auto& thresholdInterface) { |
| 133 | const auto& interface = thresholdInterface.first; |
| 134 | auto objects = |
| 135 | SDBusPlus::getSubTreeRaw(this->bus, "/", interface, 0); |
| 136 | std::for_each(objects.begin(), objects.end(), |
| 137 | [interface, this](const auto& object) { |
| 138 | const auto& path = object.first; |
| 139 | const auto& service = |
| 140 | object.second.begin()->first; |
| 141 | checkThresholds(interface, path, service); |
| 142 | }); |
Matt Spinler | 50bf816 | 2021-02-01 16:24:01 -0600 | [diff] [blame] | 143 | }); |
| 144 | } |
Matt Spinler | 403d1f5 | 2021-02-01 15:35:25 -0600 | [diff] [blame] | 145 | |
Patrick Williams | cb356d4 | 2022-07-22 19:26:53 -0500 | [diff] [blame] | 146 | void ThresholdAlarmLogger::propertiesChanged(sdbusplus::message_t& msg) |
Matt Spinler | 403d1f5 | 2021-02-01 15:35:25 -0600 | [diff] [blame] | 147 | { |
Matt Spinler | f5d3be4 | 2021-02-01 16:38:01 -0600 | [diff] [blame] | 148 | std::map<std::string, std::variant<bool>> properties; |
| 149 | std::string sensorPath = msg.get_path(); |
| 150 | std::string interface; |
| 151 | |
| 152 | msg.read(interface, properties); |
| 153 | |
Matt Spinler | 8ce6507 | 2022-11-03 15:15:55 -0400 | [diff] [blame] | 154 | checkProperties(sensorPath, interface, properties); |
| 155 | } |
| 156 | |
| 157 | void ThresholdAlarmLogger::interfacesRemoved(sdbusplus::message_t& msg) |
| 158 | { |
| 159 | sdbusplus::message::object_path path; |
| 160 | std::vector<std::string> interfaces; |
| 161 | |
| 162 | msg.read(path, interfaces); |
| 163 | |
| 164 | for (const auto& interface : interfaces) |
| 165 | { |
| 166 | if (std::find(thresholdIfaceNames.begin(), thresholdIfaceNames.end(), |
| 167 | interface) != thresholdIfaceNames.end()) |
| 168 | { |
| 169 | alarms.erase(InterfaceKey{path, interface}); |
| 170 | } |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | void ThresholdAlarmLogger::interfacesAdded(sdbusplus::message_t& msg) |
| 175 | { |
| 176 | sdbusplus::message::object_path path; |
| 177 | std::map<std::string, std::map<std::string, std::variant<bool>>> interfaces; |
| 178 | |
| 179 | msg.read(path, interfaces); |
| 180 | |
| 181 | for (const auto& [interface, properties] : interfaces) |
| 182 | { |
| 183 | if (std::find(thresholdIfaceNames.begin(), thresholdIfaceNames.end(), |
| 184 | interface) != thresholdIfaceNames.end()) |
| 185 | { |
| 186 | checkProperties(path, interface, properties); |
| 187 | } |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | void ThresholdAlarmLogger::checkProperties( |
| 192 | const std::string& sensorPath, const std::string& interface, |
| 193 | const std::map<std::string, std::variant<bool>>& properties) |
| 194 | { |
Matt Spinler | f5d3be4 | 2021-02-01 16:38:01 -0600 | [diff] [blame] | 195 | auto alarmProperties = thresholdData.find(interface); |
| 196 | if (alarmProperties == thresholdData.end()) |
| 197 | { |
| 198 | return; |
| 199 | } |
| 200 | |
| 201 | for (const auto& [propertyName, propertyValue] : properties) |
| 202 | { |
| 203 | if (alarmProperties->second.find(propertyName) != |
| 204 | alarmProperties->second.end()) |
| 205 | { |
| 206 | // If this is the first time we've seen this alarm, then |
| 207 | // assume it was off before so it doesn't create an event |
| 208 | // log for a value of false. |
| 209 | |
| 210 | InterfaceKey key{sensorPath, interface}; |
| 211 | if (alarms.find(key) == alarms.end()) |
| 212 | { |
| 213 | alarms[key][propertyName] = false; |
| 214 | } |
| 215 | |
| 216 | // Check if the value changed from what was there before. |
| 217 | auto alarmValue = std::get<bool>(propertyValue); |
| 218 | if (alarmValue != alarms[key][propertyName]) |
| 219 | { |
| 220 | alarms[key][propertyName] = alarmValue; |
Matt Spinler | 66e75a7 | 2021-05-14 10:32:47 -0500 | [diff] [blame] | 221 | |
| 222 | if (_powerState->isPowerOn()) |
| 223 | { |
| 224 | createEventLog(sensorPath, interface, propertyName, |
| 225 | alarmValue); |
| 226 | } |
Matt Spinler | f5d3be4 | 2021-02-01 16:38:01 -0600 | [diff] [blame] | 227 | } |
| 228 | } |
| 229 | } |
Matt Spinler | 403d1f5 | 2021-02-01 15:35:25 -0600 | [diff] [blame] | 230 | } |
| 231 | |
Matt Spinler | 50bf816 | 2021-02-01 16:24:01 -0600 | [diff] [blame] | 232 | void ThresholdAlarmLogger::checkThresholds(const std::string& interface, |
| 233 | const std::string& sensorPath, |
| 234 | const std::string& service) |
| 235 | { |
| 236 | auto properties = thresholdData.find(interface); |
| 237 | if (properties == thresholdData.end()) |
| 238 | { |
| 239 | return; |
| 240 | } |
| 241 | |
| 242 | for (const auto& [property, unused] : properties->second) |
| 243 | { |
| 244 | try |
| 245 | { |
| 246 | auto alarmValue = SDBusPlus::getProperty<bool>( |
| 247 | bus, service, sensorPath, interface, property); |
| 248 | alarms[InterfaceKey(sensorPath, interface)][property] = alarmValue; |
| 249 | |
| 250 | // This is just for checking alarms on startup, |
| 251 | // so only look for active alarms. |
Matt Spinler | 66e75a7 | 2021-05-14 10:32:47 -0500 | [diff] [blame] | 252 | if (alarmValue && _powerState->isPowerOn()) |
Matt Spinler | 50bf816 | 2021-02-01 16:24:01 -0600 | [diff] [blame] | 253 | { |
| 254 | createEventLog(sensorPath, interface, property, alarmValue); |
| 255 | } |
| 256 | } |
Patrick Williams | cb356d4 | 2022-07-22 19:26:53 -0500 | [diff] [blame] | 257 | catch (const sdbusplus::exception_t& e) |
Matt Spinler | 50bf816 | 2021-02-01 16:24:01 -0600 | [diff] [blame] | 258 | { |
Matt Spinler | 4b51592 | 2021-10-11 14:55:50 -0500 | [diff] [blame] | 259 | // Sensor daemons that get their direction from entity manager |
| 260 | // may only be putting either the high alarm or low alarm on |
| 261 | // D-Bus, not both. |
Matt Spinler | 50bf816 | 2021-02-01 16:24:01 -0600 | [diff] [blame] | 262 | continue; |
| 263 | } |
| 264 | } |
| 265 | } |
| 266 | |
Patrick Williams | dfddd64 | 2024-08-16 15:21:51 -0400 | [diff] [blame^] | 267 | void ThresholdAlarmLogger::createEventLog( |
| 268 | const std::string& sensorPath, const std::string& interface, |
| 269 | const std::string& alarmProperty, bool alarmValue) |
Matt Spinler | 50bf816 | 2021-02-01 16:24:01 -0600 | [diff] [blame] | 270 | { |
Matt Spinler | 2f18267 | 2021-02-01 16:51:38 -0600 | [diff] [blame] | 271 | std::map<std::string, std::string> ad; |
| 272 | |
| 273 | auto type = getSensorType(sensorPath); |
| 274 | if (skipSensorType(type)) |
| 275 | { |
| 276 | return; |
| 277 | } |
| 278 | |
| 279 | auto it = thresholdData.find(interface); |
| 280 | if (it == thresholdData.end()) |
| 281 | { |
| 282 | return; |
| 283 | } |
| 284 | |
| 285 | auto properties = it->second.find(alarmProperty); |
| 286 | if (properties == it->second.end()) |
| 287 | { |
| 288 | log<level::INFO>( |
Patrick Williams | fbf4703 | 2023-07-17 12:27:34 -0500 | [diff] [blame] | 289 | std::format("Could not find {} in threshold alarms map", |
Matt Spinler | 2f18267 | 2021-02-01 16:51:38 -0600 | [diff] [blame] | 290 | alarmProperty) |
| 291 | .c_str()); |
| 292 | return; |
| 293 | } |
| 294 | |
| 295 | ad.emplace("SENSOR_NAME", sensorPath); |
Matt Spinler | 3efec61 | 2021-05-11 15:26:17 -0500 | [diff] [blame] | 296 | ad.emplace("_PID", std::to_string(getpid())); |
Matt Spinler | 2f18267 | 2021-02-01 16:51:38 -0600 | [diff] [blame] | 297 | |
| 298 | try |
| 299 | { |
| 300 | auto sensorValue = SDBusPlus::getProperty<double>( |
| 301 | bus, sensorPath, valueInterface, "Value"); |
| 302 | |
| 303 | ad.emplace("SENSOR_VALUE", std::to_string(sensorValue)); |
| 304 | |
| 305 | log<level::INFO>( |
Patrick Williams | fbf4703 | 2023-07-17 12:27:34 -0500 | [diff] [blame] | 306 | std::format("Threshold Event {} {} = {} (sensor value {})", |
Matt Spinler | 2f18267 | 2021-02-01 16:51:38 -0600 | [diff] [blame] | 307 | sensorPath, alarmProperty, alarmValue, sensorValue) |
| 308 | .c_str()); |
| 309 | } |
| 310 | catch (const DBusServiceError& e) |
| 311 | { |
| 312 | // If the sensor was just added, the Value interface for it may |
| 313 | // not be in the mapper yet. This could only happen if the sensor |
| 314 | // application was started up after this one and the value exceeded the |
| 315 | // threshold immediately. |
Patrick Williams | fbf4703 | 2023-07-17 12:27:34 -0500 | [diff] [blame] | 316 | log<level::INFO>(std::format("Threshold Event {} {} = {}", sensorPath, |
Matt Spinler | 2f18267 | 2021-02-01 16:51:38 -0600 | [diff] [blame] | 317 | alarmProperty, alarmValue) |
| 318 | .c_str()); |
| 319 | } |
| 320 | |
| 321 | auto callout = getCallout(sensorPath); |
| 322 | if (!callout.empty()) |
| 323 | { |
| 324 | ad.emplace("CALLOUT_INVENTORY_PATH", callout); |
| 325 | } |
| 326 | |
| 327 | auto errorData = properties->second.find(alarmValue); |
| 328 | |
| 329 | // Add the base error name and the sensor type (like Temperature) to the |
| 330 | // error name that's in the thresholdData name to get something like |
| 331 | // xyz.openbmc_project.Sensor.Threshold.Error.TemperatureWarningHigh |
Delphine CC Chiu | 99914e5 | 2024-05-21 17:38:58 +0800 | [diff] [blame] | 332 | const auto& [name, status, severity] = errorData->second; |
| 333 | |
| 334 | try |
| 335 | { |
Patrick Williams | dfddd64 | 2024-08-16 15:21:51 -0400 | [diff] [blame^] | 336 | auto thresholdValue = |
| 337 | SDBusPlus::getProperty<double>(bus, sensorPath, interface, name); |
Delphine CC Chiu | 99914e5 | 2024-05-21 17:38:58 +0800 | [diff] [blame] | 338 | |
| 339 | ad.emplace("THRESHOLD_VALUE", std::to_string(thresholdValue)); |
| 340 | |
| 341 | log<level::INFO>( |
| 342 | std::format("Threshold Event {} {} = {} (threshold value {})", |
| 343 | sensorPath, alarmProperty, alarmValue, thresholdValue) |
| 344 | .c_str()); |
| 345 | } |
| 346 | catch (const DBusServiceError& e) |
| 347 | { |
| 348 | log<level::INFO>(std::format("Threshold Event {} {} = {}", sensorPath, |
| 349 | alarmProperty, alarmValue) |
| 350 | .c_str()); |
| 351 | } |
| 352 | |
Matt Spinler | 2f18267 | 2021-02-01 16:51:38 -0600 | [diff] [blame] | 353 | type.front() = toupper(type.front()); |
Delphine CC Chiu | 99914e5 | 2024-05-21 17:38:58 +0800 | [diff] [blame] | 354 | std::string errorName = errorNameBase + type + name + status; |
Matt Spinler | 2f18267 | 2021-02-01 16:51:38 -0600 | [diff] [blame] | 355 | |
| 356 | SDBusPlus::callMethod(loggingService, loggingPath, loggingCreateIface, |
| 357 | "Create", errorName, convertForMessage(severity), ad); |
| 358 | } |
| 359 | |
| 360 | std::string ThresholdAlarmLogger::getSensorType(std::string sensorPath) |
| 361 | { |
| 362 | auto pos = sensorPath.find_last_of('/'); |
| 363 | if ((sensorPath.back() == '/') || (pos == std::string::npos)) |
| 364 | { |
| 365 | log<level::ERR>( |
Patrick Williams | fbf4703 | 2023-07-17 12:27:34 -0500 | [diff] [blame] | 366 | std::format("Cannot get sensor type from sensor path {}", |
Matt Spinler | 2f18267 | 2021-02-01 16:51:38 -0600 | [diff] [blame] | 367 | sensorPath) |
| 368 | .c_str()); |
| 369 | throw std::runtime_error("Invalid sensor path"); |
| 370 | } |
| 371 | |
| 372 | sensorPath = sensorPath.substr(0, pos); |
| 373 | return sensorPath.substr(sensorPath.find_last_of('/') + 1); |
| 374 | } |
| 375 | |
| 376 | bool ThresholdAlarmLogger::skipSensorType(const std::string& type) |
| 377 | { |
| 378 | return (type == "utilization"); |
| 379 | } |
| 380 | |
| 381 | std::string ThresholdAlarmLogger::getCallout(const std::string& sensorPath) |
| 382 | { |
| 383 | const std::array<std::string, 2> assocTypes{"inventory", "chassis"}; |
| 384 | |
| 385 | // Different implementations handle the association to the FRU |
| 386 | // differently: |
| 387 | // * phosphor-inventory-manager uses the 'inventory' association |
| 388 | // to point to the FRU. |
| 389 | // * dbus-sensors/entity-manager uses the 'chassis' association'. |
| 390 | // * For virtual sensors, no association. |
| 391 | |
| 392 | for (const auto& assocType : assocTypes) |
| 393 | { |
| 394 | auto assocPath = sensorPath + "/" + assocType; |
| 395 | |
| 396 | try |
| 397 | { |
| 398 | auto endpoints = SDBusPlus::getProperty<std::vector<std::string>>( |
| 399 | bus, assocPath, assocInterface, "endpoints"); |
| 400 | |
| 401 | if (!endpoints.empty()) |
| 402 | { |
| 403 | return endpoints[0]; |
| 404 | } |
| 405 | } |
| 406 | catch (const DBusServiceError& e) |
| 407 | { |
| 408 | // The association doesn't exist |
| 409 | continue; |
| 410 | } |
| 411 | } |
| 412 | |
| 413 | return std::string{}; |
Matt Spinler | 50bf816 | 2021-02-01 16:24:01 -0600 | [diff] [blame] | 414 | } |
| 415 | |
Matt Spinler | 66e75a7 | 2021-05-14 10:32:47 -0500 | [diff] [blame] | 416 | void ThresholdAlarmLogger::powerStateChanged(bool powerStateOn) |
| 417 | { |
| 418 | if (powerStateOn) |
| 419 | { |
| 420 | checkThresholds(); |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | void ThresholdAlarmLogger::checkThresholds() |
| 425 | { |
Matt Spinler | eee2580 | 2022-11-03 15:20:13 -0400 | [diff] [blame] | 426 | std::vector<InterfaceKey> toErase; |
| 427 | |
Matt Spinler | 66e75a7 | 2021-05-14 10:32:47 -0500 | [diff] [blame] | 428 | for (const auto& [interfaceKey, alarmMap] : alarms) |
| 429 | { |
| 430 | for (const auto& [propertyName, alarmValue] : alarmMap) |
| 431 | { |
| 432 | if (alarmValue) |
| 433 | { |
| 434 | const auto& sensorPath = std::get<0>(interfaceKey); |
| 435 | const auto& interface = std::get<1>(interfaceKey); |
Matt Spinler | eee2580 | 2022-11-03 15:20:13 -0400 | [diff] [blame] | 436 | std::string service; |
Matt Spinler | 66e75a7 | 2021-05-14 10:32:47 -0500 | [diff] [blame] | 437 | |
Matt Spinler | eee2580 | 2022-11-03 15:20:13 -0400 | [diff] [blame] | 438 | try |
| 439 | { |
| 440 | // Check that the service that provides the alarm is still |
| 441 | // running, because if it died when the alarm was active |
| 442 | // there would be no indication of it unless we listened |
| 443 | // for NameOwnerChanged and tracked services, and this is |
| 444 | // easier. |
| 445 | service = SDBusPlus::getService(bus, sensorPath, interface); |
| 446 | } |
| 447 | catch (const DBusServiceError& e) |
| 448 | { |
| 449 | // No longer on D-Bus delete the alarm entry |
| 450 | toErase.emplace_back(sensorPath, interface); |
| 451 | } |
| 452 | |
| 453 | if (!service.empty()) |
| 454 | { |
| 455 | createEventLog(sensorPath, interface, propertyName, |
| 456 | alarmValue); |
| 457 | } |
Matt Spinler | 66e75a7 | 2021-05-14 10:32:47 -0500 | [diff] [blame] | 458 | } |
| 459 | } |
| 460 | } |
Matt Spinler | eee2580 | 2022-11-03 15:20:13 -0400 | [diff] [blame] | 461 | |
| 462 | for (const auto& e : toErase) |
| 463 | { |
| 464 | alarms.erase(e); |
| 465 | } |
Matt Spinler | 66e75a7 | 2021-05-14 10:32:47 -0500 | [diff] [blame] | 466 | } |
| 467 | |
Matt Spinler | 403d1f5 | 2021-02-01 15:35:25 -0600 | [diff] [blame] | 468 | } // namespace sensor::monitor |