blob: 29cd229081c80af1e728eba29d9213f8793c68f2 [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) :
Matt Spinler403d1f52021-02-01 15:35:25 -060092 bus(bus),
Matt Spinler7f6946b2021-05-14 12:43:50 -050093 event(event), _powerState(std::move(powerState)),
Matt Spinler403d1f52021-02-01 15:35:25 -060094 warningMatch(bus,
95 "type='signal',member='PropertiesChanged',"
96 "path_namespace='/xyz/openbmc_project/sensors',"
97 "arg0='" +
98 warningInterface + "'",
99 std::bind(&ThresholdAlarmLogger::propertiesChanged, this,
100 std::placeholders::_1)),
101 criticalMatch(bus,
102 "type='signal',member='PropertiesChanged',"
103 "path_namespace='/xyz/openbmc_project/sensors',"
104 "arg0='" +
105 criticalInterface + "'",
106 std::bind(&ThresholdAlarmLogger::propertiesChanged, this,
107 std::placeholders::_1)),
108 perfLossMatch(bus,
109 "type='signal',member='PropertiesChanged',"
110 "path_namespace='/xyz/openbmc_project/sensors',"
111 "arg0='" +
112 perfLossInterface + "'",
113 std::bind(&ThresholdAlarmLogger::propertiesChanged, this,
Matt Spinlerb7a55402021-10-11 13:45:35 -0500114 std::placeholders::_1)),
115 ifacesRemovedMatch(bus,
116 "type='signal',member='InterfacesRemoved',arg0path="
117 "'/xyz/openbmc_project/sensors/'",
118 std::bind(&ThresholdAlarmLogger::interfacesRemoved, this,
Matt Spinler8ce65072022-11-03 15:15:55 -0400119 std::placeholders::_1)),
120 ifacesAddedMatch(bus,
121 "type='signal',member='InterfacesAdded',arg0path="
122 "'/xyz/openbmc_project/sensors/'",
123 std::bind(&ThresholdAlarmLogger::interfacesAdded, this,
124 std::placeholders::_1))
Matt Spinler50bf8162021-02-01 16:24:01 -0600125{
Matt Spinler7f6946b2021-05-14 12:43:50 -0500126 _powerState->addCallback("thresholdMon",
127 std::bind(&ThresholdAlarmLogger::powerStateChanged,
128 this, std::placeholders::_1));
129
Matt Spinler50bf8162021-02-01 16:24:01 -0600130 // check for any currently asserted threshold alarms
Patrick Williams61b73292023-05-10 07:50:12 -0500131 std::for_each(thresholdData.begin(), thresholdData.end(),
132 [this](const auto& thresholdInterface) {
133 const auto& interface = thresholdInterface.first;
134 auto objects = SDBusPlus::getSubTreeRaw(this->bus, "/", interface, 0);
135 std::for_each(objects.begin(), objects.end(),
136 [interface, this](const auto& object) {
137 const auto& path = object.first;
138 const auto& service = object.second.begin()->first;
139 checkThresholds(interface, path, service);
Matt Spinler50bf8162021-02-01 16:24:01 -0600140 });
Patrick Williams61b73292023-05-10 07:50:12 -0500141 });
Matt Spinler50bf8162021-02-01 16:24:01 -0600142}
Matt Spinler403d1f52021-02-01 15:35:25 -0600143
Patrick Williamscb356d42022-07-22 19:26:53 -0500144void ThresholdAlarmLogger::propertiesChanged(sdbusplus::message_t& msg)
Matt Spinler403d1f52021-02-01 15:35:25 -0600145{
Matt Spinlerf5d3be42021-02-01 16:38:01 -0600146 std::map<std::string, std::variant<bool>> properties;
147 std::string sensorPath = msg.get_path();
148 std::string interface;
149
150 msg.read(interface, properties);
151
Matt Spinler8ce65072022-11-03 15:15:55 -0400152 checkProperties(sensorPath, interface, properties);
153}
154
155void ThresholdAlarmLogger::interfacesRemoved(sdbusplus::message_t& msg)
156{
157 sdbusplus::message::object_path path;
158 std::vector<std::string> interfaces;
159
160 msg.read(path, interfaces);
161
162 for (const auto& interface : interfaces)
163 {
164 if (std::find(thresholdIfaceNames.begin(), thresholdIfaceNames.end(),
165 interface) != thresholdIfaceNames.end())
166 {
167 alarms.erase(InterfaceKey{path, interface});
168 }
169 }
170}
171
172void ThresholdAlarmLogger::interfacesAdded(sdbusplus::message_t& msg)
173{
174 sdbusplus::message::object_path path;
175 std::map<std::string, std::map<std::string, std::variant<bool>>> interfaces;
176
177 msg.read(path, interfaces);
178
179 for (const auto& [interface, properties] : interfaces)
180 {
181 if (std::find(thresholdIfaceNames.begin(), thresholdIfaceNames.end(),
182 interface) != thresholdIfaceNames.end())
183 {
184 checkProperties(path, interface, properties);
185 }
186 }
187}
188
189void ThresholdAlarmLogger::checkProperties(
190 const std::string& sensorPath, const std::string& interface,
191 const std::map<std::string, std::variant<bool>>& properties)
192{
Matt Spinlerf5d3be42021-02-01 16:38:01 -0600193 auto alarmProperties = thresholdData.find(interface);
194 if (alarmProperties == thresholdData.end())
195 {
196 return;
197 }
198
199 for (const auto& [propertyName, propertyValue] : properties)
200 {
201 if (alarmProperties->second.find(propertyName) !=
202 alarmProperties->second.end())
203 {
204 // If this is the first time we've seen this alarm, then
205 // assume it was off before so it doesn't create an event
206 // log for a value of false.
207
208 InterfaceKey key{sensorPath, interface};
209 if (alarms.find(key) == alarms.end())
210 {
211 alarms[key][propertyName] = false;
212 }
213
214 // Check if the value changed from what was there before.
215 auto alarmValue = std::get<bool>(propertyValue);
216 if (alarmValue != alarms[key][propertyName])
217 {
218 alarms[key][propertyName] = alarmValue;
Matt Spinler66e75a72021-05-14 10:32:47 -0500219
220 if (_powerState->isPowerOn())
221 {
222 createEventLog(sensorPath, interface, propertyName,
223 alarmValue);
224 }
Matt Spinlerf5d3be42021-02-01 16:38:01 -0600225 }
226 }
227 }
Matt Spinler403d1f52021-02-01 15:35:25 -0600228}
229
Matt Spinler50bf8162021-02-01 16:24:01 -0600230void ThresholdAlarmLogger::checkThresholds(const std::string& interface,
231 const std::string& sensorPath,
232 const std::string& service)
233{
234 auto properties = thresholdData.find(interface);
235 if (properties == thresholdData.end())
236 {
237 return;
238 }
239
240 for (const auto& [property, unused] : properties->second)
241 {
242 try
243 {
244 auto alarmValue = SDBusPlus::getProperty<bool>(
245 bus, service, sensorPath, interface, property);
246 alarms[InterfaceKey(sensorPath, interface)][property] = alarmValue;
247
248 // This is just for checking alarms on startup,
249 // so only look for active alarms.
Matt Spinler66e75a72021-05-14 10:32:47 -0500250 if (alarmValue && _powerState->isPowerOn())
Matt Spinler50bf8162021-02-01 16:24:01 -0600251 {
252 createEventLog(sensorPath, interface, property, alarmValue);
253 }
254 }
Patrick Williamscb356d42022-07-22 19:26:53 -0500255 catch (const sdbusplus::exception_t& e)
Matt Spinler50bf8162021-02-01 16:24:01 -0600256 {
Matt Spinler4b515922021-10-11 14:55:50 -0500257 // Sensor daemons that get their direction from entity manager
258 // may only be putting either the high alarm or low alarm on
259 // D-Bus, not both.
Matt Spinler50bf8162021-02-01 16:24:01 -0600260 continue;
261 }
262 }
263}
264
265void ThresholdAlarmLogger::createEventLog(const std::string& sensorPath,
266 const std::string& interface,
267 const std::string& alarmProperty,
268 bool alarmValue)
269{
Matt Spinler2f182672021-02-01 16:51:38 -0600270 std::map<std::string, std::string> ad;
271
272 auto type = getSensorType(sensorPath);
273 if (skipSensorType(type))
274 {
275 return;
276 }
277
278 auto it = thresholdData.find(interface);
279 if (it == thresholdData.end())
280 {
281 return;
282 }
283
284 auto properties = it->second.find(alarmProperty);
285 if (properties == it->second.end())
286 {
287 log<level::INFO>(
Patrick Williamsfbf47032023-07-17 12:27:34 -0500288 std::format("Could not find {} in threshold alarms map",
Matt Spinler2f182672021-02-01 16:51:38 -0600289 alarmProperty)
290 .c_str());
291 return;
292 }
293
294 ad.emplace("SENSOR_NAME", sensorPath);
Matt Spinler3efec612021-05-11 15:26:17 -0500295 ad.emplace("_PID", std::to_string(getpid()));
Matt Spinler2f182672021-02-01 16:51:38 -0600296
297 try
298 {
299 auto sensorValue = SDBusPlus::getProperty<double>(
300 bus, sensorPath, valueInterface, "Value");
301
302 ad.emplace("SENSOR_VALUE", std::to_string(sensorValue));
303
304 log<level::INFO>(
Patrick Williamsfbf47032023-07-17 12:27:34 -0500305 std::format("Threshold Event {} {} = {} (sensor value {})",
Matt Spinler2f182672021-02-01 16:51:38 -0600306 sensorPath, alarmProperty, alarmValue, sensorValue)
307 .c_str());
308 }
309 catch (const DBusServiceError& e)
310 {
311 // If the sensor was just added, the Value interface for it may
312 // not be in the mapper yet. This could only happen if the sensor
313 // application was started up after this one and the value exceeded the
314 // threshold immediately.
Patrick Williamsfbf47032023-07-17 12:27:34 -0500315 log<level::INFO>(std::format("Threshold Event {} {} = {}", sensorPath,
Matt Spinler2f182672021-02-01 16:51:38 -0600316 alarmProperty, alarmValue)
317 .c_str());
318 }
319
320 auto callout = getCallout(sensorPath);
321 if (!callout.empty())
322 {
323 ad.emplace("CALLOUT_INVENTORY_PATH", callout);
324 }
325
326 auto errorData = properties->second.find(alarmValue);
327
328 // Add the base error name and the sensor type (like Temperature) to the
329 // error name that's in the thresholdData name to get something like
330 // xyz.openbmc_project.Sensor.Threshold.Error.TemperatureWarningHigh
Delphine CC Chiu99914e52024-05-21 17:38:58 +0800331 const auto& [name, status, severity] = errorData->second;
332
333 try
334 {
335 auto thresholdValue = SDBusPlus::getProperty<double>(bus, sensorPath,
336 interface, name);
337
338 ad.emplace("THRESHOLD_VALUE", std::to_string(thresholdValue));
339
340 log<level::INFO>(
341 std::format("Threshold Event {} {} = {} (threshold value {})",
342 sensorPath, alarmProperty, alarmValue, thresholdValue)
343 .c_str());
344 }
345 catch (const DBusServiceError& e)
346 {
347 log<level::INFO>(std::format("Threshold Event {} {} = {}", sensorPath,
348 alarmProperty, alarmValue)
349 .c_str());
350 }
351
Matt Spinler2f182672021-02-01 16:51:38 -0600352 type.front() = toupper(type.front());
Delphine CC Chiu99914e52024-05-21 17:38:58 +0800353 std::string errorName = errorNameBase + type + name + status;
Matt Spinler2f182672021-02-01 16:51:38 -0600354
355 SDBusPlus::callMethod(loggingService, loggingPath, loggingCreateIface,
356 "Create", errorName, convertForMessage(severity), ad);
357}
358
359std::string ThresholdAlarmLogger::getSensorType(std::string sensorPath)
360{
361 auto pos = sensorPath.find_last_of('/');
362 if ((sensorPath.back() == '/') || (pos == std::string::npos))
363 {
364 log<level::ERR>(
Patrick Williamsfbf47032023-07-17 12:27:34 -0500365 std::format("Cannot get sensor type from sensor path {}",
Matt Spinler2f182672021-02-01 16:51:38 -0600366 sensorPath)
367 .c_str());
368 throw std::runtime_error("Invalid sensor path");
369 }
370
371 sensorPath = sensorPath.substr(0, pos);
372 return sensorPath.substr(sensorPath.find_last_of('/') + 1);
373}
374
375bool ThresholdAlarmLogger::skipSensorType(const std::string& type)
376{
377 return (type == "utilization");
378}
379
380std::string ThresholdAlarmLogger::getCallout(const std::string& sensorPath)
381{
382 const std::array<std::string, 2> assocTypes{"inventory", "chassis"};
383
384 // Different implementations handle the association to the FRU
385 // differently:
386 // * phosphor-inventory-manager uses the 'inventory' association
387 // to point to the FRU.
388 // * dbus-sensors/entity-manager uses the 'chassis' association'.
389 // * For virtual sensors, no association.
390
391 for (const auto& assocType : assocTypes)
392 {
393 auto assocPath = sensorPath + "/" + assocType;
394
395 try
396 {
397 auto endpoints = SDBusPlus::getProperty<std::vector<std::string>>(
398 bus, assocPath, assocInterface, "endpoints");
399
400 if (!endpoints.empty())
401 {
402 return endpoints[0];
403 }
404 }
405 catch (const DBusServiceError& e)
406 {
407 // The association doesn't exist
408 continue;
409 }
410 }
411
412 return std::string{};
Matt Spinler50bf8162021-02-01 16:24:01 -0600413}
414
Matt Spinler66e75a72021-05-14 10:32:47 -0500415void ThresholdAlarmLogger::powerStateChanged(bool powerStateOn)
416{
417 if (powerStateOn)
418 {
419 checkThresholds();
420 }
421}
422
423void ThresholdAlarmLogger::checkThresholds()
424{
Matt Spinlereee25802022-11-03 15:20:13 -0400425 std::vector<InterfaceKey> toErase;
426
Matt Spinler66e75a72021-05-14 10:32:47 -0500427 for (const auto& [interfaceKey, alarmMap] : alarms)
428 {
429 for (const auto& [propertyName, alarmValue] : alarmMap)
430 {
431 if (alarmValue)
432 {
433 const auto& sensorPath = std::get<0>(interfaceKey);
434 const auto& interface = std::get<1>(interfaceKey);
Matt Spinlereee25802022-11-03 15:20:13 -0400435 std::string service;
Matt Spinler66e75a72021-05-14 10:32:47 -0500436
Matt Spinlereee25802022-11-03 15:20:13 -0400437 try
438 {
439 // Check that the service that provides the alarm is still
440 // running, because if it died when the alarm was active
441 // there would be no indication of it unless we listened
442 // for NameOwnerChanged and tracked services, and this is
443 // easier.
444 service = SDBusPlus::getService(bus, sensorPath, interface);
445 }
446 catch (const DBusServiceError& e)
447 {
448 // No longer on D-Bus delete the alarm entry
449 toErase.emplace_back(sensorPath, interface);
450 }
451
452 if (!service.empty())
453 {
454 createEventLog(sensorPath, interface, propertyName,
455 alarmValue);
456 }
Matt Spinler66e75a72021-05-14 10:32:47 -0500457 }
458 }
459 }
Matt Spinlereee25802022-11-03 15:20:13 -0400460
461 for (const auto& e : toErase)
462 {
463 alarms.erase(e);
464 }
Matt Spinler66e75a72021-05-14 10:32:47 -0500465}
466
Matt Spinler403d1f52021-02-01 15:35:25 -0600467} // namespace sensor::monitor