blob: 6d78c0a06ad71bed56143d6b3b3e7a55f825e381 [file] [log] [blame]
Matt Spinler403d1f52021-02-01 15:35:25 -06001/**
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 Spinler50bf8162021-02-01 16:24:01 -060018#include "sdbusplus.hpp"
19
Matt Spinler3efec612021-05-11 15:26:17 -050020#include <unistd.h>
Matt Spinler50bf8162021-02-01 16:24:01 -060021
22#include <phosphor-logging/log.hpp>
23#include <xyz/openbmc_project/Logging/Entry/server.hpp>
24
Patrick Williamsfbf47032023-07-17 12:27:34 -050025#include <format>
26
Matt Spinler403d1f52021-02-01 15:35:25 -060027namespace sensor::monitor
28{
29
Matt Spinler50bf8162021-02-01 16:24:01 -060030using namespace sdbusplus::xyz::openbmc_project::Logging::server;
31using namespace phosphor::logging;
Matt Spinler66e75a72021-05-14 10:32:47 -050032using namespace phosphor::fan;
Matt Spinler50bf8162021-02-01 16:24:01 -060033using namespace phosphor::fan::util;
34
Matt Spinler403d1f52021-02-01 15:35:25 -060035const std::string warningInterface =
36 "xyz.openbmc_project.Sensor.Threshold.Warning";
37const std::string criticalInterface =
38 "xyz.openbmc_project.Sensor.Threshold.Critical";
39const std::string perfLossInterface =
40 "xyz.openbmc_project.Sensor.Threshold.PerformanceLoss";
Matt Spinler2f182672021-02-01 16:51:38 -060041constexpr auto loggingService = "xyz.openbmc_project.Logging";
42constexpr auto loggingPath = "/xyz/openbmc_project/logging";
43constexpr auto loggingCreateIface = "xyz.openbmc_project.Logging.Create";
44constexpr auto errorNameBase = "xyz.openbmc_project.Sensor.Threshold.Error.";
45constexpr auto valueInterface = "xyz.openbmc_project.Sensor.Value";
46constexpr auto assocInterface = "xyz.openbmc_project.Association";
Matt Spinler403d1f52021-02-01 15:35:25 -060047
Matt Spinler8ce65072022-11-03 15:15:55 -040048const std::vector<std::string> thresholdIfaceNames{
49 warningInterface, criticalInterface, perfLossInterface};
50
Delphine CC Chiu99914e52024-05-21 17:38:58 +080051using ErrorData = std::tuple<ErrorName, ErrorStatus, Entry::Level>;
Matt Spinler50bf8162021-02-01 16:24:01 -060052
53/**
54 * Map of threshold interfaces and alarm properties and values to error data.
55 */
56const std::map<InterfaceName, std::map<PropertyName, std::map<bool, ErrorData>>>
57 thresholdData{
58
59 {warningInterface,
60 {{"WarningAlarmHigh",
Delphine CC Chiu99914e52024-05-21 17:38:58 +080061 {{true, ErrorData{"WarningHigh", "", Entry::Level::Warning}},
Matt Spinler50bf8162021-02-01 16:24:01 -060062 {false,
Delphine CC Chiu99914e52024-05-21 17:38:58 +080063 ErrorData{"WarningHigh", "Clear", Entry::Level::Informational}}}},
Matt Spinler50bf8162021-02-01 16:24:01 -060064 {"WarningAlarmLow",
Delphine CC Chiu99914e52024-05-21 17:38:58 +080065 {{true, ErrorData{"WarningLow", "", Entry::Level::Warning}},
Matt Spinler50bf8162021-02-01 16:24:01 -060066 {false,
Delphine CC Chiu99914e52024-05-21 17:38:58 +080067 ErrorData{"WarningLow", "Clear", Entry::Level::Informational}}}}}},
Matt Spinler50bf8162021-02-01 16:24:01 -060068
69 {criticalInterface,
70 {{"CriticalAlarmHigh",
Delphine CC Chiu99914e52024-05-21 17:38:58 +080071 {{true, ErrorData{"CriticalHigh", "", Entry::Level::Critical}},
Matt Spinler50bf8162021-02-01 16:24:01 -060072 {false,
Delphine CC Chiu99914e52024-05-21 17:38:58 +080073 ErrorData{"CriticalHigh", "Clear", Entry::Level::Informational}}}},
Matt Spinler50bf8162021-02-01 16:24:01 -060074 {"CriticalAlarmLow",
Delphine CC Chiu99914e52024-05-21 17:38:58 +080075 {{true, ErrorData{"CriticalLow", "", Entry::Level::Critical}},
76 {false, ErrorData{"CriticalLow", "Clear",
77 Entry::Level::Informational}}}}}},
Matt Spinler50bf8162021-02-01 16:24:01 -060078
79 {perfLossInterface,
80 {{"PerfLossAlarmHigh",
Delphine CC Chiu99914e52024-05-21 17:38:58 +080081 {{true, ErrorData{"PerformanceLossHigh", "", Entry::Level::Warning}},
82 {false, ErrorData{"PerformanceLossHigh", "Clear",
83 Entry::Level::Informational}}}},
Matt Spinler50bf8162021-02-01 16:24:01 -060084 {"PerfLossAlarmLow",
Delphine CC Chiu99914e52024-05-21 17:38:58 +080085 {{true, ErrorData{"PerformanceLossLow", "", Entry::Level::Warning}},
86 {false, ErrorData{"PerformanceLossLow", "Clear",
87 Entry::Level::Informational}}}}}}};
Matt Spinler50bf8162021-02-01 16:24:01 -060088
Matt Spinler7f6946b2021-05-14 12:43:50 -050089ThresholdAlarmLogger::ThresholdAlarmLogger(
Patrick Williamscb356d42022-07-22 19:26:53 -050090 sdbusplus::bus_t& bus, sdeventplus::Event& event,
Matt Spinler7f6946b2021-05-14 12:43:50 -050091 std::shared_ptr<PowerState> powerState) :
Patrick Williamsdfddd642024-08-16 15:21:51 -040092 bus(bus), event(event), _powerState(std::move(powerState)),
Matt Spinler403d1f52021-02-01 15:35:25 -060093 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 Spinlerb7a55402021-10-11 13:45:35 -0500113 std::placeholders::_1)),
114 ifacesRemovedMatch(bus,
115 "type='signal',member='InterfacesRemoved',arg0path="
116 "'/xyz/openbmc_project/sensors/'",
117 std::bind(&ThresholdAlarmLogger::interfacesRemoved, this,
Matt Spinler8ce65072022-11-03 15:15:55 -0400118 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 Spinler50bf8162021-02-01 16:24:01 -0600124{
Matt Spinler7f6946b2021-05-14 12:43:50 -0500125 _powerState->addCallback("thresholdMon",
126 std::bind(&ThresholdAlarmLogger::powerStateChanged,
127 this, std::placeholders::_1));
128
Matt Spinler50bf8162021-02-01 16:24:01 -0600129 // check for any currently asserted threshold alarms
Patrick Williamsdfddd642024-08-16 15:21:51 -0400130 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 Spinler50bf8162021-02-01 16:24:01 -0600143 });
144}
Matt Spinler403d1f52021-02-01 15:35:25 -0600145
Patrick Williamscb356d42022-07-22 19:26:53 -0500146void ThresholdAlarmLogger::propertiesChanged(sdbusplus::message_t& msg)
Matt Spinler403d1f52021-02-01 15:35:25 -0600147{
Matt Spinlerf5d3be42021-02-01 16:38:01 -0600148 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 Spinler8ce65072022-11-03 15:15:55 -0400154 checkProperties(sensorPath, interface, properties);
155}
156
157void 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
174void 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
191void ThresholdAlarmLogger::checkProperties(
192 const std::string& sensorPath, const std::string& interface,
193 const std::map<std::string, std::variant<bool>>& properties)
194{
Matt Spinlerf5d3be42021-02-01 16:38:01 -0600195 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 Spinler66e75a72021-05-14 10:32:47 -0500221
222 if (_powerState->isPowerOn())
223 {
224 createEventLog(sensorPath, interface, propertyName,
225 alarmValue);
226 }
Matt Spinlerf5d3be42021-02-01 16:38:01 -0600227 }
228 }
229 }
Matt Spinler403d1f52021-02-01 15:35:25 -0600230}
231
Matt Spinler50bf8162021-02-01 16:24:01 -0600232void 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 Spinler66e75a72021-05-14 10:32:47 -0500252 if (alarmValue && _powerState->isPowerOn())
Matt Spinler50bf8162021-02-01 16:24:01 -0600253 {
254 createEventLog(sensorPath, interface, property, alarmValue);
255 }
256 }
Patrick Williamscb356d42022-07-22 19:26:53 -0500257 catch (const sdbusplus::exception_t& e)
Matt Spinler50bf8162021-02-01 16:24:01 -0600258 {
Matt Spinler4b515922021-10-11 14:55:50 -0500259 // 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 Spinler50bf8162021-02-01 16:24:01 -0600262 continue;
263 }
264 }
265}
266
Patrick Williamsdfddd642024-08-16 15:21:51 -0400267void ThresholdAlarmLogger::createEventLog(
268 const std::string& sensorPath, const std::string& interface,
269 const std::string& alarmProperty, bool alarmValue)
Matt Spinler50bf8162021-02-01 16:24:01 -0600270{
Matt Spinler2f182672021-02-01 16:51:38 -0600271 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 Williamsfbf47032023-07-17 12:27:34 -0500289 std::format("Could not find {} in threshold alarms map",
Matt Spinler2f182672021-02-01 16:51:38 -0600290 alarmProperty)
291 .c_str());
292 return;
293 }
294
295 ad.emplace("SENSOR_NAME", sensorPath);
Matt Spinler3efec612021-05-11 15:26:17 -0500296 ad.emplace("_PID", std::to_string(getpid()));
Matt Spinler2f182672021-02-01 16:51:38 -0600297
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 Williamsfbf47032023-07-17 12:27:34 -0500306 std::format("Threshold Event {} {} = {} (sensor value {})",
Matt Spinler2f182672021-02-01 16:51:38 -0600307 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 Williamsfbf47032023-07-17 12:27:34 -0500316 log<level::INFO>(std::format("Threshold Event {} {} = {}", sensorPath,
Matt Spinler2f182672021-02-01 16:51:38 -0600317 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 Chiu99914e52024-05-21 17:38:58 +0800332 const auto& [name, status, severity] = errorData->second;
333
334 try
335 {
Patrick Williamsdfddd642024-08-16 15:21:51 -0400336 auto thresholdValue =
337 SDBusPlus::getProperty<double>(bus, sensorPath, interface, name);
Delphine CC Chiu99914e52024-05-21 17:38:58 +0800338
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 Spinler2f182672021-02-01 16:51:38 -0600353 type.front() = toupper(type.front());
Delphine CC Chiu99914e52024-05-21 17:38:58 +0800354 std::string errorName = errorNameBase + type + name + status;
Matt Spinler2f182672021-02-01 16:51:38 -0600355
356 SDBusPlus::callMethod(loggingService, loggingPath, loggingCreateIface,
357 "Create", errorName, convertForMessage(severity), ad);
358}
359
360std::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 Williamsfbf47032023-07-17 12:27:34 -0500366 std::format("Cannot get sensor type from sensor path {}",
Matt Spinler2f182672021-02-01 16:51:38 -0600367 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
376bool ThresholdAlarmLogger::skipSensorType(const std::string& type)
377{
378 return (type == "utilization");
379}
380
381std::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 Spinler50bf8162021-02-01 16:24:01 -0600414}
415
Matt Spinler66e75a72021-05-14 10:32:47 -0500416void ThresholdAlarmLogger::powerStateChanged(bool powerStateOn)
417{
418 if (powerStateOn)
419 {
420 checkThresholds();
421 }
422}
423
424void ThresholdAlarmLogger::checkThresholds()
425{
Matt Spinlereee25802022-11-03 15:20:13 -0400426 std::vector<InterfaceKey> toErase;
427
Matt Spinler66e75a72021-05-14 10:32:47 -0500428 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 Spinlereee25802022-11-03 15:20:13 -0400436 std::string service;
Matt Spinler66e75a72021-05-14 10:32:47 -0500437
Matt Spinlereee25802022-11-03 15:20:13 -0400438 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 Spinler66e75a72021-05-14 10:32:47 -0500458 }
459 }
460 }
Matt Spinlereee25802022-11-03 15:20:13 -0400461
462 for (const auto& e : toErase)
463 {
464 alarms.erase(e);
465 }
Matt Spinler66e75a72021-05-14 10:32:47 -0500466}
467
Matt Spinler403d1f52021-02-01 15:35:25 -0600468} // namespace sensor::monitor