Jason M. Bills | 5e049d3 | 2018-10-19 12:59:38 -0700 | [diff] [blame] | 1 | /* |
| 2 | // Copyright (c) 2018 Intel 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 | |
| 17 | #pragma once |
| 18 | #include <sel_logger.hpp> |
| 19 | #include <sensorutils.hpp> |
Zhikui Ren | 672bdfc | 2020-07-14 11:37:01 -0700 | [diff] [blame] | 20 | |
Jason M. Bills | 2b9704d | 2018-11-02 13:12:09 -0700 | [diff] [blame] | 21 | #include <string_view> |
Jason M. Bills | 2bc9f0d | 2019-03-27 11:13:12 -0700 | [diff] [blame] | 22 | #include <variant> |
Jason M. Bills | 5e049d3 | 2018-10-19 12:59:38 -0700 | [diff] [blame] | 23 | |
| 24 | enum class thresholdEventOffsets : uint8_t |
| 25 | { |
| 26 | lowerNonCritGoingLow = 0x00, |
| 27 | lowerCritGoingLow = 0x02, |
| 28 | upperNonCritGoingHigh = 0x07, |
| 29 | upperCritGoingHigh = 0x09, |
| 30 | }; |
| 31 | |
| 32 | static constexpr const uint8_t thresholdEventDataTriggerReadingByte2 = (1 << 6); |
| 33 | static constexpr const uint8_t thresholdEventDataTriggerReadingByte3 = (1 << 4); |
| 34 | |
Jason M. Bills | 60ec28a | 2019-04-11 16:53:44 -0700 | [diff] [blame] | 35 | static const std::string openBMCMessageRegistryVersion("0.1"); |
Jason M. Bills | 2b9704d | 2018-11-02 13:12:09 -0700 | [diff] [blame] | 36 | |
Patrick Williams | ccef227 | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 37 | inline static sdbusplus::bus::match_t startThresholdAssertMonitor( |
Jason M. Bills | 5e049d3 | 2018-10-19 12:59:38 -0700 | [diff] [blame] | 38 | std::shared_ptr<sdbusplus::asio::connection> conn) |
| 39 | { |
Patrick Williams | ccef227 | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 40 | auto thresholdAssertMatcherCallback = [conn](sdbusplus::message_t& msg) { |
Jason M. Bills | 5e049d3 | 2018-10-19 12:59:38 -0700 | [diff] [blame] | 41 | // This static set of std::pair<path, event> tracks asserted events to |
| 42 | // avoid duplicate logs or deasserts logged without an assert |
| 43 | static boost::container::flat_set<std::pair<std::string, std::string>> |
| 44 | assertedEvents; |
Zhikui Ren | 25b26e1 | 2020-06-26 20:18:19 -0700 | [diff] [blame] | 45 | std::vector<uint8_t> eventData(selEvtDataMaxSize, |
| 46 | selEvtDataUnspecified); |
Jason M. Bills | 5e049d3 | 2018-10-19 12:59:38 -0700 | [diff] [blame] | 47 | |
| 48 | // Get the event type and assertion details from the message |
Zhikui Ren | 25b26e1 | 2020-06-26 20:18:19 -0700 | [diff] [blame] | 49 | std::string sensorName; |
Jason M. Bills | 5e049d3 | 2018-10-19 12:59:38 -0700 | [diff] [blame] | 50 | std::string thresholdInterface; |
Zhikui Ren | 25b26e1 | 2020-06-26 20:18:19 -0700 | [diff] [blame] | 51 | std::string event; |
| 52 | bool assert; |
| 53 | double assertValue; |
| 54 | try |
Jason M. Bills | 5e049d3 | 2018-10-19 12:59:38 -0700 | [diff] [blame] | 55 | { |
Zhikui Ren | 25b26e1 | 2020-06-26 20:18:19 -0700 | [diff] [blame] | 56 | msg.read(sensorName, thresholdInterface, event, assert, |
| 57 | assertValue); |
| 58 | } |
Patrick Williams | 3f4cd97 | 2021-10-06 12:42:50 -0500 | [diff] [blame] | 59 | catch (const sdbusplus::exception_t&) |
Zhikui Ren | 25b26e1 | 2020-06-26 20:18:19 -0700 | [diff] [blame] | 60 | { |
| 61 | std::cerr << "error getting assert signal data from " |
| 62 | << msg.get_path() << "\n"; |
Jason M. Bills | 5e049d3 | 2018-10-19 12:59:38 -0700 | [diff] [blame] | 63 | return; |
| 64 | } |
Jason M. Bills | 5e049d3 | 2018-10-19 12:59:38 -0700 | [diff] [blame] | 65 | |
| 66 | // Check the asserted events to determine if we should log this event |
| 67 | std::pair<std::string, std::string> pathAndEvent( |
| 68 | std::string(msg.get_path()), event); |
| 69 | if (assert) |
| 70 | { |
| 71 | // For asserts, add the event to the set and only log it if it's new |
| 72 | if (assertedEvents.insert(pathAndEvent).second == false) |
| 73 | { |
| 74 | // event is already in the set |
| 75 | return; |
| 76 | } |
| 77 | } |
| 78 | else |
| 79 | { |
| 80 | // For deasserts, remove the event and only log the deassert if it |
| 81 | // was asserted |
| 82 | if (assertedEvents.erase(pathAndEvent) == 0) |
| 83 | { |
| 84 | // asserted event was not in the set |
| 85 | return; |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | // Set the IPMI threshold event type based on the event details from the |
| 90 | // message |
| 91 | if (event == "CriticalAlarmLow") |
| 92 | { |
| 93 | eventData[0] = |
| 94 | static_cast<uint8_t>(thresholdEventOffsets::lowerCritGoingLow); |
| 95 | } |
| 96 | else if (event == "WarningAlarmLow") |
| 97 | { |
| 98 | eventData[0] = static_cast<uint8_t>( |
| 99 | thresholdEventOffsets::lowerNonCritGoingLow); |
| 100 | } |
| 101 | else if (event == "WarningAlarmHigh") |
| 102 | { |
| 103 | eventData[0] = static_cast<uint8_t>( |
| 104 | thresholdEventOffsets::upperNonCritGoingHigh); |
| 105 | } |
| 106 | else if (event == "CriticalAlarmHigh") |
| 107 | { |
| 108 | eventData[0] = |
| 109 | static_cast<uint8_t>(thresholdEventOffsets::upperCritGoingHigh); |
| 110 | } |
| 111 | // Indicate that bytes 2 and 3 are threshold sensor trigger values |
| 112 | eventData[0] |= thresholdEventDataTriggerReadingByte2 | |
| 113 | thresholdEventDataTriggerReadingByte3; |
| 114 | |
| 115 | // Get the sensor reading to put in the event data |
Patrick Williams | ccef227 | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 116 | sdbusplus::message_t getSensorValue = |
Jason M. Bills | 5e049d3 | 2018-10-19 12:59:38 -0700 | [diff] [blame] | 117 | conn->new_method_call(msg.get_sender(), msg.get_path(), |
| 118 | "org.freedesktop.DBus.Properties", "GetAll"); |
| 119 | getSensorValue.append("xyz.openbmc_project.Sensor.Value"); |
George Hung | d9d7896 | 2019-03-27 13:25:24 +0800 | [diff] [blame] | 120 | boost::container::flat_map<std::string, std::variant<double, int64_t>> |
Jason M. Bills | 5e049d3 | 2018-10-19 12:59:38 -0700 | [diff] [blame] | 121 | sensorValue; |
| 122 | try |
| 123 | { |
Patrick Williams | ccef227 | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 124 | sdbusplus::message_t getSensorValueResp = |
Jason M. Bills | 5e049d3 | 2018-10-19 12:59:38 -0700 | [diff] [blame] | 125 | conn->call(getSensorValue); |
| 126 | getSensorValueResp.read(sensorValue); |
| 127 | } |
Patrick Williams | 3f4cd97 | 2021-10-06 12:42:50 -0500 | [diff] [blame] | 128 | catch (const sdbusplus::exception_t&) |
Jason M. Bills | 5e049d3 | 2018-10-19 12:59:38 -0700 | [diff] [blame] | 129 | { |
| 130 | std::cerr << "error getting sensor value from " << msg.get_path() |
| 131 | << "\n"; |
| 132 | return; |
| 133 | } |
Jason M. Bills | bb071fb | 2019-03-27 11:15:42 -0700 | [diff] [blame] | 134 | double max = 0; |
| 135 | auto findMax = sensorValue.find("MaxValue"); |
| 136 | if (findMax != sensorValue.end()) |
| 137 | { |
| 138 | max = std::visit(ipmi::VariantToDoubleVisitor(), findMax->second); |
| 139 | } |
| 140 | double min = 0; |
| 141 | auto findMin = sensorValue.find("MinValue"); |
| 142 | if (findMin != sensorValue.end()) |
| 143 | { |
| 144 | min = std::visit(ipmi::VariantToDoubleVisitor(), findMin->second); |
| 145 | } |
George Hung | d9d7896 | 2019-03-27 13:25:24 +0800 | [diff] [blame] | 146 | |
Jason M. Bills | 5e049d3 | 2018-10-19 12:59:38 -0700 | [diff] [blame] | 147 | try |
| 148 | { |
Zhikui Ren | 25b26e1 | 2020-06-26 20:18:19 -0700 | [diff] [blame] | 149 | eventData[1] = ipmi::getScaledIPMIValue(assertValue, max, min); |
Jason M. Bills | 5e049d3 | 2018-10-19 12:59:38 -0700 | [diff] [blame] | 150 | } |
Zhikui Ren | 672bdfc | 2020-07-14 11:37:01 -0700 | [diff] [blame] | 151 | catch (const std::exception& e) |
Jason M. Bills | 5e049d3 | 2018-10-19 12:59:38 -0700 | [diff] [blame] | 152 | { |
| 153 | std::cerr << e.what(); |
Zhikui Ren | 25b26e1 | 2020-06-26 20:18:19 -0700 | [diff] [blame] | 154 | eventData[1] = selEvtDataUnspecified; |
Jason M. Bills | 5e049d3 | 2018-10-19 12:59:38 -0700 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | // Get the threshold value to put in the event data |
| 158 | // Get the threshold parameter by removing the "Alarm" text from the |
| 159 | // event string |
| 160 | std::string alarm("Alarm"); |
| 161 | if (std::string::size_type pos = event.find(alarm); |
| 162 | pos != std::string::npos) |
| 163 | { |
| 164 | event.erase(pos, alarm.length()); |
| 165 | } |
Patrick Williams | ccef227 | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 166 | sdbusplus::message_t getThreshold = |
Jason M. Bills | 5e049d3 | 2018-10-19 12:59:38 -0700 | [diff] [blame] | 167 | conn->new_method_call(msg.get_sender(), msg.get_path(), |
| 168 | "org.freedesktop.DBus.Properties", "Get"); |
| 169 | getThreshold.append(thresholdInterface, event); |
George Hung | d9d7896 | 2019-03-27 13:25:24 +0800 | [diff] [blame] | 170 | std::variant<double, int64_t> thresholdValue; |
Jason M. Bills | 5e049d3 | 2018-10-19 12:59:38 -0700 | [diff] [blame] | 171 | try |
| 172 | { |
Patrick Williams | ccef227 | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 173 | sdbusplus::message_t getThresholdResp = conn->call(getThreshold); |
Jason M. Bills | 5e049d3 | 2018-10-19 12:59:38 -0700 | [diff] [blame] | 174 | getThresholdResp.read(thresholdValue); |
| 175 | } |
Patrick Williams | 3f4cd97 | 2021-10-06 12:42:50 -0500 | [diff] [blame] | 176 | catch (const sdbusplus::exception_t&) |
Jason M. Bills | 5e049d3 | 2018-10-19 12:59:38 -0700 | [diff] [blame] | 177 | { |
| 178 | std::cerr << "error getting sensor threshold from " |
| 179 | << msg.get_path() << "\n"; |
| 180 | return; |
| 181 | } |
Jason M. Bills | 2bc9f0d | 2019-03-27 11:13:12 -0700 | [diff] [blame] | 182 | double thresholdVal = |
| 183 | std::visit(ipmi::VariantToDoubleVisitor(), thresholdValue); |
Zhikui Ren | 25b26e1 | 2020-06-26 20:18:19 -0700 | [diff] [blame] | 184 | |
| 185 | double scale = 0; |
| 186 | auto findScale = sensorValue.find("Scale"); |
George Hung | d9d7896 | 2019-03-27 13:25:24 +0800 | [diff] [blame] | 187 | if (findScale != sensorValue.end()) |
| 188 | { |
Zhikui Ren | 25b26e1 | 2020-06-26 20:18:19 -0700 | [diff] [blame] | 189 | scale = |
| 190 | std::visit(ipmi::VariantToDoubleVisitor(), findScale->second); |
George Hung | d9d7896 | 2019-03-27 13:25:24 +0800 | [diff] [blame] | 191 | thresholdVal *= std::pow(10, scale); |
| 192 | } |
Jason M. Bills | 5e049d3 | 2018-10-19 12:59:38 -0700 | [diff] [blame] | 193 | try |
| 194 | { |
| 195 | eventData[2] = ipmi::getScaledIPMIValue(thresholdVal, max, min); |
| 196 | } |
Zhikui Ren | 672bdfc | 2020-07-14 11:37:01 -0700 | [diff] [blame] | 197 | catch (const std::exception& e) |
Jason M. Bills | 5e049d3 | 2018-10-19 12:59:38 -0700 | [diff] [blame] | 198 | { |
| 199 | std::cerr << e.what(); |
Zhikui Ren | 25b26e1 | 2020-06-26 20:18:19 -0700 | [diff] [blame] | 200 | eventData[2] = selEvtDataUnspecified; |
Jason M. Bills | 5e049d3 | 2018-10-19 12:59:38 -0700 | [diff] [blame] | 201 | } |
| 202 | |
Jason M. Bills | 2b9704d | 2018-11-02 13:12:09 -0700 | [diff] [blame] | 203 | std::string threshold; |
| 204 | std::string direction; |
Jason M. Bills | f2552a5 | 2019-03-28 11:54:48 -0700 | [diff] [blame] | 205 | std::string redfishMessageID = |
Jason M. Bills | 60ec28a | 2019-04-11 16:53:44 -0700 | [diff] [blame] | 206 | "OpenBMC." + openBMCMessageRegistryVersion; |
Jason M. Bills | 2b9704d | 2018-11-02 13:12:09 -0700 | [diff] [blame] | 207 | if (event == "CriticalLow") |
| 208 | { |
| 209 | threshold = "critical low"; |
| 210 | if (assert) |
| 211 | { |
| 212 | direction = "low"; |
Jason M. Bills | 60ec28a | 2019-04-11 16:53:44 -0700 | [diff] [blame] | 213 | redfishMessageID += ".SensorThresholdCriticalLowGoingLow"; |
Jason M. Bills | 2b9704d | 2018-11-02 13:12:09 -0700 | [diff] [blame] | 214 | } |
| 215 | else |
| 216 | { |
| 217 | direction = "high"; |
Jason M. Bills | 60ec28a | 2019-04-11 16:53:44 -0700 | [diff] [blame] | 218 | redfishMessageID += ".SensorThresholdCriticalLowGoingHigh"; |
Jason M. Bills | 2b9704d | 2018-11-02 13:12:09 -0700 | [diff] [blame] | 219 | } |
| 220 | } |
| 221 | else if (event == "WarningLow") |
| 222 | { |
| 223 | threshold = "warning low"; |
| 224 | if (assert) |
| 225 | { |
| 226 | direction = "low"; |
Jason M. Bills | 60ec28a | 2019-04-11 16:53:44 -0700 | [diff] [blame] | 227 | redfishMessageID += ".SensorThresholdWarningLowGoingLow"; |
Jason M. Bills | 2b9704d | 2018-11-02 13:12:09 -0700 | [diff] [blame] | 228 | } |
| 229 | else |
| 230 | { |
| 231 | direction = "high"; |
Jason M. Bills | 60ec28a | 2019-04-11 16:53:44 -0700 | [diff] [blame] | 232 | redfishMessageID += ".SensorThresholdWarningLowGoingHigh"; |
Jason M. Bills | 2b9704d | 2018-11-02 13:12:09 -0700 | [diff] [blame] | 233 | } |
| 234 | } |
| 235 | else if (event == "WarningHigh") |
| 236 | { |
| 237 | threshold = "warning high"; |
| 238 | if (assert) |
| 239 | { |
| 240 | direction = "high"; |
Jason M. Bills | 60ec28a | 2019-04-11 16:53:44 -0700 | [diff] [blame] | 241 | redfishMessageID += ".SensorThresholdWarningHighGoingHigh"; |
Jason M. Bills | 2b9704d | 2018-11-02 13:12:09 -0700 | [diff] [blame] | 242 | } |
| 243 | else |
| 244 | { |
| 245 | direction = "low"; |
Jason M. Bills | 60ec28a | 2019-04-11 16:53:44 -0700 | [diff] [blame] | 246 | redfishMessageID += ".SensorThresholdWarningHighGoingLow"; |
Jason M. Bills | 2b9704d | 2018-11-02 13:12:09 -0700 | [diff] [blame] | 247 | } |
| 248 | } |
| 249 | else if (event == "CriticalHigh") |
| 250 | { |
| 251 | threshold = "critical high"; |
| 252 | if (assert) |
| 253 | { |
| 254 | direction = "high"; |
Jason M. Bills | 60ec28a | 2019-04-11 16:53:44 -0700 | [diff] [blame] | 255 | redfishMessageID += ".SensorThresholdCriticalHighGoingHigh"; |
Jason M. Bills | 2b9704d | 2018-11-02 13:12:09 -0700 | [diff] [blame] | 256 | } |
| 257 | else |
| 258 | { |
| 259 | direction = "low"; |
Jason M. Bills | 60ec28a | 2019-04-11 16:53:44 -0700 | [diff] [blame] | 260 | redfishMessageID += ".SensorThresholdCriticalHighGoingLow"; |
Jason M. Bills | 2b9704d | 2018-11-02 13:12:09 -0700 | [diff] [blame] | 261 | } |
| 262 | } |
| 263 | |
| 264 | std::string journalMsg(std::string(sensorName) + " sensor crossed a " + |
| 265 | threshold + " threshold going " + direction + |
Zhikui Ren | 25b26e1 | 2020-06-26 20:18:19 -0700 | [diff] [blame] | 266 | ". Reading=" + std::to_string(assertValue) + |
Jason M. Bills | 2b9704d | 2018-11-02 13:12:09 -0700 | [diff] [blame] | 267 | " Threshold=" + std::to_string(thresholdVal) + |
| 268 | "."); |
Jason M. Bills | 5e049d3 | 2018-10-19 12:59:38 -0700 | [diff] [blame] | 269 | |
Jason M. Bills | 5ff505f | 2019-04-11 16:58:01 -0700 | [diff] [blame] | 270 | selAddSystemRecord( |
| 271 | journalMsg, std::string(msg.get_path()), eventData, assert, |
| 272 | selBMCGenID, "REDFISH_MESSAGE_ID=%s", redfishMessageID.c_str(), |
| 273 | "REDFISH_MESSAGE_ARGS=%.*s,%f,%f", sensorName.length(), |
Zhikui Ren | 25b26e1 | 2020-06-26 20:18:19 -0700 | [diff] [blame] | 274 | sensorName.data(), assertValue, thresholdVal); |
Jason M. Bills | 5e049d3 | 2018-10-19 12:59:38 -0700 | [diff] [blame] | 275 | }; |
Patrick Williams | ccef227 | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 276 | sdbusplus::bus::match_t thresholdAssertMatcher( |
| 277 | static_cast<sdbusplus::bus_t&>(*conn), |
Zhikui Ren | 25b26e1 | 2020-06-26 20:18:19 -0700 | [diff] [blame] | 278 | "type='signal', member='ThresholdAsserted'", |
| 279 | std::move(thresholdAssertMatcherCallback)); |
| 280 | return thresholdAssertMatcher; |
Jason M. Bills | 5e049d3 | 2018-10-19 12:59:38 -0700 | [diff] [blame] | 281 | } |