blob: 69ecb10a09f3969925701c493b5d336bc074f0a6 [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
20#include <fmt/format.h>
Matt Spinler3efec612021-05-11 15:26:17 -050021#include <unistd.h>
Matt Spinler50bf8162021-02-01 16:24:01 -060022
23#include <phosphor-logging/log.hpp>
24#include <xyz/openbmc_project/Logging/Entry/server.hpp>
25
Matt Spinler403d1f52021-02-01 15:35:25 -060026namespace sensor::monitor
27{
28
Matt Spinler50bf8162021-02-01 16:24:01 -060029using namespace sdbusplus::xyz::openbmc_project::Logging::server;
30using namespace phosphor::logging;
Matt Spinler66e75a72021-05-14 10:32:47 -050031using namespace phosphor::fan;
Matt Spinler50bf8162021-02-01 16:24:01 -060032using namespace phosphor::fan::util;
33
Matt Spinler403d1f52021-02-01 15:35:25 -060034const std::string warningInterface =
35 "xyz.openbmc_project.Sensor.Threshold.Warning";
36const std::string criticalInterface =
37 "xyz.openbmc_project.Sensor.Threshold.Critical";
38const std::string perfLossInterface =
39 "xyz.openbmc_project.Sensor.Threshold.PerformanceLoss";
Matt Spinler2f182672021-02-01 16:51:38 -060040constexpr auto loggingService = "xyz.openbmc_project.Logging";
41constexpr auto loggingPath = "/xyz/openbmc_project/logging";
42constexpr auto loggingCreateIface = "xyz.openbmc_project.Logging.Create";
43constexpr auto errorNameBase = "xyz.openbmc_project.Sensor.Threshold.Error.";
44constexpr auto valueInterface = "xyz.openbmc_project.Sensor.Value";
45constexpr auto assocInterface = "xyz.openbmc_project.Association";
Matt Spinler403d1f52021-02-01 15:35:25 -060046
Matt Spinler50bf8162021-02-01 16:24:01 -060047using ErrorData = std::tuple<ErrorName, Entry::Level>;
48
49/**
50 * Map of threshold interfaces and alarm properties and values to error data.
51 */
52const std::map<InterfaceName, std::map<PropertyName, std::map<bool, ErrorData>>>
53 thresholdData{
54
55 {warningInterface,
56 {{"WarningAlarmHigh",
57 {{true, ErrorData{"WarningHigh", Entry::Level::Warning}},
58 {false,
59 ErrorData{"WarningHighClear", Entry::Level::Informational}}}},
60 {"WarningAlarmLow",
61 {{true, ErrorData{"WarningLow", Entry::Level::Warning}},
62 {false,
63 ErrorData{"WarningLowClear", Entry::Level::Informational}}}}}},
64
65 {criticalInterface,
66 {{"CriticalAlarmHigh",
67 {{true, ErrorData{"CriticalHigh", Entry::Level::Critical}},
68 {false,
69 ErrorData{"CriticalHighClear", Entry::Level::Informational}}}},
70 {"CriticalAlarmLow",
71 {{true, ErrorData{"CriticalLow", Entry::Level::Critical}},
72 {false,
73 ErrorData{"CriticalLowClear", Entry::Level::Informational}}}}}},
74
75 {perfLossInterface,
76 {{"PerfLossAlarmHigh",
77 {{true, ErrorData{"PerfLossHigh", Entry::Level::Warning}},
78 {false,
79 ErrorData{"PerfLossHighClear", Entry::Level::Informational}}}},
80 {"PerfLossAlarmLow",
81 {{true, ErrorData{"PerfLossLow", Entry::Level::Warning}},
82 {false,
83 ErrorData{"PerfLossLowClear", Entry::Level::Informational}}}}}}};
84
Matt Spinler7f6946b2021-05-14 12:43:50 -050085ThresholdAlarmLogger::ThresholdAlarmLogger(
86 sdbusplus::bus::bus& bus, sdeventplus::Event& event,
87 std::shared_ptr<PowerState> powerState) :
Matt Spinler403d1f52021-02-01 15:35:25 -060088 bus(bus),
Matt Spinler7f6946b2021-05-14 12:43:50 -050089 event(event), _powerState(std::move(powerState)),
Matt Spinler403d1f52021-02-01 15:35:25 -060090 warningMatch(bus,
91 "type='signal',member='PropertiesChanged',"
92 "path_namespace='/xyz/openbmc_project/sensors',"
93 "arg0='" +
94 warningInterface + "'",
95 std::bind(&ThresholdAlarmLogger::propertiesChanged, this,
96 std::placeholders::_1)),
97 criticalMatch(bus,
98 "type='signal',member='PropertiesChanged',"
99 "path_namespace='/xyz/openbmc_project/sensors',"
100 "arg0='" +
101 criticalInterface + "'",
102 std::bind(&ThresholdAlarmLogger::propertiesChanged, this,
103 std::placeholders::_1)),
104 perfLossMatch(bus,
105 "type='signal',member='PropertiesChanged',"
106 "path_namespace='/xyz/openbmc_project/sensors',"
107 "arg0='" +
108 perfLossInterface + "'",
109 std::bind(&ThresholdAlarmLogger::propertiesChanged, this,
Matt Spinlerb7a55402021-10-11 13:45:35 -0500110 std::placeholders::_1)),
111 ifacesRemovedMatch(bus,
112 "type='signal',member='InterfacesRemoved',arg0path="
113 "'/xyz/openbmc_project/sensors/'",
114 std::bind(&ThresholdAlarmLogger::interfacesRemoved, this,
115 std::placeholders::_1))
Matt Spinler50bf8162021-02-01 16:24:01 -0600116{
Matt Spinler7f6946b2021-05-14 12:43:50 -0500117 _powerState->addCallback("thresholdMon",
118 std::bind(&ThresholdAlarmLogger::powerStateChanged,
119 this, std::placeholders::_1));
120
Matt Spinler50bf8162021-02-01 16:24:01 -0600121 // check for any currently asserted threshold alarms
122 std::for_each(
123 thresholdData.begin(), thresholdData.end(),
124 [this](const auto& thresholdInterface) {
125 const auto& interface = thresholdInterface.first;
126 auto objects =
127 SDBusPlus::getSubTreeRaw(this->bus, "/", interface, 0);
128 std::for_each(objects.begin(), objects.end(),
129 [interface, this](const auto& object) {
130 const auto& path = object.first;
131 const auto& service =
132 object.second.begin()->first;
133 checkThresholds(interface, path, service);
134 });
135 });
136}
Matt Spinler403d1f52021-02-01 15:35:25 -0600137
138void ThresholdAlarmLogger::propertiesChanged(sdbusplus::message::message& msg)
139{
Matt Spinlerf5d3be42021-02-01 16:38:01 -0600140 std::map<std::string, std::variant<bool>> properties;
141 std::string sensorPath = msg.get_path();
142 std::string interface;
143
144 msg.read(interface, properties);
145
146 auto alarmProperties = thresholdData.find(interface);
147 if (alarmProperties == thresholdData.end())
148 {
149 return;
150 }
151
152 for (const auto& [propertyName, propertyValue] : properties)
153 {
154 if (alarmProperties->second.find(propertyName) !=
155 alarmProperties->second.end())
156 {
157 // If this is the first time we've seen this alarm, then
158 // assume it was off before so it doesn't create an event
159 // log for a value of false.
160
161 InterfaceKey key{sensorPath, interface};
162 if (alarms.find(key) == alarms.end())
163 {
164 alarms[key][propertyName] = false;
165 }
166
167 // Check if the value changed from what was there before.
168 auto alarmValue = std::get<bool>(propertyValue);
169 if (alarmValue != alarms[key][propertyName])
170 {
171 alarms[key][propertyName] = alarmValue;
Matt Spinler66e75a72021-05-14 10:32:47 -0500172
173 if (_powerState->isPowerOn())
174 {
175 createEventLog(sensorPath, interface, propertyName,
176 alarmValue);
177 }
Matt Spinlerf5d3be42021-02-01 16:38:01 -0600178 }
179 }
180 }
Matt Spinler403d1f52021-02-01 15:35:25 -0600181}
182
Matt Spinlerb7a55402021-10-11 13:45:35 -0500183void ThresholdAlarmLogger::interfacesRemoved(sdbusplus::message::message& msg)
184{
185 static const std::vector<std::string> thresholdNames{
186 warningInterface, criticalInterface, perfLossInterface};
187 sdbusplus::message::object_path path;
188 std::vector<std::string> interfaces;
189
190 msg.read(path, interfaces);
191
192 for (const auto& interface : interfaces)
193 {
194 if (std::find(thresholdNames.begin(), thresholdNames.end(),
195 interface) != thresholdNames.end())
196 {
197 alarms.erase(InterfaceKey{path, interface});
198 }
199 }
200}
201
Matt Spinler50bf8162021-02-01 16:24:01 -0600202void ThresholdAlarmLogger::checkThresholds(const std::string& interface,
203 const std::string& sensorPath,
204 const std::string& service)
205{
206 auto properties = thresholdData.find(interface);
207 if (properties == thresholdData.end())
208 {
209 return;
210 }
211
212 for (const auto& [property, unused] : properties->second)
213 {
214 try
215 {
216 auto alarmValue = SDBusPlus::getProperty<bool>(
217 bus, service, sensorPath, interface, property);
218 alarms[InterfaceKey(sensorPath, interface)][property] = alarmValue;
219
220 // This is just for checking alarms on startup,
221 // so only look for active alarms.
Matt Spinler66e75a72021-05-14 10:32:47 -0500222 if (alarmValue && _powerState->isPowerOn())
Matt Spinler50bf8162021-02-01 16:24:01 -0600223 {
224 createEventLog(sensorPath, interface, property, alarmValue);
225 }
226 }
227 catch (const DBusError& e)
228 {
229 log<level::ERR>(
230 fmt::format("Failed reading sensor threshold properties: {}",
231 e.what())
232 .c_str());
233 continue;
234 }
235 }
236}
237
238void ThresholdAlarmLogger::createEventLog(const std::string& sensorPath,
239 const std::string& interface,
240 const std::string& alarmProperty,
241 bool alarmValue)
242{
Matt Spinler2f182672021-02-01 16:51:38 -0600243 std::map<std::string, std::string> ad;
244
245 auto type = getSensorType(sensorPath);
246 if (skipSensorType(type))
247 {
248 return;
249 }
250
251 auto it = thresholdData.find(interface);
252 if (it == thresholdData.end())
253 {
254 return;
255 }
256
257 auto properties = it->second.find(alarmProperty);
258 if (properties == it->second.end())
259 {
260 log<level::INFO>(
261 fmt::format("Could not find {} in threshold alarms map",
262 alarmProperty)
263 .c_str());
264 return;
265 }
266
267 ad.emplace("SENSOR_NAME", sensorPath);
Matt Spinler3efec612021-05-11 15:26:17 -0500268 ad.emplace("_PID", std::to_string(getpid()));
Matt Spinler2f182672021-02-01 16:51:38 -0600269
270 try
271 {
272 auto sensorValue = SDBusPlus::getProperty<double>(
273 bus, sensorPath, valueInterface, "Value");
274
275 ad.emplace("SENSOR_VALUE", std::to_string(sensorValue));
276
277 log<level::INFO>(
278 fmt::format("Threshold Event {} {} = {} (sensor value {})",
279 sensorPath, alarmProperty, alarmValue, sensorValue)
280 .c_str());
281 }
282 catch (const DBusServiceError& e)
283 {
284 // If the sensor was just added, the Value interface for it may
285 // not be in the mapper yet. This could only happen if the sensor
286 // application was started up after this one and the value exceeded the
287 // threshold immediately.
288 log<level::INFO>(fmt::format("Threshold Event {} {} = {}", sensorPath,
289 alarmProperty, alarmValue)
290 .c_str());
291 }
292
293 auto callout = getCallout(sensorPath);
294 if (!callout.empty())
295 {
296 ad.emplace("CALLOUT_INVENTORY_PATH", callout);
297 }
298
299 auto errorData = properties->second.find(alarmValue);
300
301 // Add the base error name and the sensor type (like Temperature) to the
302 // error name that's in the thresholdData name to get something like
303 // xyz.openbmc_project.Sensor.Threshold.Error.TemperatureWarningHigh
304 const auto& [name, severity] = errorData->second;
305 type.front() = toupper(type.front());
306 std::string errorName = errorNameBase + type + name;
307
308 SDBusPlus::callMethod(loggingService, loggingPath, loggingCreateIface,
309 "Create", errorName, convertForMessage(severity), ad);
310}
311
312std::string ThresholdAlarmLogger::getSensorType(std::string sensorPath)
313{
314 auto pos = sensorPath.find_last_of('/');
315 if ((sensorPath.back() == '/') || (pos == std::string::npos))
316 {
317 log<level::ERR>(
318 fmt::format("Cannot get sensor type from sensor path {}",
319 sensorPath)
320 .c_str());
321 throw std::runtime_error("Invalid sensor path");
322 }
323
324 sensorPath = sensorPath.substr(0, pos);
325 return sensorPath.substr(sensorPath.find_last_of('/') + 1);
326}
327
328bool ThresholdAlarmLogger::skipSensorType(const std::string& type)
329{
330 return (type == "utilization");
331}
332
333std::string ThresholdAlarmLogger::getCallout(const std::string& sensorPath)
334{
335 const std::array<std::string, 2> assocTypes{"inventory", "chassis"};
336
337 // Different implementations handle the association to the FRU
338 // differently:
339 // * phosphor-inventory-manager uses the 'inventory' association
340 // to point to the FRU.
341 // * dbus-sensors/entity-manager uses the 'chassis' association'.
342 // * For virtual sensors, no association.
343
344 for (const auto& assocType : assocTypes)
345 {
346 auto assocPath = sensorPath + "/" + assocType;
347
348 try
349 {
350 auto endpoints = SDBusPlus::getProperty<std::vector<std::string>>(
351 bus, assocPath, assocInterface, "endpoints");
352
353 if (!endpoints.empty())
354 {
355 return endpoints[0];
356 }
357 }
358 catch (const DBusServiceError& e)
359 {
360 // The association doesn't exist
361 continue;
362 }
363 }
364
365 return std::string{};
Matt Spinler50bf8162021-02-01 16:24:01 -0600366}
367
Matt Spinler66e75a72021-05-14 10:32:47 -0500368void ThresholdAlarmLogger::powerStateChanged(bool powerStateOn)
369{
370 if (powerStateOn)
371 {
372 checkThresholds();
373 }
374}
375
376void ThresholdAlarmLogger::checkThresholds()
377{
378 for (const auto& [interfaceKey, alarmMap] : alarms)
379 {
380 for (const auto& [propertyName, alarmValue] : alarmMap)
381 {
382 if (alarmValue)
383 {
384 const auto& sensorPath = std::get<0>(interfaceKey);
385 const auto& interface = std::get<1>(interfaceKey);
386
387 createEventLog(sensorPath, interface, propertyName, alarmValue);
388 }
389 }
390 }
391}
392
Matt Spinler403d1f52021-02-01 15:35:25 -0600393} // namespace sensor::monitor