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 | c352634 | 2023-03-06 13:37:53 -0800 | [diff] [blame] | 19 | #include <boost/asio/io_context.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> |
Jonico Eustaquio | 9c495c6 | 2024-07-02 16:35:14 -0500 | [diff] [blame^] | 27 | #ifdef SEL_LOGGER_ENABLE_SEL_DELETE |
| 28 | #include <xyz/openbmc_project/Common/error.hpp> |
| 29 | #endif |
George Hung | 486e42e | 2021-04-14 20:20:42 +0800 | [diff] [blame] | 30 | #ifdef SEL_LOGGER_MONITOR_THRESHOLD_ALARM_EVENTS |
| 31 | #include <threshold_alarm_event_monitor.hpp> |
| 32 | #endif |
JinFuLin | 7c2810b | 2022-12-02 13:55:28 +0800 | [diff] [blame] | 33 | #ifdef SEL_LOGGER_MONITOR_HOST_ERROR_EVENTS |
| 34 | #include <host_error_event_monitor.hpp> |
| 35 | #endif |
Zhikui Ren | 672bdfc | 2020-07-14 11:37:01 -0700 | [diff] [blame] | 36 | |
Jason M. Bills | c4a336f | 2019-04-23 10:43:10 -0700 | [diff] [blame] | 37 | #include <filesystem> |
| 38 | #include <fstream> |
Jason M. Bills | 5e049d3 | 2018-10-19 12:59:38 -0700 | [diff] [blame] | 39 | #include <iomanip> |
| 40 | #include <iostream> |
Jason M. Bills | 5e049d3 | 2018-10-19 12:59:38 -0700 | [diff] [blame] | 41 | #include <sstream> |
Jason M. Bills | 5e049d3 | 2018-10-19 12:59:38 -0700 | [diff] [blame] | 42 | |
| 43 | struct DBusInternalError final : public sdbusplus::exception_t |
| 44 | { |
Zhikui Ren | 672bdfc | 2020-07-14 11:37:01 -0700 | [diff] [blame] | 45 | const char* name() const noexcept override |
Jason M. Bills | 5e049d3 | 2018-10-19 12:59:38 -0700 | [diff] [blame] | 46 | { |
| 47 | return "org.freedesktop.DBus.Error.Failed"; |
Patrick Williams | a138ebd | 2021-09-08 15:46:34 -0500 | [diff] [blame] | 48 | } |
Zhikui Ren | 672bdfc | 2020-07-14 11:37:01 -0700 | [diff] [blame] | 49 | const char* description() const noexcept override |
Jason M. Bills | 5e049d3 | 2018-10-19 12:59:38 -0700 | [diff] [blame] | 50 | { |
| 51 | return "internal error"; |
Patrick Williams | a138ebd | 2021-09-08 15:46:34 -0500 | [diff] [blame] | 52 | } |
Zhikui Ren | 672bdfc | 2020-07-14 11:37:01 -0700 | [diff] [blame] | 53 | const char* what() const noexcept override |
Jason M. Bills | 5e049d3 | 2018-10-19 12:59:38 -0700 | [diff] [blame] | 54 | { |
| 55 | return "org.freedesktop.DBus.Error.Failed: " |
| 56 | "internal error"; |
Patrick Williams | a138ebd | 2021-09-08 15:46:34 -0500 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | int get_errno() const noexcept override |
| 60 | { |
| 61 | return EACCES; |
| 62 | } |
Jason M. Bills | 5e049d3 | 2018-10-19 12:59:38 -0700 | [diff] [blame] | 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 | |
Jonico Eustaquio | 9fa224c | 2024-01-10 13:08:52 -0600 | [diff] [blame] | 87 | static void saveClearSelTimestamp() |
| 88 | { |
| 89 | int fd = open("/var/lib/ipmi/sel_erase_time", |
| 90 | O_WRONLY | O_CREAT | O_CLOEXEC, 0644); |
| 91 | if (fd < 0) |
| 92 | { |
| 93 | std::cerr << "Failed to open file\n"; |
| 94 | return; |
| 95 | } |
| 96 | |
| 97 | if (futimens(fd, NULL) < 0) |
| 98 | { |
| 99 | std::cerr << "Failed to update SEL cleared timestamp: " |
| 100 | << std::string(strerror(errno)); |
| 101 | } |
| 102 | close(fd); |
| 103 | } |
| 104 | |
| 105 | #ifdef SEL_LOGGER_ENABLE_SEL_DELETE |
| 106 | std::vector<uint16_t> nextRecordsCache; |
| 107 | |
| 108 | static void backupCacheToFile() |
| 109 | { |
| 110 | std::ofstream nextRecordStream(selLogDir / nextRecordFilename); |
| 111 | for (auto recordIds : nextRecordsCache) |
| 112 | { |
| 113 | nextRecordStream << recordIds << '\n'; |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | static uint16_t getNewRecordId() |
| 118 | { |
| 119 | uint16_t nextRecordId = nextRecordsCache.back(); |
| 120 | // Check if SEL is full |
| 121 | if (nextRecordId == selInvalidRecID) |
| 122 | { |
| 123 | return nextRecordId; |
| 124 | } |
| 125 | nextRecordsCache.pop_back(); |
| 126 | if (nextRecordsCache.empty()) |
| 127 | { |
| 128 | nextRecordsCache.push_back(nextRecordId + 1); |
| 129 | } |
| 130 | backupCacheToFile(); |
| 131 | return nextRecordId; |
| 132 | } |
| 133 | |
| 134 | static void initializeRecordId() |
| 135 | { |
| 136 | std::ifstream nextRecordStream(selLogDir / nextRecordFilename); |
| 137 | if (!nextRecordStream.is_open()) |
| 138 | { |
| 139 | std::ofstream newStream(selLogDir / nextRecordFilename); |
| 140 | newStream << '1' << '\n'; |
| 141 | newStream.close(); |
| 142 | nextRecordStream.open(selLogDir / nextRecordFilename); |
| 143 | } |
| 144 | std::string line; |
| 145 | while (std::getline(nextRecordStream, line)) |
| 146 | { |
| 147 | nextRecordsCache.push_back(std::stoi(line)); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | void clearSelLogFiles() |
| 152 | { |
| 153 | saveClearSelTimestamp(); |
| 154 | |
| 155 | // Clear the SEL by deleting the log files |
| 156 | std::vector<std::filesystem::path> selLogFiles; |
| 157 | if (getSELLogFiles(selLogFiles)) |
| 158 | { |
| 159 | for (const std::filesystem::path& file : selLogFiles) |
| 160 | { |
| 161 | std::error_code ec; |
| 162 | std::filesystem::remove(file, ec); |
| 163 | } |
| 164 | } |
| 165 | // Reload rsyslog so it knows to start new log files |
| 166 | boost::asio::io_context io; |
| 167 | auto dbus = std::make_shared<sdbusplus::asio::connection>(io); |
| 168 | sdbusplus::message_t rsyslogReload = dbus->new_method_call( |
| 169 | "org.freedesktop.systemd1", "/org/freedesktop/systemd1", |
| 170 | "org.freedesktop.systemd1.Manager", "ReloadUnit"); |
| 171 | rsyslogReload.append("rsyslog.service", "replace"); |
| 172 | try |
| 173 | { |
| 174 | sdbusplus::message_t reloadResponse = dbus->call(rsyslogReload); |
| 175 | } |
| 176 | catch (const sdbusplus::exception_t& e) |
| 177 | { |
| 178 | std::cerr << e.what() << "\n"; |
| 179 | } |
| 180 | // Set next record to 1 |
| 181 | nextRecordsCache.clear(); |
| 182 | nextRecordsCache.push_back(1); |
| 183 | // Update backup file as well |
| 184 | std::ofstream nextRecordStream(selLogDir / nextRecordFilename); |
| 185 | nextRecordStream << '1' << '\n'; |
| 186 | } |
| 187 | |
| 188 | static bool selDeleteTargetRecord(const uint16_t& targetId) |
| 189 | { |
| 190 | bool targetEntryFound = false; |
| 191 | // Check if the ipmi_sel exist and save the path |
| 192 | std::vector<std::filesystem::path> selLogFiles; |
| 193 | if (!getSELLogFiles(selLogFiles)) |
| 194 | { |
| 195 | return targetEntryFound; |
| 196 | } |
| 197 | |
| 198 | // Go over all the ipmi_sel files to remove the entry with the target ID |
| 199 | for (const std::filesystem::path& file : selLogFiles) |
| 200 | { |
| 201 | std::fstream logStream(file, std::ios::in); |
| 202 | std::fstream tempFile(selLogDir / "temp", std::ios::out); |
| 203 | if (!logStream.is_open()) |
| 204 | { |
| 205 | return targetEntryFound; |
| 206 | } |
| 207 | std::string line; |
| 208 | while (std::getline(logStream, line)) |
| 209 | { |
| 210 | // Get the recordId of the current entry |
| 211 | int left = line.find(" "); |
| 212 | int right = line.find(","); |
| 213 | int recordLen = right - left; |
| 214 | std::string recordId = line.substr(left, recordLen); |
| 215 | int newRecordId = std::stoi(recordId); |
| 216 | |
| 217 | if (newRecordId != targetId) |
| 218 | { |
| 219 | // Copy the entry from the original ipmi_sel to the temp file |
| 220 | tempFile << line << '\n'; |
| 221 | } |
| 222 | else |
| 223 | { |
| 224 | // Skip copying the target entry |
| 225 | targetEntryFound = true; |
| 226 | } |
| 227 | } |
| 228 | logStream.close(); |
| 229 | tempFile.close(); |
| 230 | if (targetEntryFound) |
| 231 | { |
| 232 | std::fstream logStream(file, std::ios::out); |
| 233 | std::fstream tempFile(selLogDir / "temp", std::ios::in); |
| 234 | while (std::getline(tempFile, line)) |
| 235 | { |
| 236 | logStream << line << '\n'; |
| 237 | } |
| 238 | logStream.close(); |
| 239 | tempFile.close(); |
| 240 | std::error_code ec; |
| 241 | if (!std::filesystem::remove(selLogDir / "temp", ec)) |
| 242 | { |
| 243 | std::cerr << ec.message() << std::endl; |
| 244 | } |
| 245 | break; |
| 246 | } |
| 247 | } |
| 248 | return targetEntryFound; |
| 249 | } |
| 250 | |
Jonico Eustaquio | 9c495c6 | 2024-07-02 16:35:14 -0500 | [diff] [blame^] | 251 | static void selDeleteRecord(const uint16_t& recordId) |
Jonico Eustaquio | 9fa224c | 2024-01-10 13:08:52 -0600 | [diff] [blame] | 252 | { |
| 253 | std::filesystem::file_time_type prevAddTime = |
| 254 | std::filesystem::last_write_time(selLogDir / selLogFilename); |
| 255 | bool targetEntryFound = selDeleteTargetRecord(recordId); |
| 256 | |
| 257 | // Check if the Record Id was found |
| 258 | if (!targetEntryFound) |
| 259 | { |
Jonico Eustaquio | 9c495c6 | 2024-07-02 16:35:14 -0500 | [diff] [blame^] | 260 | throw sdbusplus::xyz::openbmc_project::Common::Error:: |
| 261 | ResourceNotFound(); |
Jonico Eustaquio | 9fa224c | 2024-01-10 13:08:52 -0600 | [diff] [blame] | 262 | } |
| 263 | // Add to next record cache for reuse |
| 264 | nextRecordsCache.push_back(recordId); |
| 265 | // Add to backup file |
| 266 | std::ofstream nextRecordStream(selLogDir / nextRecordFilename, |
| 267 | std::ios::app); |
| 268 | nextRecordStream << recordId << '\n'; |
| 269 | // Keep Last Add Time the same |
| 270 | std::filesystem::last_write_time(selLogDir / selLogFilename, prevAddTime); |
| 271 | // Update Last Del Time |
| 272 | saveClearSelTimestamp(); |
Jonico Eustaquio | 9fa224c | 2024-01-10 13:08:52 -0600 | [diff] [blame] | 273 | } |
| 274 | #else |
Jonico Eustaquio | 0acff27 | 2024-04-23 15:12:06 -0500 | [diff] [blame] | 275 | static unsigned int initializeRecordId() |
Jason M. Bills | 5e049d3 | 2018-10-19 12:59:38 -0700 | [diff] [blame] | 276 | { |
Jason M. Bills | c4a336f | 2019-04-23 10:43:10 -0700 | [diff] [blame] | 277 | std::vector<std::filesystem::path> selLogFiles; |
| 278 | if (!getSELLogFiles(selLogFiles)) |
Jason M. Bills | 5e049d3 | 2018-10-19 12:59:38 -0700 | [diff] [blame] | 279 | { |
Jonico Eustaquio | 0acff27 | 2024-04-23 15:12:06 -0500 | [diff] [blame] | 280 | return 0; |
Jason M. Bills | 5e049d3 | 2018-10-19 12:59:38 -0700 | [diff] [blame] | 281 | } |
Jason M. Bills | c4a336f | 2019-04-23 10:43:10 -0700 | [diff] [blame] | 282 | std::ifstream logStream(selLogFiles.front()); |
| 283 | if (!logStream.is_open()) |
Jason M. Bills | 5e049d3 | 2018-10-19 12:59:38 -0700 | [diff] [blame] | 284 | { |
Jonico Eustaquio | 0acff27 | 2024-04-23 15:12:06 -0500 | [diff] [blame] | 285 | return 0; |
Jason M. Bills | c4a336f | 2019-04-23 10:43:10 -0700 | [diff] [blame] | 286 | } |
| 287 | std::string line; |
| 288 | std::string newestEntry; |
| 289 | while (std::getline(logStream, line)) |
| 290 | { |
| 291 | newestEntry = line; |
| 292 | } |
Jason M. Bills | 5e049d3 | 2018-10-19 12:59:38 -0700 | [diff] [blame] | 293 | |
Jason M. Bills | c4a336f | 2019-04-23 10:43:10 -0700 | [diff] [blame] | 294 | std::vector<std::string> newestEntryFields; |
| 295 | boost::split(newestEntryFields, newestEntry, boost::is_any_of(" ,"), |
| 296 | boost::token_compress_on); |
| 297 | if (newestEntryFields.size() < 4) |
| 298 | { |
Jonico Eustaquio | 0acff27 | 2024-04-23 15:12:06 -0500 | [diff] [blame] | 299 | return 0; |
Jason M. Bills | 5e049d3 | 2018-10-19 12:59:38 -0700 | [diff] [blame] | 300 | } |
Jason M. Bills | c4a336f | 2019-04-23 10:43:10 -0700 | [diff] [blame] | 301 | |
| 302 | return std::stoul(newestEntryFields[1]); |
Jason M. Bills | 5e049d3 | 2018-10-19 12:59:38 -0700 | [diff] [blame] | 303 | } |
| 304 | |
Charles Boyer | 9f476e8 | 2021-07-29 16:33:01 -0500 | [diff] [blame] | 305 | static unsigned int recordId = initializeRecordId(); |
| 306 | |
Jonico Eustaquio | 9fa224c | 2024-01-10 13:08:52 -0600 | [diff] [blame] | 307 | static unsigned int getNewRecordId() |
Alexander Hansen | 8c02319 | 2023-09-26 09:15:18 +0200 | [diff] [blame] | 308 | { |
Jonico Eustaquio | 9fa224c | 2024-01-10 13:08:52 -0600 | [diff] [blame] | 309 | if (++recordId >= selInvalidRecID) |
Alexander Hansen | 8c02319 | 2023-09-26 09:15:18 +0200 | [diff] [blame] | 310 | { |
Jonico Eustaquio | 9fa224c | 2024-01-10 13:08:52 -0600 | [diff] [blame] | 311 | recordId = selInvalidRecID; |
Alexander Hansen | 8c02319 | 2023-09-26 09:15:18 +0200 | [diff] [blame] | 312 | } |
Jonico Eustaquio | 9fa224c | 2024-01-10 13:08:52 -0600 | [diff] [blame] | 313 | return recordId; |
Alexander Hansen | 8c02319 | 2023-09-26 09:15:18 +0200 | [diff] [blame] | 314 | } |
| 315 | |
Charles Boyer | 9f476e8 | 2021-07-29 16:33:01 -0500 | [diff] [blame] | 316 | void clearSelLogFiles() |
| 317 | { |
Alexander Hansen | 8c02319 | 2023-09-26 09:15:18 +0200 | [diff] [blame] | 318 | saveClearSelTimestamp(); |
| 319 | |
Charles Boyer | 9f476e8 | 2021-07-29 16:33:01 -0500 | [diff] [blame] | 320 | // Clear the SEL by deleting the log files |
| 321 | std::vector<std::filesystem::path> selLogFiles; |
| 322 | if (getSELLogFiles(selLogFiles)) |
| 323 | { |
| 324 | for (const std::filesystem::path& file : selLogFiles) |
| 325 | { |
| 326 | std::error_code ec; |
| 327 | std::filesystem::remove(file, ec); |
| 328 | } |
| 329 | } |
| 330 | |
Jonico Eustaquio | 0acff27 | 2024-04-23 15:12:06 -0500 | [diff] [blame] | 331 | recordId = 0; |
Charles Boyer | 9f476e8 | 2021-07-29 16:33:01 -0500 | [diff] [blame] | 332 | |
| 333 | // Reload rsyslog so it knows to start new log files |
Ed Tanous | c352634 | 2023-03-06 13:37:53 -0800 | [diff] [blame] | 334 | boost::asio::io_context io; |
Charles Boyer | 9f476e8 | 2021-07-29 16:33:01 -0500 | [diff] [blame] | 335 | auto dbus = std::make_shared<sdbusplus::asio::connection>(io); |
Patrick Williams | ccef227 | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 336 | sdbusplus::message_t rsyslogReload = dbus->new_method_call( |
Charles Boyer | 9f476e8 | 2021-07-29 16:33:01 -0500 | [diff] [blame] | 337 | "org.freedesktop.systemd1", "/org/freedesktop/systemd1", |
| 338 | "org.freedesktop.systemd1.Manager", "ReloadUnit"); |
| 339 | rsyslogReload.append("rsyslog.service", "replace"); |
| 340 | try |
| 341 | { |
Patrick Williams | ccef227 | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 342 | sdbusplus::message_t reloadResponse = dbus->call(rsyslogReload); |
Charles Boyer | 9f476e8 | 2021-07-29 16:33:01 -0500 | [diff] [blame] | 343 | } |
Patrick Williams | 3f4cd97 | 2021-10-06 12:42:50 -0500 | [diff] [blame] | 344 | catch (const sdbusplus::exception_t& e) |
Charles Boyer | 9f476e8 | 2021-07-29 16:33:01 -0500 | [diff] [blame] | 345 | { |
| 346 | std::cerr << e.what() << "\n"; |
| 347 | } |
| 348 | } |
Jonico Eustaquio | 9fa224c | 2024-01-10 13:08:52 -0600 | [diff] [blame] | 349 | #endif |
Lei YU | e526b86 | 2020-12-03 15:41:59 +0800 | [diff] [blame] | 350 | #endif |
Jason M. Bills | 5e049d3 | 2018-10-19 12:59:38 -0700 | [diff] [blame] | 351 | |
Zhikui Ren | 672bdfc | 2020-07-14 11:37:01 -0700 | [diff] [blame] | 352 | 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] | 353 | { |
| 354 | std::stringstream stream; |
| 355 | stream << std::hex << std::uppercase << std::setfill('0'); |
William A. Kennington III | 2e43726 | 2021-07-30 12:04:19 -0700 | [diff] [blame] | 356 | for (int v : data) |
Jason M. Bills | 5e049d3 | 2018-10-19 12:59:38 -0700 | [diff] [blame] | 357 | { |
| 358 | stream << std::setw(2) << v; |
| 359 | } |
| 360 | hexStr = stream.str(); |
| 361 | } |
| 362 | |
Jason M. Bills | 97be353 | 2018-11-02 13:09:16 -0700 | [diff] [blame] | 363 | template <typename... T> |
Vincent Chou | 9272150 | 2024-01-24 15:09:24 -0600 | [diff] [blame] | 364 | static uint16_t selAddSystemRecord( |
Konstantin Aladyshev | 6f5342d | 2023-04-19 09:23:11 +0000 | [diff] [blame] | 365 | [[maybe_unused]] std::shared_ptr<sdbusplus::asio::connection> conn, |
| 366 | [[maybe_unused]] const std::string& message, const std::string& path, |
| 367 | const std::vector<uint8_t>& selData, const bool& assert, |
| 368 | const uint16_t& genId, [[maybe_unused]] T&&... metadata) |
Jason M. Bills | 5e049d3 | 2018-10-19 12:59:38 -0700 | [diff] [blame] | 369 | { |
| 370 | // Only 3 bytes of SEL event data are allowed in a system record |
| 371 | if (selData.size() > selEvtDataMaxSize) |
| 372 | { |
| 373 | throw std::invalid_argument("Event data too large"); |
| 374 | } |
| 375 | std::string selDataStr; |
| 376 | toHexStr(selData, selDataStr); |
| 377 | |
Lei YU | e526b86 | 2020-12-03 15:41:59 +0800 | [diff] [blame] | 378 | #ifdef SEL_LOGGER_SEND_TO_LOGGING_SERVICE |
Konstantin Aladyshev | 6f5342d | 2023-04-19 09:23:11 +0000 | [diff] [blame] | 379 | sdbusplus::message_t AddToLog = conn->new_method_call( |
| 380 | "xyz.openbmc_project.Logging", "/xyz/openbmc_project/logging", |
| 381 | "xyz.openbmc_project.Logging.Create", "Create"); |
| 382 | |
Patrick Williams | 5a18f10 | 2024-08-16 15:20:38 -0400 | [diff] [blame] | 383 | std::string journalMsg( |
| 384 | message + " from " + path + ": " + |
| 385 | " RecordType=" + std::to_string(selSystemType) + |
| 386 | ", GeneratorID=" + std::to_string(genId) + |
| 387 | ", EventDir=" + std::to_string(assert) + ", EventData=" + selDataStr); |
Konstantin Aladyshev | 6f5342d | 2023-04-19 09:23:11 +0000 | [diff] [blame] | 388 | |
Patrick Williams | 5a18f10 | 2024-08-16 15:20:38 -0400 | [diff] [blame] | 389 | AddToLog.append( |
| 390 | journalMsg, "xyz.openbmc_project.Logging.Entry.Level.Informational", |
| 391 | std::map<std::string, std::string>( |
| 392 | {{"SENSOR_PATH", path}, |
| 393 | {"GENERATOR_ID", std::to_string(genId)}, |
| 394 | {"RECORD_TYPE", std::to_string(selSystemType)}, |
| 395 | {"EVENT_DIR", std::to_string(assert)}, |
| 396 | {"SENSOR_DATA", selDataStr}})); |
Konstantin Aladyshev | 6f5342d | 2023-04-19 09:23:11 +0000 | [diff] [blame] | 397 | conn->call(AddToLog); |
Vincent Chou | 9272150 | 2024-01-24 15:09:24 -0600 | [diff] [blame] | 398 | return 0; |
Lei YU | e526b86 | 2020-12-03 15:41:59 +0800 | [diff] [blame] | 399 | #else |
Jason M. Bills | 5e049d3 | 2018-10-19 12:59:38 -0700 | [diff] [blame] | 400 | unsigned int recordId = getNewRecordId(); |
Jonico Eustaquio | 0acff27 | 2024-04-23 15:12:06 -0500 | [diff] [blame] | 401 | if (recordId < selInvalidRecID) |
| 402 | { |
| 403 | sd_journal_send( |
| 404 | "MESSAGE=%s", message.c_str(), "PRIORITY=%i", selPriority, |
| 405 | "MESSAGE_ID=%s", selMessageId, "IPMI_SEL_RECORD_ID=%d", recordId, |
| 406 | "IPMI_SEL_RECORD_TYPE=%x", selSystemType, |
| 407 | "IPMI_SEL_GENERATOR_ID=%x", genId, "IPMI_SEL_SENSOR_PATH=%s", |
| 408 | path.c_str(), "IPMI_SEL_EVENT_DIR=%x", assert, "IPMI_SEL_DATA=%s", |
| 409 | selDataStr.c_str(), std::forward<T>(metadata)..., NULL); |
| 410 | } |
Vincent Chou | 9272150 | 2024-01-24 15:09:24 -0600 | [diff] [blame] | 411 | return recordId; |
Lei YU | e526b86 | 2020-12-03 15:41:59 +0800 | [diff] [blame] | 412 | #endif |
Jason M. Bills | 5e049d3 | 2018-10-19 12:59:38 -0700 | [diff] [blame] | 413 | } |
| 414 | |
Vincent Chou | 9272150 | 2024-01-24 15:09:24 -0600 | [diff] [blame] | 415 | static uint16_t selAddOemRecord( |
Konstantin Aladyshev | 6f5342d | 2023-04-19 09:23:11 +0000 | [diff] [blame] | 416 | [[maybe_unused]] std::shared_ptr<sdbusplus::asio::connection> conn, |
| 417 | [[maybe_unused]] const std::string& message, |
| 418 | const std::vector<uint8_t>& selData, const uint8_t& recordType) |
Jason M. Bills | 5e049d3 | 2018-10-19 12:59:38 -0700 | [diff] [blame] | 419 | { |
| 420 | // A maximum of 13 bytes of SEL event data are allowed in an OEM record |
| 421 | if (selData.size() > selOemDataMaxSize) |
| 422 | { |
| 423 | throw std::invalid_argument("Event data too large"); |
| 424 | } |
| 425 | std::string selDataStr; |
| 426 | toHexStr(selData, selDataStr); |
| 427 | |
Lei YU | e526b86 | 2020-12-03 15:41:59 +0800 | [diff] [blame] | 428 | #ifdef SEL_LOGGER_SEND_TO_LOGGING_SERVICE |
Konstantin Aladyshev | 6f5342d | 2023-04-19 09:23:11 +0000 | [diff] [blame] | 429 | sdbusplus::message_t AddToLog = conn->new_method_call( |
| 430 | "xyz.openbmc_project.Logging", "/xyz/openbmc_project/logging", |
| 431 | "xyz.openbmc_project.Logging.Create", "Create"); |
| 432 | |
| 433 | std::string journalMsg( |
| 434 | message + ": " + " RecordType=" + std::to_string(recordType) + |
| 435 | ", GeneratorID=" + std::to_string(0) + |
| 436 | ", EventDir=" + std::to_string(0) + ", EventData=" + selDataStr); |
| 437 | |
Patrick Williams | 5a18f10 | 2024-08-16 15:20:38 -0400 | [diff] [blame] | 438 | AddToLog.append( |
| 439 | journalMsg, "xyz.openbmc_project.Logging.Entry.Level.Informational", |
| 440 | std::map<std::string, std::string>( |
| 441 | {{"SENSOR_PATH", ""}, |
| 442 | {"GENERATOR_ID", std::to_string(0)}, |
| 443 | {"RECORD_TYPE", std::to_string(recordType)}, |
| 444 | {"EVENT_DIR", std::to_string(0)}, |
| 445 | {"SENSOR_DATA", selDataStr}})); |
Konstantin Aladyshev | 6f5342d | 2023-04-19 09:23:11 +0000 | [diff] [blame] | 446 | conn->call(AddToLog); |
Vincent Chou | 9272150 | 2024-01-24 15:09:24 -0600 | [diff] [blame] | 447 | return 0; |
Lei YU | e526b86 | 2020-12-03 15:41:59 +0800 | [diff] [blame] | 448 | #else |
Jason M. Bills | 5e049d3 | 2018-10-19 12:59:38 -0700 | [diff] [blame] | 449 | unsigned int recordId = getNewRecordId(); |
Jonico Eustaquio | 0acff27 | 2024-04-23 15:12:06 -0500 | [diff] [blame] | 450 | if (recordId < selInvalidRecID) |
| 451 | { |
| 452 | sd_journal_send("MESSAGE=%s", message.c_str(), "PRIORITY=%i", |
| 453 | selPriority, "MESSAGE_ID=%s", selMessageId, |
| 454 | "IPMI_SEL_RECORD_ID=%d", recordId, |
| 455 | "IPMI_SEL_RECORD_TYPE=%x", recordType, |
| 456 | "IPMI_SEL_DATA=%s", selDataStr.c_str(), NULL); |
| 457 | } |
Vincent Chou | 9272150 | 2024-01-24 15:09:24 -0600 | [diff] [blame] | 458 | return recordId; |
Lei YU | e526b86 | 2020-12-03 15:41:59 +0800 | [diff] [blame] | 459 | #endif |
Jason M. Bills | 5e049d3 | 2018-10-19 12:59:38 -0700 | [diff] [blame] | 460 | } |
| 461 | |
William A. Kennington III | d585c59 | 2021-07-30 12:10:25 -0700 | [diff] [blame] | 462 | int main(int, char*[]) |
Jason M. Bills | 5e049d3 | 2018-10-19 12:59:38 -0700 | [diff] [blame] | 463 | { |
Jonico Eustaquio | 9fa224c | 2024-01-10 13:08:52 -0600 | [diff] [blame] | 464 | #ifndef SEL_LOGGER_SEND_TO_LOGGING_SERVICE |
| 465 | #ifdef SEL_LOGGER_ENABLE_SEL_DELETE |
| 466 | initializeRecordId(); |
| 467 | #endif |
| 468 | #endif |
Jason M. Bills | 5e049d3 | 2018-10-19 12:59:38 -0700 | [diff] [blame] | 469 | // setup connection to dbus |
Ed Tanous | c352634 | 2023-03-06 13:37:53 -0800 | [diff] [blame] | 470 | boost::asio::io_context io; |
Jason M. Bills | 5e049d3 | 2018-10-19 12:59:38 -0700 | [diff] [blame] | 471 | auto conn = std::make_shared<sdbusplus::asio::connection>(io); |
| 472 | |
| 473 | // IPMI SEL Object |
| 474 | conn->request_name(ipmiSelObject); |
| 475 | auto server = sdbusplus::asio::object_server(conn); |
| 476 | |
| 477 | // Add SEL Interface |
| 478 | std::shared_ptr<sdbusplus::asio::dbus_interface> ifaceAddSel = |
| 479 | server.add_interface(ipmiSelPath, ipmiSelAddInterface); |
| 480 | |
| 481 | // Add a new SEL entry |
| 482 | ifaceAddSel->register_method( |
Konstantin Aladyshev | 6f5342d | 2023-04-19 09:23:11 +0000 | [diff] [blame] | 483 | "IpmiSelAdd", |
| 484 | [conn](const std::string& message, const std::string& path, |
| 485 | const std::vector<uint8_t>& selData, const bool& assert, |
| 486 | const uint16_t& genId) { |
Patrick Williams | 5a18f10 | 2024-08-16 15:20:38 -0400 | [diff] [blame] | 487 | return selAddSystemRecord(conn, message, path, selData, assert, |
| 488 | genId); |
| 489 | }); |
Jason M. Bills | 5e049d3 | 2018-10-19 12:59:38 -0700 | [diff] [blame] | 490 | // Add a new OEM SEL entry |
Patrick Williams | 5a18f10 | 2024-08-16 15:20:38 -0400 | [diff] [blame] | 491 | ifaceAddSel->register_method( |
| 492 | "IpmiSelAddOem", |
| 493 | [conn](const std::string& message, const std::vector<uint8_t>& selData, |
| 494 | const uint8_t& recordType) { |
| 495 | return selAddOemRecord(conn, message, selData, recordType); |
| 496 | }); |
Charles Boyer | 9f476e8 | 2021-07-29 16:33:01 -0500 | [diff] [blame] | 497 | |
Charles Boyer | 9f476e8 | 2021-07-29 16:33:01 -0500 | [diff] [blame] | 498 | #ifndef SEL_LOGGER_SEND_TO_LOGGING_SERVICE |
| 499 | // Clear SEL entries |
| 500 | ifaceAddSel->register_method("Clear", []() { clearSelLogFiles(); }); |
Jonico Eustaquio | 9fa224c | 2024-01-10 13:08:52 -0600 | [diff] [blame] | 501 | #ifdef SEL_LOGGER_ENABLE_SEL_DELETE |
| 502 | // Delete a SEL entry |
Jonico Eustaquio | 9c495c6 | 2024-07-02 16:35:14 -0500 | [diff] [blame^] | 503 | ifaceAddSel->register_method("SELDelete", [](const uint16_t& recordId) { |
Jonico Eustaquio | 9fa224c | 2024-01-10 13:08:52 -0600 | [diff] [blame] | 504 | return selDeleteRecord(recordId); |
| 505 | }); |
| 506 | #endif |
Charles Boyer | 9f476e8 | 2021-07-29 16:33:01 -0500 | [diff] [blame] | 507 | #endif |
Jason M. Bills | 5e049d3 | 2018-10-19 12:59:38 -0700 | [diff] [blame] | 508 | ifaceAddSel->initialize(); |
| 509 | |
| 510 | #ifdef SEL_LOGGER_MONITOR_THRESHOLD_EVENTS |
Patrick Williams | ccef227 | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 511 | sdbusplus::bus::match_t thresholdAssertMonitor = |
Zhikui Ren | 25b26e1 | 2020-06-26 20:18:19 -0700 | [diff] [blame] | 512 | startThresholdAssertMonitor(conn); |
Jason M. Bills | 5e049d3 | 2018-10-19 12:59:38 -0700 | [diff] [blame] | 513 | #endif |
| 514 | |
Nikhil Potade | afbaa09 | 2019-03-06 16:18:13 -0800 | [diff] [blame] | 515 | #ifdef REDFISH_LOG_MONITOR_PULSE_EVENTS |
Patrick Williams | ccef227 | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 516 | sdbusplus::bus::match_t pulseEventMonitor = startPulseEventMonitor(conn); |
Nikhil Potade | afbaa09 | 2019-03-06 16:18:13 -0800 | [diff] [blame] | 517 | #endif |
| 518 | |
Charles Hsu | dbd77b9 | 2020-10-29 11:20:34 +0800 | [diff] [blame] | 519 | #ifdef SEL_LOGGER_MONITOR_WATCHDOG_EVENTS |
Patrick Williams | ccef227 | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 520 | sdbusplus::bus::match_t watchdogEventMonitor = |
Charles Hsu | dbd77b9 | 2020-10-29 11:20:34 +0800 | [diff] [blame] | 521 | startWatchdogEventMonitor(conn); |
| 522 | #endif |
George Hung | 486e42e | 2021-04-14 20:20:42 +0800 | [diff] [blame] | 523 | |
| 524 | #ifdef SEL_LOGGER_MONITOR_THRESHOLD_ALARM_EVENTS |
| 525 | startThresholdAlarmMonitor(conn); |
| 526 | #endif |
JinFuLin | 7c2810b | 2022-12-02 13:55:28 +0800 | [diff] [blame] | 527 | |
| 528 | #ifdef SEL_LOGGER_MONITOR_HOST_ERROR_EVENTS |
| 529 | startHostErrorEventMonitor(conn); |
| 530 | #endif |
Jason M. Bills | 5e049d3 | 2018-10-19 12:59:38 -0700 | [diff] [blame] | 531 | io.run(); |
| 532 | |
| 533 | return 0; |
| 534 | } |