/
threshold_event_monitor.hpp
295 lines (280 loc) · 10.5 KB
/
threshold_event_monitor.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/*
// Copyright (c) 2018 Intel Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
*/
#pragma once
#include <sel_logger.hpp>
#include <sensorutils.hpp>
#include <string_view>
#include <variant>
enum class thresholdEventOffsets : uint8_t
{
lowerNonCritGoingLow = 0x00,
lowerCritGoingLow = 0x02,
upperNonCritGoingHigh = 0x07,
upperCritGoingHigh = 0x09,
};
static constexpr const uint8_t thresholdEventDataTriggerReadingByte2 = (1 << 6);
static constexpr const uint8_t thresholdEventDataTriggerReadingByte3 = (1 << 4);
static const std::string openBMCMessageRegistryVersion("0.1");
inline static sdbusplus::bus::match::match startThresholdEventMonitor(
std::shared_ptr<sdbusplus::asio::connection> conn)
{
auto thresholdEventMatcherCallback = [conn](
sdbusplus::message::message &msg) {
// This static set of std::pair<path, event> tracks asserted events to
// avoid duplicate logs or deasserts logged without an assert
static boost::container::flat_set<std::pair<std::string, std::string>>
assertedEvents;
// SEL event data is three bytes where 0xFF means unspecified
std::vector<uint8_t> eventData(selEvtDataMaxSize, 0xFF);
// Get the event type and assertion details from the message
std::string thresholdInterface;
boost::container::flat_map<std::string, std::variant<bool>>
propertiesChanged;
msg.read(thresholdInterface, propertiesChanged);
std::string event = propertiesChanged.begin()->first;
bool *pval = std::get_if<bool>(&propertiesChanged.begin()->second);
if (!pval)
{
std::cerr << "threshold event direction has invalid type\n";
return;
}
bool assert = *pval;
// Check the asserted events to determine if we should log this event
std::pair<std::string, std::string> pathAndEvent(
std::string(msg.get_path()), event);
if (assert)
{
// For asserts, add the event to the set and only log it if it's new
if (assertedEvents.insert(pathAndEvent).second == false)
{
// event is already in the set
return;
}
}
else
{
// For deasserts, remove the event and only log the deassert if it
// was asserted
if (assertedEvents.erase(pathAndEvent) == 0)
{
// asserted event was not in the set
return;
}
}
// Set the IPMI threshold event type based on the event details from the
// message
if (event == "CriticalAlarmLow")
{
eventData[0] =
static_cast<uint8_t>(thresholdEventOffsets::lowerCritGoingLow);
}
else if (event == "WarningAlarmLow")
{
eventData[0] = static_cast<uint8_t>(
thresholdEventOffsets::lowerNonCritGoingLow);
}
else if (event == "WarningAlarmHigh")
{
eventData[0] = static_cast<uint8_t>(
thresholdEventOffsets::upperNonCritGoingHigh);
}
else if (event == "CriticalAlarmHigh")
{
eventData[0] =
static_cast<uint8_t>(thresholdEventOffsets::upperCritGoingHigh);
}
// Indicate that bytes 2 and 3 are threshold sensor trigger values
eventData[0] |= thresholdEventDataTriggerReadingByte2 |
thresholdEventDataTriggerReadingByte3;
// Get the sensor reading to put in the event data
sdbusplus::message::message getSensorValue =
conn->new_method_call(msg.get_sender(), msg.get_path(),
"org.freedesktop.DBus.Properties", "GetAll");
getSensorValue.append("xyz.openbmc_project.Sensor.Value");
boost::container::flat_map<std::string, std::variant<double, int64_t>>
sensorValue;
try
{
sdbusplus::message::message getSensorValueResp =
conn->call(getSensorValue);
getSensorValueResp.read(sensorValue);
}
catch (sdbusplus::exception_t &)
{
std::cerr << "error getting sensor value from " << msg.get_path()
<< "\n";
return;
}
double max = 0;
auto findMax = sensorValue.find("MaxValue");
if (findMax != sensorValue.end())
{
max = std::visit(ipmi::VariantToDoubleVisitor(), findMax->second);
}
double min = 0;
auto findMin = sensorValue.find("MinValue");
if (findMin != sensorValue.end())
{
min = std::visit(ipmi::VariantToDoubleVisitor(), findMin->second);
}
double sensorVal = 0;
auto findVal = sensorValue.find("Value");
if (findVal != sensorValue.end())
{
sensorVal =
std::visit(ipmi::VariantToDoubleVisitor(), findVal->second);
}
double scale = 0;
auto findScale = sensorValue.find("Scale");
if (findScale != sensorValue.end())
{
scale =
std::visit(ipmi::VariantToDoubleVisitor(), findScale->second);
sensorVal *= std::pow(10, scale);
}
try
{
eventData[1] = ipmi::getScaledIPMIValue(sensorVal, max, min);
}
catch (std::runtime_error &e)
{
std::cerr << e.what();
eventData[1] = 0xFF;
}
// Get the threshold value to put in the event data
// Get the threshold parameter by removing the "Alarm" text from the
// event string
std::string alarm("Alarm");
if (std::string::size_type pos = event.find(alarm);
pos != std::string::npos)
{
event.erase(pos, alarm.length());
}
sdbusplus::message::message getThreshold =
conn->new_method_call(msg.get_sender(), msg.get_path(),
"org.freedesktop.DBus.Properties", "Get");
getThreshold.append(thresholdInterface, event);
std::variant<double, int64_t> thresholdValue;
try
{
sdbusplus::message::message getThresholdResp =
conn->call(getThreshold);
getThresholdResp.read(thresholdValue);
}
catch (sdbusplus::exception_t &)
{
std::cerr << "error getting sensor threshold from "
<< msg.get_path() << "\n";
return;
}
double thresholdVal =
std::visit(ipmi::VariantToDoubleVisitor(), thresholdValue);
if (findScale != sensorValue.end())
{
thresholdVal *= std::pow(10, scale);
}
try
{
eventData[2] = ipmi::getScaledIPMIValue(thresholdVal, max, min);
}
catch (std::runtime_error &e)
{
std::cerr << e.what();
eventData[2] = 0xFF;
}
// Construct a human-readable message of this event for the log
std::string_view sensorName(msg.get_path());
sensorName.remove_prefix(
std::min(sensorName.find_last_of("/") + 1, sensorName.size()));
std::string threshold;
std::string direction;
std::string redfishMessageID =
"OpenBMC." + openBMCMessageRegistryVersion;
if (event == "CriticalLow")
{
threshold = "critical low";
if (assert)
{
direction = "low";
redfishMessageID += ".SensorThresholdCriticalLowGoingLow";
}
else
{
direction = "high";
redfishMessageID += ".SensorThresholdCriticalLowGoingHigh";
}
}
else if (event == "WarningLow")
{
threshold = "warning low";
if (assert)
{
direction = "low";
redfishMessageID += ".SensorThresholdWarningLowGoingLow";
}
else
{
direction = "high";
redfishMessageID += ".SensorThresholdWarningLowGoingHigh";
}
}
else if (event == "WarningHigh")
{
threshold = "warning high";
if (assert)
{
direction = "high";
redfishMessageID += ".SensorThresholdWarningHighGoingHigh";
}
else
{
direction = "low";
redfishMessageID += ".SensorThresholdWarningHighGoingLow";
}
}
else if (event == "CriticalHigh")
{
threshold = "critical high";
if (assert)
{
direction = "high";
redfishMessageID += ".SensorThresholdCriticalHighGoingHigh";
}
else
{
direction = "low";
redfishMessageID += ".SensorThresholdCriticalHighGoingLow";
}
}
std::string journalMsg(std::string(sensorName) + " sensor crossed a " +
threshold + " threshold going " + direction +
". Reading=" + std::to_string(sensorVal) +
" Threshold=" + std::to_string(thresholdVal) +
".");
selAddSystemRecord(
journalMsg, std::string(msg.get_path()), eventData, assert,
selBMCGenID, "REDFISH_MESSAGE_ID=%s", redfishMessageID.c_str(),
"REDFISH_MESSAGE_ARGS=%.*s,%f,%f", sensorName.length(),
sensorName.data(), sensorVal, thresholdVal);
};
sdbusplus::bus::match::match thresholdEventMatcher(
static_cast<sdbusplus::bus::bus &>(*conn),
"type='signal',interface='org.freedesktop.DBus.Properties',member='"
"PropertiesChanged',arg0namespace='xyz.openbmc_project.Sensor."
"Threshold'",
std::move(thresholdEventMatcherCallback));
return thresholdEventMatcher;
}