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 | #include <systemd/sd-journal.h> |
| 17 | |
Jason M. Bills | c4a336f | 2019-04-23 10:43:10 -0700 | [diff] [blame] | 18 | #include <boost/algorithm/string.hpp> |
Ed Tanous | 9092699 | 2020-09-11 12:59:33 -0700 | [diff] [blame] | 19 | #include <boost/asio/io_service.hpp> |
Jason M. Bills | 5e049d3 | 2018-10-19 12:59:38 -0700 | [diff] [blame] | 20 | #include <boost/container/flat_map.hpp> |
| 21 | #include <boost/container/flat_set.hpp> |
Zhikui Ren | 672bdfc | 2020-07-14 11:37:01 -0700 | [diff] [blame] | 22 | #include <pulse_event_monitor.hpp> |
| 23 | #include <sdbusplus/asio/object_server.hpp> |
| 24 | #include <sel_logger.hpp> |
| 25 | #include <threshold_event_monitor.hpp> |
Charles Hsu | dbd77b9 | 2020-10-29 11:20:34 +0800 | [diff] [blame] | 26 | #include <watchdog_event_monitor.hpp> |
George Hung | 486e42e | 2021-04-14 20:20:42 +0800 | [diff] [blame] | 27 | #ifdef SEL_LOGGER_MONITOR_THRESHOLD_ALARM_EVENTS |
| 28 | #include <threshold_alarm_event_monitor.hpp> |
| 29 | #endif |
Zhikui Ren | 672bdfc | 2020-07-14 11:37:01 -0700 | [diff] [blame] | 30 | |
Jason M. Bills | c4a336f | 2019-04-23 10:43:10 -0700 | [diff] [blame] | 31 | #include <filesystem> |
| 32 | #include <fstream> |
Jason M. Bills | 5e049d3 | 2018-10-19 12:59:38 -0700 | [diff] [blame] | 33 | #include <iomanip> |
| 34 | #include <iostream> |
Jason M. Bills | 5e049d3 | 2018-10-19 12:59:38 -0700 | [diff] [blame] | 35 | #include <sstream> |
Jason M. Bills | 5e049d3 | 2018-10-19 12:59:38 -0700 | [diff] [blame] | 36 | |
Lei YU | e526b86 | 2020-12-03 15:41:59 +0800 | [diff] [blame] | 37 | #ifdef SEL_LOGGER_SEND_TO_LOGGING_SERVICE |
| 38 | #include <phosphor-logging/elog-errors.hpp> |
| 39 | #include <phosphor-logging/elog.hpp> |
| 40 | #include <phosphor-logging/log.hpp> |
| 41 | #include <xyz/openbmc_project/Logging/SEL/error.hpp> |
| 42 | |
| 43 | using namespace phosphor::logging; |
| 44 | using SELCreated = |
| 45 | sdbusplus::xyz::openbmc_project::Logging::SEL::Error::Created; |
| 46 | #endif |
| 47 | |
Jason M. Bills | 5e049d3 | 2018-10-19 12:59:38 -0700 | [diff] [blame] | 48 | struct DBusInternalError final : public sdbusplus::exception_t |
| 49 | { |
Zhikui Ren | 672bdfc | 2020-07-14 11:37:01 -0700 | [diff] [blame] | 50 | const char* name() const noexcept override |
Jason M. Bills | 5e049d3 | 2018-10-19 12:59:38 -0700 | [diff] [blame] | 51 | { |
| 52 | return "org.freedesktop.DBus.Error.Failed"; |
| 53 | }; |
Zhikui Ren | 672bdfc | 2020-07-14 11:37:01 -0700 | [diff] [blame] | 54 | const char* description() const noexcept override |
Jason M. Bills | 5e049d3 | 2018-10-19 12:59:38 -0700 | [diff] [blame] | 55 | { |
| 56 | return "internal error"; |
| 57 | }; |
Zhikui Ren | 672bdfc | 2020-07-14 11:37:01 -0700 | [diff] [blame] | 58 | const char* what() const noexcept override |
Jason M. Bills | 5e049d3 | 2018-10-19 12:59:38 -0700 | [diff] [blame] | 59 | { |
| 60 | return "org.freedesktop.DBus.Error.Failed: " |
| 61 | "internal error"; |
| 62 | }; |
| 63 | }; |
| 64 | |
Lei YU | e526b86 | 2020-12-03 15:41:59 +0800 | [diff] [blame] | 65 | #ifndef SEL_LOGGER_SEND_TO_LOGGING_SERVICE |
Zhikui Ren | 672bdfc | 2020-07-14 11:37:01 -0700 | [diff] [blame] | 66 | static bool getSELLogFiles(std::vector<std::filesystem::path>& selLogFiles) |
Jason M. Bills | c4a336f | 2019-04-23 10:43:10 -0700 | [diff] [blame] | 67 | { |
| 68 | // Loop through the directory looking for ipmi_sel log files |
Zhikui Ren | 672bdfc | 2020-07-14 11:37:01 -0700 | [diff] [blame] | 69 | for (const std::filesystem::directory_entry& dirEnt : |
Jason M. Bills | c4a336f | 2019-04-23 10:43:10 -0700 | [diff] [blame] | 70 | std::filesystem::directory_iterator(selLogDir)) |
| 71 | { |
| 72 | std::string filename = dirEnt.path().filename(); |
| 73 | if (boost::starts_with(filename, selLogFilename)) |
| 74 | { |
| 75 | // If we find an ipmi_sel log file, save the path |
| 76 | selLogFiles.emplace_back(selLogDir / filename); |
| 77 | } |
| 78 | } |
| 79 | // As the log files rotate, they are appended with a ".#" that is higher for |
| 80 | // the older logs. Since we don't expect more than 10 log files, we |
| 81 | // can just sort the list to get them in order from newest to oldest |
| 82 | std::sort(selLogFiles.begin(), selLogFiles.end()); |
| 83 | |
| 84 | return !selLogFiles.empty(); |
| 85 | } |
| 86 | |
Jason M. Bills | 5e049d3 | 2018-10-19 12:59:38 -0700 | [diff] [blame] | 87 | static unsigned int initializeRecordId(void) |
| 88 | { |
Jason M. Bills | c4a336f | 2019-04-23 10:43:10 -0700 | [diff] [blame] | 89 | std::vector<std::filesystem::path> selLogFiles; |
| 90 | if (!getSELLogFiles(selLogFiles)) |
Jason M. Bills | 5e049d3 | 2018-10-19 12:59:38 -0700 | [diff] [blame] | 91 | { |
Jason M. Bills | c4a336f | 2019-04-23 10:43:10 -0700 | [diff] [blame] | 92 | return selInvalidRecID; |
Jason M. Bills | 5e049d3 | 2018-10-19 12:59:38 -0700 | [diff] [blame] | 93 | } |
Jason M. Bills | c4a336f | 2019-04-23 10:43:10 -0700 | [diff] [blame] | 94 | std::ifstream logStream(selLogFiles.front()); |
| 95 | if (!logStream.is_open()) |
Jason M. Bills | 5e049d3 | 2018-10-19 12:59:38 -0700 | [diff] [blame] | 96 | { |
Jason M. Bills | c4a336f | 2019-04-23 10:43:10 -0700 | [diff] [blame] | 97 | return selInvalidRecID; |
| 98 | } |
| 99 | std::string line; |
| 100 | std::string newestEntry; |
| 101 | while (std::getline(logStream, line)) |
| 102 | { |
| 103 | newestEntry = line; |
| 104 | } |
Jason M. Bills | 5e049d3 | 2018-10-19 12:59:38 -0700 | [diff] [blame] | 105 | |
Jason M. Bills | c4a336f | 2019-04-23 10:43:10 -0700 | [diff] [blame] | 106 | std::vector<std::string> newestEntryFields; |
| 107 | boost::split(newestEntryFields, newestEntry, boost::is_any_of(" ,"), |
| 108 | boost::token_compress_on); |
| 109 | if (newestEntryFields.size() < 4) |
| 110 | { |
| 111 | return selInvalidRecID; |
Jason M. Bills | 5e049d3 | 2018-10-19 12:59:38 -0700 | [diff] [blame] | 112 | } |
Jason M. Bills | c4a336f | 2019-04-23 10:43:10 -0700 | [diff] [blame] | 113 | |
| 114 | return std::stoul(newestEntryFields[1]); |
Jason M. Bills | 5e049d3 | 2018-10-19 12:59:38 -0700 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | static unsigned int getNewRecordId(void) |
| 118 | { |
| 119 | static unsigned int recordId = initializeRecordId(); |
| 120 | |
Jason M. Bills | 6afe956 | 2019-08-29 16:33:55 -0700 | [diff] [blame] | 121 | // If the log has been cleared, also clear the current ID |
| 122 | std::vector<std::filesystem::path> selLogFiles; |
| 123 | if (!getSELLogFiles(selLogFiles)) |
| 124 | { |
| 125 | recordId = selInvalidRecID; |
| 126 | } |
| 127 | |
Jason M. Bills | 5e049d3 | 2018-10-19 12:59:38 -0700 | [diff] [blame] | 128 | if (++recordId >= selInvalidRecID) |
| 129 | { |
| 130 | recordId = 1; |
| 131 | } |
| 132 | return recordId; |
| 133 | } |
Lei YU | e526b86 | 2020-12-03 15:41:59 +0800 | [diff] [blame] | 134 | #endif |
Jason M. Bills | 5e049d3 | 2018-10-19 12:59:38 -0700 | [diff] [blame] | 135 | |
Zhikui Ren | 672bdfc | 2020-07-14 11:37:01 -0700 | [diff] [blame] | 136 | static void toHexStr(const std::vector<uint8_t>& data, std::string& hexStr) |
Jason M. Bills | 5e049d3 | 2018-10-19 12:59:38 -0700 | [diff] [blame] | 137 | { |
| 138 | std::stringstream stream; |
| 139 | stream << std::hex << std::uppercase << std::setfill('0'); |
William A. Kennington III | 2e43726 | 2021-07-30 12:04:19 -0700 | [diff] [blame] | 140 | for (int v : data) |
Jason M. Bills | 5e049d3 | 2018-10-19 12:59:38 -0700 | [diff] [blame] | 141 | { |
| 142 | stream << std::setw(2) << v; |
| 143 | } |
| 144 | hexStr = stream.str(); |
| 145 | } |
| 146 | |
Jason M. Bills | 97be353 | 2018-11-02 13:09:16 -0700 | [diff] [blame] | 147 | template <typename... T> |
William A. Kennington III | d585c59 | 2021-07-30 12:10:25 -0700 | [diff] [blame^] | 148 | static uint16_t selAddSystemRecord([[maybe_unused]] const std::string& message, |
| 149 | const std::string& path, |
| 150 | const std::vector<uint8_t>& selData, |
| 151 | const bool& assert, const uint16_t& genId, |
| 152 | [[maybe_unused]] T&&... metadata) |
Jason M. Bills | 5e049d3 | 2018-10-19 12:59:38 -0700 | [diff] [blame] | 153 | { |
| 154 | // Only 3 bytes of SEL event data are allowed in a system record |
| 155 | if (selData.size() > selEvtDataMaxSize) |
| 156 | { |
| 157 | throw std::invalid_argument("Event data too large"); |
| 158 | } |
| 159 | std::string selDataStr; |
| 160 | toHexStr(selData, selDataStr); |
| 161 | |
Lei YU | e526b86 | 2020-12-03 15:41:59 +0800 | [diff] [blame] | 162 | #ifdef SEL_LOGGER_SEND_TO_LOGGING_SERVICE |
| 163 | using namespace xyz::openbmc_project::Logging::SEL; |
Lei YU | 7d8a300 | 2021-01-21 17:16:19 +0800 | [diff] [blame] | 164 | auto entryID = report<SELCreated>( |
Lei YU | e526b86 | 2020-12-03 15:41:59 +0800 | [diff] [blame] | 165 | Created::RECORD_TYPE(selSystemType), Created::GENERATOR_ID(genId), |
| 166 | Created::SENSOR_DATA(selDataStr.c_str()), Created::EVENT_DIR(assert), |
| 167 | Created::SENSOR_PATH(path.c_str())); |
Lei YU | 7d8a300 | 2021-01-21 17:16:19 +0800 | [diff] [blame] | 168 | return static_cast<uint16_t>(entryID); |
Lei YU | e526b86 | 2020-12-03 15:41:59 +0800 | [diff] [blame] | 169 | #else |
Jason M. Bills | 5e049d3 | 2018-10-19 12:59:38 -0700 | [diff] [blame] | 170 | unsigned int recordId = getNewRecordId(); |
Jason M. Bills | 97be353 | 2018-11-02 13:09:16 -0700 | [diff] [blame] | 171 | sd_journal_send("MESSAGE=%s", message.c_str(), "PRIORITY=%i", selPriority, |
| 172 | "MESSAGE_ID=%s", selMessageId, "IPMI_SEL_RECORD_ID=%d", |
| 173 | recordId, "IPMI_SEL_RECORD_TYPE=%x", selSystemType, |
| 174 | "IPMI_SEL_GENERATOR_ID=%x", genId, |
| 175 | "IPMI_SEL_SENSOR_PATH=%s", path.c_str(), |
| 176 | "IPMI_SEL_EVENT_DIR=%x", assert, "IPMI_SEL_DATA=%s", |
| 177 | selDataStr.c_str(), std::forward<T>(metadata)..., NULL); |
Jason M. Bills | 5e049d3 | 2018-10-19 12:59:38 -0700 | [diff] [blame] | 178 | return recordId; |
Lei YU | e526b86 | 2020-12-03 15:41:59 +0800 | [diff] [blame] | 179 | #endif |
Jason M. Bills | 5e049d3 | 2018-10-19 12:59:38 -0700 | [diff] [blame] | 180 | } |
| 181 | |
William A. Kennington III | d585c59 | 2021-07-30 12:10:25 -0700 | [diff] [blame^] | 182 | static uint16_t selAddOemRecord([[maybe_unused]] const std::string& message, |
Zhikui Ren | 672bdfc | 2020-07-14 11:37:01 -0700 | [diff] [blame] | 183 | const std::vector<uint8_t>& selData, |
| 184 | const uint8_t& recordType) |
Jason M. Bills | 5e049d3 | 2018-10-19 12:59:38 -0700 | [diff] [blame] | 185 | { |
| 186 | // A maximum of 13 bytes of SEL event data are allowed in an OEM record |
| 187 | if (selData.size() > selOemDataMaxSize) |
| 188 | { |
| 189 | throw std::invalid_argument("Event data too large"); |
| 190 | } |
| 191 | std::string selDataStr; |
| 192 | toHexStr(selData, selDataStr); |
| 193 | |
Lei YU | e526b86 | 2020-12-03 15:41:59 +0800 | [diff] [blame] | 194 | #ifdef SEL_LOGGER_SEND_TO_LOGGING_SERVICE |
| 195 | using namespace xyz::openbmc_project::Logging::SEL; |
Lei YU | 7d8a300 | 2021-01-21 17:16:19 +0800 | [diff] [blame] | 196 | auto entryID = report<SELCreated>( |
| 197 | Created::RECORD_TYPE(recordType), Created::GENERATOR_ID(0), |
| 198 | Created::SENSOR_DATA(selDataStr.c_str()), Created::EVENT_DIR(0), |
| 199 | Created::SENSOR_PATH("")); |
| 200 | return static_cast<uint16_t>(entryID); |
Lei YU | e526b86 | 2020-12-03 15:41:59 +0800 | [diff] [blame] | 201 | #else |
Jason M. Bills | 5e049d3 | 2018-10-19 12:59:38 -0700 | [diff] [blame] | 202 | unsigned int recordId = getNewRecordId(); |
| 203 | sd_journal_send("MESSAGE=%s", message.c_str(), "PRIORITY=%i", selPriority, |
| 204 | "MESSAGE_ID=%s", selMessageId, "IPMI_SEL_RECORD_ID=%d", |
| 205 | recordId, "IPMI_SEL_RECORD_TYPE=%x", recordType, |
| 206 | "IPMI_SEL_DATA=%s", selDataStr.c_str(), NULL); |
| 207 | return recordId; |
Lei YU | e526b86 | 2020-12-03 15:41:59 +0800 | [diff] [blame] | 208 | #endif |
Jason M. Bills | 5e049d3 | 2018-10-19 12:59:38 -0700 | [diff] [blame] | 209 | } |
| 210 | |
William A. Kennington III | d585c59 | 2021-07-30 12:10:25 -0700 | [diff] [blame^] | 211 | int main(int, char*[]) |
Jason M. Bills | 5e049d3 | 2018-10-19 12:59:38 -0700 | [diff] [blame] | 212 | { |
| 213 | // setup connection to dbus |
| 214 | boost::asio::io_service io; |
| 215 | auto conn = std::make_shared<sdbusplus::asio::connection>(io); |
| 216 | |
| 217 | // IPMI SEL Object |
| 218 | conn->request_name(ipmiSelObject); |
| 219 | auto server = sdbusplus::asio::object_server(conn); |
| 220 | |
| 221 | // Add SEL Interface |
| 222 | std::shared_ptr<sdbusplus::asio::dbus_interface> ifaceAddSel = |
| 223 | server.add_interface(ipmiSelPath, ipmiSelAddInterface); |
| 224 | |
| 225 | // Add a new SEL entry |
| 226 | ifaceAddSel->register_method( |
Zhikui Ren | 672bdfc | 2020-07-14 11:37:01 -0700 | [diff] [blame] | 227 | "IpmiSelAdd", [](const std::string& message, const std::string& path, |
| 228 | const std::vector<uint8_t>& selData, |
| 229 | const bool& assert, const uint16_t& genId) { |
Jason M. Bills | 5e049d3 | 2018-10-19 12:59:38 -0700 | [diff] [blame] | 230 | return selAddSystemRecord(message, path, selData, assert, genId); |
| 231 | }); |
| 232 | // Add a new OEM SEL entry |
| 233 | ifaceAddSel->register_method( |
| 234 | "IpmiSelAddOem", |
Zhikui Ren | 672bdfc | 2020-07-14 11:37:01 -0700 | [diff] [blame] | 235 | [](const std::string& message, const std::vector<uint8_t>& selData, |
| 236 | const uint8_t& recordType) { |
Jason M. Bills | 5e049d3 | 2018-10-19 12:59:38 -0700 | [diff] [blame] | 237 | return selAddOemRecord(message, selData, recordType); |
| 238 | }); |
| 239 | ifaceAddSel->initialize(); |
| 240 | |
| 241 | #ifdef SEL_LOGGER_MONITOR_THRESHOLD_EVENTS |
Zhikui Ren | 25b26e1 | 2020-06-26 20:18:19 -0700 | [diff] [blame] | 242 | sdbusplus::bus::match::match thresholdAssertMonitor = |
| 243 | startThresholdAssertMonitor(conn); |
Jason M. Bills | 5e049d3 | 2018-10-19 12:59:38 -0700 | [diff] [blame] | 244 | #endif |
| 245 | |
Nikhil Potade | afbaa09 | 2019-03-06 16:18:13 -0800 | [diff] [blame] | 246 | #ifdef REDFISH_LOG_MONITOR_PULSE_EVENTS |
| 247 | sdbusplus::bus::match::match pulseEventMonitor = |
| 248 | startPulseEventMonitor(conn); |
| 249 | #endif |
| 250 | |
Charles Hsu | dbd77b9 | 2020-10-29 11:20:34 +0800 | [diff] [blame] | 251 | #ifdef SEL_LOGGER_MONITOR_WATCHDOG_EVENTS |
| 252 | sdbusplus::bus::match::match watchdogEventMonitor = |
| 253 | startWatchdogEventMonitor(conn); |
| 254 | #endif |
George Hung | 486e42e | 2021-04-14 20:20:42 +0800 | [diff] [blame] | 255 | |
| 256 | #ifdef SEL_LOGGER_MONITOR_THRESHOLD_ALARM_EVENTS |
| 257 | startThresholdAlarmMonitor(conn); |
| 258 | #endif |
Jason M. Bills | 5e049d3 | 2018-10-19 12:59:38 -0700 | [diff] [blame] | 259 | io.run(); |
| 260 | |
| 261 | return 0; |
| 262 | } |