blob: 1e5a34f15b6c136007c728505ede15cb411d25fa [file] [log] [blame]
Ed Tanous1da66f72018-07-27 16:13:37 -07001/*
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#pragma once
17
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080018#include "app.hpp"
George Liu7a1dbc42022-12-07 16:03:22 +080019#include "dbus_utility.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080020#include "error_messages.hpp"
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -060021#include "generated/enums/log_entry.hpp"
Spencer Kub7028eb2021-10-26 15:27:35 +080022#include "gzfile.hpp"
George Liu647b3cd2021-07-05 12:43:56 +080023#include "http_utility.hpp"
Spencer Kub7028eb2021-10-26 15:27:35 +080024#include "human_sort.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080025#include "query.hpp"
Jason M. Bills4851d452019-03-28 11:27:48 -070026#include "registries.hpp"
27#include "registries/base_message_registry.hpp"
28#include "registries/openbmc_message_registry.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080029#include "registries/privilege_registry.hpp"
James Feist46229572020-02-19 15:11:58 -080030#include "task.hpp"
Ed Tanous5b904292024-04-16 11:10:17 -070031#include "task_messages.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080032#include "utils/dbus_utils.hpp"
Ed Tanous5b904292024-04-16 11:10:17 -070033#include "utils/json_utils.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080034#include "utils/time_utils.hpp"
Ed Tanous1da66f72018-07-27 16:13:37 -070035
Myung Bae75e8e212023-11-30 12:53:46 -080036#include <systemd/sd-id128.h>
Jason M. Billse1f26342018-07-18 12:12:00 -070037#include <systemd/sd-journal.h>
Asmitha Karunanithi8e317782020-12-10 03:35:05 -060038#include <tinyxml2.h>
Adriana Kobylak400fd1f2021-01-29 09:01:30 -060039#include <unistd.h>
Jason M. Billse1f26342018-07-18 12:12:00 -070040
Ed Tanous07c8c202022-07-11 10:08:08 -070041#include <boost/beast/http/verb.hpp>
Ed Tanous1da66f72018-07-27 16:13:37 -070042#include <boost/container/flat_map.hpp>
Jason M. Bills1ddcf012019-11-26 14:59:21 -080043#include <boost/system/linux_error.hpp>
Ed Tanousef4c65b2023-04-24 15:28:50 -070044#include <boost/url/format.hpp>
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +020045#include <sdbusplus/asio/property.hpp>
46#include <sdbusplus/unpack_properties.hpp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050047
George Liu7a1dbc42022-12-07 16:03:22 +080048#include <array>
George Liu647b3cd2021-07-05 12:43:56 +080049#include <charconv>
Abhilash Rajub5f288d2023-11-08 22:32:44 -060050#include <cstddef>
James Feist4418c7f2019-04-15 11:09:15 -070051#include <filesystem>
Ed Tanous18f8f602023-07-18 10:07:23 -070052#include <iterator>
Xiaochao Ma75710de2021-01-21 17:56:02 +080053#include <optional>
Ed Tanous3544d2a2023-08-06 18:12:20 -070054#include <ranges>
Ed Tanous26702d02021-11-03 15:02:33 -070055#include <span>
Ed Tanous18f8f602023-07-18 10:07:23 -070056#include <string>
Jason M. Billscd225da2019-05-08 15:31:57 -070057#include <string_view>
Ed Tanousabf2add2019-01-22 16:40:12 -080058#include <variant>
Ed Tanous1da66f72018-07-27 16:13:37 -070059
60namespace redfish
61{
62
Patrick Williams89492a12023-05-10 07:51:34 -050063constexpr const char* crashdumpObject = "com.intel.crashdump";
64constexpr const char* crashdumpPath = "/com/intel/crashdump";
65constexpr const char* crashdumpInterface = "com.intel.crashdump";
66constexpr const char* deleteAllInterface =
Jason M. Bills5b61b5e2019-10-16 10:59:02 -070067 "xyz.openbmc_project.Collection.DeleteAll";
Patrick Williams89492a12023-05-10 07:51:34 -050068constexpr const char* crashdumpOnDemandInterface =
Jason M. Bills424c4172019-03-21 13:50:33 -070069 "com.intel.crashdump.OnDemand";
Patrick Williams89492a12023-05-10 07:51:34 -050070constexpr const char* crashdumpTelemetryInterface =
Kenny L. Ku6eda7682020-06-19 09:48:36 -070071 "com.intel.crashdump.Telemetry";
Ed Tanous1da66f72018-07-27 16:13:37 -070072
Asmitha Karunanithi8e317782020-12-10 03:35:05 -060073enum class DumpCreationProgress
74{
75 DUMP_CREATE_SUCCESS,
76 DUMP_CREATE_FAILED,
77 DUMP_CREATE_INPROGRESS
78};
79
James Feistf6150402019-01-08 10:36:20 -080080namespace fs = std::filesystem;
Ed Tanous1da66f72018-07-27 16:13:37 -070081
Gunnar Mills1214b7e2020-06-04 10:11:30 -050082inline std::string translateSeverityDbusToRedfish(const std::string& s)
Andrew Geisslercb92c032018-08-17 07:56:14 -070083{
Ed Tanousd4d25792020-09-29 15:15:03 -070084 if ((s == "xyz.openbmc_project.Logging.Entry.Level.Alert") ||
85 (s == "xyz.openbmc_project.Logging.Entry.Level.Critical") ||
86 (s == "xyz.openbmc_project.Logging.Entry.Level.Emergency") ||
87 (s == "xyz.openbmc_project.Logging.Entry.Level.Error"))
Andrew Geisslercb92c032018-08-17 07:56:14 -070088 {
89 return "Critical";
90 }
Ed Tanous3174e4d2020-10-07 11:41:22 -070091 if ((s == "xyz.openbmc_project.Logging.Entry.Level.Debug") ||
92 (s == "xyz.openbmc_project.Logging.Entry.Level.Informational") ||
93 (s == "xyz.openbmc_project.Logging.Entry.Level.Notice"))
Andrew Geisslercb92c032018-08-17 07:56:14 -070094 {
95 return "OK";
96 }
Ed Tanous3174e4d2020-10-07 11:41:22 -070097 if (s == "xyz.openbmc_project.Logging.Entry.Level.Warning")
Andrew Geisslercb92c032018-08-17 07:56:14 -070098 {
99 return "Warning";
100 }
101 return "";
102}
103
Abhishek Patel9017faf2021-09-14 22:48:55 -0500104inline std::optional<bool> getProviderNotifyAction(const std::string& notify)
105{
106 std::optional<bool> notifyAction;
107 if (notify == "xyz.openbmc_project.Logging.Entry.Notify.Notify")
108 {
109 notifyAction = true;
110 }
111 else if (notify == "xyz.openbmc_project.Logging.Entry.Notify.Inhibit")
112 {
113 notifyAction = false;
114 }
115
116 return notifyAction;
117}
118
Ed Tanous18f8f602023-07-18 10:07:23 -0700119inline std::string getDumpPath(std::string_view dumpType)
120{
121 std::string dbusDumpPath = "/xyz/openbmc_project/dump/";
122 std::ranges::transform(dumpType, std::back_inserter(dbusDumpPath),
123 bmcweb::asciiToLower);
124
125 return dbusDumpPath;
126}
127
Ed Tanousdf254f22024-04-01 13:25:46 -0700128inline int getJournalMetadata(sd_journal* journal, std::string_view field,
129 std::string_view& contents)
Jason M. Bills16428a12018-11-02 12:42:29 -0700130{
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500131 const char* data = nullptr;
Jason M. Bills16428a12018-11-02 12:42:29 -0700132 size_t length = 0;
133 int ret = 0;
134 // Get the metadata from the requested field of the journal entry
Ed Tanous46ff87b2022-01-07 09:25:51 -0800135 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
136 const void** dataVoid = reinterpret_cast<const void**>(&data);
137
138 ret = sd_journal_get_data(journal, field.data(), dataVoid, &length);
Jason M. Bills16428a12018-11-02 12:42:29 -0700139 if (ret < 0)
140 {
141 return ret;
142 }
Ed Tanous39e77502019-03-04 17:35:53 -0800143 contents = std::string_view(data, length);
Jason M. Bills16428a12018-11-02 12:42:29 -0700144 // Only use the content after the "=" character.
Ed Tanous81ce6092020-12-17 16:54:55 +0000145 contents.remove_prefix(std::min(contents.find('=') + 1, contents.size()));
Jason M. Bills16428a12018-11-02 12:42:29 -0700146 return ret;
147}
148
Ed Tanousdf254f22024-04-01 13:25:46 -0700149inline int getJournalMetadata(sd_journal* journal, std::string_view field,
150 const int& base, long int& contents)
Jason M. Bills16428a12018-11-02 12:42:29 -0700151{
152 int ret = 0;
Ed Tanous39e77502019-03-04 17:35:53 -0800153 std::string_view metadata;
Jason M. Bills16428a12018-11-02 12:42:29 -0700154 // Get the metadata from the requested field of the journal entry
155 ret = getJournalMetadata(journal, field, metadata);
156 if (ret < 0)
157 {
158 return ret;
159 }
Ed Tanousb01bf292019-03-25 19:25:26 +0000160 contents = strtol(metadata.data(), nullptr, base);
Jason M. Bills16428a12018-11-02 12:42:29 -0700161 return ret;
162}
163
Ed Tanousdf254f22024-04-01 13:25:46 -0700164inline bool getEntryTimestamp(sd_journal* journal, std::string& entryTimestamp)
ZhikuiRena3316fc2020-01-29 14:58:08 -0800165{
166 int ret = 0;
167 uint64_t timestamp = 0;
168 ret = sd_journal_get_realtime_usec(journal, &timestamp);
169 if (ret < 0)
170 {
Ed Tanous62598e32023-07-17 17:06:25 -0700171 BMCWEB_LOG_ERROR("Failed to read entry timestamp: {}", strerror(-ret));
ZhikuiRena3316fc2020-01-29 14:58:08 -0800172 return false;
173 }
Konstantin Aladysheve645c5e2023-02-17 13:09:53 +0300174 entryTimestamp = redfish::time_utils::getDateTimeUintUs(timestamp);
Asmitha Karunanithi9c620e22020-08-02 11:55:21 -0500175 return true;
ZhikuiRena3316fc2020-01-29 14:58:08 -0800176}
Ed Tanous50b8a432022-02-03 16:29:50 -0800177
Ed Tanousdf254f22024-04-01 13:25:46 -0700178inline bool getUniqueEntryID(sd_journal* journal, std::string& entryID,
179 const bool firstEntry = true)
Jason M. Bills16428a12018-11-02 12:42:29 -0700180{
181 int ret = 0;
Myung Bae75e8e212023-11-30 12:53:46 -0800182 static sd_id128_t prevBootID{};
Jason M. Bills16428a12018-11-02 12:42:29 -0700183 static uint64_t prevTs = 0;
184 static int index = 0;
Jason M. Billse85d6b12019-07-29 17:01:15 -0700185 if (firstEntry)
186 {
Myung Bae75e8e212023-11-30 12:53:46 -0800187 prevBootID = {};
Jason M. Billse85d6b12019-07-29 17:01:15 -0700188 prevTs = 0;
189 }
190
Jason M. Bills16428a12018-11-02 12:42:29 -0700191 // Get the entry timestamp
192 uint64_t curTs = 0;
Myung Bae75e8e212023-11-30 12:53:46 -0800193 sd_id128_t curBootID{};
194 ret = sd_journal_get_monotonic_usec(journal, &curTs, &curBootID);
Jason M. Bills16428a12018-11-02 12:42:29 -0700195 if (ret < 0)
196 {
Ed Tanous62598e32023-07-17 17:06:25 -0700197 BMCWEB_LOG_ERROR("Failed to read entry timestamp: {}", strerror(-ret));
Jason M. Bills16428a12018-11-02 12:42:29 -0700198 return false;
199 }
Myung Bae75e8e212023-11-30 12:53:46 -0800200 // If the timestamp isn't unique on the same boot, increment the index
201 bool sameBootIDs = sd_id128_equal(curBootID, prevBootID) != 0;
202 if (sameBootIDs && (curTs == prevTs))
Jason M. Bills16428a12018-11-02 12:42:29 -0700203 {
204 index++;
205 }
206 else
207 {
208 // Otherwise, reset it
209 index = 0;
210 }
Myung Bae75e8e212023-11-30 12:53:46 -0800211
212 if (!sameBootIDs)
213 {
214 // Save the bootID
215 prevBootID = curBootID;
216 }
Jason M. Bills16428a12018-11-02 12:42:29 -0700217 // Save the timestamp
218 prevTs = curTs;
219
Myung Bae75e8e212023-11-30 12:53:46 -0800220 // make entryID as <bootID>_<timestamp>[_<index>]
221 std::array<char, SD_ID128_STRING_MAX> bootIDStr{};
222 sd_id128_to_string(curBootID, bootIDStr.data());
223 entryID = std::format("{}_{}", bootIDStr.data(), curTs);
Jason M. Bills16428a12018-11-02 12:42:29 -0700224 if (index > 0)
225 {
226 entryID += "_" + std::to_string(index);
227 }
228 return true;
229}
230
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500231static bool getUniqueEntryID(const std::string& logEntry, std::string& entryID,
Jason M. Billse85d6b12019-07-29 17:01:15 -0700232 const bool firstEntry = true)
Jason M. Bills95820182019-04-22 16:25:34 -0700233{
Ed Tanous271584a2019-07-09 16:24:22 -0700234 static time_t prevTs = 0;
Jason M. Bills95820182019-04-22 16:25:34 -0700235 static int index = 0;
Jason M. Billse85d6b12019-07-29 17:01:15 -0700236 if (firstEntry)
237 {
238 prevTs = 0;
239 }
240
Jason M. Bills95820182019-04-22 16:25:34 -0700241 // Get the entry timestamp
Ed Tanous271584a2019-07-09 16:24:22 -0700242 std::time_t curTs = 0;
Jason M. Bills95820182019-04-22 16:25:34 -0700243 std::tm timeStruct = {};
244 std::istringstream entryStream(logEntry);
245 if (entryStream >> std::get_time(&timeStruct, "%Y-%m-%dT%H:%M:%S"))
246 {
247 curTs = std::mktime(&timeStruct);
248 }
249 // If the timestamp isn't unique, increment the index
250 if (curTs == prevTs)
251 {
252 index++;
253 }
254 else
255 {
256 // Otherwise, reset it
257 index = 0;
258 }
259 // Save the timestamp
260 prevTs = curTs;
261
262 entryID = std::to_string(curTs);
263 if (index > 0)
264 {
265 entryID += "_" + std::to_string(index);
266 }
267 return true;
268}
269
Myung Bae75e8e212023-11-30 12:53:46 -0800270// Entry is formed like "BootID_timestamp" or "BootID_timestamp_index"
Ed Tanousdf254f22024-04-01 13:25:46 -0700271inline bool
zhanghch058d1b46d2021-04-01 11:18:24 +0800272 getTimestampFromID(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous099984c2024-04-01 13:23:11 -0700273 std::string_view entryIDStrView, sd_id128_t& bootID,
Myung Bae75e8e212023-11-30 12:53:46 -0800274 uint64_t& timestamp, uint64_t& index)
Jason M. Bills16428a12018-11-02 12:42:29 -0700275{
Myung Bae75e8e212023-11-30 12:53:46 -0800276 // Convert the unique ID back to a bootID + timestamp to find the entry
Myung Bae75e8e212023-11-30 12:53:46 -0800277 auto underscore1Pos = entryIDStrView.find('_');
278 if (underscore1Pos == std::string_view::npos)
279 {
280 // EntryID has no bootID or timestamp
Ed Tanous099984c2024-04-01 13:23:11 -0700281 messages::resourceNotFound(asyncResp->res, "LogEntry", entryIDStrView);
Myung Bae75e8e212023-11-30 12:53:46 -0800282 return false;
283 }
284
285 // EntryID has bootID + timestamp
286
287 // Convert entryIDViewString to BootID
288 // NOTE: bootID string which needs to be null-terminated for
289 // sd_id128_from_string()
Ed Tanous099984c2024-04-01 13:23:11 -0700290 std::string bootIDStr(entryIDStrView.substr(0, underscore1Pos));
Myung Bae75e8e212023-11-30 12:53:46 -0800291 if (sd_id128_from_string(bootIDStr.c_str(), &bootID) < 0)
292 {
Ed Tanous099984c2024-04-01 13:23:11 -0700293 messages::resourceNotFound(asyncResp->res, "LogEntry", entryIDStrView);
Myung Bae75e8e212023-11-30 12:53:46 -0800294 return false;
295 }
296
297 // Get the timestamp from entryID
Ed Tanous099984c2024-04-01 13:23:11 -0700298 entryIDStrView.remove_prefix(underscore1Pos + 1);
Myung Bae75e8e212023-11-30 12:53:46 -0800299
Ed Tanous099984c2024-04-01 13:23:11 -0700300 auto [timestampEnd, tstampEc] = std::from_chars(
301 entryIDStrView.begin(), entryIDStrView.end(), timestamp);
302 if (tstampEc != std::errc())
Jason M. Bills16428a12018-11-02 12:42:29 -0700303 {
Ed Tanous099984c2024-04-01 13:23:11 -0700304 messages::resourceNotFound(asyncResp->res, "LogEntry", entryIDStrView);
305 return false;
Jason M. Bills16428a12018-11-02 12:42:29 -0700306 }
Ed Tanous099984c2024-04-01 13:23:11 -0700307 entryIDStrView = std::string_view(
308 timestampEnd,
309 static_cast<size_t>(std::distance(timestampEnd, entryIDStrView.end())));
310 if (entryIDStrView.empty())
Jason M. Bills16428a12018-11-02 12:42:29 -0700311 {
Ed Tanous099984c2024-04-01 13:23:11 -0700312 index = 0U;
313 return true;
314 }
315 // Timestamp might include optional index, if two events happened at the
316 // same "time".
317 if (entryIDStrView[0] != '_')
318 {
319 messages::resourceNotFound(asyncResp->res, "LogEntry", entryIDStrView);
320 return false;
321 }
322 entryIDStrView.remove_prefix(1);
323 auto [ptr, indexEc] = std::from_chars(entryIDStrView.begin(),
324 entryIDStrView.end(), index);
325 if (indexEc != std::errc() || ptr != entryIDStrView.end())
326 {
327 messages::resourceNotFound(asyncResp->res, "LogEntry", entryIDStrView);
Jason M. Bills16428a12018-11-02 12:42:29 -0700328 return false;
329 }
330 return true;
331}
332
Jason M. Bills95820182019-04-22 16:25:34 -0700333static bool
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500334 getRedfishLogFiles(std::vector<std::filesystem::path>& redfishLogFiles)
Jason M. Bills95820182019-04-22 16:25:34 -0700335{
336 static const std::filesystem::path redfishLogDir = "/var/log";
337 static const std::string redfishLogFilename = "redfish";
338
339 // Loop through the directory looking for redfish log files
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500340 for (const std::filesystem::directory_entry& dirEnt :
Jason M. Bills95820182019-04-22 16:25:34 -0700341 std::filesystem::directory_iterator(redfishLogDir))
342 {
343 // If we find a redfish log file, save the path
344 std::string filename = dirEnt.path().filename();
Ed Tanous11ba3972022-07-11 09:50:41 -0700345 if (filename.starts_with(redfishLogFilename))
Jason M. Bills95820182019-04-22 16:25:34 -0700346 {
347 redfishLogFiles.emplace_back(redfishLogDir / filename);
348 }
349 }
350 // As the log files rotate, they are appended with a ".#" that is higher for
351 // the older logs. Since we don't expect more than 10 log files, we
352 // can just sort the list to get them in order from newest to oldest
Ed Tanous3544d2a2023-08-06 18:12:20 -0700353 std::ranges::sort(redfishLogFiles);
Jason M. Bills95820182019-04-22 16:25:34 -0700354
355 return !redfishLogFiles.empty();
356}
357
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -0600358inline log_entry::OriginatorTypes
359 mapDbusOriginatorTypeToRedfish(const std::string& originatorType)
360{
361 if (originatorType ==
362 "xyz.openbmc_project.Common.OriginatedBy.OriginatorTypes.Client")
363 {
364 return log_entry::OriginatorTypes::Client;
365 }
366 if (originatorType ==
367 "xyz.openbmc_project.Common.OriginatedBy.OriginatorTypes.Internal")
368 {
369 return log_entry::OriginatorTypes::Internal;
370 }
371 if (originatorType ==
372 "xyz.openbmc_project.Common.OriginatedBy.OriginatorTypes.SupportingService")
373 {
374 return log_entry::OriginatorTypes::SupportingService;
375 }
376 return log_entry::OriginatorTypes::Invalid;
377}
378
Claire Weinanaefe3782022-07-15 19:17:19 -0700379inline void parseDumpEntryFromDbusObject(
Jiaqing Zhao2d613eb2022-08-15 16:03:00 +0800380 const dbus::utility::ManagedObjectType::value_type& object,
Claire Weinanc6fecda2022-07-15 10:43:25 -0700381 std::string& dumpStatus, uint64_t& size, uint64_t& timestampUs,
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -0600382 std::string& originatorId, log_entry::OriginatorTypes& originatorType,
Claire Weinanaefe3782022-07-15 19:17:19 -0700383 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
384{
385 for (const auto& interfaceMap : object.second)
386 {
387 if (interfaceMap.first == "xyz.openbmc_project.Common.Progress")
388 {
389 for (const auto& propertyMap : interfaceMap.second)
390 {
391 if (propertyMap.first == "Status")
392 {
393 const auto* status =
394 std::get_if<std::string>(&propertyMap.second);
395 if (status == nullptr)
396 {
397 messages::internalError(asyncResp->res);
398 break;
399 }
400 dumpStatus = *status;
401 }
402 }
403 }
404 else if (interfaceMap.first == "xyz.openbmc_project.Dump.Entry")
405 {
406 for (const auto& propertyMap : interfaceMap.second)
407 {
408 if (propertyMap.first == "Size")
409 {
410 const auto* sizePtr =
411 std::get_if<uint64_t>(&propertyMap.second);
412 if (sizePtr == nullptr)
413 {
414 messages::internalError(asyncResp->res);
415 break;
416 }
417 size = *sizePtr;
418 break;
419 }
420 }
421 }
422 else if (interfaceMap.first == "xyz.openbmc_project.Time.EpochTime")
423 {
424 for (const auto& propertyMap : interfaceMap.second)
425 {
426 if (propertyMap.first == "Elapsed")
427 {
428 const uint64_t* usecsTimeStamp =
429 std::get_if<uint64_t>(&propertyMap.second);
430 if (usecsTimeStamp == nullptr)
431 {
432 messages::internalError(asyncResp->res);
433 break;
434 }
Claire Weinanc6fecda2022-07-15 10:43:25 -0700435 timestampUs = *usecsTimeStamp;
Claire Weinanaefe3782022-07-15 19:17:19 -0700436 break;
437 }
438 }
439 }
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -0600440 else if (interfaceMap.first ==
441 "xyz.openbmc_project.Common.OriginatedBy")
442 {
443 for (const auto& propertyMap : interfaceMap.second)
444 {
445 if (propertyMap.first == "OriginatorId")
446 {
447 const std::string* id =
448 std::get_if<std::string>(&propertyMap.second);
449 if (id == nullptr)
450 {
451 messages::internalError(asyncResp->res);
452 break;
453 }
454 originatorId = *id;
455 }
456
457 if (propertyMap.first == "OriginatorType")
458 {
459 const std::string* type =
460 std::get_if<std::string>(&propertyMap.second);
461 if (type == nullptr)
462 {
463 messages::internalError(asyncResp->res);
464 break;
465 }
466
467 originatorType = mapDbusOriginatorTypeToRedfish(*type);
468 if (originatorType == log_entry::OriginatorTypes::Invalid)
469 {
470 messages::internalError(asyncResp->res);
471 break;
472 }
473 }
474 }
475 }
Claire Weinanaefe3782022-07-15 19:17:19 -0700476 }
477}
478
Nan Zhou21ab4042022-06-26 23:07:40 +0000479static std::string getDumpEntriesPath(const std::string& dumpType)
Claire Weinanfdd26902022-03-01 14:18:25 -0800480{
481 std::string entriesPath;
482
483 if (dumpType == "BMC")
484 {
485 entriesPath = "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/";
486 }
487 else if (dumpType == "FaultLog")
488 {
489 entriesPath = "/redfish/v1/Managers/bmc/LogServices/FaultLog/Entries/";
490 }
491 else if (dumpType == "System")
492 {
493 entriesPath = "/redfish/v1/Systems/system/LogServices/Dump/Entries/";
494 }
495 else
496 {
Ed Tanous62598e32023-07-17 17:06:25 -0700497 BMCWEB_LOG_ERROR("getDumpEntriesPath() invalid dump type: {}",
498 dumpType);
Claire Weinanfdd26902022-03-01 14:18:25 -0800499 }
500
501 // Returns empty string on error
502 return entriesPath;
503}
504
zhanghch058d1b46d2021-04-01 11:18:24 +0800505inline void
506 getDumpEntryCollection(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
507 const std::string& dumpType)
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500508{
Claire Weinanfdd26902022-03-01 14:18:25 -0800509 std::string entriesPath = getDumpEntriesPath(dumpType);
510 if (entriesPath.empty())
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500511 {
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500512 messages::internalError(asyncResp->res);
513 return;
514 }
515
George Liu5eb468d2023-06-20 17:03:24 +0800516 sdbusplus::message::object_path path("/xyz/openbmc_project/dump");
517 dbus::utility::getManagedObjects(
518 "xyz.openbmc_project.Dump.Manager", path,
Claire Weinanfdd26902022-03-01 14:18:25 -0800519 [asyncResp, entriesPath,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800520 dumpType](const boost::system::error_code& ec,
George Liu5eb468d2023-06-20 17:03:24 +0800521 const dbus::utility::ManagedObjectType& objects) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700522 if (ec)
523 {
Ed Tanous62598e32023-07-17 17:06:25 -0700524 BMCWEB_LOG_ERROR("DumpEntry resp_handler got error {}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -0700525 messages::internalError(asyncResp->res);
526 return;
527 }
528
Claire Weinanfdd26902022-03-01 14:18:25 -0800529 // Remove ending slash
530 std::string odataIdStr = entriesPath;
531 if (!odataIdStr.empty())
532 {
533 odataIdStr.pop_back();
534 }
535
536 asyncResp->res.jsonValue["@odata.type"] =
537 "#LogEntryCollection.LogEntryCollection";
538 asyncResp->res.jsonValue["@odata.id"] = std::move(odataIdStr);
539 asyncResp->res.jsonValue["Name"] = dumpType + " Dump Entries";
Patrick Williams89492a12023-05-10 07:51:34 -0500540 asyncResp->res.jsonValue["Description"] = "Collection of " + dumpType +
541 " Dump Entries";
Claire Weinanfdd26902022-03-01 14:18:25 -0800542
Ed Tanous3544d2a2023-08-06 18:12:20 -0700543 nlohmann::json::array_t entriesArray;
Ed Tanous18f8f602023-07-18 10:07:23 -0700544 std::string dumpEntryPath = getDumpPath(dumpType) + "/entry/";
Ed Tanous002d39b2022-05-31 08:59:27 -0700545
George Liu5eb468d2023-06-20 17:03:24 +0800546 dbus::utility::ManagedObjectType resp(objects);
Ed Tanous3544d2a2023-08-06 18:12:20 -0700547 std::ranges::sort(resp, [](const auto& l, const auto& r) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700548 return AlphanumLess<std::string>()(l.first.filename(),
549 r.first.filename());
550 });
551
552 for (auto& object : resp)
553 {
554 if (object.first.str.find(dumpEntryPath) == std::string::npos)
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500555 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700556 continue;
557 }
Claire Weinanc6fecda2022-07-15 10:43:25 -0700558 uint64_t timestampUs = 0;
Ed Tanous002d39b2022-05-31 08:59:27 -0700559 uint64_t size = 0;
560 std::string dumpStatus;
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -0600561 std::string originatorId;
562 log_entry::OriginatorTypes originatorType =
563 log_entry::OriginatorTypes::Internal;
Jason M. Bills433b68b2022-06-28 12:24:26 -0700564 nlohmann::json::object_t thisEntry;
Ed Tanous002d39b2022-05-31 08:59:27 -0700565
566 std::string entryID = object.first.filename();
567 if (entryID.empty())
568 {
569 continue;
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500570 }
571
Claire Weinanc6fecda2022-07-15 10:43:25 -0700572 parseDumpEntryFromDbusObject(object, dumpStatus, size, timestampUs,
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -0600573 originatorId, originatorType,
Claire Weinanaefe3782022-07-15 19:17:19 -0700574 asyncResp);
Ed Tanous002d39b2022-05-31 08:59:27 -0700575
576 if (dumpStatus !=
577 "xyz.openbmc_project.Common.Progress.OperationStatus.Completed" &&
578 !dumpStatus.empty())
579 {
580 // Dump status is not Complete, no need to enumerate
581 continue;
582 }
583
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -0600584 thisEntry["@odata.type"] = "#LogEntry.v1_11_0.LogEntry";
Claire Weinanfdd26902022-03-01 14:18:25 -0800585 thisEntry["@odata.id"] = entriesPath + entryID;
Ed Tanous002d39b2022-05-31 08:59:27 -0700586 thisEntry["Id"] = entryID;
587 thisEntry["EntryType"] = "Event";
Ed Tanous002d39b2022-05-31 08:59:27 -0700588 thisEntry["Name"] = dumpType + " Dump Entry";
Claire Weinanbbd80db2022-10-26 16:55:52 -0700589 thisEntry["Created"] =
590 redfish::time_utils::getDateTimeUintUs(timestampUs);
Ed Tanous002d39b2022-05-31 08:59:27 -0700591
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -0600592 if (!originatorId.empty())
593 {
594 thisEntry["Originator"] = originatorId;
595 thisEntry["OriginatorType"] = originatorType;
596 }
597
Ed Tanous002d39b2022-05-31 08:59:27 -0700598 if (dumpType == "BMC")
599 {
600 thisEntry["DiagnosticDataType"] = "Manager";
Patrick Williams89492a12023-05-10 07:51:34 -0500601 thisEntry["AdditionalDataURI"] = entriesPath + entryID +
602 "/attachment";
Claire Weinanfdd26902022-03-01 14:18:25 -0800603 thisEntry["AdditionalDataSizeBytes"] = size;
Ed Tanous002d39b2022-05-31 08:59:27 -0700604 }
605 else if (dumpType == "System")
606 {
607 thisEntry["DiagnosticDataType"] = "OEM";
608 thisEntry["OEMDiagnosticDataType"] = "System";
Patrick Williams89492a12023-05-10 07:51:34 -0500609 thisEntry["AdditionalDataURI"] = entriesPath + entryID +
610 "/attachment";
Claire Weinanfdd26902022-03-01 14:18:25 -0800611 thisEntry["AdditionalDataSizeBytes"] = size;
Ed Tanous002d39b2022-05-31 08:59:27 -0700612 }
Patrick Williamsb2ba3072023-05-12 10:27:39 -0500613 entriesArray.emplace_back(std::move(thisEntry));
Ed Tanous002d39b2022-05-31 08:59:27 -0700614 }
615 asyncResp->res.jsonValue["Members@odata.count"] = entriesArray.size();
Ed Tanous3544d2a2023-08-06 18:12:20 -0700616 asyncResp->res.jsonValue["Members"] = std::move(entriesArray);
Patrick Williams5a39f772023-10-20 11:20:21 -0500617 });
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500618}
619
zhanghch058d1b46d2021-04-01 11:18:24 +0800620inline void
Claire Weinanc7a6d662022-06-13 16:36:39 -0700621 getDumpEntryById(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
zhanghch058d1b46d2021-04-01 11:18:24 +0800622 const std::string& entryID, const std::string& dumpType)
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500623{
Claire Weinanfdd26902022-03-01 14:18:25 -0800624 std::string entriesPath = getDumpEntriesPath(dumpType);
625 if (entriesPath.empty())
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500626 {
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500627 messages::internalError(asyncResp->res);
628 return;
629 }
630
George Liu5eb468d2023-06-20 17:03:24 +0800631 sdbusplus::message::object_path path("/xyz/openbmc_project/dump");
632 dbus::utility::getManagedObjects(
633 "xyz.openbmc_project.Dump.Manager", path,
Claire Weinanfdd26902022-03-01 14:18:25 -0800634 [asyncResp, entryID, dumpType,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800635 entriesPath](const boost::system::error_code& ec,
Ed Tanous02cad962022-06-30 16:50:15 -0700636 const dbus::utility::ManagedObjectType& resp) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700637 if (ec)
638 {
Ed Tanous62598e32023-07-17 17:06:25 -0700639 BMCWEB_LOG_ERROR("DumpEntry resp_handler got error {}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -0700640 messages::internalError(asyncResp->res);
641 return;
642 }
643
644 bool foundDumpEntry = false;
Ed Tanous18f8f602023-07-18 10:07:23 -0700645 std::string dumpEntryPath = getDumpPath(dumpType) + "/entry/";
Ed Tanous002d39b2022-05-31 08:59:27 -0700646
647 for (const auto& objectPath : resp)
648 {
649 if (objectPath.first.str != dumpEntryPath + entryID)
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500650 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700651 continue;
652 }
653
654 foundDumpEntry = true;
Claire Weinanc6fecda2022-07-15 10:43:25 -0700655 uint64_t timestampUs = 0;
Ed Tanous002d39b2022-05-31 08:59:27 -0700656 uint64_t size = 0;
657 std::string dumpStatus;
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -0600658 std::string originatorId;
659 log_entry::OriginatorTypes originatorType =
660 log_entry::OriginatorTypes::Internal;
Ed Tanous002d39b2022-05-31 08:59:27 -0700661
Claire Weinanaefe3782022-07-15 19:17:19 -0700662 parseDumpEntryFromDbusObject(objectPath, dumpStatus, size,
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -0600663 timestampUs, originatorId,
664 originatorType, asyncResp);
Ed Tanous002d39b2022-05-31 08:59:27 -0700665
666 if (dumpStatus !=
667 "xyz.openbmc_project.Common.Progress.OperationStatus.Completed" &&
668 !dumpStatus.empty())
669 {
670 // Dump status is not Complete
671 // return not found until status is changed to Completed
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +0200672 messages::resourceNotFound(asyncResp->res, dumpType + " dump",
673 entryID);
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500674 return;
675 }
676
Ed Tanous002d39b2022-05-31 08:59:27 -0700677 asyncResp->res.jsonValue["@odata.type"] =
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -0600678 "#LogEntry.v1_11_0.LogEntry";
Claire Weinanfdd26902022-03-01 14:18:25 -0800679 asyncResp->res.jsonValue["@odata.id"] = entriesPath + entryID;
Ed Tanous002d39b2022-05-31 08:59:27 -0700680 asyncResp->res.jsonValue["Id"] = entryID;
681 asyncResp->res.jsonValue["EntryType"] = "Event";
Ed Tanous002d39b2022-05-31 08:59:27 -0700682 asyncResp->res.jsonValue["Name"] = dumpType + " Dump Entry";
Claire Weinanbbd80db2022-10-26 16:55:52 -0700683 asyncResp->res.jsonValue["Created"] =
684 redfish::time_utils::getDateTimeUintUs(timestampUs);
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500685
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -0600686 if (!originatorId.empty())
687 {
688 asyncResp->res.jsonValue["Originator"] = originatorId;
689 asyncResp->res.jsonValue["OriginatorType"] = originatorType;
690 }
691
Ed Tanous002d39b2022-05-31 08:59:27 -0700692 if (dumpType == "BMC")
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500693 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700694 asyncResp->res.jsonValue["DiagnosticDataType"] = "Manager";
695 asyncResp->res.jsonValue["AdditionalDataURI"] =
Claire Weinanfdd26902022-03-01 14:18:25 -0800696 entriesPath + entryID + "/attachment";
697 asyncResp->res.jsonValue["AdditionalDataSizeBytes"] = size;
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500698 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700699 else if (dumpType == "System")
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500700 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700701 asyncResp->res.jsonValue["DiagnosticDataType"] = "OEM";
702 asyncResp->res.jsonValue["OEMDiagnosticDataType"] = "System";
703 asyncResp->res.jsonValue["AdditionalDataURI"] =
Claire Weinanfdd26902022-03-01 14:18:25 -0800704 entriesPath + entryID + "/attachment";
705 asyncResp->res.jsonValue["AdditionalDataSizeBytes"] = size;
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500706 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700707 }
708 if (!foundDumpEntry)
709 {
Ed Tanous62598e32023-07-17 17:06:25 -0700710 BMCWEB_LOG_WARNING("Can't find Dump Entry {}", entryID);
Myung Baeb90d14f2023-05-31 14:40:39 -0500711 messages::resourceNotFound(asyncResp->res, dumpType + " dump",
712 entryID);
Ed Tanous002d39b2022-05-31 08:59:27 -0700713 return;
714 }
Patrick Williams5a39f772023-10-20 11:20:21 -0500715 });
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500716}
717
zhanghch058d1b46d2021-04-01 11:18:24 +0800718inline void deleteDumpEntry(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Stanley Chu98782562020-11-04 16:10:24 +0800719 const std::string& entryID,
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500720 const std::string& dumpType)
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500721{
Patrick Williams5a39f772023-10-20 11:20:21 -0500722 auto respHandler = [asyncResp,
723 entryID](const boost::system::error_code& ec) {
Ed Tanous62598e32023-07-17 17:06:25 -0700724 BMCWEB_LOG_DEBUG("Dump Entry doDelete callback: Done");
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500725 if (ec)
726 {
George Liu3de8d8b2021-03-22 17:49:39 +0800727 if (ec.value() == EBADR)
728 {
729 messages::resourceNotFound(asyncResp->res, "LogEntry", entryID);
730 return;
731 }
Ed Tanous62598e32023-07-17 17:06:25 -0700732 BMCWEB_LOG_ERROR(
733 "Dump (DBus) doDelete respHandler got error {} entryID={}", ec,
734 entryID);
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500735 messages::internalError(asyncResp->res);
736 return;
737 }
738 };
Ed Tanous18f8f602023-07-18 10:07:23 -0700739
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500740 crow::connections::systemBus->async_method_call(
741 respHandler, "xyz.openbmc_project.Dump.Manager",
Ed Tanous18f8f602023-07-18 10:07:23 -0700742 std::format("{}/entry/{}", getDumpPath(dumpType), entryID),
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500743 "xyz.openbmc_project.Object.Delete", "Delete");
744}
Abhilash Rajub5f288d2023-11-08 22:32:44 -0600745inline bool checkSizeLimit(int fd, crow::Response& res)
746{
747 long long int size = lseek(fd, 0, SEEK_END);
748 if (size <= 0)
749 {
750 BMCWEB_LOG_ERROR("Failed to get size of file, lseek() returned {}",
751 size);
752 messages::internalError(res);
753 return false;
754 }
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500755
Abhilash Rajub5f288d2023-11-08 22:32:44 -0600756 // Arbitrary max size of 20MB to accommodate BMC dumps
757 constexpr long long int maxFileSize = 20LL * 1024LL * 1024LL;
758 if (size > maxFileSize)
759 {
760 BMCWEB_LOG_ERROR("File size {} exceeds maximum allowed size of {}",
761 size, maxFileSize);
762 messages::internalError(res);
763 return false;
764 }
765 off_t rc = lseek(fd, 0, SEEK_SET);
766 if (rc < 0)
767 {
768 BMCWEB_LOG_ERROR("Failed to reset file offset to 0");
769 messages::internalError(res);
770 return false;
771 }
772 return true;
773}
Carson Labrado168d1b12023-03-27 17:04:46 +0000774inline void
775 downloadEntryCallback(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
776 const std::string& entryID,
777 const std::string& downloadEntryType,
778 const boost::system::error_code& ec,
779 const sdbusplus::message::unix_fd& unixfd)
780{
781 if (ec.value() == EBADR)
782 {
783 messages::resourceNotFound(asyncResp->res, "EntryAttachment", entryID);
784 return;
785 }
786 if (ec)
787 {
788 BMCWEB_LOG_ERROR("DBUS response error: {}", ec);
789 messages::internalError(asyncResp->res);
790 return;
791 }
792
793 // Make sure we know how to process the retrieved entry attachment
794 if ((downloadEntryType != "BMC") && (downloadEntryType != "System"))
795 {
796 BMCWEB_LOG_ERROR("downloadEntryCallback() invalid entry type: {}",
797 downloadEntryType);
798 messages::internalError(asyncResp->res);
799 }
800
801 int fd = -1;
802 fd = dup(unixfd);
803 if (fd < 0)
804 {
805 BMCWEB_LOG_ERROR("Failed to open file");
806 messages::internalError(asyncResp->res);
807 return;
808 }
Abhilash Rajub5f288d2023-11-08 22:32:44 -0600809 if (!checkSizeLimit(fd, asyncResp->res))
Carson Labrado168d1b12023-03-27 17:04:46 +0000810 {
Carson Labrado168d1b12023-03-27 17:04:46 +0000811 close(fd);
812 return;
813 }
Carson Labrado168d1b12023-03-27 17:04:46 +0000814 if (downloadEntryType == "System")
815 {
Abhilash Rajub5f288d2023-11-08 22:32:44 -0600816 if (!asyncResp->res.openFd(fd, bmcweb::EncodingType::Base64))
817 {
818 messages::internalError(asyncResp->res);
819 close(fd);
820 return;
821 }
Carson Labrado168d1b12023-03-27 17:04:46 +0000822 asyncResp->res.addHeader(
823 boost::beast::http::field::content_transfer_encoding, "Base64");
Abhilash Rajub5f288d2023-11-08 22:32:44 -0600824 return;
Carson Labrado168d1b12023-03-27 17:04:46 +0000825 }
Abhilash Rajub5f288d2023-11-08 22:32:44 -0600826 if (!asyncResp->res.openFd(fd))
Ed Tanous27b0cf92023-08-07 12:02:40 -0700827 {
Abhilash Rajub5f288d2023-11-08 22:32:44 -0600828 messages::internalError(asyncResp->res);
829 close(fd);
830 return;
Ed Tanous27b0cf92023-08-07 12:02:40 -0700831 }
Carson Labrado168d1b12023-03-27 17:04:46 +0000832 asyncResp->res.addHeader(boost::beast::http::field::content_type,
833 "application/octet-stream");
834}
835
836inline void
837 downloadDumpEntry(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
838 const std::string& entryID, const std::string& dumpType)
839{
840 if (dumpType != "BMC")
841 {
842 BMCWEB_LOG_WARNING("Can't find Dump Entry {}", entryID);
843 messages::resourceNotFound(asyncResp->res, dumpType + " dump", entryID);
844 return;
845 }
846
Ed Tanous18f8f602023-07-18 10:07:23 -0700847 std::string dumpEntryPath = std::format("{}/entry/{}",
848 getDumpPath(dumpType), entryID);
Carson Labrado168d1b12023-03-27 17:04:46 +0000849
850 auto downloadDumpEntryHandler =
851 [asyncResp, entryID,
852 dumpType](const boost::system::error_code& ec,
853 const sdbusplus::message::unix_fd& unixfd) {
854 downloadEntryCallback(asyncResp, entryID, dumpType, ec, unixfd);
855 };
856
857 crow::connections::systemBus->async_method_call(
858 std::move(downloadDumpEntryHandler), "xyz.openbmc_project.Dump.Manager",
859 dumpEntryPath, "xyz.openbmc_project.Dump.Entry", "GetFileHandle");
860}
861
862inline void
863 downloadEventLogEntry(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
864 const std::string& systemName,
865 const std::string& entryID,
866 const std::string& dumpType)
867{
868 if constexpr (bmcwebEnableMultiHost)
869 {
870 // Option currently returns no systems. TBD
871 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
872 systemName);
873 return;
874 }
875 if (systemName != "system")
876 {
877 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
878 systemName);
879 return;
880 }
881
882 std::string entryPath =
883 sdbusplus::message::object_path("/xyz/openbmc_project/logging/entry") /
884 entryID;
885
886 auto downloadEventLogEntryHandler =
887 [asyncResp, entryID,
888 dumpType](const boost::system::error_code& ec,
889 const sdbusplus::message::unix_fd& unixfd) {
890 downloadEntryCallback(asyncResp, entryID, dumpType, ec, unixfd);
891 };
892
893 crow::connections::systemBus->async_method_call(
894 std::move(downloadEventLogEntryHandler), "xyz.openbmc_project.Logging",
895 entryPath, "xyz.openbmc_project.Logging.Entry", "GetEntry");
896}
897
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600898inline DumpCreationProgress
899 mapDbusStatusToDumpProgress(const std::string& status)
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500900{
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600901 if (status ==
902 "xyz.openbmc_project.Common.Progress.OperationStatus.Failed" ||
903 status == "xyz.openbmc_project.Common.Progress.OperationStatus.Aborted")
904 {
905 return DumpCreationProgress::DUMP_CREATE_FAILED;
906 }
907 if (status ==
908 "xyz.openbmc_project.Common.Progress.OperationStatus.Completed")
909 {
910 return DumpCreationProgress::DUMP_CREATE_SUCCESS;
911 }
912 return DumpCreationProgress::DUMP_CREATE_INPROGRESS;
913}
914
915inline DumpCreationProgress
916 getDumpCompletionStatus(const dbus::utility::DBusPropertiesMap& values)
917{
918 for (const auto& [key, val] : values)
919 {
920 if (key == "Status")
Ed Tanous002d39b2022-05-31 08:59:27 -0700921 {
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600922 const std::string* value = std::get_if<std::string>(&val);
923 if (value == nullptr)
924 {
Ed Tanous62598e32023-07-17 17:06:25 -0700925 BMCWEB_LOG_ERROR("Status property value is null");
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600926 return DumpCreationProgress::DUMP_CREATE_FAILED;
927 }
928 return mapDbusStatusToDumpProgress(*value);
929 }
930 }
931 return DumpCreationProgress::DUMP_CREATE_INPROGRESS;
932}
933
934inline std::string getDumpEntryPath(const std::string& dumpPath)
935{
936 if (dumpPath == "/xyz/openbmc_project/dump/bmc/entry")
937 {
938 return "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/";
939 }
940 if (dumpPath == "/xyz/openbmc_project/dump/system/entry")
941 {
942 return "/redfish/v1/Systems/system/LogServices/Dump/Entries/";
943 }
944 return "";
945}
946
947inline void createDumpTaskCallback(
948 task::Payload&& payload,
949 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
950 const sdbusplus::message::object_path& createdObjPath)
951{
952 const std::string dumpPath = createdObjPath.parent_path().str;
953 const std::string dumpId = createdObjPath.filename();
954
955 std::string dumpEntryPath = getDumpEntryPath(dumpPath);
956
957 if (dumpEntryPath.empty())
958 {
Ed Tanous62598e32023-07-17 17:06:25 -0700959 BMCWEB_LOG_ERROR("Invalid dump type received");
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600960 messages::internalError(asyncResp->res);
961 return;
962 }
963
964 crow::connections::systemBus->async_method_call(
Ed Tanous8cb2c022024-03-27 16:31:46 -0700965 [asyncResp, payload = std::move(payload), createdObjPath,
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600966 dumpEntryPath{std::move(dumpEntryPath)},
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800967 dumpId](const boost::system::error_code& ec,
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600968 const std::string& introspectXml) {
969 if (ec)
970 {
Ed Tanous62598e32023-07-17 17:06:25 -0700971 BMCWEB_LOG_ERROR("Introspect call failed with error: {}",
972 ec.message());
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600973 messages::internalError(asyncResp->res);
974 return;
Ed Tanous002d39b2022-05-31 08:59:27 -0700975 }
976
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600977 // Check if the created dump object has implemented Progress
978 // interface to track dump completion. If yes, fetch the "Status"
979 // property of the interface, modify the task state accordingly.
980 // Else, return task completed.
981 tinyxml2::XMLDocument doc;
Ed Tanous002d39b2022-05-31 08:59:27 -0700982
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600983 doc.Parse(introspectXml.data(), introspectXml.size());
984 tinyxml2::XMLNode* pRoot = doc.FirstChildElement("node");
985 if (pRoot == nullptr)
Ed Tanous002d39b2022-05-31 08:59:27 -0700986 {
Ed Tanous62598e32023-07-17 17:06:25 -0700987 BMCWEB_LOG_ERROR("XML document failed to parse");
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600988 messages::internalError(asyncResp->res);
989 return;
990 }
991 tinyxml2::XMLElement* interfaceNode =
992 pRoot->FirstChildElement("interface");
993
994 bool isProgressIntfPresent = false;
995 while (interfaceNode != nullptr)
996 {
997 const char* thisInterfaceName = interfaceNode->Attribute("name");
998 if (thisInterfaceName != nullptr)
999 {
1000 if (thisInterfaceName ==
1001 std::string_view("xyz.openbmc_project.Common.Progress"))
1002 {
1003 interfaceNode =
1004 interfaceNode->NextSiblingElement("interface");
1005 continue;
1006 }
1007 isProgressIntfPresent = true;
1008 break;
1009 }
1010 interfaceNode = interfaceNode->NextSiblingElement("interface");
1011 }
1012
1013 std::shared_ptr<task::TaskData> task = task::TaskData::createTask(
1014 [createdObjPath, dumpEntryPath, dumpId, isProgressIntfPresent](
Ed Tanous8b242752023-06-27 17:17:13 -07001015 const boost::system::error_code& ec2, sdbusplus::message_t& msg,
Asmitha Karunanithi8e317782020-12-10 03:35:05 -06001016 const std::shared_ptr<task::TaskData>& taskData) {
Ed Tanous8b242752023-06-27 17:17:13 -07001017 if (ec2)
Asmitha Karunanithi8e317782020-12-10 03:35:05 -06001018 {
Ed Tanous62598e32023-07-17 17:06:25 -07001019 BMCWEB_LOG_ERROR("{}: Error in creating dump",
1020 createdObjPath.str);
Asmitha Karunanithi8e317782020-12-10 03:35:05 -06001021 taskData->messages.emplace_back(messages::internalError());
1022 taskData->state = "Cancelled";
1023 return task::completed;
1024 }
1025
1026 if (isProgressIntfPresent)
1027 {
1028 dbus::utility::DBusPropertiesMap values;
1029 std::string prop;
1030 msg.read(prop, values);
1031
1032 DumpCreationProgress dumpStatus =
1033 getDumpCompletionStatus(values);
1034 if (dumpStatus == DumpCreationProgress::DUMP_CREATE_FAILED)
1035 {
Ed Tanous62598e32023-07-17 17:06:25 -07001036 BMCWEB_LOG_ERROR("{}: Error in creating dump",
1037 createdObjPath.str);
Asmitha Karunanithi8e317782020-12-10 03:35:05 -06001038 taskData->state = "Cancelled";
1039 return task::completed;
1040 }
1041
1042 if (dumpStatus == DumpCreationProgress::DUMP_CREATE_INPROGRESS)
1043 {
Ed Tanous62598e32023-07-17 17:06:25 -07001044 BMCWEB_LOG_DEBUG("{}: Dump creation task is in progress",
1045 createdObjPath.str);
Asmitha Karunanithi8e317782020-12-10 03:35:05 -06001046 return !task::completed;
1047 }
1048 }
1049
Ed Tanous002d39b2022-05-31 08:59:27 -07001050 nlohmann::json retMessage = messages::success();
1051 taskData->messages.emplace_back(retMessage);
1052
Ed Tanousc51a58e2023-03-27 14:43:19 -07001053 boost::urls::url url = boost::urls::format(
1054 "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/{}", dumpId);
1055
1056 std::string headerLoc = "Location: ";
1057 headerLoc += url.buffer();
1058
Ed Tanous002d39b2022-05-31 08:59:27 -07001059 taskData->payload->httpHeaders.emplace_back(std::move(headerLoc));
1060
Ed Tanous62598e32023-07-17 17:06:25 -07001061 BMCWEB_LOG_DEBUG("{}: Dump creation task completed",
1062 createdObjPath.str);
Ed Tanous002d39b2022-05-31 08:59:27 -07001063 taskData->state = "Completed";
1064 return task::completed;
Patrick Williams5a39f772023-10-20 11:20:21 -05001065 },
Asmitha Karunanithi8e317782020-12-10 03:35:05 -06001066 "type='signal',interface='org.freedesktop.DBus.Properties',"
1067 "member='PropertiesChanged',path='" +
1068 createdObjPath.str + "'");
Asmitha Karunanithia43be802020-05-07 05:05:36 -05001069
Asmitha Karunanithi8e317782020-12-10 03:35:05 -06001070 // The task timer is set to max time limit within which the
1071 // requested dump will be collected.
1072 task->startTimer(std::chrono::minutes(6));
1073 task->populateResp(asyncResp->res);
1074 task->payload.emplace(payload);
Patrick Williams5a39f772023-10-20 11:20:21 -05001075 },
Asmitha Karunanithi8e317782020-12-10 03:35:05 -06001076 "xyz.openbmc_project.Dump.Manager", createdObjPath,
1077 "org.freedesktop.DBus.Introspectable", "Introspect");
Asmitha Karunanithia43be802020-05-07 05:05:36 -05001078}
1079
zhanghch058d1b46d2021-04-01 11:18:24 +08001080inline void createDump(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1081 const crow::Request& req, const std::string& dumpType)
Asmitha Karunanithia43be802020-05-07 05:05:36 -05001082{
Claire Weinanfdd26902022-03-01 14:18:25 -08001083 std::string dumpPath = getDumpEntriesPath(dumpType);
1084 if (dumpPath.empty())
Asmitha Karunanithia43be802020-05-07 05:05:36 -05001085 {
Asmitha Karunanithia43be802020-05-07 05:05:36 -05001086 messages::internalError(asyncResp->res);
1087 return;
1088 }
1089
1090 std::optional<std::string> diagnosticDataType;
1091 std::optional<std::string> oemDiagnosticDataType;
1092
Willy Tu15ed6782021-12-14 11:03:16 -08001093 if (!redfish::json_util::readJsonAction(
Asmitha Karunanithia43be802020-05-07 05:05:36 -05001094 req, asyncResp->res, "DiagnosticDataType", diagnosticDataType,
1095 "OEMDiagnosticDataType", oemDiagnosticDataType))
1096 {
1097 return;
1098 }
1099
1100 if (dumpType == "System")
1101 {
1102 if (!oemDiagnosticDataType || !diagnosticDataType)
1103 {
Ed Tanous62598e32023-07-17 17:06:25 -07001104 BMCWEB_LOG_ERROR(
1105 "CreateDump action parameter 'DiagnosticDataType'/'OEMDiagnosticDataType' value not found!");
Asmitha Karunanithia43be802020-05-07 05:05:36 -05001106 messages::actionParameterMissing(
1107 asyncResp->res, "CollectDiagnosticData",
1108 "DiagnosticDataType & OEMDiagnosticDataType");
1109 return;
1110 }
Ed Tanous3174e4d2020-10-07 11:41:22 -07001111 if ((*oemDiagnosticDataType != "System") ||
1112 (*diagnosticDataType != "OEM"))
Asmitha Karunanithia43be802020-05-07 05:05:36 -05001113 {
Ed Tanous62598e32023-07-17 17:06:25 -07001114 BMCWEB_LOG_ERROR("Wrong parameter values passed");
Ed Tanousace85d62021-10-26 12:45:59 -07001115 messages::internalError(asyncResp->res);
Asmitha Karunanithia43be802020-05-07 05:05:36 -05001116 return;
1117 }
Asmitha Karunanithi59075712021-10-22 01:17:41 -05001118 dumpPath = "/redfish/v1/Systems/system/LogServices/Dump/";
Asmitha Karunanithia43be802020-05-07 05:05:36 -05001119 }
1120 else if (dumpType == "BMC")
1121 {
1122 if (!diagnosticDataType)
1123 {
Ed Tanous62598e32023-07-17 17:06:25 -07001124 BMCWEB_LOG_ERROR(
1125 "CreateDump action parameter 'DiagnosticDataType' not found!");
Asmitha Karunanithia43be802020-05-07 05:05:36 -05001126 messages::actionParameterMissing(
1127 asyncResp->res, "CollectDiagnosticData", "DiagnosticDataType");
1128 return;
1129 }
Ed Tanous3174e4d2020-10-07 11:41:22 -07001130 if (*diagnosticDataType != "Manager")
Asmitha Karunanithia43be802020-05-07 05:05:36 -05001131 {
Ed Tanous62598e32023-07-17 17:06:25 -07001132 BMCWEB_LOG_ERROR(
1133 "Wrong parameter value passed for 'DiagnosticDataType'");
Ed Tanousace85d62021-10-26 12:45:59 -07001134 messages::internalError(asyncResp->res);
Asmitha Karunanithia43be802020-05-07 05:05:36 -05001135 return;
1136 }
Asmitha Karunanithi59075712021-10-22 01:17:41 -05001137 dumpPath = "/redfish/v1/Managers/bmc/LogServices/Dump/";
1138 }
1139 else
1140 {
Ed Tanous62598e32023-07-17 17:06:25 -07001141 BMCWEB_LOG_ERROR("CreateDump failed. Unknown dump type");
Asmitha Karunanithi59075712021-10-22 01:17:41 -05001142 messages::internalError(asyncResp->res);
1143 return;
Asmitha Karunanithia43be802020-05-07 05:05:36 -05001144 }
1145
Asmitha Karunanithi8e317782020-12-10 03:35:05 -06001146 std::vector<std::pair<std::string, std::variant<std::string, uint64_t>>>
1147 createDumpParamVec;
1148
Carson Labradof574a8e2023-03-22 02:26:00 +00001149 if (req.session != nullptr)
1150 {
1151 createDumpParamVec.emplace_back(
1152 "xyz.openbmc_project.Dump.Create.CreateParameters.OriginatorId",
1153 req.session->clientIp);
1154 createDumpParamVec.emplace_back(
1155 "xyz.openbmc_project.Dump.Create.CreateParameters.OriginatorType",
1156 "xyz.openbmc_project.Common.OriginatedBy.OriginatorTypes.Client");
1157 }
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -06001158
Asmitha Karunanithia43be802020-05-07 05:05:36 -05001159 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001160 [asyncResp, payload(task::Payload(req)),
1161 dumpPath](const boost::system::error_code& ec,
1162 const sdbusplus::message_t& msg,
1163 const sdbusplus::message::object_path& objPath) mutable {
Ed Tanous002d39b2022-05-31 08:59:27 -07001164 if (ec)
1165 {
Ed Tanous62598e32023-07-17 17:06:25 -07001166 BMCWEB_LOG_ERROR("CreateDump resp_handler got error {}", ec);
Asmitha Karunanithi59075712021-10-22 01:17:41 -05001167 const sd_bus_error* dbusError = msg.get_error();
1168 if (dbusError == nullptr)
1169 {
1170 messages::internalError(asyncResp->res);
1171 return;
1172 }
1173
Ed Tanous62598e32023-07-17 17:06:25 -07001174 BMCWEB_LOG_ERROR("CreateDump DBus error: {} and error msg: {}",
1175 dbusError->name, dbusError->message);
Asmitha Karunanithi59075712021-10-22 01:17:41 -05001176 if (std::string_view(
1177 "xyz.openbmc_project.Common.Error.NotAllowed") ==
1178 dbusError->name)
1179 {
1180 messages::resourceInStandby(asyncResp->res);
1181 return;
1182 }
1183 if (std::string_view(
1184 "xyz.openbmc_project.Dump.Create.Error.Disabled") ==
1185 dbusError->name)
1186 {
1187 messages::serviceDisabled(asyncResp->res, dumpPath);
1188 return;
1189 }
1190 if (std::string_view(
1191 "xyz.openbmc_project.Common.Error.Unavailable") ==
1192 dbusError->name)
1193 {
1194 messages::resourceInUse(asyncResp->res);
1195 return;
1196 }
1197 // Other Dbus errors such as:
1198 // xyz.openbmc_project.Common.Error.InvalidArgument &
1199 // org.freedesktop.DBus.Error.InvalidArgs are all related to
1200 // the dbus call that is made here in the bmcweb
1201 // implementation and has nothing to do with the client's
1202 // input in the request. Hence, returning internal error
1203 // back to the client.
Ed Tanous002d39b2022-05-31 08:59:27 -07001204 messages::internalError(asyncResp->res);
1205 return;
1206 }
Ed Tanous62598e32023-07-17 17:06:25 -07001207 BMCWEB_LOG_DEBUG("Dump Created. Path: {}", objPath.str);
Asmitha Karunanithi8e317782020-12-10 03:35:05 -06001208 createDumpTaskCallback(std::move(payload), asyncResp, objPath);
Patrick Williams5a39f772023-10-20 11:20:21 -05001209 },
Ed Tanous18f8f602023-07-18 10:07:23 -07001210 "xyz.openbmc_project.Dump.Manager", getDumpPath(dumpType),
Asmitha Karunanithi8e317782020-12-10 03:35:05 -06001211 "xyz.openbmc_project.Dump.Create", "CreateDump", createDumpParamVec);
Asmitha Karunanithia43be802020-05-07 05:05:36 -05001212}
1213
zhanghch058d1b46d2021-04-01 11:18:24 +08001214inline void clearDump(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1215 const std::string& dumpType)
Asmitha Karunanithi80319af2020-05-07 05:30:21 -05001216{
Claire Weinan0d946212022-07-13 19:40:19 -07001217 crow::connections::systemBus->async_method_call(
1218 [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001219 if (ec)
1220 {
Ed Tanous62598e32023-07-17 17:06:25 -07001221 BMCWEB_LOG_ERROR("clearDump resp_handler got error {}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -07001222 messages::internalError(asyncResp->res);
1223 return;
1224 }
Patrick Williams5a39f772023-10-20 11:20:21 -05001225 },
Ed Tanous18f8f602023-07-18 10:07:23 -07001226 "xyz.openbmc_project.Dump.Manager", getDumpPath(dumpType),
Claire Weinan0d946212022-07-13 19:40:19 -07001227 "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
Asmitha Karunanithi80319af2020-05-07 05:30:21 -05001228}
1229
Ed Tanousdf254f22024-04-01 13:25:46 -07001230inline void
Ed Tanousb9d36b42022-02-26 21:42:46 -08001231 parseCrashdumpParameters(const dbus::utility::DBusPropertiesMap& params,
1232 std::string& filename, std::string& timestamp,
1233 std::string& logfile)
Johnathan Mantey043a0532020-03-10 17:15:28 -07001234{
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001235 const std::string* filenamePtr = nullptr;
1236 const std::string* timestampPtr = nullptr;
1237 const std::string* logfilePtr = nullptr;
1238
1239 const bool success = sdbusplus::unpackPropertiesNoThrow(
1240 dbus_utils::UnpackErrorPrinter(), params, "Timestamp", timestampPtr,
1241 "Filename", filenamePtr, "Log", logfilePtr);
1242
1243 if (!success)
Johnathan Mantey043a0532020-03-10 17:15:28 -07001244 {
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001245 return;
1246 }
1247
1248 if (filenamePtr != nullptr)
1249 {
1250 filename = *filenamePtr;
1251 }
1252
1253 if (timestampPtr != nullptr)
1254 {
1255 timestamp = *timestampPtr;
1256 }
1257
1258 if (logfilePtr != nullptr)
1259 {
1260 logfile = *logfilePtr;
Johnathan Mantey043a0532020-03-10 17:15:28 -07001261 }
1262}
1263
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001264inline void requestRoutesSystemLogServiceCollection(App& app)
Ed Tanous1da66f72018-07-27 16:13:37 -07001265{
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001266 /**
1267 * Functions triggers appropriate requests on DBus
1268 */
Ed Tanous22d268c2022-05-19 09:39:07 -07001269 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/")
Ed Tanoused398212021-06-09 17:05:54 -07001270 .privileges(redfish::privileges::getLogServiceCollection)
Ed Tanous002d39b2022-05-31 08:59:27 -07001271 .methods(boost::beast::http::verb::get)(
1272 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07001273 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1274 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001275 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001276 {
1277 return;
1278 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08001279 if constexpr (bmcwebEnableMultiHost)
1280 {
1281 // Option currently returns no systems. TBD
1282 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1283 systemName);
1284 return;
1285 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001286 if (systemName != "system")
1287 {
1288 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1289 systemName);
1290 return;
1291 }
1292
Ed Tanous002d39b2022-05-31 08:59:27 -07001293 // Collections don't include the static data added by SubRoute
1294 // because it has a duplicate entry for members
1295 asyncResp->res.jsonValue["@odata.type"] =
1296 "#LogServiceCollection.LogServiceCollection";
1297 asyncResp->res.jsonValue["@odata.id"] =
1298 "/redfish/v1/Systems/system/LogServices";
1299 asyncResp->res.jsonValue["Name"] = "System Log Services Collection";
1300 asyncResp->res.jsonValue["Description"] =
1301 "Collection of LogServices for this Computer System";
1302 nlohmann::json& logServiceArray = asyncResp->res.jsonValue["Members"];
1303 logServiceArray = nlohmann::json::array();
1304 nlohmann::json::object_t eventLog;
1305 eventLog["@odata.id"] =
1306 "/redfish/v1/Systems/system/LogServices/EventLog";
Patrick Williamsb2ba3072023-05-12 10:27:39 -05001307 logServiceArray.emplace_back(std::move(eventLog));
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -05001308#ifdef BMCWEB_ENABLE_REDFISH_DUMP_LOG
Ed Tanous002d39b2022-05-31 08:59:27 -07001309 nlohmann::json::object_t dumpLog;
1310 dumpLog["@odata.id"] = "/redfish/v1/Systems/system/LogServices/Dump";
Patrick Williamsb2ba3072023-05-12 10:27:39 -05001311 logServiceArray.emplace_back(std::move(dumpLog));
raviteja-bc9bb6862020-02-03 11:53:32 -06001312#endif
1313
Jason M. Billsd53dd412019-02-12 17:16:22 -08001314#ifdef BMCWEB_ENABLE_REDFISH_CPU_LOG
Ed Tanous002d39b2022-05-31 08:59:27 -07001315 nlohmann::json::object_t crashdump;
1316 crashdump["@odata.id"] =
1317 "/redfish/v1/Systems/system/LogServices/Crashdump";
Patrick Williamsb2ba3072023-05-12 10:27:39 -05001318 logServiceArray.emplace_back(std::move(crashdump));
Jason M. Billsd53dd412019-02-12 17:16:22 -08001319#endif
Spencer Kub7028eb2021-10-26 15:27:35 +08001320
1321#ifdef BMCWEB_ENABLE_REDFISH_HOST_LOGGER
Ed Tanous002d39b2022-05-31 08:59:27 -07001322 nlohmann::json::object_t hostlogger;
1323 hostlogger["@odata.id"] =
1324 "/redfish/v1/Systems/system/LogServices/HostLogger";
Patrick Williamsb2ba3072023-05-12 10:27:39 -05001325 logServiceArray.emplace_back(std::move(hostlogger));
Spencer Kub7028eb2021-10-26 15:27:35 +08001326#endif
Ed Tanous002d39b2022-05-31 08:59:27 -07001327 asyncResp->res.jsonValue["Members@odata.count"] =
1328 logServiceArray.size();
ZhikuiRena3316fc2020-01-29 14:58:08 -08001329
George Liu7a1dbc42022-12-07 16:03:22 +08001330 constexpr std::array<std::string_view, 1> interfaces = {
1331 "xyz.openbmc_project.State.Boot.PostCode"};
1332 dbus::utility::getSubTreePaths(
1333 "/", 0, interfaces,
1334 [asyncResp](const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07001335 const dbus::utility::MapperGetSubTreePathsResponse&
1336 subtreePath) {
1337 if (ec)
1338 {
Ed Tanous62598e32023-07-17 17:06:25 -07001339 BMCWEB_LOG_ERROR("{}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -07001340 return;
1341 }
Ed Tanous45ca1b82022-03-25 13:07:27 -07001342
Ed Tanous002d39b2022-05-31 08:59:27 -07001343 for (const auto& pathStr : subtreePath)
1344 {
1345 if (pathStr.find("PostCode") != std::string::npos)
1346 {
1347 nlohmann::json& logServiceArrayLocal =
1348 asyncResp->res.jsonValue["Members"];
Ed Tanous613dabe2022-07-09 11:17:36 -07001349 nlohmann::json::object_t member;
1350 member["@odata.id"] =
1351 "/redfish/v1/Systems/system/LogServices/PostCodes";
1352
Patrick Williamsb2ba3072023-05-12 10:27:39 -05001353 logServiceArrayLocal.emplace_back(std::move(member));
Ed Tanous613dabe2022-07-09 11:17:36 -07001354
Ed Tanous002d39b2022-05-31 08:59:27 -07001355 asyncResp->res.jsonValue["Members@odata.count"] =
1356 logServiceArrayLocal.size();
1357 return;
1358 }
1359 }
Ed Tanous45ca1b82022-03-25 13:07:27 -07001360 });
Patrick Williams5a39f772023-10-20 11:20:21 -05001361 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001362}
1363
1364inline void requestRoutesEventLogService(App& app)
1365{
Ed Tanous22d268c2022-05-19 09:39:07 -07001366 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/EventLog/")
Ed Tanoused398212021-06-09 17:05:54 -07001367 .privileges(redfish::privileges::getLogService)
Ed Tanous002d39b2022-05-31 08:59:27 -07001368 .methods(boost::beast::http::verb::get)(
1369 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07001370 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1371 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001372 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001373 {
1374 return;
1375 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001376 if (systemName != "system")
1377 {
1378 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1379 systemName);
1380 return;
1381 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001382 asyncResp->res.jsonValue["@odata.id"] =
1383 "/redfish/v1/Systems/system/LogServices/EventLog";
1384 asyncResp->res.jsonValue["@odata.type"] =
Janet Adkinsb25644a2023-08-16 11:23:45 -05001385 "#LogService.v1_2_0.LogService";
Ed Tanous002d39b2022-05-31 08:59:27 -07001386 asyncResp->res.jsonValue["Name"] = "Event Log Service";
1387 asyncResp->res.jsonValue["Description"] = "System Event Log Service";
1388 asyncResp->res.jsonValue["Id"] = "EventLog";
1389 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
Tejas Patil7c8c4052021-06-04 17:43:14 +05301390
Ed Tanous002d39b2022-05-31 08:59:27 -07001391 std::pair<std::string, std::string> redfishDateTimeOffset =
Ed Tanous2b829372022-08-03 14:22:34 -07001392 redfish::time_utils::getDateTimeOffsetNow();
Tejas Patil7c8c4052021-06-04 17:43:14 +05301393
Ed Tanous002d39b2022-05-31 08:59:27 -07001394 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
1395 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
1396 redfishDateTimeOffset.second;
Tejas Patil7c8c4052021-06-04 17:43:14 +05301397
Ed Tanous002d39b2022-05-31 08:59:27 -07001398 asyncResp->res.jsonValue["Entries"]["@odata.id"] =
1399 "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
1400 asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"] = {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001401
Ed Tanous002d39b2022-05-31 08:59:27 -07001402 {"target",
1403 "/redfish/v1/Systems/system/LogServices/EventLog/Actions/LogService.ClearLog"}};
Patrick Williams5a39f772023-10-20 11:20:21 -05001404 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001405}
1406
1407inline void requestRoutesJournalEventLogClear(App& app)
1408{
Jason M. Bills4978b632022-02-22 14:17:43 -08001409 BMCWEB_ROUTE(
1410 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07001411 "/redfish/v1/Systems/<str>/LogServices/EventLog/Actions/LogService.ClearLog/")
Ed Tanous432a8902021-06-14 15:28:56 -07001412 .privileges({{"ConfigureComponents"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001413 .methods(boost::beast::http::verb::post)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07001414 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07001415 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1416 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001417 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001418 {
1419 return;
1420 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001421 if (systemName != "system")
1422 {
1423 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1424 systemName);
1425 return;
1426 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001427 // Clear the EventLog by deleting the log files
1428 std::vector<std::filesystem::path> redfishLogFiles;
1429 if (getRedfishLogFiles(redfishLogFiles))
1430 {
1431 for (const std::filesystem::path& file : redfishLogFiles)
1432 {
1433 std::error_code ec;
1434 std::filesystem::remove(file, ec);
1435 }
1436 }
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001437
Ed Tanous002d39b2022-05-31 08:59:27 -07001438 // Reload rsyslog so it knows to start new log files
1439 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001440 [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001441 if (ec)
1442 {
Ed Tanous62598e32023-07-17 17:06:25 -07001443 BMCWEB_LOG_ERROR("Failed to reload rsyslog: {}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -07001444 messages::internalError(asyncResp->res);
1445 return;
1446 }
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001447
Ed Tanous002d39b2022-05-31 08:59:27 -07001448 messages::success(asyncResp->res);
Patrick Williams5a39f772023-10-20 11:20:21 -05001449 },
Ed Tanous002d39b2022-05-31 08:59:27 -07001450 "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
1451 "org.freedesktop.systemd1.Manager", "ReloadUnit", "rsyslog.service",
1452 "replace");
Patrick Williams5a39f772023-10-20 11:20:21 -05001453 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001454}
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001455
Jason M. Billsac992cd2022-06-24 13:31:46 -07001456enum class LogParseError
1457{
1458 success,
1459 parseFailed,
1460 messageIdNotInRegistry,
1461};
1462
1463static LogParseError
1464 fillEventLogEntryJson(const std::string& logEntryID,
1465 const std::string& logEntry,
1466 nlohmann::json::object_t& logEntryJson)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001467{
Jason M. Bills95820182019-04-22 16:25:34 -07001468 // The redfish log format is "<Timestamp> <MessageId>,<MessageArgs>"
Jason M. Billscd225da2019-05-08 15:31:57 -07001469 // First get the Timestamp
Ed Tanousf23b7292020-10-15 09:41:17 -07001470 size_t space = logEntry.find_first_of(' ');
Jason M. Billscd225da2019-05-08 15:31:57 -07001471 if (space == std::string::npos)
Jason M. Bills95820182019-04-22 16:25:34 -07001472 {
Jason M. Billsac992cd2022-06-24 13:31:46 -07001473 return LogParseError::parseFailed;
Jason M. Bills95820182019-04-22 16:25:34 -07001474 }
Jason M. Billscd225da2019-05-08 15:31:57 -07001475 std::string timestamp = logEntry.substr(0, space);
1476 // Then get the log contents
Ed Tanousf23b7292020-10-15 09:41:17 -07001477 size_t entryStart = logEntry.find_first_not_of(' ', space);
Jason M. Billscd225da2019-05-08 15:31:57 -07001478 if (entryStart == std::string::npos)
1479 {
Jason M. Billsac992cd2022-06-24 13:31:46 -07001480 return LogParseError::parseFailed;
Jason M. Billscd225da2019-05-08 15:31:57 -07001481 }
1482 std::string_view entry(logEntry);
1483 entry.remove_prefix(entryStart);
1484 // Use split to separate the entry into its fields
1485 std::vector<std::string> logEntryFields;
Ed Tanous50ebd4a2023-01-19 19:03:17 -08001486 bmcweb::split(logEntryFields, entry, ',');
Jason M. Billscd225da2019-05-08 15:31:57 -07001487 // We need at least a MessageId to be valid
Ed Tanous1e6deaf2022-02-17 11:32:37 -08001488 auto logEntryIter = logEntryFields.begin();
1489 if (logEntryIter == logEntryFields.end())
Jason M. Billscd225da2019-05-08 15:31:57 -07001490 {
Jason M. Billsac992cd2022-06-24 13:31:46 -07001491 return LogParseError::parseFailed;
Jason M. Billscd225da2019-05-08 15:31:57 -07001492 }
Ed Tanous1e6deaf2022-02-17 11:32:37 -08001493 std::string& messageID = *logEntryIter;
Jason M. Bills4851d452019-03-28 11:27:48 -07001494 // Get the Message from the MessageRegistry
Ed Tanousfffb8c12022-02-07 23:53:03 -08001495 const registries::Message* message = registries::getMessage(messageID);
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001496
Ed Tanous1e6deaf2022-02-17 11:32:37 -08001497 logEntryIter++;
Sui Chen54417b02022-03-24 14:59:52 -07001498 if (message == nullptr)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001499 {
Ed Tanous62598e32023-07-17 17:06:25 -07001500 BMCWEB_LOG_WARNING("Log entry not found in registry: {}", logEntry);
Jason M. Billsac992cd2022-06-24 13:31:46 -07001501 return LogParseError::messageIdNotInRegistry;
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001502 }
1503
Ed Tanous1e6deaf2022-02-17 11:32:37 -08001504 std::vector<std::string_view> messageArgs(logEntryIter,
1505 logEntryFields.end());
Ed Tanousc05bba42023-06-28 08:33:29 -07001506 messageArgs.resize(message->numberOfArgs);
1507
Ed Tanous1e6deaf2022-02-17 11:32:37 -08001508 std::string msg = redfish::registries::fillMessageArgs(messageArgs,
1509 message->message);
1510 if (msg.empty())
Jason M. Bills4851d452019-03-28 11:27:48 -07001511 {
Ed Tanous1e6deaf2022-02-17 11:32:37 -08001512 return LogParseError::parseFailed;
Jason M. Bills4851d452019-03-28 11:27:48 -07001513 }
1514
Jason M. Bills95820182019-04-22 16:25:34 -07001515 // Get the Created time from the timestamp. The log timestamp is in RFC3339
1516 // format which matches the Redfish format except for the fractional seconds
1517 // between the '.' and the '+', so just remove them.
Ed Tanousf23b7292020-10-15 09:41:17 -07001518 std::size_t dot = timestamp.find_first_of('.');
1519 std::size_t plus = timestamp.find_first_of('+');
Jason M. Bills95820182019-04-22 16:25:34 -07001520 if (dot != std::string::npos && plus != std::string::npos)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001521 {
Jason M. Bills95820182019-04-22 16:25:34 -07001522 timestamp.erase(dot, plus - dot);
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001523 }
1524
1525 // Fill in the log entry with the gathered data
Vijay Lobo9c11a172021-10-07 16:53:16 -05001526 logEntryJson["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
Ed Tanousef4c65b2023-04-24 15:28:50 -07001527 logEntryJson["@odata.id"] = boost::urls::format(
1528 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/{}",
1529 logEntryID);
Jason M. Bills84afc482022-06-24 12:38:23 -07001530 logEntryJson["Name"] = "System Event Log Entry";
1531 logEntryJson["Id"] = logEntryID;
1532 logEntryJson["Message"] = std::move(msg);
1533 logEntryJson["MessageId"] = std::move(messageID);
1534 logEntryJson["MessageArgs"] = messageArgs;
1535 logEntryJson["EntryType"] = "Event";
1536 logEntryJson["Severity"] = message->messageSeverity;
1537 logEntryJson["Created"] = std::move(timestamp);
Jason M. Billsac992cd2022-06-24 13:31:46 -07001538 return LogParseError::success;
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001539}
1540
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001541inline void requestRoutesJournalEventLogEntryCollection(App& app)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001542{
Ed Tanous22d268c2022-05-19 09:39:07 -07001543 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/")
Gunnar Mills8b6a35f2021-07-30 14:52:53 -05001544 .privileges(redfish::privileges::getLogEntryCollection)
Ed Tanous002d39b2022-05-31 08:59:27 -07001545 .methods(boost::beast::http::verb::get)(
1546 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07001547 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1548 const std::string& systemName) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001549 query_param::QueryCapabilities capabilities = {
1550 .canDelegateTop = true,
1551 .canDelegateSkip = true,
1552 };
1553 query_param::Query delegatedQuery;
1554 if (!redfish::setUpRedfishRouteWithDelegation(
Carson Labrado3ba00072022-06-06 19:40:56 +00001555 app, req, asyncResp, delegatedQuery, capabilities))
Ed Tanous002d39b2022-05-31 08:59:27 -07001556 {
1557 return;
1558 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08001559 if constexpr (bmcwebEnableMultiHost)
1560 {
1561 // Option currently returns no systems. TBD
1562 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1563 systemName);
1564 return;
1565 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001566 if (systemName != "system")
1567 {
1568 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1569 systemName);
1570 return;
1571 }
1572
Jiaqing Zhao5143f7a2022-07-22 09:33:33 +08001573 size_t top = delegatedQuery.top.value_or(query_param::Query::maxTop);
Ed Tanous3648c8b2022-07-25 13:39:59 -07001574 size_t skip = delegatedQuery.skip.value_or(0);
1575
Ed Tanous002d39b2022-05-31 08:59:27 -07001576 // Collections don't include the static data added by SubRoute
1577 // because it has a duplicate entry for members
1578 asyncResp->res.jsonValue["@odata.type"] =
1579 "#LogEntryCollection.LogEntryCollection";
1580 asyncResp->res.jsonValue["@odata.id"] =
1581 "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
1582 asyncResp->res.jsonValue["Name"] = "System Event Log Entries";
1583 asyncResp->res.jsonValue["Description"] =
1584 "Collection of System Event Log Entries";
1585
1586 nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
1587 logEntryArray = nlohmann::json::array();
1588 // Go through the log files and create a unique ID for each
1589 // entry
1590 std::vector<std::filesystem::path> redfishLogFiles;
1591 getRedfishLogFiles(redfishLogFiles);
1592 uint64_t entryCount = 0;
1593 std::string logEntry;
1594
1595 // Oldest logs are in the last file, so start there and loop
1596 // backwards
1597 for (auto it = redfishLogFiles.rbegin(); it < redfishLogFiles.rend();
1598 it++)
1599 {
1600 std::ifstream logStream(*it);
1601 if (!logStream.is_open())
Jason M. Bills4978b632022-02-22 14:17:43 -08001602 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001603 continue;
Jason M. Bills4978b632022-02-22 14:17:43 -08001604 }
Jason M. Bills897967d2019-07-29 17:05:30 -07001605
Ed Tanous002d39b2022-05-31 08:59:27 -07001606 // Reset the unique ID on the first entry
1607 bool firstEntry = true;
1608 while (std::getline(logStream, logEntry))
Jason M. Bills4978b632022-02-22 14:17:43 -08001609 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001610 std::string idStr;
1611 if (!getUniqueEntryID(logEntry, idStr, firstEntry))
Jason M. Bills4978b632022-02-22 14:17:43 -08001612 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001613 continue;
1614 }
Jason M. Billsefde4ec2022-06-24 08:59:52 -07001615 firstEntry = false;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001616
Jason M. Billsde703c52022-06-23 14:19:04 -07001617 nlohmann::json::object_t bmcLogEntry;
Patrick Williams89492a12023-05-10 07:51:34 -05001618 LogParseError status = fillEventLogEntryJson(idStr, logEntry,
1619 bmcLogEntry);
Jason M. Billsac992cd2022-06-24 13:31:46 -07001620 if (status == LogParseError::messageIdNotInRegistry)
1621 {
1622 continue;
1623 }
1624 if (status != LogParseError::success)
Ed Tanous002d39b2022-05-31 08:59:27 -07001625 {
1626 messages::internalError(asyncResp->res);
1627 return;
Andrew Geisslercb92c032018-08-17 07:56:14 -07001628 }
Jason M. Billsde703c52022-06-23 14:19:04 -07001629
Jason M. Billsde703c52022-06-23 14:19:04 -07001630 entryCount++;
1631 // Handle paging using skip (number of entries to skip from the
1632 // start) and top (number of entries to display)
Ed Tanous3648c8b2022-07-25 13:39:59 -07001633 if (entryCount <= skip || entryCount > skip + top)
Jason M. Billsde703c52022-06-23 14:19:04 -07001634 {
1635 continue;
1636 }
1637
Patrick Williamsb2ba3072023-05-12 10:27:39 -05001638 logEntryArray.emplace_back(std::move(bmcLogEntry));
Jason M. Bills4978b632022-02-22 14:17:43 -08001639 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001640 }
1641 asyncResp->res.jsonValue["Members@odata.count"] = entryCount;
Ed Tanous3648c8b2022-07-25 13:39:59 -07001642 if (skip + top < entryCount)
Ed Tanous002d39b2022-05-31 08:59:27 -07001643 {
1644 asyncResp->res.jsonValue["Members@odata.nextLink"] =
1645 "/redfish/v1/Systems/system/LogServices/EventLog/Entries?$skip=" +
Ed Tanous3648c8b2022-07-25 13:39:59 -07001646 std::to_string(skip + top);
Ed Tanous002d39b2022-05-31 08:59:27 -07001647 }
Patrick Williams5a39f772023-10-20 11:20:21 -05001648 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001649}
Chicago Duan336e96c2019-07-15 14:22:08 +08001650
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001651inline void requestRoutesJournalEventLogEntry(App& app)
1652{
1653 BMCWEB_ROUTE(
Ed Tanous22d268c2022-05-19 09:39:07 -07001654 app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001655 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001656 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07001657 [&app](const crow::Request& req,
1658 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07001659 const std::string& systemName, const std::string& param) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001660 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001661 {
1662 return;
1663 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08001664 if constexpr (bmcwebEnableMultiHost)
1665 {
1666 // Option currently returns no systems. TBD
1667 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1668 systemName);
1669 return;
1670 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001671
1672 if (systemName != "system")
1673 {
1674 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1675 systemName);
1676 return;
1677 }
1678
Ed Tanous002d39b2022-05-31 08:59:27 -07001679 const std::string& targetID = param;
1680
1681 // Go through the log files and check the unique ID for each
1682 // entry to find the target entry
1683 std::vector<std::filesystem::path> redfishLogFiles;
1684 getRedfishLogFiles(redfishLogFiles);
1685 std::string logEntry;
1686
1687 // Oldest logs are in the last file, so start there and loop
1688 // backwards
1689 for (auto it = redfishLogFiles.rbegin(); it < redfishLogFiles.rend();
1690 it++)
1691 {
1692 std::ifstream logStream(*it);
1693 if (!logStream.is_open())
1694 {
1695 continue;
1696 }
1697
1698 // Reset the unique ID on the first entry
1699 bool firstEntry = true;
1700 while (std::getline(logStream, logEntry))
1701 {
1702 std::string idStr;
1703 if (!getUniqueEntryID(logEntry, idStr, firstEntry))
Ed Tanous45ca1b82022-03-25 13:07:27 -07001704 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001705 continue;
1706 }
Jason M. Billsefde4ec2022-06-24 08:59:52 -07001707 firstEntry = false;
Ed Tanous002d39b2022-05-31 08:59:27 -07001708
1709 if (idStr == targetID)
1710 {
Jason M. Billsde703c52022-06-23 14:19:04 -07001711 nlohmann::json::object_t bmcLogEntry;
Jason M. Billsac992cd2022-06-24 13:31:46 -07001712 LogParseError status =
1713 fillEventLogEntryJson(idStr, logEntry, bmcLogEntry);
1714 if (status != LogParseError::success)
Ed Tanous002d39b2022-05-31 08:59:27 -07001715 {
1716 messages::internalError(asyncResp->res);
1717 return;
1718 }
Jason M. Billsd405bb52022-06-24 10:52:05 -07001719 asyncResp->res.jsonValue.update(bmcLogEntry);
Ed Tanous45ca1b82022-03-25 13:07:27 -07001720 return;
1721 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001722 }
1723 }
1724 // Requested ID was not found
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08001725 messages::resourceNotFound(asyncResp->res, "LogEntry", targetID);
Patrick Williams5a39f772023-10-20 11:20:21 -05001726 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001727}
1728
1729inline void requestRoutesDBusEventLogEntryCollection(App& app)
1730{
Ed Tanous22d268c2022-05-19 09:39:07 -07001731 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07001732 .privileges(redfish::privileges::getLogEntryCollection)
Ed Tanous002d39b2022-05-31 08:59:27 -07001733 .methods(boost::beast::http::verb::get)(
1734 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07001735 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1736 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001737 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001738 {
1739 return;
1740 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08001741 if constexpr (bmcwebEnableMultiHost)
1742 {
1743 // Option currently returns no systems. TBD
1744 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1745 systemName);
1746 return;
1747 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001748 if (systemName != "system")
1749 {
1750 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1751 systemName);
1752 return;
1753 }
1754
Ed Tanous002d39b2022-05-31 08:59:27 -07001755 // Collections don't include the static data added by SubRoute
1756 // because it has a duplicate entry for members
1757 asyncResp->res.jsonValue["@odata.type"] =
1758 "#LogEntryCollection.LogEntryCollection";
1759 asyncResp->res.jsonValue["@odata.id"] =
1760 "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
1761 asyncResp->res.jsonValue["Name"] = "System Event Log Entries";
1762 asyncResp->res.jsonValue["Description"] =
1763 "Collection of System Event Log Entries";
1764
1765 // DBus implementation of EventLog/Entries
1766 // Make call to Logging Service to find all log entry objects
George Liu5eb468d2023-06-20 17:03:24 +08001767 sdbusplus::message::object_path path("/xyz/openbmc_project/logging");
1768 dbus::utility::getManagedObjects(
1769 "xyz.openbmc_project.Logging", path,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001770 [asyncResp](const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07001771 const dbus::utility::ManagedObjectType& resp) {
1772 if (ec)
Ed Tanous45ca1b82022-03-25 13:07:27 -07001773 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001774 // TODO Handle for specific error code
Ed Tanous62598e32023-07-17 17:06:25 -07001775 BMCWEB_LOG_ERROR(
1776 "getLogEntriesIfaceData resp_handler got error {}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -07001777 messages::internalError(asyncResp->res);
Ed Tanous45ca1b82022-03-25 13:07:27 -07001778 return;
1779 }
Ed Tanous3544d2a2023-08-06 18:12:20 -07001780 nlohmann::json::array_t entriesArray;
Ed Tanous002d39b2022-05-31 08:59:27 -07001781 for (const auto& objectPath : resp)
1782 {
1783 const uint32_t* id = nullptr;
1784 const uint64_t* timestamp = nullptr;
1785 const uint64_t* updateTimestamp = nullptr;
1786 const std::string* severity = nullptr;
1787 const std::string* message = nullptr;
1788 const std::string* filePath = nullptr;
Vijay Lobo9c11a172021-10-07 16:53:16 -05001789 const std::string* resolution = nullptr;
Ed Tanous002d39b2022-05-31 08:59:27 -07001790 bool resolved = false;
Abhishek Patel9017faf2021-09-14 22:48:55 -05001791 const std::string* notify = nullptr;
1792
Ed Tanous002d39b2022-05-31 08:59:27 -07001793 for (const auto& interfaceMap : objectPath.second)
1794 {
1795 if (interfaceMap.first ==
1796 "xyz.openbmc_project.Logging.Entry")
Xiaochao Ma75710de2021-01-21 17:56:02 +08001797 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001798 for (const auto& propertyMap : interfaceMap.second)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001799 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001800 if (propertyMap.first == "Id")
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001801 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001802 id = std::get_if<uint32_t>(&propertyMap.second);
1803 }
1804 else if (propertyMap.first == "Timestamp")
1805 {
1806 timestamp =
1807 std::get_if<uint64_t>(&propertyMap.second);
1808 }
1809 else if (propertyMap.first == "UpdateTimestamp")
1810 {
1811 updateTimestamp =
1812 std::get_if<uint64_t>(&propertyMap.second);
1813 }
1814 else if (propertyMap.first == "Severity")
1815 {
1816 severity = std::get_if<std::string>(
1817 &propertyMap.second);
1818 }
Vijay Lobo9c11a172021-10-07 16:53:16 -05001819 else if (propertyMap.first == "Resolution")
1820 {
1821 resolution = std::get_if<std::string>(
1822 &propertyMap.second);
1823 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001824 else if (propertyMap.first == "Message")
1825 {
1826 message = std::get_if<std::string>(
1827 &propertyMap.second);
1828 }
1829 else if (propertyMap.first == "Resolved")
1830 {
1831 const bool* resolveptr =
1832 std::get_if<bool>(&propertyMap.second);
1833 if (resolveptr == nullptr)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001834 {
1835 messages::internalError(asyncResp->res);
1836 return;
1837 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001838 resolved = *resolveptr;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001839 }
Abhishek Patel9017faf2021-09-14 22:48:55 -05001840 else if (propertyMap.first ==
1841 "ServiceProviderNotify")
1842 {
1843 notify = std::get_if<std::string>(
1844 &propertyMap.second);
1845 if (notify == nullptr)
1846 {
1847 messages::internalError(asyncResp->res);
1848 return;
1849 }
1850 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001851 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001852 if (id == nullptr || message == nullptr ||
Ed Tanous002d39b2022-05-31 08:59:27 -07001853 severity == nullptr)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001854 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001855 messages::internalError(asyncResp->res);
1856 return;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001857 }
1858 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001859 else if (interfaceMap.first ==
1860 "xyz.openbmc_project.Common.FilePath")
1861 {
1862 for (const auto& propertyMap : interfaceMap.second)
1863 {
1864 if (propertyMap.first == "Path")
1865 {
1866 filePath = std::get_if<std::string>(
1867 &propertyMap.second);
1868 }
1869 }
1870 }
1871 }
1872 // Object path without the
1873 // xyz.openbmc_project.Logging.Entry interface, ignore
1874 // and continue.
1875 if (id == nullptr || message == nullptr ||
1876 severity == nullptr || timestamp == nullptr ||
1877 updateTimestamp == nullptr)
1878 {
1879 continue;
1880 }
Ed Tanous3544d2a2023-08-06 18:12:20 -07001881 nlohmann::json& thisEntry = entriesArray.emplace_back();
Vijay Lobo9c11a172021-10-07 16:53:16 -05001882 thisEntry["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
Ed Tanousef4c65b2023-04-24 15:28:50 -07001883 thisEntry["@odata.id"] = boost::urls::format(
1884 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/{}",
1885 std::to_string(*id));
Ed Tanous002d39b2022-05-31 08:59:27 -07001886 thisEntry["Name"] = "System Event Log Entry";
1887 thisEntry["Id"] = std::to_string(*id);
1888 thisEntry["Message"] = *message;
1889 thisEntry["Resolved"] = resolved;
Vijay Lobo9c11a172021-10-07 16:53:16 -05001890 if ((resolution != nullptr) && (!(*resolution).empty()))
1891 {
1892 thisEntry["Resolution"] = *resolution;
1893 }
Abhishek Patel9017faf2021-09-14 22:48:55 -05001894 std::optional<bool> notifyAction =
1895 getProviderNotifyAction(*notify);
1896 if (notifyAction)
1897 {
1898 thisEntry["ServiceProviderNotified"] = *notifyAction;
1899 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001900 thisEntry["EntryType"] = "Event";
1901 thisEntry["Severity"] =
1902 translateSeverityDbusToRedfish(*severity);
1903 thisEntry["Created"] =
Ed Tanous2b829372022-08-03 14:22:34 -07001904 redfish::time_utils::getDateTimeUintMs(*timestamp);
Ed Tanous002d39b2022-05-31 08:59:27 -07001905 thisEntry["Modified"] =
Ed Tanous2b829372022-08-03 14:22:34 -07001906 redfish::time_utils::getDateTimeUintMs(*updateTimestamp);
Ed Tanous002d39b2022-05-31 08:59:27 -07001907 if (filePath != nullptr)
1908 {
1909 thisEntry["AdditionalDataURI"] =
1910 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
1911 std::to_string(*id) + "/attachment";
1912 }
1913 }
Ed Tanous3544d2a2023-08-06 18:12:20 -07001914 std::ranges::sort(entriesArray, [](const nlohmann::json& left,
1915 const nlohmann::json& right) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001916 return (left["Id"] <= right["Id"]);
Ed Tanous3544d2a2023-08-06 18:12:20 -07001917 });
Ed Tanous002d39b2022-05-31 08:59:27 -07001918 asyncResp->res.jsonValue["Members@odata.count"] =
1919 entriesArray.size();
Ed Tanous3544d2a2023-08-06 18:12:20 -07001920 asyncResp->res.jsonValue["Members"] = std::move(entriesArray);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001921 });
Patrick Williams5a39f772023-10-20 11:20:21 -05001922 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001923}
Xiaochao Ma75710de2021-01-21 17:56:02 +08001924
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001925inline void requestRoutesDBusEventLogEntry(App& app)
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001926{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001927 BMCWEB_ROUTE(
Ed Tanous22d268c2022-05-19 09:39:07 -07001928 app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001929 .privileges(redfish::privileges::getLogEntry)
Ed Tanous002d39b2022-05-31 08:59:27 -07001930 .methods(boost::beast::http::verb::get)(
1931 [&app](const crow::Request& req,
1932 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07001933 const std::string& systemName, const std::string& param) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001934 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001935 {
1936 return;
1937 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08001938 if constexpr (bmcwebEnableMultiHost)
1939 {
1940 // Option currently returns no systems. TBD
1941 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1942 systemName);
1943 return;
1944 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001945 if (systemName != "system")
1946 {
1947 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1948 systemName);
1949 return;
1950 }
1951
Ed Tanous002d39b2022-05-31 08:59:27 -07001952 std::string entryID = param;
1953 dbus::utility::escapePathForDbus(entryID);
1954
1955 // DBus implementation of EventLog/Entries
1956 // Make call to Logging Service to find all log entry objects
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001957 sdbusplus::asio::getAllProperties(
1958 *crow::connections::systemBus, "xyz.openbmc_project.Logging",
1959 "/xyz/openbmc_project/logging/entry/" + entryID, "",
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001960 [asyncResp, entryID](const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07001961 const dbus::utility::DBusPropertiesMap& resp) {
1962 if (ec.value() == EBADR)
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001963 {
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001964 messages::resourceNotFound(asyncResp->res, "EventLogEntry",
1965 entryID);
Ed Tanous45ca1b82022-03-25 13:07:27 -07001966 return;
1967 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001968 if (ec)
1969 {
Ed Tanous62598e32023-07-17 17:06:25 -07001970 BMCWEB_LOG_ERROR(
1971 "EventLogEntry (DBus) resp_handler got error {}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -07001972 messages::internalError(asyncResp->res);
1973 return;
1974 }
1975 const uint32_t* id = nullptr;
1976 const uint64_t* timestamp = nullptr;
1977 const uint64_t* updateTimestamp = nullptr;
1978 const std::string* severity = nullptr;
1979 const std::string* message = nullptr;
1980 const std::string* filePath = nullptr;
Vijay Lobo9c11a172021-10-07 16:53:16 -05001981 const std::string* resolution = nullptr;
Ed Tanous002d39b2022-05-31 08:59:27 -07001982 bool resolved = false;
Abhishek Patel9017faf2021-09-14 22:48:55 -05001983 const std::string* notify = nullptr;
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001984
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001985 const bool success = sdbusplus::unpackPropertiesNoThrow(
1986 dbus_utils::UnpackErrorPrinter(), resp, "Id", id, "Timestamp",
1987 timestamp, "UpdateTimestamp", updateTimestamp, "Severity",
Vijay Lobo9c11a172021-10-07 16:53:16 -05001988 severity, "Message", message, "Resolved", resolved,
Abhishek Patel9017faf2021-09-14 22:48:55 -05001989 "Resolution", resolution, "Path", filePath,
1990 "ServiceProviderNotify", notify);
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001991
1992 if (!success)
Ed Tanous002d39b2022-05-31 08:59:27 -07001993 {
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001994 messages::internalError(asyncResp->res);
1995 return;
Ed Tanous002d39b2022-05-31 08:59:27 -07001996 }
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001997
Ed Tanous002d39b2022-05-31 08:59:27 -07001998 if (id == nullptr || message == nullptr || severity == nullptr ||
Abhishek Patel9017faf2021-09-14 22:48:55 -05001999 timestamp == nullptr || updateTimestamp == nullptr ||
2000 notify == nullptr)
Ed Tanous002d39b2022-05-31 08:59:27 -07002001 {
2002 messages::internalError(asyncResp->res);
2003 return;
2004 }
Abhishek Patel9017faf2021-09-14 22:48:55 -05002005
Ed Tanous002d39b2022-05-31 08:59:27 -07002006 asyncResp->res.jsonValue["@odata.type"] =
Vijay Lobo9c11a172021-10-07 16:53:16 -05002007 "#LogEntry.v1_9_0.LogEntry";
Ed Tanousef4c65b2023-04-24 15:28:50 -07002008 asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
2009 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/{}",
2010 std::to_string(*id));
Ed Tanous002d39b2022-05-31 08:59:27 -07002011 asyncResp->res.jsonValue["Name"] = "System Event Log Entry";
2012 asyncResp->res.jsonValue["Id"] = std::to_string(*id);
2013 asyncResp->res.jsonValue["Message"] = *message;
2014 asyncResp->res.jsonValue["Resolved"] = resolved;
Abhishek Patel9017faf2021-09-14 22:48:55 -05002015 std::optional<bool> notifyAction = getProviderNotifyAction(*notify);
2016 if (notifyAction)
2017 {
2018 asyncResp->res.jsonValue["ServiceProviderNotified"] =
2019 *notifyAction;
2020 }
Vijay Lobo9c11a172021-10-07 16:53:16 -05002021 if ((resolution != nullptr) && (!(*resolution).empty()))
2022 {
2023 asyncResp->res.jsonValue["Resolution"] = *resolution;
2024 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002025 asyncResp->res.jsonValue["EntryType"] = "Event";
2026 asyncResp->res.jsonValue["Severity"] =
2027 translateSeverityDbusToRedfish(*severity);
2028 asyncResp->res.jsonValue["Created"] =
Ed Tanous2b829372022-08-03 14:22:34 -07002029 redfish::time_utils::getDateTimeUintMs(*timestamp);
Ed Tanous002d39b2022-05-31 08:59:27 -07002030 asyncResp->res.jsonValue["Modified"] =
Ed Tanous2b829372022-08-03 14:22:34 -07002031 redfish::time_utils::getDateTimeUintMs(*updateTimestamp);
Ed Tanous002d39b2022-05-31 08:59:27 -07002032 if (filePath != nullptr)
2033 {
2034 asyncResp->res.jsonValue["AdditionalDataURI"] =
2035 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
2036 std::to_string(*id) + "/attachment";
2037 }
Ed Tanous45ca1b82022-03-25 13:07:27 -07002038 });
Patrick Williams5a39f772023-10-20 11:20:21 -05002039 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002040
2041 BMCWEB_ROUTE(
Ed Tanous22d268c2022-05-19 09:39:07 -07002042 app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002043 .privileges(redfish::privileges::patchLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002044 .methods(boost::beast::http::verb::patch)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002045 [&app](const crow::Request& req,
2046 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07002047 const std::string& systemName, const std::string& entryId) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002048 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002049 {
2050 return;
2051 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08002052 if constexpr (bmcwebEnableMultiHost)
2053 {
2054 // Option currently returns no systems. TBD
2055 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
2056 systemName);
2057 return;
2058 }
Ed Tanous22d268c2022-05-19 09:39:07 -07002059 if (systemName != "system")
2060 {
2061 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
2062 systemName);
2063 return;
2064 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002065 std::optional<bool> resolved;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002066
Ed Tanous002d39b2022-05-31 08:59:27 -07002067 if (!json_util::readJsonPatch(req, asyncResp->res, "Resolved",
2068 resolved))
2069 {
2070 return;
2071 }
Ed Tanous62598e32023-07-17 17:06:25 -07002072 BMCWEB_LOG_DEBUG("Set Resolved");
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06002073
Asmitha Karunanithi3eb66652024-04-02 16:34:36 +00002074 setDbusProperty(asyncResp, "xyz.openbmc_project.Logging",
2075 "/xyz/openbmc_project/logging/entry/" + entryId,
2076 "xyz.openbmc_project.Logging.Entry", "Resolved",
2077 "Resolved", *resolved);
Patrick Williams5a39f772023-10-20 11:20:21 -05002078 });
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06002079
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002080 BMCWEB_ROUTE(
Ed Tanous22d268c2022-05-19 09:39:07 -07002081 app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002082 .privileges(redfish::privileges::deleteLogEntry)
2083
Ed Tanous002d39b2022-05-31 08:59:27 -07002084 .methods(boost::beast::http::verb::delete_)(
2085 [&app](const crow::Request& req,
2086 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07002087 const std::string& systemName, const std::string& param) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002088 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002089 {
2090 return;
2091 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08002092 if constexpr (bmcwebEnableMultiHost)
2093 {
2094 // Option currently returns no systems. TBD
2095 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
2096 systemName);
2097 return;
2098 }
Ed Tanous22d268c2022-05-19 09:39:07 -07002099 if (systemName != "system")
2100 {
2101 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
2102 systemName);
2103 return;
2104 }
Ed Tanous62598e32023-07-17 17:06:25 -07002105 BMCWEB_LOG_DEBUG("Do delete single event entries.");
Ed Tanous002d39b2022-05-31 08:59:27 -07002106
2107 std::string entryID = param;
2108
2109 dbus::utility::escapePathForDbus(entryID);
2110
2111 // Process response from Logging service.
Patrick Williams5a39f772023-10-20 11:20:21 -05002112 auto respHandler = [asyncResp,
2113 entryID](const boost::system::error_code& ec) {
Ed Tanous62598e32023-07-17 17:06:25 -07002114 BMCWEB_LOG_DEBUG("EventLogEntry (DBus) doDelete callback: Done");
Ed Tanous002d39b2022-05-31 08:59:27 -07002115 if (ec)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002116 {
Ed Tanous002d39b2022-05-31 08:59:27 -07002117 if (ec.value() == EBADR)
Ed Tanous45ca1b82022-03-25 13:07:27 -07002118 {
Ed Tanous002d39b2022-05-31 08:59:27 -07002119 messages::resourceNotFound(asyncResp->res, "LogEntry",
2120 entryID);
Ed Tanous45ca1b82022-03-25 13:07:27 -07002121 return;
2122 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002123 // TODO Handle for specific error code
Ed Tanous62598e32023-07-17 17:06:25 -07002124 BMCWEB_LOG_ERROR(
2125 "EventLogEntry (DBus) doDelete respHandler got error {}",
2126 ec);
Ed Tanous002d39b2022-05-31 08:59:27 -07002127 asyncResp->res.result(
2128 boost::beast::http::status::internal_server_error);
2129 return;
2130 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002131
Ed Tanous002d39b2022-05-31 08:59:27 -07002132 asyncResp->res.result(boost::beast::http::status::ok);
2133 };
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002134
Ed Tanous002d39b2022-05-31 08:59:27 -07002135 // Make call to Logging service to request Delete Log
2136 crow::connections::systemBus->async_method_call(
2137 respHandler, "xyz.openbmc_project.Logging",
2138 "/xyz/openbmc_project/logging/entry/" + entryID,
2139 "xyz.openbmc_project.Object.Delete", "Delete");
Patrick Williams5a39f772023-10-20 11:20:21 -05002140 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002141}
2142
Spencer Kub7028eb2021-10-26 15:27:35 +08002143constexpr const char* hostLoggerFolderPath = "/var/log/console";
2144
2145inline bool
2146 getHostLoggerFiles(const std::string& hostLoggerFilePath,
2147 std::vector<std::filesystem::path>& hostLoggerFiles)
2148{
2149 std::error_code ec;
2150 std::filesystem::directory_iterator logPath(hostLoggerFilePath, ec);
2151 if (ec)
2152 {
Carson Labradobf2dded2023-08-10 00:37:06 +00002153 BMCWEB_LOG_WARNING("{}", ec.message());
Spencer Kub7028eb2021-10-26 15:27:35 +08002154 return false;
2155 }
2156 for (const std::filesystem::directory_entry& it : logPath)
2157 {
2158 std::string filename = it.path().filename();
2159 // Prefix of each log files is "log". Find the file and save the
2160 // path
Ed Tanous11ba3972022-07-11 09:50:41 -07002161 if (filename.starts_with("log"))
Spencer Kub7028eb2021-10-26 15:27:35 +08002162 {
2163 hostLoggerFiles.emplace_back(it.path());
2164 }
2165 }
2166 // As the log files rotate, they are appended with a ".#" that is higher for
2167 // the older logs. Since we start from oldest logs, sort the name in
2168 // descending order.
2169 std::sort(hostLoggerFiles.rbegin(), hostLoggerFiles.rend(),
2170 AlphanumLess<std::string>());
2171
2172 return true;
2173}
2174
Ed Tanous02cad962022-06-30 16:50:15 -07002175inline bool getHostLoggerEntries(
2176 const std::vector<std::filesystem::path>& hostLoggerFiles, uint64_t skip,
2177 uint64_t top, std::vector<std::string>& logEntries, size_t& logCount)
Spencer Kub7028eb2021-10-26 15:27:35 +08002178{
2179 GzFileReader logFile;
2180
2181 // Go though all log files and expose host logs.
2182 for (const std::filesystem::path& it : hostLoggerFiles)
2183 {
2184 if (!logFile.gzGetLines(it.string(), skip, top, logEntries, logCount))
2185 {
Ed Tanous62598e32023-07-17 17:06:25 -07002186 BMCWEB_LOG_ERROR("fail to expose host logs");
Spencer Kub7028eb2021-10-26 15:27:35 +08002187 return false;
2188 }
2189 }
2190 // Get lastMessage from constructor by getter
2191 std::string lastMessage = logFile.getLastMessage();
2192 if (!lastMessage.empty())
2193 {
2194 logCount++;
2195 if (logCount > skip && logCount <= (skip + top))
2196 {
2197 logEntries.push_back(lastMessage);
2198 }
2199 }
2200 return true;
2201}
2202
Ed Tanous6f056f22024-04-07 13:35:51 -07002203inline void fillHostLoggerEntryJson(std::string_view logEntryID,
2204 std::string_view msg,
Jason M. Bills6d6574c2022-06-28 12:30:16 -07002205 nlohmann::json::object_t& logEntryJson)
Spencer Kub7028eb2021-10-26 15:27:35 +08002206{
2207 // Fill in the log entry with the gathered data.
Vijay Lobo9c11a172021-10-07 16:53:16 -05002208 logEntryJson["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
Ed Tanousef4c65b2023-04-24 15:28:50 -07002209 logEntryJson["@odata.id"] = boost::urls::format(
2210 "/redfish/v1/Systems/system/LogServices/HostLogger/Entries/{}",
2211 logEntryID);
Jason M. Bills6d6574c2022-06-28 12:30:16 -07002212 logEntryJson["Name"] = "Host Logger Entry";
2213 logEntryJson["Id"] = logEntryID;
2214 logEntryJson["Message"] = msg;
2215 logEntryJson["EntryType"] = "Oem";
2216 logEntryJson["Severity"] = "OK";
2217 logEntryJson["OemRecordFormat"] = "Host Logger Entry";
Spencer Kub7028eb2021-10-26 15:27:35 +08002218}
2219
2220inline void requestRoutesSystemHostLogger(App& app)
2221{
Ed Tanous22d268c2022-05-19 09:39:07 -07002222 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/HostLogger/")
Spencer Kub7028eb2021-10-26 15:27:35 +08002223 .privileges(redfish::privileges::getLogService)
Ed Tanous14766872022-03-15 10:44:42 -07002224 .methods(boost::beast::http::verb::get)(
2225 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07002226 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2227 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002228 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002229 {
2230 return;
2231 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08002232 if constexpr (bmcwebEnableMultiHost)
2233 {
2234 // Option currently returns no systems. TBD
2235 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
2236 systemName);
2237 return;
2238 }
Ed Tanous22d268c2022-05-19 09:39:07 -07002239 if (systemName != "system")
2240 {
2241 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
2242 systemName);
2243 return;
2244 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002245 asyncResp->res.jsonValue["@odata.id"] =
2246 "/redfish/v1/Systems/system/LogServices/HostLogger";
2247 asyncResp->res.jsonValue["@odata.type"] =
Janet Adkinsb25644a2023-08-16 11:23:45 -05002248 "#LogService.v1_2_0.LogService";
Ed Tanous002d39b2022-05-31 08:59:27 -07002249 asyncResp->res.jsonValue["Name"] = "Host Logger Service";
2250 asyncResp->res.jsonValue["Description"] = "Host Logger Service";
2251 asyncResp->res.jsonValue["Id"] = "HostLogger";
2252 asyncResp->res.jsonValue["Entries"]["@odata.id"] =
2253 "/redfish/v1/Systems/system/LogServices/HostLogger/Entries";
Patrick Williams5a39f772023-10-20 11:20:21 -05002254 });
Spencer Kub7028eb2021-10-26 15:27:35 +08002255}
2256
2257inline void requestRoutesSystemHostLoggerCollection(App& app)
2258{
2259 BMCWEB_ROUTE(app,
Ed Tanous22d268c2022-05-19 09:39:07 -07002260 "/redfish/v1/Systems/<str>/LogServices/HostLogger/Entries/")
Spencer Kub7028eb2021-10-26 15:27:35 +08002261 .privileges(redfish::privileges::getLogEntry)
Ed Tanous002d39b2022-05-31 08:59:27 -07002262 .methods(boost::beast::http::verb::get)(
2263 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07002264 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2265 const std::string& systemName) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002266 query_param::QueryCapabilities capabilities = {
2267 .canDelegateTop = true,
2268 .canDelegateSkip = true,
2269 };
2270 query_param::Query delegatedQuery;
2271 if (!redfish::setUpRedfishRouteWithDelegation(
Carson Labrado3ba00072022-06-06 19:40:56 +00002272 app, req, asyncResp, delegatedQuery, capabilities))
Ed Tanous002d39b2022-05-31 08:59:27 -07002273 {
2274 return;
2275 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08002276 if constexpr (bmcwebEnableMultiHost)
2277 {
2278 // Option currently returns no systems. TBD
2279 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
2280 systemName);
2281 return;
2282 }
Ed Tanous22d268c2022-05-19 09:39:07 -07002283 if (systemName != "system")
2284 {
2285 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
2286 systemName);
2287 return;
2288 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002289 asyncResp->res.jsonValue["@odata.id"] =
2290 "/redfish/v1/Systems/system/LogServices/HostLogger/Entries";
2291 asyncResp->res.jsonValue["@odata.type"] =
2292 "#LogEntryCollection.LogEntryCollection";
2293 asyncResp->res.jsonValue["Name"] = "HostLogger Entries";
2294 asyncResp->res.jsonValue["Description"] =
2295 "Collection of HostLogger Entries";
2296 nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
2297 logEntryArray = nlohmann::json::array();
2298 asyncResp->res.jsonValue["Members@odata.count"] = 0;
Spencer Kub7028eb2021-10-26 15:27:35 +08002299
Ed Tanous002d39b2022-05-31 08:59:27 -07002300 std::vector<std::filesystem::path> hostLoggerFiles;
2301 if (!getHostLoggerFiles(hostLoggerFolderPath, hostLoggerFiles))
2302 {
Carson Labradobf2dded2023-08-10 00:37:06 +00002303 BMCWEB_LOG_DEBUG("Failed to get host log file path");
Ed Tanous002d39b2022-05-31 08:59:27 -07002304 return;
2305 }
Ed Tanous3648c8b2022-07-25 13:39:59 -07002306 // If we weren't provided top and skip limits, use the defaults.
2307 size_t skip = delegatedQuery.skip.value_or(0);
Jiaqing Zhao5143f7a2022-07-22 09:33:33 +08002308 size_t top = delegatedQuery.top.value_or(query_param::Query::maxTop);
Ed Tanous002d39b2022-05-31 08:59:27 -07002309 size_t logCount = 0;
2310 // This vector only store the entries we want to expose that
2311 // control by skip and top.
2312 std::vector<std::string> logEntries;
Ed Tanous3648c8b2022-07-25 13:39:59 -07002313 if (!getHostLoggerEntries(hostLoggerFiles, skip, top, logEntries,
2314 logCount))
Ed Tanous002d39b2022-05-31 08:59:27 -07002315 {
2316 messages::internalError(asyncResp->res);
2317 return;
2318 }
2319 // If vector is empty, that means skip value larger than total
2320 // log count
2321 if (logEntries.empty())
2322 {
2323 asyncResp->res.jsonValue["Members@odata.count"] = logCount;
2324 return;
2325 }
2326 if (!logEntries.empty())
2327 {
2328 for (size_t i = 0; i < logEntries.size(); i++)
George Liu0fda0f12021-11-16 10:06:17 +08002329 {
Jason M. Bills6d6574c2022-06-28 12:30:16 -07002330 nlohmann::json::object_t hostLogEntry;
Ed Tanous3648c8b2022-07-25 13:39:59 -07002331 fillHostLoggerEntryJson(std::to_string(skip + i), logEntries[i],
2332 hostLogEntry);
Patrick Williamsb2ba3072023-05-12 10:27:39 -05002333 logEntryArray.emplace_back(std::move(hostLogEntry));
George Liu0fda0f12021-11-16 10:06:17 +08002334 }
2335
Ed Tanous002d39b2022-05-31 08:59:27 -07002336 asyncResp->res.jsonValue["Members@odata.count"] = logCount;
Ed Tanous3648c8b2022-07-25 13:39:59 -07002337 if (skip + top < logCount)
George Liu0fda0f12021-11-16 10:06:17 +08002338 {
Ed Tanous002d39b2022-05-31 08:59:27 -07002339 asyncResp->res.jsonValue["Members@odata.nextLink"] =
2340 "/redfish/v1/Systems/system/LogServices/HostLogger/Entries?$skip=" +
Ed Tanous3648c8b2022-07-25 13:39:59 -07002341 std::to_string(skip + top);
George Liu0fda0f12021-11-16 10:06:17 +08002342 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002343 }
Patrick Williams5a39f772023-10-20 11:20:21 -05002344 });
Spencer Kub7028eb2021-10-26 15:27:35 +08002345}
2346
2347inline void requestRoutesSystemHostLoggerLogEntry(App& app)
2348{
2349 BMCWEB_ROUTE(
Ed Tanous22d268c2022-05-19 09:39:07 -07002350 app, "/redfish/v1/Systems/<str>/LogServices/HostLogger/Entries/<str>/")
Spencer Kub7028eb2021-10-26 15:27:35 +08002351 .privileges(redfish::privileges::getLogEntry)
2352 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002353 [&app](const crow::Request& req,
2354 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07002355 const std::string& systemName, const std::string& param) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002356 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002357 {
2358 return;
2359 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08002360 if constexpr (bmcwebEnableMultiHost)
2361 {
2362 // Option currently returns no systems. TBD
2363 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
2364 systemName);
2365 return;
2366 }
Ed Tanous22d268c2022-05-19 09:39:07 -07002367 if (systemName != "system")
2368 {
2369 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
2370 systemName);
2371 return;
2372 }
Ed Tanous6f056f22024-04-07 13:35:51 -07002373 std::string_view targetID = param;
Spencer Kub7028eb2021-10-26 15:27:35 +08002374
Ed Tanous002d39b2022-05-31 08:59:27 -07002375 uint64_t idInt = 0;
Ed Tanousca45aa32022-01-07 09:28:45 -08002376
Ed Tanous6f056f22024-04-07 13:35:51 -07002377 auto [ptr, ec] = std::from_chars(targetID.begin(), targetID.end(),
Patrick Williams84396af2023-05-11 11:47:45 -05002378 idInt);
Ed Tanous6f056f22024-04-07 13:35:51 -07002379 if (ec != std::errc{} || ptr != targetID.end())
Ed Tanous002d39b2022-05-31 08:59:27 -07002380 {
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08002381 messages::resourceNotFound(asyncResp->res, "LogEntry", param);
Ed Tanous002d39b2022-05-31 08:59:27 -07002382 return;
2383 }
Spencer Kub7028eb2021-10-26 15:27:35 +08002384
Ed Tanous002d39b2022-05-31 08:59:27 -07002385 std::vector<std::filesystem::path> hostLoggerFiles;
2386 if (!getHostLoggerFiles(hostLoggerFolderPath, hostLoggerFiles))
2387 {
Carson Labradobf2dded2023-08-10 00:37:06 +00002388 BMCWEB_LOG_DEBUG("Failed to get host log file path");
Ed Tanous002d39b2022-05-31 08:59:27 -07002389 return;
2390 }
Spencer Kub7028eb2021-10-26 15:27:35 +08002391
Ed Tanous002d39b2022-05-31 08:59:27 -07002392 size_t logCount = 0;
Ed Tanous3648c8b2022-07-25 13:39:59 -07002393 size_t top = 1;
Ed Tanous002d39b2022-05-31 08:59:27 -07002394 std::vector<std::string> logEntries;
2395 // We can get specific entry by skip and top. For example, if we
2396 // want to get nth entry, we can set skip = n-1 and top = 1 to
2397 // get that entry
2398 if (!getHostLoggerEntries(hostLoggerFiles, idInt, top, logEntries,
2399 logCount))
2400 {
2401 messages::internalError(asyncResp->res);
2402 return;
2403 }
Spencer Kub7028eb2021-10-26 15:27:35 +08002404
Ed Tanous002d39b2022-05-31 08:59:27 -07002405 if (!logEntries.empty())
2406 {
Jason M. Bills6d6574c2022-06-28 12:30:16 -07002407 nlohmann::json::object_t hostLogEntry;
2408 fillHostLoggerEntryJson(targetID, logEntries[0], hostLogEntry);
2409 asyncResp->res.jsonValue.update(hostLogEntry);
Ed Tanous002d39b2022-05-31 08:59:27 -07002410 return;
2411 }
Spencer Kub7028eb2021-10-26 15:27:35 +08002412
Ed Tanous002d39b2022-05-31 08:59:27 -07002413 // Requested ID was not found
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08002414 messages::resourceNotFound(asyncResp->res, "LogEntry", param);
Patrick Williams5a39f772023-10-20 11:20:21 -05002415 });
Spencer Kub7028eb2021-10-26 15:27:35 +08002416}
2417
Claire Weinandd72e872022-08-15 14:20:06 -07002418inline void handleBMCLogServicesCollectionGet(
Claire Weinanfdd26902022-03-01 14:18:25 -08002419 crow::App& app, const crow::Request& req,
2420 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2421{
2422 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2423 {
2424 return;
2425 }
2426 // Collections don't include the static data added by SubRoute
2427 // because it has a duplicate entry for members
2428 asyncResp->res.jsonValue["@odata.type"] =
2429 "#LogServiceCollection.LogServiceCollection";
2430 asyncResp->res.jsonValue["@odata.id"] =
2431 "/redfish/v1/Managers/bmc/LogServices";
2432 asyncResp->res.jsonValue["Name"] = "Open BMC Log Services Collection";
2433 asyncResp->res.jsonValue["Description"] =
2434 "Collection of LogServices for this Manager";
2435 nlohmann::json& logServiceArray = asyncResp->res.jsonValue["Members"];
2436 logServiceArray = nlohmann::json::array();
2437
2438#ifdef BMCWEB_ENABLE_REDFISH_BMC_JOURNAL
Ed Tanous613dabe2022-07-09 11:17:36 -07002439 nlohmann::json::object_t journal;
2440 journal["@odata.id"] = "/redfish/v1/Managers/bmc/LogServices/Journal";
Patrick Williamsb2ba3072023-05-12 10:27:39 -05002441 logServiceArray.emplace_back(std::move(journal));
Claire Weinanfdd26902022-03-01 14:18:25 -08002442#endif
2443
2444 asyncResp->res.jsonValue["Members@odata.count"] = logServiceArray.size();
2445
2446#ifdef BMCWEB_ENABLE_REDFISH_DUMP_LOG
George Liu15912152023-01-11 10:18:18 +08002447 constexpr std::array<std::string_view, 1> interfaces = {
George Liu7a1dbc42022-12-07 16:03:22 +08002448 "xyz.openbmc_project.Collection.DeleteAll"};
2449 dbus::utility::getSubTreePaths(
2450 "/xyz/openbmc_project/dump", 0, interfaces,
Claire Weinanfdd26902022-03-01 14:18:25 -08002451 [asyncResp](
George Liu7a1dbc42022-12-07 16:03:22 +08002452 const boost::system::error_code& ec,
Claire Weinanfdd26902022-03-01 14:18:25 -08002453 const dbus::utility::MapperGetSubTreePathsResponse& subTreePaths) {
2454 if (ec)
2455 {
Ed Tanous62598e32023-07-17 17:06:25 -07002456 BMCWEB_LOG_ERROR(
2457 "handleBMCLogServicesCollectionGet respHandler got error {}",
2458 ec);
Claire Weinanfdd26902022-03-01 14:18:25 -08002459 // Assume that getting an error simply means there are no dump
2460 // LogServices. Return without adding any error response.
2461 return;
2462 }
2463
2464 nlohmann::json& logServiceArrayLocal =
2465 asyncResp->res.jsonValue["Members"];
2466
2467 for (const std::string& path : subTreePaths)
2468 {
2469 if (path == "/xyz/openbmc_project/dump/bmc")
2470 {
Ed Tanous613dabe2022-07-09 11:17:36 -07002471 nlohmann::json::object_t member;
2472 member["@odata.id"] =
2473 "/redfish/v1/Managers/bmc/LogServices/Dump";
Patrick Williamsb2ba3072023-05-12 10:27:39 -05002474 logServiceArrayLocal.emplace_back(std::move(member));
Claire Weinanfdd26902022-03-01 14:18:25 -08002475 }
2476 else if (path == "/xyz/openbmc_project/dump/faultlog")
2477 {
Ed Tanous613dabe2022-07-09 11:17:36 -07002478 nlohmann::json::object_t member;
2479 member["@odata.id"] =
2480 "/redfish/v1/Managers/bmc/LogServices/FaultLog";
Patrick Williamsb2ba3072023-05-12 10:27:39 -05002481 logServiceArrayLocal.emplace_back(std::move(member));
Claire Weinanfdd26902022-03-01 14:18:25 -08002482 }
2483 }
2484
2485 asyncResp->res.jsonValue["Members@odata.count"] =
2486 logServiceArrayLocal.size();
Patrick Williams5a39f772023-10-20 11:20:21 -05002487 });
Claire Weinanfdd26902022-03-01 14:18:25 -08002488#endif
2489}
2490
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002491inline void requestRoutesBMCLogServiceCollection(App& app)
2492{
2493 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/")
Gunnar Millsad89dcf2021-07-30 14:40:11 -05002494 .privileges(redfish::privileges::getLogServiceCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002495 .methods(boost::beast::http::verb::get)(
Claire Weinandd72e872022-08-15 14:20:06 -07002496 std::bind_front(handleBMCLogServicesCollectionGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002497}
Ed Tanous1da66f72018-07-27 16:13:37 -07002498
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002499inline void requestRoutesBMCJournalLogService(App& app)
Ed Tanous1da66f72018-07-27 16:13:37 -07002500{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002501 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Journal/")
Ed Tanoused398212021-06-09 17:05:54 -07002502 .privileges(redfish::privileges::getLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002503 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002504 [&app](const crow::Request& req,
2505 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002506 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002507 {
2508 return;
2509 }
2510 asyncResp->res.jsonValue["@odata.type"] =
Janet Adkinsb25644a2023-08-16 11:23:45 -05002511 "#LogService.v1_2_0.LogService";
Ed Tanous002d39b2022-05-31 08:59:27 -07002512 asyncResp->res.jsonValue["@odata.id"] =
2513 "/redfish/v1/Managers/bmc/LogServices/Journal";
2514 asyncResp->res.jsonValue["Name"] = "Open BMC Journal Log Service";
2515 asyncResp->res.jsonValue["Description"] = "BMC Journal Log Service";
Ed Tanoused34a4a2023-02-08 15:43:27 -08002516 asyncResp->res.jsonValue["Id"] = "Journal";
Ed Tanous002d39b2022-05-31 08:59:27 -07002517 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
Tejas Patil7c8c4052021-06-04 17:43:14 +05302518
Ed Tanous002d39b2022-05-31 08:59:27 -07002519 std::pair<std::string, std::string> redfishDateTimeOffset =
Ed Tanous2b829372022-08-03 14:22:34 -07002520 redfish::time_utils::getDateTimeOffsetNow();
Ed Tanous002d39b2022-05-31 08:59:27 -07002521 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
2522 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
2523 redfishDateTimeOffset.second;
Tejas Patil7c8c4052021-06-04 17:43:14 +05302524
Ed Tanous002d39b2022-05-31 08:59:27 -07002525 asyncResp->res.jsonValue["Entries"]["@odata.id"] =
2526 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries";
Patrick Williams5a39f772023-10-20 11:20:21 -05002527 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002528}
Jason M. Billse1f26342018-07-18 12:12:00 -07002529
Jason M. Bills3a48b3a2022-06-24 10:10:15 -07002530static int
2531 fillBMCJournalLogEntryJson(const std::string& bmcJournalLogEntryID,
2532 sd_journal* journal,
2533 nlohmann::json::object_t& bmcJournalLogEntryJson)
Jason M. Billse1f26342018-07-18 12:12:00 -07002534{
2535 // Get the Log Entry contents
2536 int ret = 0;
Jason M. Billse1f26342018-07-18 12:12:00 -07002537
Jason M. Billsa8fe54f2020-11-20 15:57:55 -08002538 std::string message;
2539 std::string_view syslogID;
2540 ret = getJournalMetadata(journal, "SYSLOG_IDENTIFIER", syslogID);
2541 if (ret < 0)
2542 {
Carson Labradobf2dded2023-08-10 00:37:06 +00002543 BMCWEB_LOG_DEBUG("Failed to read SYSLOG_IDENTIFIER field: {}",
Ed Tanous62598e32023-07-17 17:06:25 -07002544 strerror(-ret));
Jason M. Billsa8fe54f2020-11-20 15:57:55 -08002545 }
2546 if (!syslogID.empty())
2547 {
2548 message += std::string(syslogID) + ": ";
2549 }
2550
Ed Tanous39e77502019-03-04 17:35:53 -08002551 std::string_view msg;
Jason M. Bills16428a12018-11-02 12:42:29 -07002552 ret = getJournalMetadata(journal, "MESSAGE", msg);
Jason M. Billse1f26342018-07-18 12:12:00 -07002553 if (ret < 0)
2554 {
Ed Tanous62598e32023-07-17 17:06:25 -07002555 BMCWEB_LOG_ERROR("Failed to read MESSAGE field: {}", strerror(-ret));
Jason M. Billse1f26342018-07-18 12:12:00 -07002556 return 1;
2557 }
Jason M. Billsa8fe54f2020-11-20 15:57:55 -08002558 message += std::string(msg);
Jason M. Billse1f26342018-07-18 12:12:00 -07002559
2560 // Get the severity from the PRIORITY field
Ed Tanous271584a2019-07-09 16:24:22 -07002561 long int severity = 8; // Default to an invalid priority
Jason M. Bills16428a12018-11-02 12:42:29 -07002562 ret = getJournalMetadata(journal, "PRIORITY", 10, severity);
Jason M. Billse1f26342018-07-18 12:12:00 -07002563 if (ret < 0)
2564 {
Carson Labradobf2dded2023-08-10 00:37:06 +00002565 BMCWEB_LOG_DEBUG("Failed to read PRIORITY field: {}", strerror(-ret));
Jason M. Billse1f26342018-07-18 12:12:00 -07002566 }
Jason M. Billse1f26342018-07-18 12:12:00 -07002567
2568 // Get the Created time from the timestamp
Jason M. Bills16428a12018-11-02 12:42:29 -07002569 std::string entryTimeStr;
2570 if (!getEntryTimestamp(journal, entryTimeStr))
Jason M. Billse1f26342018-07-18 12:12:00 -07002571 {
Jason M. Bills16428a12018-11-02 12:42:29 -07002572 return 1;
Jason M. Billse1f26342018-07-18 12:12:00 -07002573 }
Jason M. Billse1f26342018-07-18 12:12:00 -07002574
2575 // Fill in the log entry with the gathered data
Vijay Lobo9c11a172021-10-07 16:53:16 -05002576 bmcJournalLogEntryJson["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
Ed Tanousef4c65b2023-04-24 15:28:50 -07002577 bmcJournalLogEntryJson["@odata.id"] = boost::urls::format(
2578 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/{}",
Willy Tueddfc432022-09-26 16:46:38 +00002579 bmcJournalLogEntryID);
Jason M. Bills84afc482022-06-24 12:38:23 -07002580 bmcJournalLogEntryJson["Name"] = "BMC Journal Entry";
2581 bmcJournalLogEntryJson["Id"] = bmcJournalLogEntryID;
2582 bmcJournalLogEntryJson["Message"] = std::move(message);
2583 bmcJournalLogEntryJson["EntryType"] = "Oem";
Ed Tanousddf35642024-03-27 14:12:21 -07002584 log_entry::EventSeverity severityEnum = log_entry::EventSeverity::OK;
2585 if (severity <= 2)
2586 {
2587 severityEnum = log_entry::EventSeverity::Critical;
2588 }
2589 else if (severity <= 4)
2590 {
2591 severityEnum = log_entry::EventSeverity::Warning;
2592 }
2593
2594 bmcJournalLogEntryJson["Severity"] = severityEnum;
Jason M. Bills84afc482022-06-24 12:38:23 -07002595 bmcJournalLogEntryJson["OemRecordFormat"] = "BMC Journal Entry";
2596 bmcJournalLogEntryJson["Created"] = std::move(entryTimeStr);
Jason M. Billse1f26342018-07-18 12:12:00 -07002597 return 0;
2598}
2599
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002600inline void requestRoutesBMCJournalLogEntryCollection(App& app)
Jason M. Billse1f26342018-07-18 12:12:00 -07002601{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002602 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07002603 .privileges(redfish::privileges::getLogEntryCollection)
Ed Tanous002d39b2022-05-31 08:59:27 -07002604 .methods(boost::beast::http::verb::get)(
2605 [&app](const crow::Request& req,
2606 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2607 query_param::QueryCapabilities capabilities = {
2608 .canDelegateTop = true,
2609 .canDelegateSkip = true,
2610 };
2611 query_param::Query delegatedQuery;
2612 if (!redfish::setUpRedfishRouteWithDelegation(
Carson Labrado3ba00072022-06-06 19:40:56 +00002613 app, req, asyncResp, delegatedQuery, capabilities))
Ed Tanous002d39b2022-05-31 08:59:27 -07002614 {
2615 return;
2616 }
Ed Tanous3648c8b2022-07-25 13:39:59 -07002617
2618 size_t skip = delegatedQuery.skip.value_or(0);
Jiaqing Zhao5143f7a2022-07-22 09:33:33 +08002619 size_t top = delegatedQuery.top.value_or(query_param::Query::maxTop);
Ed Tanous3648c8b2022-07-25 13:39:59 -07002620
Ed Tanous002d39b2022-05-31 08:59:27 -07002621 // Collections don't include the static data added by SubRoute
2622 // because it has a duplicate entry for members
2623 asyncResp->res.jsonValue["@odata.type"] =
2624 "#LogEntryCollection.LogEntryCollection";
2625 asyncResp->res.jsonValue["@odata.id"] =
2626 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries";
2627 asyncResp->res.jsonValue["Name"] = "Open BMC Journal Entries";
2628 asyncResp->res.jsonValue["Description"] =
2629 "Collection of BMC Journal Entries";
2630 nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
2631 logEntryArray = nlohmann::json::array();
Jason M. Billse1f26342018-07-18 12:12:00 -07002632
Ed Tanous002d39b2022-05-31 08:59:27 -07002633 // Go through the journal and use the timestamp to create a
2634 // unique ID for each entry
2635 sd_journal* journalTmp = nullptr;
2636 int ret = sd_journal_open(&journalTmp, SD_JOURNAL_LOCAL_ONLY);
2637 if (ret < 0)
2638 {
Ed Tanous62598e32023-07-17 17:06:25 -07002639 BMCWEB_LOG_ERROR("failed to open journal: {}", strerror(-ret));
Ed Tanous002d39b2022-05-31 08:59:27 -07002640 messages::internalError(asyncResp->res);
2641 return;
2642 }
2643 std::unique_ptr<sd_journal, decltype(&sd_journal_close)> journal(
2644 journalTmp, sd_journal_close);
2645 journalTmp = nullptr;
2646 uint64_t entryCount = 0;
2647 // Reset the unique ID on the first entry
2648 bool firstEntry = true;
2649 SD_JOURNAL_FOREACH(journal.get())
2650 {
2651 entryCount++;
2652 // Handle paging using skip (number of entries to skip from
2653 // the start) and top (number of entries to display)
Ed Tanous3648c8b2022-07-25 13:39:59 -07002654 if (entryCount <= skip || entryCount > skip + top)
George Liu0fda0f12021-11-16 10:06:17 +08002655 {
Ed Tanous002d39b2022-05-31 08:59:27 -07002656 continue;
2657 }
2658
2659 std::string idStr;
2660 if (!getUniqueEntryID(journal.get(), idStr, firstEntry))
2661 {
2662 continue;
2663 }
Jason M. Billsefde4ec2022-06-24 08:59:52 -07002664 firstEntry = false;
Ed Tanous002d39b2022-05-31 08:59:27 -07002665
Jason M. Bills3a48b3a2022-06-24 10:10:15 -07002666 nlohmann::json::object_t bmcJournalLogEntry;
Ed Tanous002d39b2022-05-31 08:59:27 -07002667 if (fillBMCJournalLogEntryJson(idStr, journal.get(),
2668 bmcJournalLogEntry) != 0)
2669 {
George Liu0fda0f12021-11-16 10:06:17 +08002670 messages::internalError(asyncResp->res);
2671 return;
2672 }
Patrick Williamsb2ba3072023-05-12 10:27:39 -05002673 logEntryArray.emplace_back(std::move(bmcJournalLogEntry));
Ed Tanous002d39b2022-05-31 08:59:27 -07002674 }
2675 asyncResp->res.jsonValue["Members@odata.count"] = entryCount;
Ed Tanous3648c8b2022-07-25 13:39:59 -07002676 if (skip + top < entryCount)
Ed Tanous002d39b2022-05-31 08:59:27 -07002677 {
2678 asyncResp->res.jsonValue["Members@odata.nextLink"] =
2679 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries?$skip=" +
Ed Tanous3648c8b2022-07-25 13:39:59 -07002680 std::to_string(skip + top);
Ed Tanous002d39b2022-05-31 08:59:27 -07002681 }
Patrick Williams5a39f772023-10-20 11:20:21 -05002682 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002683}
Jason M. Billse1f26342018-07-18 12:12:00 -07002684
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002685inline void requestRoutesBMCJournalLogEntry(App& app)
Jason M. Billse1f26342018-07-18 12:12:00 -07002686{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002687 BMCWEB_ROUTE(app,
2688 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002689 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002690 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002691 [&app](const crow::Request& req,
2692 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2693 const std::string& entryID) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002694 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002695 {
2696 return;
2697 }
2698 // Convert the unique ID back to a timestamp to find the entry
Myung Bae75e8e212023-11-30 12:53:46 -08002699 sd_id128_t bootID{};
Ed Tanous002d39b2022-05-31 08:59:27 -07002700 uint64_t ts = 0;
2701 uint64_t index = 0;
Myung Bae75e8e212023-11-30 12:53:46 -08002702 if (!getTimestampFromID(asyncResp, entryID, bootID, ts, index))
Ed Tanous002d39b2022-05-31 08:59:27 -07002703 {
2704 return;
2705 }
Jason M. Billse1f26342018-07-18 12:12:00 -07002706
Ed Tanous002d39b2022-05-31 08:59:27 -07002707 sd_journal* journalTmp = nullptr;
2708 int ret = sd_journal_open(&journalTmp, SD_JOURNAL_LOCAL_ONLY);
2709 if (ret < 0)
2710 {
Ed Tanous62598e32023-07-17 17:06:25 -07002711 BMCWEB_LOG_ERROR("failed to open journal: {}", strerror(-ret));
Ed Tanous002d39b2022-05-31 08:59:27 -07002712 messages::internalError(asyncResp->res);
2713 return;
2714 }
2715 std::unique_ptr<sd_journal, decltype(&sd_journal_close)> journal(
2716 journalTmp, sd_journal_close);
2717 journalTmp = nullptr;
2718 // Go to the timestamp in the log and move to the entry at the
2719 // index tracking the unique ID
2720 std::string idStr;
2721 bool firstEntry = true;
Myung Bae75e8e212023-11-30 12:53:46 -08002722 ret = sd_journal_seek_monotonic_usec(journal.get(), bootID, ts);
Ed Tanous002d39b2022-05-31 08:59:27 -07002723 if (ret < 0)
2724 {
Ed Tanous62598e32023-07-17 17:06:25 -07002725 BMCWEB_LOG_ERROR("failed to seek to an entry in journal{}",
2726 strerror(-ret));
Ed Tanous002d39b2022-05-31 08:59:27 -07002727 messages::internalError(asyncResp->res);
2728 return;
2729 }
2730 for (uint64_t i = 0; i <= index; i++)
2731 {
2732 sd_journal_next(journal.get());
2733 if (!getUniqueEntryID(journal.get(), idStr, firstEntry))
2734 {
2735 messages::internalError(asyncResp->res);
2736 return;
2737 }
Jason M. Billsefde4ec2022-06-24 08:59:52 -07002738 firstEntry = false;
Ed Tanous002d39b2022-05-31 08:59:27 -07002739 }
2740 // Confirm that the entry ID matches what was requested
2741 if (idStr != entryID)
2742 {
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08002743 messages::resourceNotFound(asyncResp->res, "LogEntry", entryID);
Ed Tanous002d39b2022-05-31 08:59:27 -07002744 return;
2745 }
zhanghch058d1b46d2021-04-01 11:18:24 +08002746
Jason M. Bills3a48b3a2022-06-24 10:10:15 -07002747 nlohmann::json::object_t bmcJournalLogEntry;
Ed Tanous002d39b2022-05-31 08:59:27 -07002748 if (fillBMCJournalLogEntryJson(entryID, journal.get(),
Jason M. Bills3a48b3a2022-06-24 10:10:15 -07002749 bmcJournalLogEntry) != 0)
Ed Tanous002d39b2022-05-31 08:59:27 -07002750 {
2751 messages::internalError(asyncResp->res);
2752 return;
2753 }
Jason M. Billsd405bb52022-06-24 10:52:05 -07002754 asyncResp->res.jsonValue.update(bmcJournalLogEntry);
Patrick Williams5a39f772023-10-20 11:20:21 -05002755 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002756}
2757
Claire Weinanfdd26902022-03-01 14:18:25 -08002758inline void
2759 getDumpServiceInfo(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2760 const std::string& dumpType)
2761{
2762 std::string dumpPath;
2763 std::string overWritePolicy;
2764 bool collectDiagnosticDataSupported = false;
2765
2766 if (dumpType == "BMC")
2767 {
2768 dumpPath = "/redfish/v1/Managers/bmc/LogServices/Dump";
2769 overWritePolicy = "WrapsWhenFull";
2770 collectDiagnosticDataSupported = true;
2771 }
2772 else if (dumpType == "FaultLog")
2773 {
2774 dumpPath = "/redfish/v1/Managers/bmc/LogServices/FaultLog";
2775 overWritePolicy = "Unknown";
2776 collectDiagnosticDataSupported = false;
2777 }
2778 else if (dumpType == "System")
2779 {
2780 dumpPath = "/redfish/v1/Systems/system/LogServices/Dump";
2781 overWritePolicy = "WrapsWhenFull";
2782 collectDiagnosticDataSupported = true;
2783 }
2784 else
2785 {
Ed Tanous62598e32023-07-17 17:06:25 -07002786 BMCWEB_LOG_ERROR("getDumpServiceInfo() invalid dump type: {}",
2787 dumpType);
Claire Weinanfdd26902022-03-01 14:18:25 -08002788 messages::internalError(asyncResp->res);
2789 return;
2790 }
2791
2792 asyncResp->res.jsonValue["@odata.id"] = dumpPath;
2793 asyncResp->res.jsonValue["@odata.type"] = "#LogService.v1_2_0.LogService";
2794 asyncResp->res.jsonValue["Name"] = "Dump LogService";
2795 asyncResp->res.jsonValue["Description"] = dumpType + " Dump LogService";
2796 asyncResp->res.jsonValue["Id"] = std::filesystem::path(dumpPath).filename();
2797 asyncResp->res.jsonValue["OverWritePolicy"] = std::move(overWritePolicy);
2798
2799 std::pair<std::string, std::string> redfishDateTimeOffset =
Ed Tanous2b829372022-08-03 14:22:34 -07002800 redfish::time_utils::getDateTimeOffsetNow();
Claire Weinanfdd26902022-03-01 14:18:25 -08002801 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
2802 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
2803 redfishDateTimeOffset.second;
2804
2805 asyncResp->res.jsonValue["Entries"]["@odata.id"] = dumpPath + "/Entries";
Claire Weinanfdd26902022-03-01 14:18:25 -08002806
2807 if (collectDiagnosticDataSupported)
2808 {
2809 asyncResp->res.jsonValue["Actions"]["#LogService.CollectDiagnosticData"]
2810 ["target"] =
2811 dumpPath + "/Actions/LogService.CollectDiagnosticData";
2812 }
Claire Weinan0d946212022-07-13 19:40:19 -07002813
2814 constexpr std::array<std::string_view, 1> interfaces = {deleteAllInterface};
2815 dbus::utility::getSubTreePaths(
2816 "/xyz/openbmc_project/dump", 0, interfaces,
2817 [asyncResp, dumpType, dumpPath](
2818 const boost::system::error_code& ec,
2819 const dbus::utility::MapperGetSubTreePathsResponse& subTreePaths) {
2820 if (ec)
2821 {
Ed Tanous62598e32023-07-17 17:06:25 -07002822 BMCWEB_LOG_ERROR("getDumpServiceInfo respHandler got error {}", ec);
Claire Weinan0d946212022-07-13 19:40:19 -07002823 // Assume that getting an error simply means there are no dump
2824 // LogServices. Return without adding any error response.
2825 return;
2826 }
Ed Tanous18f8f602023-07-18 10:07:23 -07002827 std::string dbusDumpPath = getDumpPath(dumpType);
Claire Weinan0d946212022-07-13 19:40:19 -07002828 for (const std::string& path : subTreePaths)
2829 {
2830 if (path == dbusDumpPath)
2831 {
2832 asyncResp->res
2833 .jsonValue["Actions"]["#LogService.ClearLog"]["target"] =
2834 dumpPath + "/Actions/LogService.ClearLog";
2835 break;
2836 }
2837 }
Patrick Williams5a39f772023-10-20 11:20:21 -05002838 });
Claire Weinanfdd26902022-03-01 14:18:25 -08002839}
2840
2841inline void handleLogServicesDumpServiceGet(
2842 crow::App& app, const std::string& dumpType, const crow::Request& req,
2843 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2844{
2845 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2846 {
2847 return;
2848 }
2849 getDumpServiceInfo(asyncResp, dumpType);
2850}
2851
Ed Tanous22d268c2022-05-19 09:39:07 -07002852inline void handleLogServicesDumpServiceComputerSystemGet(
2853 crow::App& app, const crow::Request& req,
2854 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2855 const std::string& chassisId)
2856{
2857 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2858 {
2859 return;
2860 }
2861 if (chassisId != "system")
2862 {
2863 messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
2864 return;
2865 }
2866 getDumpServiceInfo(asyncResp, "System");
2867}
2868
Claire Weinanfdd26902022-03-01 14:18:25 -08002869inline void handleLogServicesDumpEntriesCollectionGet(
2870 crow::App& app, const std::string& dumpType, const crow::Request& req,
2871 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2872{
2873 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2874 {
2875 return;
2876 }
2877 getDumpEntryCollection(asyncResp, dumpType);
2878}
2879
Ed Tanous22d268c2022-05-19 09:39:07 -07002880inline void handleLogServicesDumpEntriesCollectionComputerSystemGet(
2881 crow::App& app, const crow::Request& req,
2882 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2883 const std::string& chassisId)
2884{
2885 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2886 {
2887 return;
2888 }
2889 if (chassisId != "system")
2890 {
2891 messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
2892 return;
2893 }
2894 getDumpEntryCollection(asyncResp, "System");
2895}
2896
Claire Weinanfdd26902022-03-01 14:18:25 -08002897inline void handleLogServicesDumpEntryGet(
2898 crow::App& app, const std::string& dumpType, const crow::Request& req,
2899 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2900 const std::string& dumpId)
2901{
2902 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2903 {
2904 return;
2905 }
2906 getDumpEntryById(asyncResp, dumpId, dumpType);
2907}
Carson Labrado168d1b12023-03-27 17:04:46 +00002908
Ed Tanous22d268c2022-05-19 09:39:07 -07002909inline void handleLogServicesDumpEntryComputerSystemGet(
2910 crow::App& app, const crow::Request& req,
2911 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2912 const std::string& chassisId, const std::string& dumpId)
2913{
2914 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2915 {
2916 return;
2917 }
2918 if (chassisId != "system")
2919 {
2920 messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
2921 return;
2922 }
2923 getDumpEntryById(asyncResp, dumpId, "System");
2924}
Claire Weinanfdd26902022-03-01 14:18:25 -08002925
2926inline void handleLogServicesDumpEntryDelete(
2927 crow::App& app, const std::string& dumpType, const crow::Request& req,
2928 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2929 const std::string& dumpId)
2930{
2931 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2932 {
2933 return;
2934 }
2935 deleteDumpEntry(asyncResp, dumpId, dumpType);
2936}
2937
Ed Tanous22d268c2022-05-19 09:39:07 -07002938inline void handleLogServicesDumpEntryComputerSystemDelete(
2939 crow::App& app, const crow::Request& req,
2940 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2941 const std::string& chassisId, const std::string& dumpId)
2942{
2943 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2944 {
2945 return;
2946 }
2947 if (chassisId != "system")
2948 {
2949 messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
2950 return;
2951 }
2952 deleteDumpEntry(asyncResp, dumpId, "System");
2953}
2954
Carson Labrado168d1b12023-03-27 17:04:46 +00002955inline void handleLogServicesDumpEntryDownloadGet(
2956 crow::App& app, const std::string& dumpType, const crow::Request& req,
2957 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2958 const std::string& dumpId)
2959{
2960 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2961 {
2962 return;
2963 }
2964 downloadDumpEntry(asyncResp, dumpId, dumpType);
2965}
2966
2967inline void handleDBusEventLogEntryDownloadGet(
2968 crow::App& app, const std::string& dumpType, const crow::Request& req,
2969 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2970 const std::string& systemName, const std::string& entryID)
2971{
2972 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2973 {
2974 return;
2975 }
2976 if (!http_helpers::isContentTypeAllowed(
2977 req.getHeaderValue("Accept"),
2978 http_helpers::ContentType::OctetStream, true))
2979 {
2980 asyncResp->res.result(boost::beast::http::status::bad_request);
2981 return;
2982 }
2983 downloadEventLogEntry(asyncResp, systemName, entryID, dumpType);
2984}
2985
Claire Weinanfdd26902022-03-01 14:18:25 -08002986inline void handleLogServicesDumpCollectDiagnosticDataPost(
2987 crow::App& app, const std::string& dumpType, const crow::Request& req,
2988 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2989{
2990 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2991 {
2992 return;
2993 }
2994 createDump(asyncResp, req, dumpType);
2995}
2996
Ed Tanous22d268c2022-05-19 09:39:07 -07002997inline void handleLogServicesDumpCollectDiagnosticDataComputerSystemPost(
2998 crow::App& app, const crow::Request& req,
2999 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous7f3e84a2022-12-28 16:22:54 -08003000 const std::string& systemName)
Ed Tanous22d268c2022-05-19 09:39:07 -07003001{
3002 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
3003 {
3004 return;
3005 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08003006
3007 if constexpr (bmcwebEnableMultiHost)
Ed Tanous22d268c2022-05-19 09:39:07 -07003008 {
Ed Tanous7f3e84a2022-12-28 16:22:54 -08003009 // Option currently returns no systems. TBD
3010 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3011 systemName);
3012 return;
3013 }
3014 if (systemName != "system")
3015 {
3016 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3017 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07003018 return;
3019 }
3020 createDump(asyncResp, req, "System");
3021}
3022
Claire Weinanfdd26902022-03-01 14:18:25 -08003023inline void handleLogServicesDumpClearLogPost(
3024 crow::App& app, const std::string& dumpType, const crow::Request& req,
3025 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
3026{
3027 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
3028 {
3029 return;
3030 }
3031 clearDump(asyncResp, dumpType);
3032}
3033
Ed Tanous22d268c2022-05-19 09:39:07 -07003034inline void handleLogServicesDumpClearLogComputerSystemPost(
3035 crow::App& app, const crow::Request& req,
3036 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous7f3e84a2022-12-28 16:22:54 -08003037 const std::string& systemName)
Ed Tanous22d268c2022-05-19 09:39:07 -07003038{
3039 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
3040 {
3041 return;
3042 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08003043 if constexpr (bmcwebEnableMultiHost)
Ed Tanous22d268c2022-05-19 09:39:07 -07003044 {
Ed Tanous7f3e84a2022-12-28 16:22:54 -08003045 // Option currently returns no systems. TBD
3046 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3047 systemName);
3048 return;
3049 }
3050 if (systemName != "system")
3051 {
3052 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3053 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07003054 return;
3055 }
3056 clearDump(asyncResp, "System");
3057}
3058
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003059inline void requestRoutesBMCDumpService(App& app)
3060{
3061 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Dump/")
Ed Tanoused398212021-06-09 17:05:54 -07003062 .privileges(redfish::privileges::getLogService)
Claire Weinanfdd26902022-03-01 14:18:25 -08003063 .methods(boost::beast::http::verb::get)(std::bind_front(
3064 handleLogServicesDumpServiceGet, std::ref(app), "BMC"));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003065}
3066
3067inline void requestRoutesBMCDumpEntryCollection(App& app)
3068{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003069 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07003070 .privileges(redfish::privileges::getLogEntryCollection)
Claire Weinanfdd26902022-03-01 14:18:25 -08003071 .methods(boost::beast::http::verb::get)(std::bind_front(
3072 handleLogServicesDumpEntriesCollectionGet, std::ref(app), "BMC"));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003073}
3074
3075inline void requestRoutesBMCDumpEntry(App& app)
3076{
3077 BMCWEB_ROUTE(app,
3078 "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07003079 .privileges(redfish::privileges::getLogEntry)
Claire Weinanfdd26902022-03-01 14:18:25 -08003080 .methods(boost::beast::http::verb::get)(std::bind_front(
3081 handleLogServicesDumpEntryGet, std::ref(app), "BMC"));
3082
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003083 BMCWEB_ROUTE(app,
3084 "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07003085 .privileges(redfish::privileges::deleteLogEntry)
Claire Weinanfdd26902022-03-01 14:18:25 -08003086 .methods(boost::beast::http::verb::delete_)(std::bind_front(
3087 handleLogServicesDumpEntryDelete, std::ref(app), "BMC"));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003088}
3089
Carson Labrado168d1b12023-03-27 17:04:46 +00003090inline void requestRoutesBMCDumpEntryDownload(App& app)
3091{
3092 BMCWEB_ROUTE(
3093 app,
Ravi Teja9e9d99d2023-11-08 05:33:59 -06003094 "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/<str>/attachment/")
Carson Labrado168d1b12023-03-27 17:04:46 +00003095 .privileges(redfish::privileges::getLogEntry)
3096 .methods(boost::beast::http::verb::get)(std::bind_front(
3097 handleLogServicesDumpEntryDownloadGet, std::ref(app), "BMC"));
3098}
3099
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003100inline void requestRoutesBMCDumpCreate(App& app)
3101{
George Liu0fda0f12021-11-16 10:06:17 +08003102 BMCWEB_ROUTE(
3103 app,
3104 "/redfish/v1/Managers/bmc/LogServices/Dump/Actions/LogService.CollectDiagnosticData/")
Ed Tanoused398212021-06-09 17:05:54 -07003105 .privileges(redfish::privileges::postLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003106 .methods(boost::beast::http::verb::post)(
Claire Weinanfdd26902022-03-01 14:18:25 -08003107 std::bind_front(handleLogServicesDumpCollectDiagnosticDataPost,
3108 std::ref(app), "BMC"));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003109}
3110
3111inline void requestRoutesBMCDumpClear(App& app)
3112{
George Liu0fda0f12021-11-16 10:06:17 +08003113 BMCWEB_ROUTE(
3114 app,
3115 "/redfish/v1/Managers/bmc/LogServices/Dump/Actions/LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07003116 .privileges(redfish::privileges::postLogService)
Claire Weinanfdd26902022-03-01 14:18:25 -08003117 .methods(boost::beast::http::verb::post)(std::bind_front(
3118 handleLogServicesDumpClearLogPost, std::ref(app), "BMC"));
3119}
3120
Carson Labrado168d1b12023-03-27 17:04:46 +00003121inline void requestRoutesDBusEventLogEntryDownload(App& app)
3122{
3123 BMCWEB_ROUTE(
3124 app,
Ravi Teja9e9d99d2023-11-08 05:33:59 -06003125 "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/<str>/attachment/")
Carson Labrado168d1b12023-03-27 17:04:46 +00003126 .privileges(redfish::privileges::getLogEntry)
3127 .methods(boost::beast::http::verb::get)(std::bind_front(
3128 handleDBusEventLogEntryDownloadGet, std::ref(app), "System"));
3129}
3130
Claire Weinanfdd26902022-03-01 14:18:25 -08003131inline void requestRoutesFaultLogDumpService(App& app)
3132{
3133 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/FaultLog/")
3134 .privileges(redfish::privileges::getLogService)
3135 .methods(boost::beast::http::verb::get)(std::bind_front(
3136 handleLogServicesDumpServiceGet, std::ref(app), "FaultLog"));
3137}
3138
3139inline void requestRoutesFaultLogDumpEntryCollection(App& app)
3140{
3141 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/FaultLog/Entries/")
3142 .privileges(redfish::privileges::getLogEntryCollection)
3143 .methods(boost::beast::http::verb::get)(
3144 std::bind_front(handleLogServicesDumpEntriesCollectionGet,
3145 std::ref(app), "FaultLog"));
3146}
3147
3148inline void requestRoutesFaultLogDumpEntry(App& app)
3149{
3150 BMCWEB_ROUTE(app,
3151 "/redfish/v1/Managers/bmc/LogServices/FaultLog/Entries/<str>/")
3152 .privileges(redfish::privileges::getLogEntry)
3153 .methods(boost::beast::http::verb::get)(std::bind_front(
3154 handleLogServicesDumpEntryGet, std::ref(app), "FaultLog"));
3155
3156 BMCWEB_ROUTE(app,
3157 "/redfish/v1/Managers/bmc/LogServices/FaultLog/Entries/<str>/")
3158 .privileges(redfish::privileges::deleteLogEntry)
3159 .methods(boost::beast::http::verb::delete_)(std::bind_front(
3160 handleLogServicesDumpEntryDelete, std::ref(app), "FaultLog"));
3161}
3162
3163inline void requestRoutesFaultLogDumpClear(App& app)
3164{
3165 BMCWEB_ROUTE(
3166 app,
3167 "/redfish/v1/Managers/bmc/LogServices/FaultLog/Actions/LogService.ClearLog/")
3168 .privileges(redfish::privileges::postLogService)
3169 .methods(boost::beast::http::verb::post)(std::bind_front(
3170 handleLogServicesDumpClearLogPost, std::ref(app), "FaultLog"));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003171}
3172
3173inline void requestRoutesSystemDumpService(App& app)
3174{
Ed Tanous22d268c2022-05-19 09:39:07 -07003175 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/Dump/")
Ed Tanoused398212021-06-09 17:05:54 -07003176 .privileges(redfish::privileges::getLogService)
Claire Weinan6ab9ad52022-08-12 18:20:17 -07003177 .methods(boost::beast::http::verb::get)(std::bind_front(
Ed Tanous22d268c2022-05-19 09:39:07 -07003178 handleLogServicesDumpServiceComputerSystemGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003179}
3180
3181inline void requestRoutesSystemDumpEntryCollection(App& app)
3182{
Ed Tanous22d268c2022-05-19 09:39:07 -07003183 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/Dump/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07003184 .privileges(redfish::privileges::getLogEntryCollection)
Ed Tanous22d268c2022-05-19 09:39:07 -07003185 .methods(boost::beast::http::verb::get)(std::bind_front(
3186 handleLogServicesDumpEntriesCollectionComputerSystemGet,
3187 std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003188}
3189
3190inline void requestRoutesSystemDumpEntry(App& app)
3191{
3192 BMCWEB_ROUTE(app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003193 "/redfish/v1/Systems/<str>/LogServices/Dump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07003194 .privileges(redfish::privileges::getLogEntry)
Claire Weinan6ab9ad52022-08-12 18:20:17 -07003195 .methods(boost::beast::http::verb::get)(std::bind_front(
Ed Tanous22d268c2022-05-19 09:39:07 -07003196 handleLogServicesDumpEntryComputerSystemGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003197
3198 BMCWEB_ROUTE(app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003199 "/redfish/v1/Systems/<str>/LogServices/Dump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07003200 .privileges(redfish::privileges::deleteLogEntry)
Claire Weinan6ab9ad52022-08-12 18:20:17 -07003201 .methods(boost::beast::http::verb::delete_)(std::bind_front(
Ed Tanous22d268c2022-05-19 09:39:07 -07003202 handleLogServicesDumpEntryComputerSystemDelete, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003203}
3204
3205inline void requestRoutesSystemDumpCreate(App& app)
3206{
George Liu0fda0f12021-11-16 10:06:17 +08003207 BMCWEB_ROUTE(
3208 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003209 "/redfish/v1/Systems/<str>/LogServices/Dump/Actions/LogService.CollectDiagnosticData/")
Ed Tanoused398212021-06-09 17:05:54 -07003210 .privileges(redfish::privileges::postLogService)
Ed Tanous22d268c2022-05-19 09:39:07 -07003211 .methods(boost::beast::http::verb::post)(std::bind_front(
3212 handleLogServicesDumpCollectDiagnosticDataComputerSystemPost,
3213 std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003214}
3215
3216inline void requestRoutesSystemDumpClear(App& app)
3217{
George Liu0fda0f12021-11-16 10:06:17 +08003218 BMCWEB_ROUTE(
3219 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003220 "/redfish/v1/Systems/<str>/LogServices/Dump/Actions/LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07003221 .privileges(redfish::privileges::postLogService)
Claire Weinan6ab9ad52022-08-12 18:20:17 -07003222 .methods(boost::beast::http::verb::post)(std::bind_front(
Ed Tanous22d268c2022-05-19 09:39:07 -07003223 handleLogServicesDumpClearLogComputerSystemPost, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003224}
3225
3226inline void requestRoutesCrashdumpService(App& app)
3227{
3228 // Note: Deviated from redfish privilege registry for GET & HEAD
3229 // method for security reasons.
3230 /**
3231 * Functions triggers appropriate requests on DBus
3232 */
Ed Tanous22d268c2022-05-19 09:39:07 -07003233 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/Crashdump/")
Ed Tanoused398212021-06-09 17:05:54 -07003234 // This is incorrect, should be:
3235 //.privileges(redfish::privileges::getLogService)
Ed Tanous432a8902021-06-14 15:28:56 -07003236 .privileges({{"ConfigureManager"}})
Ed Tanous002d39b2022-05-31 08:59:27 -07003237 .methods(boost::beast::http::verb::get)(
3238 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07003239 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3240 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003241 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003242 {
3243 return;
3244 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08003245 if constexpr (bmcwebEnableMultiHost)
3246 {
3247 // Option currently returns no systems. TBD
3248 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3249 systemName);
3250 return;
3251 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003252 if (systemName != "system")
3253 {
3254 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3255 systemName);
3256 return;
3257 }
3258
Ed Tanous002d39b2022-05-31 08:59:27 -07003259 // Copy over the static data to include the entries added by
3260 // SubRoute
3261 asyncResp->res.jsonValue["@odata.id"] =
3262 "/redfish/v1/Systems/system/LogServices/Crashdump";
3263 asyncResp->res.jsonValue["@odata.type"] =
3264 "#LogService.v1_2_0.LogService";
3265 asyncResp->res.jsonValue["Name"] = "Open BMC Oem Crashdump Service";
3266 asyncResp->res.jsonValue["Description"] = "Oem Crashdump Service";
V-Sanjana15b89722023-05-11 16:27:03 +05303267 asyncResp->res.jsonValue["Id"] = "Crashdump";
Ed Tanous002d39b2022-05-31 08:59:27 -07003268 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
3269 asyncResp->res.jsonValue["MaxNumberOfRecords"] = 3;
Tejas Patil7c8c4052021-06-04 17:43:14 +05303270
Ed Tanous002d39b2022-05-31 08:59:27 -07003271 std::pair<std::string, std::string> redfishDateTimeOffset =
Ed Tanous2b829372022-08-03 14:22:34 -07003272 redfish::time_utils::getDateTimeOffsetNow();
Ed Tanous002d39b2022-05-31 08:59:27 -07003273 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
3274 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
3275 redfishDateTimeOffset.second;
Tejas Patil7c8c4052021-06-04 17:43:14 +05303276
Ed Tanous002d39b2022-05-31 08:59:27 -07003277 asyncResp->res.jsonValue["Entries"]["@odata.id"] =
Ed Tanousef4c65b2023-04-24 15:28:50 -07003278 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries";
Ed Tanous002d39b2022-05-31 08:59:27 -07003279 asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"]["target"] =
3280 "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.ClearLog";
3281 asyncResp->res.jsonValue["Actions"]["#LogService.CollectDiagnosticData"]
3282 ["target"] =
3283 "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.CollectDiagnosticData";
Patrick Williams5a39f772023-10-20 11:20:21 -05003284 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003285}
3286
3287void inline requestRoutesCrashdumpClear(App& app)
3288{
George Liu0fda0f12021-11-16 10:06:17 +08003289 BMCWEB_ROUTE(
3290 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003291 "/redfish/v1/Systems/<str>/LogServices/Crashdump/Actions/LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07003292 // This is incorrect, should be:
3293 //.privileges(redfish::privileges::postLogService)
Ed Tanous432a8902021-06-14 15:28:56 -07003294 .privileges({{"ConfigureComponents"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003295 .methods(boost::beast::http::verb::post)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07003296 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07003297 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3298 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003299 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003300 {
3301 return;
3302 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08003303 if constexpr (bmcwebEnableMultiHost)
3304 {
3305 // Option currently returns no systems. TBD
3306 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3307 systemName);
3308 return;
3309 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003310 if (systemName != "system")
3311 {
3312 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3313 systemName);
3314 return;
3315 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003316 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08003317 [asyncResp](const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07003318 const std::string&) {
3319 if (ec)
3320 {
3321 messages::internalError(asyncResp->res);
3322 return;
3323 }
3324 messages::success(asyncResp->res);
Patrick Williams5a39f772023-10-20 11:20:21 -05003325 },
Ed Tanous002d39b2022-05-31 08:59:27 -07003326 crashdumpObject, crashdumpPath, deleteAllInterface, "DeleteAll");
Patrick Williams5a39f772023-10-20 11:20:21 -05003327 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003328}
Jason M. Bills5b61b5e2019-10-16 10:59:02 -07003329
zhanghch058d1b46d2021-04-01 11:18:24 +08003330static void
3331 logCrashdumpEntry(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3332 const std::string& logID, nlohmann::json& logEntryJson)
Jason M. Billse855dd22019-10-08 11:37:48 -07003333{
Johnathan Mantey043a0532020-03-10 17:15:28 -07003334 auto getStoredLogCallback =
Ed Tanousb9d36b42022-02-26 21:42:46 -08003335 [asyncResp, logID,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08003336 &logEntryJson](const boost::system::error_code& ec,
Ed Tanousb9d36b42022-02-26 21:42:46 -08003337 const dbus::utility::DBusPropertiesMap& params) {
Ed Tanous002d39b2022-05-31 08:59:27 -07003338 if (ec)
3339 {
Ed Tanous62598e32023-07-17 17:06:25 -07003340 BMCWEB_LOG_DEBUG("failed to get log ec: {}", ec.message());
Ed Tanous002d39b2022-05-31 08:59:27 -07003341 if (ec.value() ==
3342 boost::system::linux_error::bad_request_descriptor)
Jason M. Bills1ddcf012019-11-26 14:59:21 -08003343 {
Ed Tanous002d39b2022-05-31 08:59:27 -07003344 messages::resourceNotFound(asyncResp->res, "LogEntry", logID);
Jason M. Bills2b20ef62022-01-06 15:48:07 -08003345 }
3346 else
3347 {
Ed Tanous002d39b2022-05-31 08:59:27 -07003348 messages::internalError(asyncResp->res);
Jason M. Bills2b20ef62022-01-06 15:48:07 -08003349 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003350 return;
3351 }
3352
3353 std::string timestamp{};
3354 std::string filename{};
3355 std::string logfile{};
3356 parseCrashdumpParameters(params, filename, timestamp, logfile);
3357
3358 if (filename.empty() || timestamp.empty())
3359 {
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08003360 messages::resourceNotFound(asyncResp->res, "LogEntry", logID);
Ed Tanous002d39b2022-05-31 08:59:27 -07003361 return;
3362 }
3363
3364 std::string crashdumpURI =
3365 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/" +
3366 logID + "/" + filename;
Jason M. Bills84afc482022-06-24 12:38:23 -07003367 nlohmann::json::object_t logEntry;
Vijay Lobo9c11a172021-10-07 16:53:16 -05003368 logEntry["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
Ed Tanousef4c65b2023-04-24 15:28:50 -07003369 logEntry["@odata.id"] = boost::urls::format(
3370 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/{}",
3371 logID);
Jason M. Bills84afc482022-06-24 12:38:23 -07003372 logEntry["Name"] = "CPU Crashdump";
3373 logEntry["Id"] = logID;
3374 logEntry["EntryType"] = "Oem";
3375 logEntry["AdditionalDataURI"] = std::move(crashdumpURI);
3376 logEntry["DiagnosticDataType"] = "OEM";
3377 logEntry["OEMDiagnosticDataType"] = "PECICrashdump";
3378 logEntry["Created"] = std::move(timestamp);
Ed Tanous002d39b2022-05-31 08:59:27 -07003379
3380 // If logEntryJson references an array of LogEntry resources
3381 // ('Members' list), then push this as a new entry, otherwise set it
3382 // directly
3383 if (logEntryJson.is_array())
3384 {
3385 logEntryJson.push_back(logEntry);
3386 asyncResp->res.jsonValue["Members@odata.count"] =
3387 logEntryJson.size();
3388 }
3389 else
3390 {
Jason M. Billsd405bb52022-06-24 10:52:05 -07003391 logEntryJson.update(logEntry);
Ed Tanous002d39b2022-05-31 08:59:27 -07003392 }
3393 };
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02003394 sdbusplus::asio::getAllProperties(
3395 *crow::connections::systemBus, crashdumpObject,
3396 crashdumpPath + std::string("/") + logID, crashdumpInterface,
3397 std::move(getStoredLogCallback));
Jason M. Billse855dd22019-10-08 11:37:48 -07003398}
3399
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003400inline void requestRoutesCrashdumpEntryCollection(App& app)
Ed Tanous1da66f72018-07-27 16:13:37 -07003401{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003402 // Note: Deviated from redfish privilege registry for GET & HEAD
3403 // method for security reasons.
Ed Tanous1da66f72018-07-27 16:13:37 -07003404 /**
3405 * Functions triggers appropriate requests on DBus
3406 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003407 BMCWEB_ROUTE(app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003408 "/redfish/v1/Systems/<str>/LogServices/Crashdump/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07003409 // This is incorrect, should be.
3410 //.privileges(redfish::privileges::postLogEntryCollection)
Ed Tanous432a8902021-06-14 15:28:56 -07003411 .privileges({{"ConfigureComponents"}})
Ed Tanous002d39b2022-05-31 08:59:27 -07003412 .methods(boost::beast::http::verb::get)(
3413 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07003414 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3415 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003416 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003417 {
3418 return;
3419 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08003420 if constexpr (bmcwebEnableMultiHost)
3421 {
3422 // Option currently returns no systems. TBD
3423 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3424 systemName);
3425 return;
3426 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003427 if (systemName != "system")
3428 {
3429 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3430 systemName);
3431 return;
3432 }
3433
George Liu7a1dbc42022-12-07 16:03:22 +08003434 constexpr std::array<std::string_view, 1> interfaces = {
3435 crashdumpInterface};
3436 dbus::utility::getSubTreePaths(
3437 "/", 0, interfaces,
3438 [asyncResp](const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07003439 const std::vector<std::string>& resp) {
3440 if (ec)
Ed Tanous45ca1b82022-03-25 13:07:27 -07003441 {
Ed Tanous002d39b2022-05-31 08:59:27 -07003442 if (ec.value() !=
3443 boost::system::errc::no_such_file_or_directory)
3444 {
Ed Tanous62598e32023-07-17 17:06:25 -07003445 BMCWEB_LOG_DEBUG("failed to get entries ec: {}",
3446 ec.message());
Ed Tanous002d39b2022-05-31 08:59:27 -07003447 messages::internalError(asyncResp->res);
3448 return;
3449 }
Ed Tanous45ca1b82022-03-25 13:07:27 -07003450 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003451 asyncResp->res.jsonValue["@odata.type"] =
3452 "#LogEntryCollection.LogEntryCollection";
3453 asyncResp->res.jsonValue["@odata.id"] =
3454 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries";
3455 asyncResp->res.jsonValue["Name"] = "Open BMC Crashdump Entries";
3456 asyncResp->res.jsonValue["Description"] =
3457 "Collection of Crashdump Entries";
3458 asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
3459 asyncResp->res.jsonValue["Members@odata.count"] = 0;
Jason M. Bills2b20ef62022-01-06 15:48:07 -08003460
Ed Tanous002d39b2022-05-31 08:59:27 -07003461 for (const std::string& path : resp)
3462 {
3463 const sdbusplus::message::object_path objPath(path);
3464 // Get the log ID
3465 std::string logID = objPath.filename();
3466 if (logID.empty())
3467 {
3468 continue;
3469 }
3470 // Add the log entry to the array
3471 logCrashdumpEntry(asyncResp, logID,
3472 asyncResp->res.jsonValue["Members"]);
3473 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003474 });
Patrick Williams5a39f772023-10-20 11:20:21 -05003475 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003476}
Ed Tanous1da66f72018-07-27 16:13:37 -07003477
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003478inline void requestRoutesCrashdumpEntry(App& app)
Ed Tanous1da66f72018-07-27 16:13:37 -07003479{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003480 // Note: Deviated from redfish privilege registry for GET & HEAD
3481 // method for security reasons.
Ed Tanous1da66f72018-07-27 16:13:37 -07003482
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003483 BMCWEB_ROUTE(
Ed Tanous22d268c2022-05-19 09:39:07 -07003484 app, "/redfish/v1/Systems/<str>/LogServices/Crashdump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07003485 // this is incorrect, should be
3486 // .privileges(redfish::privileges::getLogEntry)
Ed Tanous432a8902021-06-14 15:28:56 -07003487 .privileges({{"ConfigureComponents"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003488 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07003489 [&app](const crow::Request& req,
3490 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07003491 const std::string& systemName, const std::string& param) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003492 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003493 {
3494 return;
3495 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08003496 if constexpr (bmcwebEnableMultiHost)
3497 {
3498 // Option currently returns no systems. TBD
3499 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3500 systemName);
3501 return;
3502 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003503 if (systemName != "system")
3504 {
3505 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3506 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07003507 return;
3508 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003509 const std::string& logID = param;
3510 logCrashdumpEntry(asyncResp, logID, asyncResp->res.jsonValue);
Patrick Williams5a39f772023-10-20 11:20:21 -05003511 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003512}
Ed Tanous1da66f72018-07-27 16:13:37 -07003513
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003514inline void requestRoutesCrashdumpFile(App& app)
3515{
3516 // Note: Deviated from redfish privilege registry for GET & HEAD
3517 // method for security reasons.
3518 BMCWEB_ROUTE(
3519 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003520 "/redfish/v1/Systems/<str>/LogServices/Crashdump/Entries/<str>/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07003521 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003522 .methods(boost::beast::http::verb::get)(
Nan Zhoua4ce1142022-08-02 18:45:25 +00003523 [](const crow::Request& req,
3524 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07003525 const std::string& systemName, const std::string& logID,
3526 const std::string& fileName) {
Shounak Mitra2a9beee2022-07-20 18:41:30 +00003527 // Do not call getRedfishRoute here since the crashdump file is not a
3528 // Redfish resource.
Ed Tanous22d268c2022-05-19 09:39:07 -07003529
Ed Tanous7f3e84a2022-12-28 16:22:54 -08003530 if constexpr (bmcwebEnableMultiHost)
3531 {
3532 // Option currently returns no systems. TBD
3533 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3534 systemName);
3535 return;
3536 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003537 if (systemName != "system")
3538 {
3539 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3540 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07003541 return;
3542 }
3543
Ed Tanous002d39b2022-05-31 08:59:27 -07003544 auto getStoredLogCallback =
Ed Tanous39662a32023-02-06 15:09:46 -08003545 [asyncResp, logID, fileName, url(boost::urls::url(req.url()))](
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08003546 const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07003547 const std::vector<
3548 std::pair<std::string, dbus::utility::DbusVariantType>>&
3549 resp) {
3550 if (ec)
3551 {
Ed Tanous62598e32023-07-17 17:06:25 -07003552 BMCWEB_LOG_DEBUG("failed to get log ec: {}", ec.message());
Ed Tanous002d39b2022-05-31 08:59:27 -07003553 messages::internalError(asyncResp->res);
3554 return;
3555 }
Jason M. Bills8e6c0992021-03-11 16:26:53 -08003556
Ed Tanous002d39b2022-05-31 08:59:27 -07003557 std::string dbusFilename{};
3558 std::string dbusTimestamp{};
3559 std::string dbusFilepath{};
Jason M. Bills8e6c0992021-03-11 16:26:53 -08003560
Ed Tanous002d39b2022-05-31 08:59:27 -07003561 parseCrashdumpParameters(resp, dbusFilename, dbusTimestamp,
3562 dbusFilepath);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003563
Ed Tanous002d39b2022-05-31 08:59:27 -07003564 if (dbusFilename.empty() || dbusTimestamp.empty() ||
3565 dbusFilepath.empty())
3566 {
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08003567 messages::resourceNotFound(asyncResp->res, "LogEntry", logID);
Ed Tanous002d39b2022-05-31 08:59:27 -07003568 return;
3569 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003570
Ed Tanous002d39b2022-05-31 08:59:27 -07003571 // Verify the file name parameter is correct
3572 if (fileName != dbusFilename)
3573 {
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08003574 messages::resourceNotFound(asyncResp->res, "LogEntry", logID);
Ed Tanous002d39b2022-05-31 08:59:27 -07003575 return;
3576 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003577
Ed Tanous27b0cf92023-08-07 12:02:40 -07003578 if (!asyncResp->res.openFile(dbusFilepath))
Ed Tanous002d39b2022-05-31 08:59:27 -07003579 {
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08003580 messages::resourceNotFound(asyncResp->res, "LogEntry", logID);
Ed Tanous002d39b2022-05-31 08:59:27 -07003581 return;
3582 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003583
Ed Tanous002d39b2022-05-31 08:59:27 -07003584 // Configure this to be a file download when accessed
3585 // from a browser
Ed Tanousd9f6c622022-03-17 09:12:17 -07003586 asyncResp->res.addHeader(
3587 boost::beast::http::field::content_disposition, "attachment");
Ed Tanous002d39b2022-05-31 08:59:27 -07003588 };
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02003589 sdbusplus::asio::getAllProperties(
3590 *crow::connections::systemBus, crashdumpObject,
3591 crashdumpPath + std::string("/") + logID, crashdumpInterface,
3592 std::move(getStoredLogCallback));
Patrick Williams5a39f772023-10-20 11:20:21 -05003593 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003594}
3595
Jason M. Billsc5a4c822022-01-06 15:51:23 -08003596enum class OEMDiagnosticType
3597{
3598 onDemand,
3599 telemetry,
3600 invalid,
3601};
3602
Ed Tanous26ccae32023-02-16 10:28:44 -08003603inline OEMDiagnosticType getOEMDiagnosticType(std::string_view oemDiagStr)
Jason M. Billsc5a4c822022-01-06 15:51:23 -08003604{
3605 if (oemDiagStr == "OnDemand")
3606 {
3607 return OEMDiagnosticType::onDemand;
3608 }
3609 if (oemDiagStr == "Telemetry")
3610 {
3611 return OEMDiagnosticType::telemetry;
3612 }
3613
3614 return OEMDiagnosticType::invalid;
3615}
3616
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003617inline void requestRoutesCrashdumpCollect(App& app)
3618{
3619 // Note: Deviated from redfish privilege registry for GET & HEAD
3620 // method for security reasons.
George Liu0fda0f12021-11-16 10:06:17 +08003621 BMCWEB_ROUTE(
3622 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003623 "/redfish/v1/Systems/<str>/LogServices/Crashdump/Actions/LogService.CollectDiagnosticData/")
Ed Tanoused398212021-06-09 17:05:54 -07003624 // The below is incorrect; Should be ConfigureManager
3625 //.privileges(redfish::privileges::postLogService)
Ed Tanous432a8902021-06-14 15:28:56 -07003626 .privileges({{"ConfigureComponents"}})
Ed Tanous002d39b2022-05-31 08:59:27 -07003627 .methods(boost::beast::http::verb::post)(
3628 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07003629 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3630 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003631 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003632 {
3633 return;
3634 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003635
Ed Tanous7f3e84a2022-12-28 16:22:54 -08003636 if constexpr (bmcwebEnableMultiHost)
3637 {
3638 // Option currently returns no systems. TBD
3639 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3640 systemName);
3641 return;
3642 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003643 if (systemName != "system")
3644 {
3645 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3646 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07003647 return;
3648 }
3649
Ed Tanous002d39b2022-05-31 08:59:27 -07003650 std::string diagnosticDataType;
3651 std::string oemDiagnosticDataType;
3652 if (!redfish::json_util::readJsonAction(
3653 req, asyncResp->res, "DiagnosticDataType", diagnosticDataType,
3654 "OEMDiagnosticDataType", oemDiagnosticDataType))
3655 {
3656 return;
3657 }
3658
3659 if (diagnosticDataType != "OEM")
3660 {
Ed Tanous62598e32023-07-17 17:06:25 -07003661 BMCWEB_LOG_ERROR(
3662 "Only OEM DiagnosticDataType supported for Crashdump");
Ed Tanous002d39b2022-05-31 08:59:27 -07003663 messages::actionParameterValueFormatError(
3664 asyncResp->res, diagnosticDataType, "DiagnosticDataType",
3665 "CollectDiagnosticData");
3666 return;
3667 }
3668
3669 OEMDiagnosticType oemDiagType =
3670 getOEMDiagnosticType(oemDiagnosticDataType);
3671
3672 std::string iface;
3673 std::string method;
3674 std::string taskMatchStr;
3675 if (oemDiagType == OEMDiagnosticType::onDemand)
3676 {
3677 iface = crashdumpOnDemandInterface;
3678 method = "GenerateOnDemandLog";
3679 taskMatchStr = "type='signal',"
3680 "interface='org.freedesktop.DBus.Properties',"
3681 "member='PropertiesChanged',"
3682 "arg0namespace='com.intel.crashdump'";
3683 }
3684 else if (oemDiagType == OEMDiagnosticType::telemetry)
3685 {
3686 iface = crashdumpTelemetryInterface;
3687 method = "GenerateTelemetryLog";
3688 taskMatchStr = "type='signal',"
3689 "interface='org.freedesktop.DBus.Properties',"
3690 "member='PropertiesChanged',"
3691 "arg0namespace='com.intel.crashdump'";
3692 }
3693 else
3694 {
Ed Tanous62598e32023-07-17 17:06:25 -07003695 BMCWEB_LOG_ERROR("Unsupported OEMDiagnosticDataType: {}",
3696 oemDiagnosticDataType);
Ed Tanous002d39b2022-05-31 08:59:27 -07003697 messages::actionParameterValueFormatError(
3698 asyncResp->res, oemDiagnosticDataType, "OEMDiagnosticDataType",
3699 "CollectDiagnosticData");
3700 return;
3701 }
3702
3703 auto collectCrashdumpCallback =
3704 [asyncResp, payload(task::Payload(req)),
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08003705 taskMatchStr](const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07003706 const std::string&) mutable {
3707 if (ec)
Ed Tanous45ca1b82022-03-25 13:07:27 -07003708 {
Ed Tanous002d39b2022-05-31 08:59:27 -07003709 if (ec.value() == boost::system::errc::operation_not_supported)
3710 {
3711 messages::resourceInStandby(asyncResp->res);
3712 }
3713 else if (ec.value() ==
3714 boost::system::errc::device_or_resource_busy)
3715 {
3716 messages::serviceTemporarilyUnavailable(asyncResp->res,
3717 "60");
3718 }
3719 else
3720 {
3721 messages::internalError(asyncResp->res);
3722 }
Ed Tanous45ca1b82022-03-25 13:07:27 -07003723 return;
3724 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003725 std::shared_ptr<task::TaskData> task = task::TaskData::createTask(
Ed Tanous8b242752023-06-27 17:17:13 -07003726 [](const boost::system::error_code& ec2, sdbusplus::message_t&,
Ed Tanous002d39b2022-05-31 08:59:27 -07003727 const std::shared_ptr<task::TaskData>& taskData) {
Ed Tanous8b242752023-06-27 17:17:13 -07003728 if (!ec2)
Ed Tanous002d39b2022-05-31 08:59:27 -07003729 {
3730 taskData->messages.emplace_back(messages::taskCompletedOK(
3731 std::to_string(taskData->index)));
3732 taskData->state = "Completed";
3733 }
3734 return task::completed;
Patrick Williams5a39f772023-10-20 11:20:21 -05003735 },
Ed Tanous002d39b2022-05-31 08:59:27 -07003736 taskMatchStr);
Ed Tanous1da66f72018-07-27 16:13:37 -07003737
Ed Tanous002d39b2022-05-31 08:59:27 -07003738 task->startTimer(std::chrono::minutes(5));
3739 task->populateResp(asyncResp->res);
3740 task->payload.emplace(std::move(payload));
3741 };
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003742
Ed Tanous002d39b2022-05-31 08:59:27 -07003743 crow::connections::systemBus->async_method_call(
3744 std::move(collectCrashdumpCallback), crashdumpObject, crashdumpPath,
3745 iface, method);
Patrick Williams5a39f772023-10-20 11:20:21 -05003746 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003747}
Kenny L. Ku6eda7682020-06-19 09:48:36 -07003748
Andrew Geisslercb92c032018-08-17 07:56:14 -07003749/**
3750 * DBusLogServiceActionsClear class supports POST method for ClearLog action.
3751 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003752inline void requestRoutesDBusLogServiceActionsClear(App& app)
Andrew Geisslercb92c032018-08-17 07:56:14 -07003753{
Andrew Geisslercb92c032018-08-17 07:56:14 -07003754 /**
3755 * Function handles POST method request.
3756 * The Clear Log actions does not require any parameter.The action deletes
3757 * all entries found in the Entries collection for this Log Service.
3758 */
Andrew Geisslercb92c032018-08-17 07:56:14 -07003759
George Liu0fda0f12021-11-16 10:06:17 +08003760 BMCWEB_ROUTE(
3761 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003762 "/redfish/v1/Systems/<str>/LogServices/EventLog/Actions/LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07003763 .privileges(redfish::privileges::postLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003764 .methods(boost::beast::http::verb::post)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07003765 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07003766 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3767 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003768 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003769 {
3770 return;
3771 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08003772 if constexpr (bmcwebEnableMultiHost)
3773 {
3774 // Option currently returns no systems. TBD
3775 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3776 systemName);
3777 return;
3778 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003779 if (systemName != "system")
3780 {
3781 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3782 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07003783 return;
3784 }
Ed Tanous62598e32023-07-17 17:06:25 -07003785 BMCWEB_LOG_DEBUG("Do delete all entries.");
Andrew Geisslercb92c032018-08-17 07:56:14 -07003786
Ed Tanous002d39b2022-05-31 08:59:27 -07003787 // Process response from Logging service.
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08003788 auto respHandler = [asyncResp](const boost::system::error_code& ec) {
Ed Tanous62598e32023-07-17 17:06:25 -07003789 BMCWEB_LOG_DEBUG("doClearLog resp_handler callback: Done");
Ed Tanous002d39b2022-05-31 08:59:27 -07003790 if (ec)
3791 {
3792 // TODO Handle for specific error code
Ed Tanous62598e32023-07-17 17:06:25 -07003793 BMCWEB_LOG_ERROR("doClearLog resp_handler got error {}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -07003794 asyncResp->res.result(
3795 boost::beast::http::status::internal_server_error);
3796 return;
3797 }
Andrew Geisslercb92c032018-08-17 07:56:14 -07003798
Ed Tanous002d39b2022-05-31 08:59:27 -07003799 asyncResp->res.result(boost::beast::http::status::no_content);
3800 };
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003801
Ed Tanous002d39b2022-05-31 08:59:27 -07003802 // Make call to Logging service to request Clear Log
3803 crow::connections::systemBus->async_method_call(
3804 respHandler, "xyz.openbmc_project.Logging",
3805 "/xyz/openbmc_project/logging",
3806 "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
Patrick Williams5a39f772023-10-20 11:20:21 -05003807 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003808}
ZhikuiRena3316fc2020-01-29 14:58:08 -08003809
3810/****************************************************
3811 * Redfish PostCode interfaces
3812 * using DBUS interface: getPostCodesTS
3813 ******************************************************/
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003814inline void requestRoutesPostCodesLogService(App& app)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003815{
Ed Tanous22d268c2022-05-19 09:39:07 -07003816 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/PostCodes/")
Ed Tanoused398212021-06-09 17:05:54 -07003817 .privileges(redfish::privileges::getLogService)
Ed Tanous002d39b2022-05-31 08:59:27 -07003818 .methods(boost::beast::http::verb::get)(
3819 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07003820 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3821 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003822 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003823 {
3824 return;
3825 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08003826 if constexpr (bmcwebEnableMultiHost)
3827 {
3828 // Option currently returns no systems. TBD
3829 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3830 systemName);
3831 return;
3832 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003833 if (systemName != "system")
3834 {
3835 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3836 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07003837 return;
3838 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003839 asyncResp->res.jsonValue["@odata.id"] =
3840 "/redfish/v1/Systems/system/LogServices/PostCodes";
3841 asyncResp->res.jsonValue["@odata.type"] =
Janet Adkinsb25644a2023-08-16 11:23:45 -05003842 "#LogService.v1_2_0.LogService";
Ed Tanous002d39b2022-05-31 08:59:27 -07003843 asyncResp->res.jsonValue["Name"] = "POST Code Log Service";
3844 asyncResp->res.jsonValue["Description"] = "POST Code Log Service";
Ed Tanoused34a4a2023-02-08 15:43:27 -08003845 asyncResp->res.jsonValue["Id"] = "PostCodes";
Ed Tanous002d39b2022-05-31 08:59:27 -07003846 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
3847 asyncResp->res.jsonValue["Entries"]["@odata.id"] =
3848 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries";
Tejas Patil7c8c4052021-06-04 17:43:14 +05303849
Ed Tanous002d39b2022-05-31 08:59:27 -07003850 std::pair<std::string, std::string> redfishDateTimeOffset =
Ed Tanous2b829372022-08-03 14:22:34 -07003851 redfish::time_utils::getDateTimeOffsetNow();
Ed Tanous002d39b2022-05-31 08:59:27 -07003852 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
3853 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
3854 redfishDateTimeOffset.second;
Tejas Patil7c8c4052021-06-04 17:43:14 +05303855
Ed Tanous002d39b2022-05-31 08:59:27 -07003856 asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"] = {
3857 {"target",
3858 "/redfish/v1/Systems/system/LogServices/PostCodes/Actions/LogService.ClearLog"}};
Patrick Williams5a39f772023-10-20 11:20:21 -05003859 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003860}
ZhikuiRena3316fc2020-01-29 14:58:08 -08003861
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003862inline void requestRoutesPostCodesClear(App& app)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003863{
George Liu0fda0f12021-11-16 10:06:17 +08003864 BMCWEB_ROUTE(
3865 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003866 "/redfish/v1/Systems/<str>/LogServices/PostCodes/Actions/LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07003867 // The following privilege is incorrect; It should be ConfigureManager
3868 //.privileges(redfish::privileges::postLogService)
Ed Tanous432a8902021-06-14 15:28:56 -07003869 .privileges({{"ConfigureComponents"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003870 .methods(boost::beast::http::verb::post)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07003871 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07003872 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3873 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003874 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003875 {
3876 return;
3877 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08003878 if constexpr (bmcwebEnableMultiHost)
3879 {
3880 // Option currently returns no systems. TBD
3881 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3882 systemName);
3883 return;
3884 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003885 if (systemName != "system")
3886 {
3887 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3888 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07003889 return;
3890 }
Ed Tanous62598e32023-07-17 17:06:25 -07003891 BMCWEB_LOG_DEBUG("Do delete all postcodes entries.");
ZhikuiRena3316fc2020-01-29 14:58:08 -08003892
Ed Tanous002d39b2022-05-31 08:59:27 -07003893 // Make call to post-code service to request clear all
3894 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08003895 [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07003896 if (ec)
3897 {
3898 // TODO Handle for specific error code
Ed Tanous62598e32023-07-17 17:06:25 -07003899 BMCWEB_LOG_ERROR("doClearPostCodes resp_handler got error {}",
3900 ec);
Ed Tanous002d39b2022-05-31 08:59:27 -07003901 asyncResp->res.result(
3902 boost::beast::http::status::internal_server_error);
3903 messages::internalError(asyncResp->res);
3904 return;
3905 }
Tony Lee18fc70c2023-08-24 16:15:54 +08003906 messages::success(asyncResp->res);
Patrick Williams5a39f772023-10-20 11:20:21 -05003907 },
Ed Tanous002d39b2022-05-31 08:59:27 -07003908 "xyz.openbmc_project.State.Boot.PostCode0",
3909 "/xyz/openbmc_project/State/Boot/PostCode0",
3910 "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
Patrick Williams5a39f772023-10-20 11:20:21 -05003911 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003912}
ZhikuiRena3316fc2020-01-29 14:58:08 -08003913
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003914/**
3915 * @brief Parse post code ID and get the current value and index value
3916 * eg: postCodeID=B1-2, currentValue=1, index=2
3917 *
3918 * @param[in] postCodeID Post Code ID
3919 * @param[out] currentValue Current value
3920 * @param[out] index Index value
3921 *
3922 * @return bool true if the parsing is successful, false the parsing fails
3923 */
Ed Tanous6f056f22024-04-07 13:35:51 -07003924inline bool parsePostCode(std::string_view postCodeID, uint64_t& currentValue,
Ed Tanousdf254f22024-04-01 13:25:46 -07003925 uint16_t& index)
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003926{
3927 std::vector<std::string> split;
Ed Tanous50ebd4a2023-01-19 19:03:17 -08003928 bmcweb::split(split, postCodeID, '-');
Ed Tanous6f056f22024-04-07 13:35:51 -07003929 if (split.size() != 2)
3930 {
3931 return false;
3932 }
3933 std::string_view postCodeNumber = split[0];
3934 if (postCodeNumber.size() < 2)
3935 {
3936 return false;
3937 }
3938 if (postCodeNumber[0] != 'B')
3939 {
3940 return false;
3941 }
3942 postCodeNumber.remove_prefix(1);
3943 auto [ptrIndex, ecIndex] = std::from_chars(postCodeNumber.begin(),
3944 postCodeNumber.end(), index);
3945 if (ptrIndex != postCodeNumber.end() || ecIndex != std::errc())
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003946 {
3947 return false;
3948 }
3949
Ed Tanous6f056f22024-04-07 13:35:51 -07003950 std::string_view postCodeIndex = split[1];
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003951
Ed Tanous6f056f22024-04-07 13:35:51 -07003952 auto [ptrValue, ecValue] = std::from_chars(
3953 postCodeIndex.begin(), postCodeIndex.end(), currentValue);
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003954
Ed Tanous6f056f22024-04-07 13:35:51 -07003955 return ptrValue == postCodeIndex.end() && ecValue == std::errc();
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003956}
3957
3958static bool fillPostCodeEntry(
Ed Tanousac106bf2023-06-07 09:24:59 -07003959 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05303960 const boost::container::flat_map<
3961 uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>& postcode,
ZhikuiRena3316fc2020-01-29 14:58:08 -08003962 const uint16_t bootIndex, const uint64_t codeIndex = 0,
3963 const uint64_t skip = 0, const uint64_t top = 0)
3964{
3965 // Get the Message from the MessageRegistry
Ed Tanousfffb8c12022-02-07 23:53:03 -08003966 const registries::Message* message =
3967 registries::getMessage("OpenBMC.0.2.BIOSPOSTCode");
Ed Tanousdc8cfa62024-04-07 13:37:25 -07003968 if (message == nullptr)
3969 {
3970 BMCWEB_LOG_ERROR("Couldn't find known message?");
3971 return false;
3972 }
ZhikuiRena3316fc2020-01-29 14:58:08 -08003973 uint64_t currentCodeIndex = 0;
ZhikuiRena3316fc2020-01-29 14:58:08 -08003974 uint64_t firstCodeTimeUs = 0;
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05303975 for (const std::pair<uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
3976 code : postcode)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003977 {
3978 currentCodeIndex++;
3979 std::string postcodeEntryID =
3980 "B" + std::to_string(bootIndex) + "-" +
3981 std::to_string(currentCodeIndex); // 1 based index in EntryID string
3982
3983 uint64_t usecSinceEpoch = code.first;
3984 uint64_t usTimeOffset = 0;
3985
3986 if (1 == currentCodeIndex)
3987 { // already incremented
3988 firstCodeTimeUs = code.first;
3989 }
3990 else
3991 {
3992 usTimeOffset = code.first - firstCodeTimeUs;
3993 }
3994
3995 // skip if no specific codeIndex is specified and currentCodeIndex does
3996 // not fall between top and skip
3997 if ((codeIndex == 0) &&
3998 (currentCodeIndex <= skip || currentCodeIndex > top))
3999 {
4000 continue;
4001 }
4002
Gunnar Mills4e0453b2020-07-08 14:00:30 -05004003 // skip if a specific codeIndex is specified and does not match the
ZhikuiRena3316fc2020-01-29 14:58:08 -08004004 // currentIndex
4005 if ((codeIndex > 0) && (currentCodeIndex != codeIndex))
4006 {
4007 // This is done for simplicity. 1st entry is needed to calculate
4008 // time offset. To improve efficiency, one can get to the entry
4009 // directly (possibly with flatmap's nth method)
4010 continue;
4011 }
4012
4013 // currentCodeIndex is within top and skip or equal to specified code
4014 // index
4015
4016 // Get the Created time from the timestamp
4017 std::string entryTimeStr;
Konstantin Aladyshev2a025612023-02-15 11:52:58 +03004018 entryTimeStr = redfish::time_utils::getDateTimeUintUs(usecSinceEpoch);
ZhikuiRena3316fc2020-01-29 14:58:08 -08004019
4020 // assemble messageArgs: BootIndex, TimeOffset(100us), PostCode(hex)
4021 std::ostringstream hexCode;
4022 hexCode << "0x" << std::setfill('0') << std::setw(2) << std::hex
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05304023 << std::get<0>(code.second);
ZhikuiRena3316fc2020-01-29 14:58:08 -08004024 std::ostringstream timeOffsetStr;
4025 // Set Fixed -Point Notation
4026 timeOffsetStr << std::fixed;
4027 // Set precision to 4 digits
4028 timeOffsetStr << std::setprecision(4);
4029 // Add double to stream
4030 timeOffsetStr << static_cast<double>(usTimeOffset) / 1000 / 1000;
ZhikuiRena3316fc2020-01-29 14:58:08 -08004031
Ed Tanous1e6deaf2022-02-17 11:32:37 -08004032 std::string bootIndexStr = std::to_string(bootIndex);
4033 std::string timeOffsetString = timeOffsetStr.str();
4034 std::string hexCodeStr = hexCode.str();
4035
4036 std::array<std::string_view, 3> messageArgs = {
4037 bootIndexStr, timeOffsetString, hexCodeStr};
4038
4039 std::string msg =
4040 redfish::registries::fillMessageArgs(messageArgs, message->message);
4041 if (msg.empty())
ZhikuiRena3316fc2020-01-29 14:58:08 -08004042 {
Ed Tanous1e6deaf2022-02-17 11:32:37 -08004043 messages::internalError(asyncResp->res);
4044 return false;
ZhikuiRena3316fc2020-01-29 14:58:08 -08004045 }
4046
Tim Leed4342a92020-04-27 11:47:58 +08004047 // Get Severity template from message registry
4048 std::string severity;
4049 if (message != nullptr)
4050 {
Ed Tanous5f2b84e2022-02-08 00:41:53 -08004051 severity = message->messageSeverity;
Tim Leed4342a92020-04-27 11:47:58 +08004052 }
4053
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08004054 // Format entry
4055 nlohmann::json::object_t bmcLogEntry;
Vijay Lobo9c11a172021-10-07 16:53:16 -05004056 bmcLogEntry["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
Ed Tanousef4c65b2023-04-24 15:28:50 -07004057 bmcLogEntry["@odata.id"] = boost::urls::format(
4058 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/{}",
4059 postcodeEntryID);
Jason M. Bills84afc482022-06-24 12:38:23 -07004060 bmcLogEntry["Name"] = "POST Code Log Entry";
4061 bmcLogEntry["Id"] = postcodeEntryID;
4062 bmcLogEntry["Message"] = std::move(msg);
4063 bmcLogEntry["MessageId"] = "OpenBMC.0.2.BIOSPOSTCode";
Ed Tanous1e6deaf2022-02-17 11:32:37 -08004064 bmcLogEntry["MessageArgs"] = messageArgs;
Jason M. Bills84afc482022-06-24 12:38:23 -07004065 bmcLogEntry["EntryType"] = "Event";
4066 bmcLogEntry["Severity"] = std::move(severity);
4067 bmcLogEntry["Created"] = entryTimeStr;
George Liu647b3cd2021-07-05 12:43:56 +08004068 if (!std::get<std::vector<uint8_t>>(code.second).empty())
4069 {
4070 bmcLogEntry["AdditionalDataURI"] =
4071 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/" +
4072 postcodeEntryID + "/attachment";
4073 }
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08004074
4075 // codeIndex is only specified when querying single entry, return only
4076 // that entry in this case
4077 if (codeIndex != 0)
4078 {
Ed Tanousac106bf2023-06-07 09:24:59 -07004079 asyncResp->res.jsonValue.update(bmcLogEntry);
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08004080 return true;
4081 }
4082
Ed Tanousac106bf2023-06-07 09:24:59 -07004083 nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
Patrick Williamsb2ba3072023-05-12 10:27:39 -05004084 logEntryArray.emplace_back(std::move(bmcLogEntry));
ZhikuiRena3316fc2020-01-29 14:58:08 -08004085 }
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08004086
4087 // Return value is always false when querying multiple entries
4088 return false;
ZhikuiRena3316fc2020-01-29 14:58:08 -08004089}
4090
Ed Tanousac106bf2023-06-07 09:24:59 -07004091static void
4092 getPostCodeForEntry(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
4093 const std::string& entryId)
ZhikuiRena3316fc2020-01-29 14:58:08 -08004094{
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08004095 uint16_t bootIndex = 0;
4096 uint64_t codeIndex = 0;
4097 if (!parsePostCode(entryId, codeIndex, bootIndex))
4098 {
4099 // Requested ID was not found
Ed Tanousac106bf2023-06-07 09:24:59 -07004100 messages::resourceNotFound(asyncResp->res, "LogEntry", entryId);
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08004101 return;
4102 }
4103
4104 if (bootIndex == 0 || codeIndex == 0)
4105 {
4106 // 0 is an invalid index
Ed Tanousac106bf2023-06-07 09:24:59 -07004107 messages::resourceNotFound(asyncResp->res, "LogEntry", entryId);
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08004108 return;
4109 }
4110
ZhikuiRena3316fc2020-01-29 14:58:08 -08004111 crow::connections::systemBus->async_method_call(
Ed Tanousac106bf2023-06-07 09:24:59 -07004112 [asyncResp, entryId, bootIndex,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08004113 codeIndex](const boost::system::error_code& ec,
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05304114 const boost::container::flat_map<
4115 uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
4116 postcode) {
Ed Tanous002d39b2022-05-31 08:59:27 -07004117 if (ec)
4118 {
Ed Tanous62598e32023-07-17 17:06:25 -07004119 BMCWEB_LOG_DEBUG("DBUS POST CODE PostCode response error");
Ed Tanousac106bf2023-06-07 09:24:59 -07004120 messages::internalError(asyncResp->res);
Ed Tanous002d39b2022-05-31 08:59:27 -07004121 return;
4122 }
ZhikuiRena3316fc2020-01-29 14:58:08 -08004123
Ed Tanous002d39b2022-05-31 08:59:27 -07004124 if (postcode.empty())
4125 {
Ed Tanousac106bf2023-06-07 09:24:59 -07004126 messages::resourceNotFound(asyncResp->res, "LogEntry", entryId);
Ed Tanous002d39b2022-05-31 08:59:27 -07004127 return;
4128 }
ZhikuiRena3316fc2020-01-29 14:58:08 -08004129
Ed Tanousac106bf2023-06-07 09:24:59 -07004130 if (!fillPostCodeEntry(asyncResp, postcode, bootIndex, codeIndex))
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08004131 {
Ed Tanousac106bf2023-06-07 09:24:59 -07004132 messages::resourceNotFound(asyncResp->res, "LogEntry", entryId);
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08004133 return;
4134 }
Patrick Williams5a39f772023-10-20 11:20:21 -05004135 },
Jonathan Doman15124762021-01-07 17:54:17 -08004136 "xyz.openbmc_project.State.Boot.PostCode0",
4137 "/xyz/openbmc_project/State/Boot/PostCode0",
ZhikuiRena3316fc2020-01-29 14:58:08 -08004138 "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodesWithTimeStamp",
4139 bootIndex);
4140}
4141
Ed Tanousac106bf2023-06-07 09:24:59 -07004142static void
4143 getPostCodeForBoot(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
4144 const uint16_t bootIndex, const uint16_t bootCount,
4145 const uint64_t entryCount, size_t skip, size_t top)
ZhikuiRena3316fc2020-01-29 14:58:08 -08004146{
4147 crow::connections::systemBus->async_method_call(
Ed Tanousac106bf2023-06-07 09:24:59 -07004148 [asyncResp, bootIndex, bootCount, entryCount, skip,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08004149 top](const boost::system::error_code& ec,
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05304150 const boost::container::flat_map<
4151 uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
4152 postcode) {
Ed Tanous002d39b2022-05-31 08:59:27 -07004153 if (ec)
4154 {
Ed Tanous62598e32023-07-17 17:06:25 -07004155 BMCWEB_LOG_DEBUG("DBUS POST CODE PostCode response error");
Ed Tanousac106bf2023-06-07 09:24:59 -07004156 messages::internalError(asyncResp->res);
Ed Tanous002d39b2022-05-31 08:59:27 -07004157 return;
4158 }
ZhikuiRena3316fc2020-01-29 14:58:08 -08004159
Ed Tanous002d39b2022-05-31 08:59:27 -07004160 uint64_t endCount = entryCount;
4161 if (!postcode.empty())
4162 {
4163 endCount = entryCount + postcode.size();
Ed Tanous3648c8b2022-07-25 13:39:59 -07004164 if (skip < endCount && (top + skip) > entryCount)
ZhikuiRena3316fc2020-01-29 14:58:08 -08004165 {
Patrick Williams89492a12023-05-10 07:51:34 -05004166 uint64_t thisBootSkip = std::max(static_cast<uint64_t>(skip),
4167 entryCount) -
4168 entryCount;
Ed Tanous002d39b2022-05-31 08:59:27 -07004169 uint64_t thisBootTop =
Ed Tanous3648c8b2022-07-25 13:39:59 -07004170 std::min(static_cast<uint64_t>(top + skip), endCount) -
4171 entryCount;
Ed Tanous002d39b2022-05-31 08:59:27 -07004172
Ed Tanousac106bf2023-06-07 09:24:59 -07004173 fillPostCodeEntry(asyncResp, postcode, bootIndex, 0,
4174 thisBootSkip, thisBootTop);
ZhikuiRena3316fc2020-01-29 14:58:08 -08004175 }
Ed Tanousac106bf2023-06-07 09:24:59 -07004176 asyncResp->res.jsonValue["Members@odata.count"] = endCount;
Ed Tanous002d39b2022-05-31 08:59:27 -07004177 }
4178
4179 // continue to previous bootIndex
4180 if (bootIndex < bootCount)
4181 {
Ed Tanousac106bf2023-06-07 09:24:59 -07004182 getPostCodeForBoot(asyncResp, static_cast<uint16_t>(bootIndex + 1),
Ed Tanous002d39b2022-05-31 08:59:27 -07004183 bootCount, endCount, skip, top);
4184 }
Jiaqing Zhao81584ab2022-07-28 00:33:45 +08004185 else if (skip + top < endCount)
Ed Tanous002d39b2022-05-31 08:59:27 -07004186 {
Ed Tanousac106bf2023-06-07 09:24:59 -07004187 asyncResp->res.jsonValue["Members@odata.nextLink"] =
Ed Tanous002d39b2022-05-31 08:59:27 -07004188 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries?$skip=" +
4189 std::to_string(skip + top);
4190 }
Patrick Williams5a39f772023-10-20 11:20:21 -05004191 },
Jonathan Doman15124762021-01-07 17:54:17 -08004192 "xyz.openbmc_project.State.Boot.PostCode0",
4193 "/xyz/openbmc_project/State/Boot/PostCode0",
ZhikuiRena3316fc2020-01-29 14:58:08 -08004194 "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodesWithTimeStamp",
4195 bootIndex);
4196}
4197
zhanghch058d1b46d2021-04-01 11:18:24 +08004198static void
Ed Tanousac106bf2023-06-07 09:24:59 -07004199 getCurrentBootNumber(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous3648c8b2022-07-25 13:39:59 -07004200 size_t skip, size_t top)
ZhikuiRena3316fc2020-01-29 14:58:08 -08004201{
4202 uint64_t entryCount = 0;
Jonathan Doman1e1e5982021-06-11 09:36:17 -07004203 sdbusplus::asio::getProperty<uint16_t>(
4204 *crow::connections::systemBus,
4205 "xyz.openbmc_project.State.Boot.PostCode0",
4206 "/xyz/openbmc_project/State/Boot/PostCode0",
4207 "xyz.openbmc_project.State.Boot.PostCode", "CurrentBootCycleCount",
Ed Tanousac106bf2023-06-07 09:24:59 -07004208 [asyncResp, entryCount, skip, top](const boost::system::error_code& ec,
4209 const uint16_t bootCount) {
Ed Tanous002d39b2022-05-31 08:59:27 -07004210 if (ec)
4211 {
Ed Tanous62598e32023-07-17 17:06:25 -07004212 BMCWEB_LOG_DEBUG("DBUS response error {}", ec);
Ed Tanousac106bf2023-06-07 09:24:59 -07004213 messages::internalError(asyncResp->res);
Ed Tanous002d39b2022-05-31 08:59:27 -07004214 return;
4215 }
Ed Tanousac106bf2023-06-07 09:24:59 -07004216 getPostCodeForBoot(asyncResp, 1, bootCount, entryCount, skip, top);
Patrick Williams5a39f772023-10-20 11:20:21 -05004217 });
ZhikuiRena3316fc2020-01-29 14:58:08 -08004218}
4219
John Edward Broadbent7e860f12021-04-08 15:57:16 -07004220inline void requestRoutesPostCodesEntryCollection(App& app)
ZhikuiRena3316fc2020-01-29 14:58:08 -08004221{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07004222 BMCWEB_ROUTE(app,
Ed Tanous22d268c2022-05-19 09:39:07 -07004223 "/redfish/v1/Systems/<str>/LogServices/PostCodes/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07004224 .privileges(redfish::privileges::getLogEntryCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07004225 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07004226 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07004227 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
4228 const std::string& systemName) {
Ed Tanous002d39b2022-05-31 08:59:27 -07004229 query_param::QueryCapabilities capabilities = {
4230 .canDelegateTop = true,
4231 .canDelegateSkip = true,
4232 };
4233 query_param::Query delegatedQuery;
4234 if (!redfish::setUpRedfishRouteWithDelegation(
Carson Labrado3ba00072022-06-06 19:40:56 +00004235 app, req, asyncResp, delegatedQuery, capabilities))
Ed Tanous002d39b2022-05-31 08:59:27 -07004236 {
4237 return;
4238 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08004239 if constexpr (bmcwebEnableMultiHost)
4240 {
4241 // Option currently returns no systems. TBD
4242 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
4243 systemName);
4244 return;
4245 }
Ed Tanous22d268c2022-05-19 09:39:07 -07004246
4247 if (systemName != "system")
4248 {
4249 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
4250 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07004251 return;
4252 }
Ed Tanous002d39b2022-05-31 08:59:27 -07004253 asyncResp->res.jsonValue["@odata.type"] =
4254 "#LogEntryCollection.LogEntryCollection";
4255 asyncResp->res.jsonValue["@odata.id"] =
4256 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries";
4257 asyncResp->res.jsonValue["Name"] = "BIOS POST Code Log Entries";
4258 asyncResp->res.jsonValue["Description"] =
4259 "Collection of POST Code Log Entries";
4260 asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
4261 asyncResp->res.jsonValue["Members@odata.count"] = 0;
Ed Tanous3648c8b2022-07-25 13:39:59 -07004262 size_t skip = delegatedQuery.skip.value_or(0);
Jiaqing Zhao5143f7a2022-07-22 09:33:33 +08004263 size_t top = delegatedQuery.top.value_or(query_param::Query::maxTop);
Ed Tanous3648c8b2022-07-25 13:39:59 -07004264 getCurrentBootNumber(asyncResp, skip, top);
Patrick Williams5a39f772023-10-20 11:20:21 -05004265 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07004266}
ZhikuiRena3316fc2020-01-29 14:58:08 -08004267
George Liu647b3cd2021-07-05 12:43:56 +08004268inline void requestRoutesPostCodesEntryAdditionalData(App& app)
4269{
George Liu0fda0f12021-11-16 10:06:17 +08004270 BMCWEB_ROUTE(
4271 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07004272 "/redfish/v1/Systems/<str>/LogServices/PostCodes/Entries/<str>/attachment/")
George Liu647b3cd2021-07-05 12:43:56 +08004273 .privileges(redfish::privileges::getLogEntry)
4274 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07004275 [&app](const crow::Request& req,
4276 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07004277 const std::string& systemName,
Ed Tanous45ca1b82022-03-25 13:07:27 -07004278 const std::string& postCodeID) {
Carson Labrado3ba00072022-06-06 19:40:56 +00004279 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07004280 {
4281 return;
4282 }
Matt Spinler72e21372023-04-19 12:53:33 -05004283 if (!http_helpers::isContentTypeAllowed(
Ed Tanous99351cd2022-08-07 16:42:51 -07004284 req.getHeaderValue("Accept"),
Ed Tanous4a0e1a02022-09-21 15:28:04 -07004285 http_helpers::ContentType::OctetStream, true))
Ed Tanous002d39b2022-05-31 08:59:27 -07004286 {
4287 asyncResp->res.result(boost::beast::http::status::bad_request);
4288 return;
4289 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08004290 if constexpr (bmcwebEnableMultiHost)
4291 {
4292 // Option currently returns no systems. TBD
4293 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
4294 systemName);
4295 return;
4296 }
Ed Tanous22d268c2022-05-19 09:39:07 -07004297 if (systemName != "system")
4298 {
4299 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
4300 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07004301 return;
4302 }
George Liu647b3cd2021-07-05 12:43:56 +08004303
Ed Tanous002d39b2022-05-31 08:59:27 -07004304 uint64_t currentValue = 0;
4305 uint16_t index = 0;
4306 if (!parsePostCode(postCodeID, currentValue, index))
4307 {
4308 messages::resourceNotFound(asyncResp->res, "LogEntry", postCodeID);
4309 return;
4310 }
George Liu647b3cd2021-07-05 12:43:56 +08004311
Ed Tanous002d39b2022-05-31 08:59:27 -07004312 crow::connections::systemBus->async_method_call(
4313 [asyncResp, postCodeID, currentValue](
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08004314 const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07004315 const std::vector<std::tuple<uint64_t, std::vector<uint8_t>>>&
4316 postcodes) {
4317 if (ec.value() == EBADR)
4318 {
4319 messages::resourceNotFound(asyncResp->res, "LogEntry",
4320 postCodeID);
4321 return;
4322 }
4323 if (ec)
4324 {
Ed Tanous62598e32023-07-17 17:06:25 -07004325 BMCWEB_LOG_DEBUG("DBUS response error {}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -07004326 messages::internalError(asyncResp->res);
4327 return;
4328 }
George Liu647b3cd2021-07-05 12:43:56 +08004329
Ed Tanous002d39b2022-05-31 08:59:27 -07004330 size_t value = static_cast<size_t>(currentValue) - 1;
4331 if (value == std::string::npos || postcodes.size() < currentValue)
4332 {
Ed Tanous62598e32023-07-17 17:06:25 -07004333 BMCWEB_LOG_WARNING("Wrong currentValue value");
Ed Tanous002d39b2022-05-31 08:59:27 -07004334 messages::resourceNotFound(asyncResp->res, "LogEntry",
4335 postCodeID);
4336 return;
4337 }
George Liu647b3cd2021-07-05 12:43:56 +08004338
Ed Tanous002d39b2022-05-31 08:59:27 -07004339 const auto& [tID, c] = postcodes[value];
4340 if (c.empty())
4341 {
Ed Tanous62598e32023-07-17 17:06:25 -07004342 BMCWEB_LOG_WARNING("No found post code data");
Ed Tanous002d39b2022-05-31 08:59:27 -07004343 messages::resourceNotFound(asyncResp->res, "LogEntry",
4344 postCodeID);
4345 return;
4346 }
4347 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
4348 const char* d = reinterpret_cast<const char*>(c.data());
4349 std::string_view strData(d, c.size());
George Liu647b3cd2021-07-05 12:43:56 +08004350
Ed Tanousd9f6c622022-03-17 09:12:17 -07004351 asyncResp->res.addHeader(boost::beast::http::field::content_type,
Ed Tanous002d39b2022-05-31 08:59:27 -07004352 "application/octet-stream");
Ed Tanousd9f6c622022-03-17 09:12:17 -07004353 asyncResp->res.addHeader(
4354 boost::beast::http::field::content_transfer_encoding, "Base64");
Ed Tanous27b0cf92023-08-07 12:02:40 -07004355 asyncResp->res.write(crow::utility::base64encode(strData));
Patrick Williams5a39f772023-10-20 11:20:21 -05004356 },
Ed Tanous002d39b2022-05-31 08:59:27 -07004357 "xyz.openbmc_project.State.Boot.PostCode0",
4358 "/xyz/openbmc_project/State/Boot/PostCode0",
4359 "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodes", index);
Patrick Williams5a39f772023-10-20 11:20:21 -05004360 });
George Liu647b3cd2021-07-05 12:43:56 +08004361}
4362
John Edward Broadbent7e860f12021-04-08 15:57:16 -07004363inline void requestRoutesPostCodesEntry(App& app)
ZhikuiRena3316fc2020-01-29 14:58:08 -08004364{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07004365 BMCWEB_ROUTE(
Ed Tanous22d268c2022-05-19 09:39:07 -07004366 app, "/redfish/v1/Systems/<str>/LogServices/PostCodes/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07004367 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07004368 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07004369 [&app](const crow::Request& req,
4370 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07004371 const std::string& systemName, const std::string& targetID) {
Carson Labrado3ba00072022-06-06 19:40:56 +00004372 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07004373 {
4374 return;
4375 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08004376 if constexpr (bmcwebEnableMultiHost)
4377 {
4378 // Option currently returns no systems. TBD
4379 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
4380 systemName);
4381 return;
4382 }
Ed Tanous22d268c2022-05-19 09:39:07 -07004383 if (systemName != "system")
4384 {
4385 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
4386 systemName);
4387 return;
4388 }
4389
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08004390 getPostCodeForEntry(asyncResp, targetID);
Patrick Williams5a39f772023-10-20 11:20:21 -05004391 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07004392}
ZhikuiRena3316fc2020-01-29 14:58:08 -08004393
Ed Tanous1da66f72018-07-27 16:13:37 -07004394} // namespace redfish