blob: 4ea01e3531b4eafdb5ab0097c8bf68fa95c77707 [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 Tanous3ccb3ad2023-01-13 17:40:03 -080031#include "utils/dbus_utils.hpp"
32#include "utils/time_utils.hpp"
Ed Tanous1da66f72018-07-27 16:13:37 -070033
Myung Bae75e8e212023-11-30 12:53:46 -080034#include <systemd/sd-id128.h>
Jason M. Billse1f26342018-07-18 12:12:00 -070035#include <systemd/sd-journal.h>
Asmitha Karunanithi8e317782020-12-10 03:35:05 -060036#include <tinyxml2.h>
Adriana Kobylak400fd1f2021-01-29 09:01:30 -060037#include <unistd.h>
Jason M. Billse1f26342018-07-18 12:12:00 -070038
Ed Tanous07c8c202022-07-11 10:08:08 -070039#include <boost/beast/http/verb.hpp>
Ed Tanous1da66f72018-07-27 16:13:37 -070040#include <boost/container/flat_map.hpp>
Jason M. Bills1ddcf012019-11-26 14:59:21 -080041#include <boost/system/linux_error.hpp>
Ed Tanousef4c65b2023-04-24 15:28:50 -070042#include <boost/url/format.hpp>
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +020043#include <sdbusplus/asio/property.hpp>
44#include <sdbusplus/unpack_properties.hpp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050045
George Liu7a1dbc42022-12-07 16:03:22 +080046#include <array>
George Liu647b3cd2021-07-05 12:43:56 +080047#include <charconv>
Abhilash Rajub5f288d2023-11-08 22:32:44 -060048#include <cstddef>
James Feist4418c7f2019-04-15 11:09:15 -070049#include <filesystem>
Ed Tanous18f8f602023-07-18 10:07:23 -070050#include <iterator>
Xiaochao Ma75710de2021-01-21 17:56:02 +080051#include <optional>
Ed Tanous3544d2a2023-08-06 18:12:20 -070052#include <ranges>
Ed Tanous26702d02021-11-03 15:02:33 -070053#include <span>
Ed Tanous18f8f602023-07-18 10:07:23 -070054#include <string>
Jason M. Billscd225da2019-05-08 15:31:57 -070055#include <string_view>
Ed Tanousabf2add2019-01-22 16:40:12 -080056#include <variant>
Ed Tanous1da66f72018-07-27 16:13:37 -070057
58namespace redfish
59{
60
Patrick Williams89492a12023-05-10 07:51:34 -050061constexpr const char* crashdumpObject = "com.intel.crashdump";
62constexpr const char* crashdumpPath = "/com/intel/crashdump";
63constexpr const char* crashdumpInterface = "com.intel.crashdump";
64constexpr const char* deleteAllInterface =
Jason M. Bills5b61b5e2019-10-16 10:59:02 -070065 "xyz.openbmc_project.Collection.DeleteAll";
Patrick Williams89492a12023-05-10 07:51:34 -050066constexpr const char* crashdumpOnDemandInterface =
Jason M. Bills424c4172019-03-21 13:50:33 -070067 "com.intel.crashdump.OnDemand";
Patrick Williams89492a12023-05-10 07:51:34 -050068constexpr const char* crashdumpTelemetryInterface =
Kenny L. Ku6eda7682020-06-19 09:48:36 -070069 "com.intel.crashdump.Telemetry";
Ed Tanous1da66f72018-07-27 16:13:37 -070070
Asmitha Karunanithi8e317782020-12-10 03:35:05 -060071enum class DumpCreationProgress
72{
73 DUMP_CREATE_SUCCESS,
74 DUMP_CREATE_FAILED,
75 DUMP_CREATE_INPROGRESS
76};
77
James Feistf6150402019-01-08 10:36:20 -080078namespace fs = std::filesystem;
Ed Tanous1da66f72018-07-27 16:13:37 -070079
Gunnar Mills1214b7e2020-06-04 10:11:30 -050080inline std::string translateSeverityDbusToRedfish(const std::string& s)
Andrew Geisslercb92c032018-08-17 07:56:14 -070081{
Ed Tanousd4d25792020-09-29 15:15:03 -070082 if ((s == "xyz.openbmc_project.Logging.Entry.Level.Alert") ||
83 (s == "xyz.openbmc_project.Logging.Entry.Level.Critical") ||
84 (s == "xyz.openbmc_project.Logging.Entry.Level.Emergency") ||
85 (s == "xyz.openbmc_project.Logging.Entry.Level.Error"))
Andrew Geisslercb92c032018-08-17 07:56:14 -070086 {
87 return "Critical";
88 }
Ed Tanous3174e4d2020-10-07 11:41:22 -070089 if ((s == "xyz.openbmc_project.Logging.Entry.Level.Debug") ||
90 (s == "xyz.openbmc_project.Logging.Entry.Level.Informational") ||
91 (s == "xyz.openbmc_project.Logging.Entry.Level.Notice"))
Andrew Geisslercb92c032018-08-17 07:56:14 -070092 {
93 return "OK";
94 }
Ed Tanous3174e4d2020-10-07 11:41:22 -070095 if (s == "xyz.openbmc_project.Logging.Entry.Level.Warning")
Andrew Geisslercb92c032018-08-17 07:56:14 -070096 {
97 return "Warning";
98 }
99 return "";
100}
101
Abhishek Patel9017faf2021-09-14 22:48:55 -0500102inline std::optional<bool> getProviderNotifyAction(const std::string& notify)
103{
104 std::optional<bool> notifyAction;
105 if (notify == "xyz.openbmc_project.Logging.Entry.Notify.Notify")
106 {
107 notifyAction = true;
108 }
109 else if (notify == "xyz.openbmc_project.Logging.Entry.Notify.Inhibit")
110 {
111 notifyAction = false;
112 }
113
114 return notifyAction;
115}
116
Ed Tanous18f8f602023-07-18 10:07:23 -0700117inline std::string getDumpPath(std::string_view dumpType)
118{
119 std::string dbusDumpPath = "/xyz/openbmc_project/dump/";
120 std::ranges::transform(dumpType, std::back_inserter(dbusDumpPath),
121 bmcweb::asciiToLower);
122
123 return dbusDumpPath;
124}
125
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700126inline static int getJournalMetadata(sd_journal* journal,
Ed Tanous26ccae32023-02-16 10:28:44 -0800127 std::string_view field,
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700128 std::string_view& contents)
Jason M. Bills16428a12018-11-02 12:42:29 -0700129{
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500130 const char* data = nullptr;
Jason M. Bills16428a12018-11-02 12:42:29 -0700131 size_t length = 0;
132 int ret = 0;
133 // Get the metadata from the requested field of the journal entry
Ed Tanous46ff87b2022-01-07 09:25:51 -0800134 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
135 const void** dataVoid = reinterpret_cast<const void**>(&data);
136
137 ret = sd_journal_get_data(journal, field.data(), dataVoid, &length);
Jason M. Bills16428a12018-11-02 12:42:29 -0700138 if (ret < 0)
139 {
140 return ret;
141 }
Ed Tanous39e77502019-03-04 17:35:53 -0800142 contents = std::string_view(data, length);
Jason M. Bills16428a12018-11-02 12:42:29 -0700143 // Only use the content after the "=" character.
Ed Tanous81ce6092020-12-17 16:54:55 +0000144 contents.remove_prefix(std::min(contents.find('=') + 1, contents.size()));
Jason M. Bills16428a12018-11-02 12:42:29 -0700145 return ret;
146}
147
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700148inline static int getJournalMetadata(sd_journal* journal,
Ed Tanous26ccae32023-02-16 10:28:44 -0800149 std::string_view field, const int& base,
150 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
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700164inline static bool getEntryTimestamp(sd_journal* journal,
165 std::string& entryTimestamp)
ZhikuiRena3316fc2020-01-29 14:58:08 -0800166{
167 int ret = 0;
168 uint64_t timestamp = 0;
169 ret = sd_journal_get_realtime_usec(journal, &timestamp);
170 if (ret < 0)
171 {
Ed Tanous62598e32023-07-17 17:06:25 -0700172 BMCWEB_LOG_ERROR("Failed to read entry timestamp: {}", strerror(-ret));
ZhikuiRena3316fc2020-01-29 14:58:08 -0800173 return false;
174 }
Konstantin Aladysheve645c5e2023-02-17 13:09:53 +0300175 entryTimestamp = redfish::time_utils::getDateTimeUintUs(timestamp);
Asmitha Karunanithi9c620e22020-08-02 11:55:21 -0500176 return true;
ZhikuiRena3316fc2020-01-29 14:58:08 -0800177}
Ed Tanous50b8a432022-02-03 16:29:50 -0800178
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700179inline static bool getUniqueEntryID(sd_journal* journal, std::string& entryID,
180 const bool firstEntry = true)
Jason M. Bills16428a12018-11-02 12:42:29 -0700181{
182 int ret = 0;
Myung Bae75e8e212023-11-30 12:53:46 -0800183 static sd_id128_t prevBootID{};
Jason M. Bills16428a12018-11-02 12:42:29 -0700184 static uint64_t prevTs = 0;
185 static int index = 0;
Jason M. Billse85d6b12019-07-29 17:01:15 -0700186 if (firstEntry)
187 {
Myung Bae75e8e212023-11-30 12:53:46 -0800188 prevBootID = {};
Jason M. Billse85d6b12019-07-29 17:01:15 -0700189 prevTs = 0;
190 }
191
Jason M. Bills16428a12018-11-02 12:42:29 -0700192 // Get the entry timestamp
193 uint64_t curTs = 0;
Myung Bae75e8e212023-11-30 12:53:46 -0800194 sd_id128_t curBootID{};
195 ret = sd_journal_get_monotonic_usec(journal, &curTs, &curBootID);
Jason M. Bills16428a12018-11-02 12:42:29 -0700196 if (ret < 0)
197 {
Ed Tanous62598e32023-07-17 17:06:25 -0700198 BMCWEB_LOG_ERROR("Failed to read entry timestamp: {}", strerror(-ret));
Jason M. Bills16428a12018-11-02 12:42:29 -0700199 return false;
200 }
Myung Bae75e8e212023-11-30 12:53:46 -0800201 // If the timestamp isn't unique on the same boot, increment the index
202 bool sameBootIDs = sd_id128_equal(curBootID, prevBootID) != 0;
203 if (sameBootIDs && (curTs == prevTs))
Jason M. Bills16428a12018-11-02 12:42:29 -0700204 {
205 index++;
206 }
207 else
208 {
209 // Otherwise, reset it
210 index = 0;
211 }
Myung Bae75e8e212023-11-30 12:53:46 -0800212
213 if (!sameBootIDs)
214 {
215 // Save the bootID
216 prevBootID = curBootID;
217 }
Jason M. Bills16428a12018-11-02 12:42:29 -0700218 // Save the timestamp
219 prevTs = curTs;
220
Myung Bae75e8e212023-11-30 12:53:46 -0800221 // make entryID as <bootID>_<timestamp>[_<index>]
222 std::array<char, SD_ID128_STRING_MAX> bootIDStr{};
223 sd_id128_to_string(curBootID, bootIDStr.data());
224 entryID = std::format("{}_{}", bootIDStr.data(), curTs);
Jason M. Bills16428a12018-11-02 12:42:29 -0700225 if (index > 0)
226 {
227 entryID += "_" + std::to_string(index);
228 }
229 return true;
230}
231
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500232static bool getUniqueEntryID(const std::string& logEntry, std::string& entryID,
Jason M. Billse85d6b12019-07-29 17:01:15 -0700233 const bool firstEntry = true)
Jason M. Bills95820182019-04-22 16:25:34 -0700234{
Ed Tanous271584a2019-07-09 16:24:22 -0700235 static time_t prevTs = 0;
Jason M. Bills95820182019-04-22 16:25:34 -0700236 static int index = 0;
Jason M. Billse85d6b12019-07-29 17:01:15 -0700237 if (firstEntry)
238 {
239 prevTs = 0;
240 }
241
Jason M. Bills95820182019-04-22 16:25:34 -0700242 // Get the entry timestamp
Ed Tanous271584a2019-07-09 16:24:22 -0700243 std::time_t curTs = 0;
Jason M. Bills95820182019-04-22 16:25:34 -0700244 std::tm timeStruct = {};
245 std::istringstream entryStream(logEntry);
246 if (entryStream >> std::get_time(&timeStruct, "%Y-%m-%dT%H:%M:%S"))
247 {
248 curTs = std::mktime(&timeStruct);
249 }
250 // If the timestamp isn't unique, increment the index
251 if (curTs == prevTs)
252 {
253 index++;
254 }
255 else
256 {
257 // Otherwise, reset it
258 index = 0;
259 }
260 // Save the timestamp
261 prevTs = curTs;
262
263 entryID = std::to_string(curTs);
264 if (index > 0)
265 {
266 entryID += "_" + std::to_string(index);
267 }
268 return true;
269}
270
Myung Bae75e8e212023-11-30 12:53:46 -0800271// Entry is formed like "BootID_timestamp" or "BootID_timestamp_index"
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700272inline static bool
zhanghch058d1b46d2021-04-01 11:18:24 +0800273 getTimestampFromID(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Myung Bae75e8e212023-11-30 12:53:46 -0800274 const std::string& entryID, sd_id128_t& bootID,
275 uint64_t& timestamp, uint64_t& index)
Jason M. Bills16428a12018-11-02 12:42:29 -0700276{
277 if (entryID.empty())
278 {
279 return false;
280 }
Jason M. Bills16428a12018-11-02 12:42:29 -0700281
Myung Bae75e8e212023-11-30 12:53:46 -0800282 // Convert the unique ID back to a bootID + timestamp to find the entry
283 std::string_view entryIDStrView(entryID);
284 auto underscore1Pos = entryIDStrView.find('_');
285 if (underscore1Pos == std::string_view::npos)
286 {
287 // EntryID has no bootID or timestamp
288 messages::resourceNotFound(asyncResp->res, "LogEntry", entryID);
289 return false;
290 }
291
292 // EntryID has bootID + timestamp
293
294 // Convert entryIDViewString to BootID
295 // NOTE: bootID string which needs to be null-terminated for
296 // sd_id128_from_string()
297 std::string bootIDStr(entryID, 0, underscore1Pos);
298 if (sd_id128_from_string(bootIDStr.c_str(), &bootID) < 0)
299 {
300 messages::resourceNotFound(asyncResp->res, "LogEntry", entryID);
301 return false;
302 }
303
304 // Get the timestamp from entryID
305 std::string_view timestampStrView = entryIDStrView;
306 timestampStrView.remove_prefix(underscore1Pos + 1);
307
308 // Check the index in timestamp
309 auto underscore2Pos = timestampStrView.find('_');
310 if (underscore2Pos != std::string_view::npos)
Jason M. Bills16428a12018-11-02 12:42:29 -0700311 {
312 // Timestamp has an index
Myung Bae75e8e212023-11-30 12:53:46 -0800313 timestampStrView.remove_suffix(timestampStrView.size() -
314 underscore2Pos);
315 std::string_view indexStr(timestampStrView);
316 indexStr.remove_prefix(underscore2Pos + 1);
Patrick Williams84396af2023-05-11 11:47:45 -0500317 auto [ptr, ec] = std::from_chars(indexStr.begin(), indexStr.end(),
318 index);
Ed Tanousc0bd5e42021-09-13 17:00:19 -0700319 if (ec != std::errc())
Jason M. Bills16428a12018-11-02 12:42:29 -0700320 {
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +0800321 messages::resourceNotFound(asyncResp->res, "LogEntry", entryID);
Jason M. Bills16428a12018-11-02 12:42:29 -0700322 return false;
323 }
324 }
Myung Bae75e8e212023-11-30 12:53:46 -0800325
326 // Now timestamp has no index
327 auto [ptr, ec] = std::from_chars(timestampStrView.begin(),
328 timestampStrView.end(), timestamp);
Ed Tanousc0bd5e42021-09-13 17:00:19 -0700329 if (ec != std::errc())
Jason M. Bills16428a12018-11-02 12:42:29 -0700330 {
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +0800331 messages::resourceNotFound(asyncResp->res, "LogEntry", entryID);
Jason M. Bills16428a12018-11-02 12:42:29 -0700332 return false;
333 }
334 return true;
335}
336
Jason M. Bills95820182019-04-22 16:25:34 -0700337static bool
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500338 getRedfishLogFiles(std::vector<std::filesystem::path>& redfishLogFiles)
Jason M. Bills95820182019-04-22 16:25:34 -0700339{
340 static const std::filesystem::path redfishLogDir = "/var/log";
341 static const std::string redfishLogFilename = "redfish";
342
343 // Loop through the directory looking for redfish log files
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500344 for (const std::filesystem::directory_entry& dirEnt :
Jason M. Bills95820182019-04-22 16:25:34 -0700345 std::filesystem::directory_iterator(redfishLogDir))
346 {
347 // If we find a redfish log file, save the path
348 std::string filename = dirEnt.path().filename();
Ed Tanous11ba3972022-07-11 09:50:41 -0700349 if (filename.starts_with(redfishLogFilename))
Jason M. Bills95820182019-04-22 16:25:34 -0700350 {
351 redfishLogFiles.emplace_back(redfishLogDir / filename);
352 }
353 }
354 // As the log files rotate, they are appended with a ".#" that is higher for
355 // the older logs. Since we don't expect more than 10 log files, we
356 // can just sort the list to get them in order from newest to oldest
Ed Tanous3544d2a2023-08-06 18:12:20 -0700357 std::ranges::sort(redfishLogFiles);
Jason M. Bills95820182019-04-22 16:25:34 -0700358
359 return !redfishLogFiles.empty();
360}
361
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -0600362inline log_entry::OriginatorTypes
363 mapDbusOriginatorTypeToRedfish(const std::string& originatorType)
364{
365 if (originatorType ==
366 "xyz.openbmc_project.Common.OriginatedBy.OriginatorTypes.Client")
367 {
368 return log_entry::OriginatorTypes::Client;
369 }
370 if (originatorType ==
371 "xyz.openbmc_project.Common.OriginatedBy.OriginatorTypes.Internal")
372 {
373 return log_entry::OriginatorTypes::Internal;
374 }
375 if (originatorType ==
376 "xyz.openbmc_project.Common.OriginatedBy.OriginatorTypes.SupportingService")
377 {
378 return log_entry::OriginatorTypes::SupportingService;
379 }
380 return log_entry::OriginatorTypes::Invalid;
381}
382
Claire Weinanaefe3782022-07-15 19:17:19 -0700383inline void parseDumpEntryFromDbusObject(
Jiaqing Zhao2d613eb2022-08-15 16:03:00 +0800384 const dbus::utility::ManagedObjectType::value_type& object,
Claire Weinanc6fecda2022-07-15 10:43:25 -0700385 std::string& dumpStatus, uint64_t& size, uint64_t& timestampUs,
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -0600386 std::string& originatorId, log_entry::OriginatorTypes& originatorType,
Claire Weinanaefe3782022-07-15 19:17:19 -0700387 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
388{
389 for (const auto& interfaceMap : object.second)
390 {
391 if (interfaceMap.first == "xyz.openbmc_project.Common.Progress")
392 {
393 for (const auto& propertyMap : interfaceMap.second)
394 {
395 if (propertyMap.first == "Status")
396 {
397 const auto* status =
398 std::get_if<std::string>(&propertyMap.second);
399 if (status == nullptr)
400 {
401 messages::internalError(asyncResp->res);
402 break;
403 }
404 dumpStatus = *status;
405 }
406 }
407 }
408 else if (interfaceMap.first == "xyz.openbmc_project.Dump.Entry")
409 {
410 for (const auto& propertyMap : interfaceMap.second)
411 {
412 if (propertyMap.first == "Size")
413 {
414 const auto* sizePtr =
415 std::get_if<uint64_t>(&propertyMap.second);
416 if (sizePtr == nullptr)
417 {
418 messages::internalError(asyncResp->res);
419 break;
420 }
421 size = *sizePtr;
422 break;
423 }
424 }
425 }
426 else if (interfaceMap.first == "xyz.openbmc_project.Time.EpochTime")
427 {
428 for (const auto& propertyMap : interfaceMap.second)
429 {
430 if (propertyMap.first == "Elapsed")
431 {
432 const uint64_t* usecsTimeStamp =
433 std::get_if<uint64_t>(&propertyMap.second);
434 if (usecsTimeStamp == nullptr)
435 {
436 messages::internalError(asyncResp->res);
437 break;
438 }
Claire Weinanc6fecda2022-07-15 10:43:25 -0700439 timestampUs = *usecsTimeStamp;
Claire Weinanaefe3782022-07-15 19:17:19 -0700440 break;
441 }
442 }
443 }
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -0600444 else if (interfaceMap.first ==
445 "xyz.openbmc_project.Common.OriginatedBy")
446 {
447 for (const auto& propertyMap : interfaceMap.second)
448 {
449 if (propertyMap.first == "OriginatorId")
450 {
451 const std::string* id =
452 std::get_if<std::string>(&propertyMap.second);
453 if (id == nullptr)
454 {
455 messages::internalError(asyncResp->res);
456 break;
457 }
458 originatorId = *id;
459 }
460
461 if (propertyMap.first == "OriginatorType")
462 {
463 const std::string* type =
464 std::get_if<std::string>(&propertyMap.second);
465 if (type == nullptr)
466 {
467 messages::internalError(asyncResp->res);
468 break;
469 }
470
471 originatorType = mapDbusOriginatorTypeToRedfish(*type);
472 if (originatorType == log_entry::OriginatorTypes::Invalid)
473 {
474 messages::internalError(asyncResp->res);
475 break;
476 }
477 }
478 }
479 }
Claire Weinanaefe3782022-07-15 19:17:19 -0700480 }
481}
482
Nan Zhou21ab4042022-06-26 23:07:40 +0000483static std::string getDumpEntriesPath(const std::string& dumpType)
Claire Weinanfdd26902022-03-01 14:18:25 -0800484{
485 std::string entriesPath;
486
487 if (dumpType == "BMC")
488 {
489 entriesPath = "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/";
490 }
491 else if (dumpType == "FaultLog")
492 {
493 entriesPath = "/redfish/v1/Managers/bmc/LogServices/FaultLog/Entries/";
494 }
495 else if (dumpType == "System")
496 {
497 entriesPath = "/redfish/v1/Systems/system/LogServices/Dump/Entries/";
498 }
499 else
500 {
Ed Tanous62598e32023-07-17 17:06:25 -0700501 BMCWEB_LOG_ERROR("getDumpEntriesPath() invalid dump type: {}",
502 dumpType);
Claire Weinanfdd26902022-03-01 14:18:25 -0800503 }
504
505 // Returns empty string on error
506 return entriesPath;
507}
508
zhanghch058d1b46d2021-04-01 11:18:24 +0800509inline void
510 getDumpEntryCollection(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
511 const std::string& dumpType)
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500512{
Claire Weinanfdd26902022-03-01 14:18:25 -0800513 std::string entriesPath = getDumpEntriesPath(dumpType);
514 if (entriesPath.empty())
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500515 {
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500516 messages::internalError(asyncResp->res);
517 return;
518 }
519
George Liu5eb468d2023-06-20 17:03:24 +0800520 sdbusplus::message::object_path path("/xyz/openbmc_project/dump");
521 dbus::utility::getManagedObjects(
522 "xyz.openbmc_project.Dump.Manager", path,
Claire Weinanfdd26902022-03-01 14:18:25 -0800523 [asyncResp, entriesPath,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800524 dumpType](const boost::system::error_code& ec,
George Liu5eb468d2023-06-20 17:03:24 +0800525 const dbus::utility::ManagedObjectType& objects) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700526 if (ec)
527 {
Ed Tanous62598e32023-07-17 17:06:25 -0700528 BMCWEB_LOG_ERROR("DumpEntry resp_handler got error {}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -0700529 messages::internalError(asyncResp->res);
530 return;
531 }
532
Claire Weinanfdd26902022-03-01 14:18:25 -0800533 // Remove ending slash
534 std::string odataIdStr = entriesPath;
535 if (!odataIdStr.empty())
536 {
537 odataIdStr.pop_back();
538 }
539
540 asyncResp->res.jsonValue["@odata.type"] =
541 "#LogEntryCollection.LogEntryCollection";
542 asyncResp->res.jsonValue["@odata.id"] = std::move(odataIdStr);
543 asyncResp->res.jsonValue["Name"] = dumpType + " Dump Entries";
Patrick Williams89492a12023-05-10 07:51:34 -0500544 asyncResp->res.jsonValue["Description"] = "Collection of " + dumpType +
545 " Dump Entries";
Claire Weinanfdd26902022-03-01 14:18:25 -0800546
Ed Tanous3544d2a2023-08-06 18:12:20 -0700547 nlohmann::json::array_t entriesArray;
Ed Tanous18f8f602023-07-18 10:07:23 -0700548 std::string dumpEntryPath = getDumpPath(dumpType) + "/entry/";
Ed Tanous002d39b2022-05-31 08:59:27 -0700549
George Liu5eb468d2023-06-20 17:03:24 +0800550 dbus::utility::ManagedObjectType resp(objects);
Ed Tanous3544d2a2023-08-06 18:12:20 -0700551 std::ranges::sort(resp, [](const auto& l, const auto& r) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700552 return AlphanumLess<std::string>()(l.first.filename(),
553 r.first.filename());
554 });
555
556 for (auto& object : resp)
557 {
558 if (object.first.str.find(dumpEntryPath) == std::string::npos)
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500559 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700560 continue;
561 }
Claire Weinanc6fecda2022-07-15 10:43:25 -0700562 uint64_t timestampUs = 0;
Ed Tanous002d39b2022-05-31 08:59:27 -0700563 uint64_t size = 0;
564 std::string dumpStatus;
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -0600565 std::string originatorId;
566 log_entry::OriginatorTypes originatorType =
567 log_entry::OriginatorTypes::Internal;
Jason M. Bills433b68b2022-06-28 12:24:26 -0700568 nlohmann::json::object_t thisEntry;
Ed Tanous002d39b2022-05-31 08:59:27 -0700569
570 std::string entryID = object.first.filename();
571 if (entryID.empty())
572 {
573 continue;
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500574 }
575
Claire Weinanc6fecda2022-07-15 10:43:25 -0700576 parseDumpEntryFromDbusObject(object, dumpStatus, size, timestampUs,
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -0600577 originatorId, originatorType,
Claire Weinanaefe3782022-07-15 19:17:19 -0700578 asyncResp);
Ed Tanous002d39b2022-05-31 08:59:27 -0700579
580 if (dumpStatus !=
581 "xyz.openbmc_project.Common.Progress.OperationStatus.Completed" &&
582 !dumpStatus.empty())
583 {
584 // Dump status is not Complete, no need to enumerate
585 continue;
586 }
587
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -0600588 thisEntry["@odata.type"] = "#LogEntry.v1_11_0.LogEntry";
Claire Weinanfdd26902022-03-01 14:18:25 -0800589 thisEntry["@odata.id"] = entriesPath + entryID;
Ed Tanous002d39b2022-05-31 08:59:27 -0700590 thisEntry["Id"] = entryID;
591 thisEntry["EntryType"] = "Event";
Ed Tanous002d39b2022-05-31 08:59:27 -0700592 thisEntry["Name"] = dumpType + " Dump Entry";
Claire Weinanbbd80db2022-10-26 16:55:52 -0700593 thisEntry["Created"] =
594 redfish::time_utils::getDateTimeUintUs(timestampUs);
Ed Tanous002d39b2022-05-31 08:59:27 -0700595
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -0600596 if (!originatorId.empty())
597 {
598 thisEntry["Originator"] = originatorId;
599 thisEntry["OriginatorType"] = originatorType;
600 }
601
Ed Tanous002d39b2022-05-31 08:59:27 -0700602 if (dumpType == "BMC")
603 {
604 thisEntry["DiagnosticDataType"] = "Manager";
Patrick Williams89492a12023-05-10 07:51:34 -0500605 thisEntry["AdditionalDataURI"] = entriesPath + entryID +
606 "/attachment";
Claire Weinanfdd26902022-03-01 14:18:25 -0800607 thisEntry["AdditionalDataSizeBytes"] = size;
Ed Tanous002d39b2022-05-31 08:59:27 -0700608 }
609 else if (dumpType == "System")
610 {
611 thisEntry["DiagnosticDataType"] = "OEM";
612 thisEntry["OEMDiagnosticDataType"] = "System";
Patrick Williams89492a12023-05-10 07:51:34 -0500613 thisEntry["AdditionalDataURI"] = entriesPath + entryID +
614 "/attachment";
Claire Weinanfdd26902022-03-01 14:18:25 -0800615 thisEntry["AdditionalDataSizeBytes"] = size;
Ed Tanous002d39b2022-05-31 08:59:27 -0700616 }
Patrick Williamsb2ba3072023-05-12 10:27:39 -0500617 entriesArray.emplace_back(std::move(thisEntry));
Ed Tanous002d39b2022-05-31 08:59:27 -0700618 }
619 asyncResp->res.jsonValue["Members@odata.count"] = entriesArray.size();
Ed Tanous3544d2a2023-08-06 18:12:20 -0700620 asyncResp->res.jsonValue["Members"] = std::move(entriesArray);
Patrick Williams5a39f772023-10-20 11:20:21 -0500621 });
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500622}
623
zhanghch058d1b46d2021-04-01 11:18:24 +0800624inline void
Claire Weinanc7a6d662022-06-13 16:36:39 -0700625 getDumpEntryById(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
zhanghch058d1b46d2021-04-01 11:18:24 +0800626 const std::string& entryID, const std::string& dumpType)
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500627{
Claire Weinanfdd26902022-03-01 14:18:25 -0800628 std::string entriesPath = getDumpEntriesPath(dumpType);
629 if (entriesPath.empty())
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500630 {
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500631 messages::internalError(asyncResp->res);
632 return;
633 }
634
George Liu5eb468d2023-06-20 17:03:24 +0800635 sdbusplus::message::object_path path("/xyz/openbmc_project/dump");
636 dbus::utility::getManagedObjects(
637 "xyz.openbmc_project.Dump.Manager", path,
Claire Weinanfdd26902022-03-01 14:18:25 -0800638 [asyncResp, entryID, dumpType,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800639 entriesPath](const boost::system::error_code& ec,
Ed Tanous02cad962022-06-30 16:50:15 -0700640 const dbus::utility::ManagedObjectType& resp) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700641 if (ec)
642 {
Ed Tanous62598e32023-07-17 17:06:25 -0700643 BMCWEB_LOG_ERROR("DumpEntry resp_handler got error {}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -0700644 messages::internalError(asyncResp->res);
645 return;
646 }
647
648 bool foundDumpEntry = false;
Ed Tanous18f8f602023-07-18 10:07:23 -0700649 std::string dumpEntryPath = getDumpPath(dumpType) + "/entry/";
Ed Tanous002d39b2022-05-31 08:59:27 -0700650
651 for (const auto& objectPath : resp)
652 {
653 if (objectPath.first.str != dumpEntryPath + entryID)
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500654 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700655 continue;
656 }
657
658 foundDumpEntry = true;
Claire Weinanc6fecda2022-07-15 10:43:25 -0700659 uint64_t timestampUs = 0;
Ed Tanous002d39b2022-05-31 08:59:27 -0700660 uint64_t size = 0;
661 std::string dumpStatus;
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -0600662 std::string originatorId;
663 log_entry::OriginatorTypes originatorType =
664 log_entry::OriginatorTypes::Internal;
Ed Tanous002d39b2022-05-31 08:59:27 -0700665
Claire Weinanaefe3782022-07-15 19:17:19 -0700666 parseDumpEntryFromDbusObject(objectPath, dumpStatus, size,
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -0600667 timestampUs, originatorId,
668 originatorType, asyncResp);
Ed Tanous002d39b2022-05-31 08:59:27 -0700669
670 if (dumpStatus !=
671 "xyz.openbmc_project.Common.Progress.OperationStatus.Completed" &&
672 !dumpStatus.empty())
673 {
674 // Dump status is not Complete
675 // return not found until status is changed to Completed
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +0200676 messages::resourceNotFound(asyncResp->res, dumpType + " dump",
677 entryID);
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500678 return;
679 }
680
Ed Tanous002d39b2022-05-31 08:59:27 -0700681 asyncResp->res.jsonValue["@odata.type"] =
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -0600682 "#LogEntry.v1_11_0.LogEntry";
Claire Weinanfdd26902022-03-01 14:18:25 -0800683 asyncResp->res.jsonValue["@odata.id"] = entriesPath + entryID;
Ed Tanous002d39b2022-05-31 08:59:27 -0700684 asyncResp->res.jsonValue["Id"] = entryID;
685 asyncResp->res.jsonValue["EntryType"] = "Event";
Ed Tanous002d39b2022-05-31 08:59:27 -0700686 asyncResp->res.jsonValue["Name"] = dumpType + " Dump Entry";
Claire Weinanbbd80db2022-10-26 16:55:52 -0700687 asyncResp->res.jsonValue["Created"] =
688 redfish::time_utils::getDateTimeUintUs(timestampUs);
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500689
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -0600690 if (!originatorId.empty())
691 {
692 asyncResp->res.jsonValue["Originator"] = originatorId;
693 asyncResp->res.jsonValue["OriginatorType"] = originatorType;
694 }
695
Ed Tanous002d39b2022-05-31 08:59:27 -0700696 if (dumpType == "BMC")
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500697 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700698 asyncResp->res.jsonValue["DiagnosticDataType"] = "Manager";
699 asyncResp->res.jsonValue["AdditionalDataURI"] =
Claire Weinanfdd26902022-03-01 14:18:25 -0800700 entriesPath + entryID + "/attachment";
701 asyncResp->res.jsonValue["AdditionalDataSizeBytes"] = size;
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500702 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700703 else if (dumpType == "System")
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500704 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700705 asyncResp->res.jsonValue["DiagnosticDataType"] = "OEM";
706 asyncResp->res.jsonValue["OEMDiagnosticDataType"] = "System";
707 asyncResp->res.jsonValue["AdditionalDataURI"] =
Claire Weinanfdd26902022-03-01 14:18:25 -0800708 entriesPath + entryID + "/attachment";
709 asyncResp->res.jsonValue["AdditionalDataSizeBytes"] = size;
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500710 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700711 }
712 if (!foundDumpEntry)
713 {
Ed Tanous62598e32023-07-17 17:06:25 -0700714 BMCWEB_LOG_WARNING("Can't find Dump Entry {}", entryID);
Myung Baeb90d14f2023-05-31 14:40:39 -0500715 messages::resourceNotFound(asyncResp->res, dumpType + " dump",
716 entryID);
Ed Tanous002d39b2022-05-31 08:59:27 -0700717 return;
718 }
Patrick Williams5a39f772023-10-20 11:20:21 -0500719 });
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500720}
721
zhanghch058d1b46d2021-04-01 11:18:24 +0800722inline void deleteDumpEntry(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Stanley Chu98782562020-11-04 16:10:24 +0800723 const std::string& entryID,
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500724 const std::string& dumpType)
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500725{
Patrick Williams5a39f772023-10-20 11:20:21 -0500726 auto respHandler = [asyncResp,
727 entryID](const boost::system::error_code& ec) {
Ed Tanous62598e32023-07-17 17:06:25 -0700728 BMCWEB_LOG_DEBUG("Dump Entry doDelete callback: Done");
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500729 if (ec)
730 {
George Liu3de8d8b2021-03-22 17:49:39 +0800731 if (ec.value() == EBADR)
732 {
733 messages::resourceNotFound(asyncResp->res, "LogEntry", entryID);
734 return;
735 }
Ed Tanous62598e32023-07-17 17:06:25 -0700736 BMCWEB_LOG_ERROR(
737 "Dump (DBus) doDelete respHandler got error {} entryID={}", ec,
738 entryID);
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500739 messages::internalError(asyncResp->res);
740 return;
741 }
742 };
Ed Tanous18f8f602023-07-18 10:07:23 -0700743
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500744 crow::connections::systemBus->async_method_call(
745 respHandler, "xyz.openbmc_project.Dump.Manager",
Ed Tanous18f8f602023-07-18 10:07:23 -0700746 std::format("{}/entry/{}", getDumpPath(dumpType), entryID),
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500747 "xyz.openbmc_project.Object.Delete", "Delete");
748}
Abhilash Rajub5f288d2023-11-08 22:32:44 -0600749inline bool checkSizeLimit(int fd, crow::Response& res)
750{
751 long long int size = lseek(fd, 0, SEEK_END);
752 if (size <= 0)
753 {
754 BMCWEB_LOG_ERROR("Failed to get size of file, lseek() returned {}",
755 size);
756 messages::internalError(res);
757 return false;
758 }
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500759
Abhilash Rajub5f288d2023-11-08 22:32:44 -0600760 // Arbitrary max size of 20MB to accommodate BMC dumps
761 constexpr long long int maxFileSize = 20LL * 1024LL * 1024LL;
762 if (size > maxFileSize)
763 {
764 BMCWEB_LOG_ERROR("File size {} exceeds maximum allowed size of {}",
765 size, maxFileSize);
766 messages::internalError(res);
767 return false;
768 }
769 off_t rc = lseek(fd, 0, SEEK_SET);
770 if (rc < 0)
771 {
772 BMCWEB_LOG_ERROR("Failed to reset file offset to 0");
773 messages::internalError(res);
774 return false;
775 }
776 return true;
777}
Carson Labrado168d1b12023-03-27 17:04:46 +0000778inline void
779 downloadEntryCallback(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
780 const std::string& entryID,
781 const std::string& downloadEntryType,
782 const boost::system::error_code& ec,
783 const sdbusplus::message::unix_fd& unixfd)
784{
785 if (ec.value() == EBADR)
786 {
787 messages::resourceNotFound(asyncResp->res, "EntryAttachment", entryID);
788 return;
789 }
790 if (ec)
791 {
792 BMCWEB_LOG_ERROR("DBUS response error: {}", ec);
793 messages::internalError(asyncResp->res);
794 return;
795 }
796
797 // Make sure we know how to process the retrieved entry attachment
798 if ((downloadEntryType != "BMC") && (downloadEntryType != "System"))
799 {
800 BMCWEB_LOG_ERROR("downloadEntryCallback() invalid entry type: {}",
801 downloadEntryType);
802 messages::internalError(asyncResp->res);
803 }
804
805 int fd = -1;
806 fd = dup(unixfd);
807 if (fd < 0)
808 {
809 BMCWEB_LOG_ERROR("Failed to open file");
810 messages::internalError(asyncResp->res);
811 return;
812 }
Abhilash Rajub5f288d2023-11-08 22:32:44 -0600813 if (!checkSizeLimit(fd, asyncResp->res))
Carson Labrado168d1b12023-03-27 17:04:46 +0000814 {
Carson Labrado168d1b12023-03-27 17:04:46 +0000815 close(fd);
816 return;
817 }
Carson Labrado168d1b12023-03-27 17:04:46 +0000818 if (downloadEntryType == "System")
819 {
Abhilash Rajub5f288d2023-11-08 22:32:44 -0600820 if (!asyncResp->res.openFd(fd, bmcweb::EncodingType::Base64))
821 {
822 messages::internalError(asyncResp->res);
823 close(fd);
824 return;
825 }
Carson Labrado168d1b12023-03-27 17:04:46 +0000826 asyncResp->res.addHeader(
827 boost::beast::http::field::content_transfer_encoding, "Base64");
Abhilash Rajub5f288d2023-11-08 22:32:44 -0600828 return;
Carson Labrado168d1b12023-03-27 17:04:46 +0000829 }
Abhilash Rajub5f288d2023-11-08 22:32:44 -0600830 if (!asyncResp->res.openFd(fd))
Ed Tanous27b0cf92023-08-07 12:02:40 -0700831 {
Abhilash Rajub5f288d2023-11-08 22:32:44 -0600832 messages::internalError(asyncResp->res);
833 close(fd);
834 return;
Ed Tanous27b0cf92023-08-07 12:02:40 -0700835 }
Carson Labrado168d1b12023-03-27 17:04:46 +0000836 asyncResp->res.addHeader(boost::beast::http::field::content_type,
837 "application/octet-stream");
838}
839
840inline void
841 downloadDumpEntry(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
842 const std::string& entryID, const std::string& dumpType)
843{
844 if (dumpType != "BMC")
845 {
846 BMCWEB_LOG_WARNING("Can't find Dump Entry {}", entryID);
847 messages::resourceNotFound(asyncResp->res, dumpType + " dump", entryID);
848 return;
849 }
850
Ed Tanous18f8f602023-07-18 10:07:23 -0700851 std::string dumpEntryPath = std::format("{}/entry/{}",
852 getDumpPath(dumpType), entryID);
Carson Labrado168d1b12023-03-27 17:04:46 +0000853
854 auto downloadDumpEntryHandler =
855 [asyncResp, entryID,
856 dumpType](const boost::system::error_code& ec,
857 const sdbusplus::message::unix_fd& unixfd) {
858 downloadEntryCallback(asyncResp, entryID, dumpType, ec, unixfd);
859 };
860
861 crow::connections::systemBus->async_method_call(
862 std::move(downloadDumpEntryHandler), "xyz.openbmc_project.Dump.Manager",
863 dumpEntryPath, "xyz.openbmc_project.Dump.Entry", "GetFileHandle");
864}
865
866inline void
867 downloadEventLogEntry(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
868 const std::string& systemName,
869 const std::string& entryID,
870 const std::string& dumpType)
871{
872 if constexpr (bmcwebEnableMultiHost)
873 {
874 // Option currently returns no systems. TBD
875 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
876 systemName);
877 return;
878 }
879 if (systemName != "system")
880 {
881 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
882 systemName);
883 return;
884 }
885
886 std::string entryPath =
887 sdbusplus::message::object_path("/xyz/openbmc_project/logging/entry") /
888 entryID;
889
890 auto downloadEventLogEntryHandler =
891 [asyncResp, entryID,
892 dumpType](const boost::system::error_code& ec,
893 const sdbusplus::message::unix_fd& unixfd) {
894 downloadEntryCallback(asyncResp, entryID, dumpType, ec, unixfd);
895 };
896
897 crow::connections::systemBus->async_method_call(
898 std::move(downloadEventLogEntryHandler), "xyz.openbmc_project.Logging",
899 entryPath, "xyz.openbmc_project.Logging.Entry", "GetEntry");
900}
901
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600902inline DumpCreationProgress
903 mapDbusStatusToDumpProgress(const std::string& status)
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500904{
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600905 if (status ==
906 "xyz.openbmc_project.Common.Progress.OperationStatus.Failed" ||
907 status == "xyz.openbmc_project.Common.Progress.OperationStatus.Aborted")
908 {
909 return DumpCreationProgress::DUMP_CREATE_FAILED;
910 }
911 if (status ==
912 "xyz.openbmc_project.Common.Progress.OperationStatus.Completed")
913 {
914 return DumpCreationProgress::DUMP_CREATE_SUCCESS;
915 }
916 return DumpCreationProgress::DUMP_CREATE_INPROGRESS;
917}
918
919inline DumpCreationProgress
920 getDumpCompletionStatus(const dbus::utility::DBusPropertiesMap& values)
921{
922 for (const auto& [key, val] : values)
923 {
924 if (key == "Status")
Ed Tanous002d39b2022-05-31 08:59:27 -0700925 {
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600926 const std::string* value = std::get_if<std::string>(&val);
927 if (value == nullptr)
928 {
Ed Tanous62598e32023-07-17 17:06:25 -0700929 BMCWEB_LOG_ERROR("Status property value is null");
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600930 return DumpCreationProgress::DUMP_CREATE_FAILED;
931 }
932 return mapDbusStatusToDumpProgress(*value);
933 }
934 }
935 return DumpCreationProgress::DUMP_CREATE_INPROGRESS;
936}
937
938inline std::string getDumpEntryPath(const std::string& dumpPath)
939{
940 if (dumpPath == "/xyz/openbmc_project/dump/bmc/entry")
941 {
942 return "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/";
943 }
944 if (dumpPath == "/xyz/openbmc_project/dump/system/entry")
945 {
946 return "/redfish/v1/Systems/system/LogServices/Dump/Entries/";
947 }
948 return "";
949}
950
951inline void createDumpTaskCallback(
952 task::Payload&& payload,
953 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
954 const sdbusplus::message::object_path& createdObjPath)
955{
956 const std::string dumpPath = createdObjPath.parent_path().str;
957 const std::string dumpId = createdObjPath.filename();
958
959 std::string dumpEntryPath = getDumpEntryPath(dumpPath);
960
961 if (dumpEntryPath.empty())
962 {
Ed Tanous62598e32023-07-17 17:06:25 -0700963 BMCWEB_LOG_ERROR("Invalid dump type received");
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600964 messages::internalError(asyncResp->res);
965 return;
966 }
967
968 crow::connections::systemBus->async_method_call(
969 [asyncResp, payload, createdObjPath,
970 dumpEntryPath{std::move(dumpEntryPath)},
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800971 dumpId](const boost::system::error_code& ec,
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600972 const std::string& introspectXml) {
973 if (ec)
974 {
Ed Tanous62598e32023-07-17 17:06:25 -0700975 BMCWEB_LOG_ERROR("Introspect call failed with error: {}",
976 ec.message());
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600977 messages::internalError(asyncResp->res);
978 return;
Ed Tanous002d39b2022-05-31 08:59:27 -0700979 }
980
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600981 // Check if the created dump object has implemented Progress
982 // interface to track dump completion. If yes, fetch the "Status"
983 // property of the interface, modify the task state accordingly.
984 // Else, return task completed.
985 tinyxml2::XMLDocument doc;
Ed Tanous002d39b2022-05-31 08:59:27 -0700986
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600987 doc.Parse(introspectXml.data(), introspectXml.size());
988 tinyxml2::XMLNode* pRoot = doc.FirstChildElement("node");
989 if (pRoot == nullptr)
Ed Tanous002d39b2022-05-31 08:59:27 -0700990 {
Ed Tanous62598e32023-07-17 17:06:25 -0700991 BMCWEB_LOG_ERROR("XML document failed to parse");
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600992 messages::internalError(asyncResp->res);
993 return;
994 }
995 tinyxml2::XMLElement* interfaceNode =
996 pRoot->FirstChildElement("interface");
997
998 bool isProgressIntfPresent = false;
999 while (interfaceNode != nullptr)
1000 {
1001 const char* thisInterfaceName = interfaceNode->Attribute("name");
1002 if (thisInterfaceName != nullptr)
1003 {
1004 if (thisInterfaceName ==
1005 std::string_view("xyz.openbmc_project.Common.Progress"))
1006 {
1007 interfaceNode =
1008 interfaceNode->NextSiblingElement("interface");
1009 continue;
1010 }
1011 isProgressIntfPresent = true;
1012 break;
1013 }
1014 interfaceNode = interfaceNode->NextSiblingElement("interface");
1015 }
1016
1017 std::shared_ptr<task::TaskData> task = task::TaskData::createTask(
1018 [createdObjPath, dumpEntryPath, dumpId, isProgressIntfPresent](
Ed Tanous8b242752023-06-27 17:17:13 -07001019 const boost::system::error_code& ec2, sdbusplus::message_t& msg,
Asmitha Karunanithi8e317782020-12-10 03:35:05 -06001020 const std::shared_ptr<task::TaskData>& taskData) {
Ed Tanous8b242752023-06-27 17:17:13 -07001021 if (ec2)
Asmitha Karunanithi8e317782020-12-10 03:35:05 -06001022 {
Ed Tanous62598e32023-07-17 17:06:25 -07001023 BMCWEB_LOG_ERROR("{}: Error in creating dump",
1024 createdObjPath.str);
Asmitha Karunanithi8e317782020-12-10 03:35:05 -06001025 taskData->messages.emplace_back(messages::internalError());
1026 taskData->state = "Cancelled";
1027 return task::completed;
1028 }
1029
1030 if (isProgressIntfPresent)
1031 {
1032 dbus::utility::DBusPropertiesMap values;
1033 std::string prop;
1034 msg.read(prop, values);
1035
1036 DumpCreationProgress dumpStatus =
1037 getDumpCompletionStatus(values);
1038 if (dumpStatus == DumpCreationProgress::DUMP_CREATE_FAILED)
1039 {
Ed Tanous62598e32023-07-17 17:06:25 -07001040 BMCWEB_LOG_ERROR("{}: Error in creating dump",
1041 createdObjPath.str);
Asmitha Karunanithi8e317782020-12-10 03:35:05 -06001042 taskData->state = "Cancelled";
1043 return task::completed;
1044 }
1045
1046 if (dumpStatus == DumpCreationProgress::DUMP_CREATE_INPROGRESS)
1047 {
Ed Tanous62598e32023-07-17 17:06:25 -07001048 BMCWEB_LOG_DEBUG("{}: Dump creation task is in progress",
1049 createdObjPath.str);
Asmitha Karunanithi8e317782020-12-10 03:35:05 -06001050 return !task::completed;
1051 }
1052 }
1053
Ed Tanous002d39b2022-05-31 08:59:27 -07001054 nlohmann::json retMessage = messages::success();
1055 taskData->messages.emplace_back(retMessage);
1056
Ed Tanousc51a58e2023-03-27 14:43:19 -07001057 boost::urls::url url = boost::urls::format(
1058 "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/{}", dumpId);
1059
1060 std::string headerLoc = "Location: ";
1061 headerLoc += url.buffer();
1062
Ed Tanous002d39b2022-05-31 08:59:27 -07001063 taskData->payload->httpHeaders.emplace_back(std::move(headerLoc));
1064
Ed Tanous62598e32023-07-17 17:06:25 -07001065 BMCWEB_LOG_DEBUG("{}: Dump creation task completed",
1066 createdObjPath.str);
Ed Tanous002d39b2022-05-31 08:59:27 -07001067 taskData->state = "Completed";
1068 return task::completed;
Patrick Williams5a39f772023-10-20 11:20:21 -05001069 },
Asmitha Karunanithi8e317782020-12-10 03:35:05 -06001070 "type='signal',interface='org.freedesktop.DBus.Properties',"
1071 "member='PropertiesChanged',path='" +
1072 createdObjPath.str + "'");
Asmitha Karunanithia43be802020-05-07 05:05:36 -05001073
Asmitha Karunanithi8e317782020-12-10 03:35:05 -06001074 // The task timer is set to max time limit within which the
1075 // requested dump will be collected.
1076 task->startTimer(std::chrono::minutes(6));
1077 task->populateResp(asyncResp->res);
1078 task->payload.emplace(payload);
Patrick Williams5a39f772023-10-20 11:20:21 -05001079 },
Asmitha Karunanithi8e317782020-12-10 03:35:05 -06001080 "xyz.openbmc_project.Dump.Manager", createdObjPath,
1081 "org.freedesktop.DBus.Introspectable", "Introspect");
Asmitha Karunanithia43be802020-05-07 05:05:36 -05001082}
1083
zhanghch058d1b46d2021-04-01 11:18:24 +08001084inline void createDump(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1085 const crow::Request& req, const std::string& dumpType)
Asmitha Karunanithia43be802020-05-07 05:05:36 -05001086{
Claire Weinanfdd26902022-03-01 14:18:25 -08001087 std::string dumpPath = getDumpEntriesPath(dumpType);
1088 if (dumpPath.empty())
Asmitha Karunanithia43be802020-05-07 05:05:36 -05001089 {
Asmitha Karunanithia43be802020-05-07 05:05:36 -05001090 messages::internalError(asyncResp->res);
1091 return;
1092 }
1093
1094 std::optional<std::string> diagnosticDataType;
1095 std::optional<std::string> oemDiagnosticDataType;
1096
Willy Tu15ed6782021-12-14 11:03:16 -08001097 if (!redfish::json_util::readJsonAction(
Asmitha Karunanithia43be802020-05-07 05:05:36 -05001098 req, asyncResp->res, "DiagnosticDataType", diagnosticDataType,
1099 "OEMDiagnosticDataType", oemDiagnosticDataType))
1100 {
1101 return;
1102 }
1103
1104 if (dumpType == "System")
1105 {
1106 if (!oemDiagnosticDataType || !diagnosticDataType)
1107 {
Ed Tanous62598e32023-07-17 17:06:25 -07001108 BMCWEB_LOG_ERROR(
1109 "CreateDump action parameter 'DiagnosticDataType'/'OEMDiagnosticDataType' value not found!");
Asmitha Karunanithia43be802020-05-07 05:05:36 -05001110 messages::actionParameterMissing(
1111 asyncResp->res, "CollectDiagnosticData",
1112 "DiagnosticDataType & OEMDiagnosticDataType");
1113 return;
1114 }
Ed Tanous3174e4d2020-10-07 11:41:22 -07001115 if ((*oemDiagnosticDataType != "System") ||
1116 (*diagnosticDataType != "OEM"))
Asmitha Karunanithia43be802020-05-07 05:05:36 -05001117 {
Ed Tanous62598e32023-07-17 17:06:25 -07001118 BMCWEB_LOG_ERROR("Wrong parameter values passed");
Ed Tanousace85d62021-10-26 12:45:59 -07001119 messages::internalError(asyncResp->res);
Asmitha Karunanithia43be802020-05-07 05:05:36 -05001120 return;
1121 }
Asmitha Karunanithi59075712021-10-22 01:17:41 -05001122 dumpPath = "/redfish/v1/Systems/system/LogServices/Dump/";
Asmitha Karunanithia43be802020-05-07 05:05:36 -05001123 }
1124 else if (dumpType == "BMC")
1125 {
1126 if (!diagnosticDataType)
1127 {
Ed Tanous62598e32023-07-17 17:06:25 -07001128 BMCWEB_LOG_ERROR(
1129 "CreateDump action parameter 'DiagnosticDataType' not found!");
Asmitha Karunanithia43be802020-05-07 05:05:36 -05001130 messages::actionParameterMissing(
1131 asyncResp->res, "CollectDiagnosticData", "DiagnosticDataType");
1132 return;
1133 }
Ed Tanous3174e4d2020-10-07 11:41:22 -07001134 if (*diagnosticDataType != "Manager")
Asmitha Karunanithia43be802020-05-07 05:05:36 -05001135 {
Ed Tanous62598e32023-07-17 17:06:25 -07001136 BMCWEB_LOG_ERROR(
1137 "Wrong parameter value passed for 'DiagnosticDataType'");
Ed Tanousace85d62021-10-26 12:45:59 -07001138 messages::internalError(asyncResp->res);
Asmitha Karunanithia43be802020-05-07 05:05:36 -05001139 return;
1140 }
Asmitha Karunanithi59075712021-10-22 01:17:41 -05001141 dumpPath = "/redfish/v1/Managers/bmc/LogServices/Dump/";
1142 }
1143 else
1144 {
Ed Tanous62598e32023-07-17 17:06:25 -07001145 BMCWEB_LOG_ERROR("CreateDump failed. Unknown dump type");
Asmitha Karunanithi59075712021-10-22 01:17:41 -05001146 messages::internalError(asyncResp->res);
1147 return;
Asmitha Karunanithia43be802020-05-07 05:05:36 -05001148 }
1149
Asmitha Karunanithi8e317782020-12-10 03:35:05 -06001150 std::vector<std::pair<std::string, std::variant<std::string, uint64_t>>>
1151 createDumpParamVec;
1152
Carson Labradof574a8e2023-03-22 02:26:00 +00001153 if (req.session != nullptr)
1154 {
1155 createDumpParamVec.emplace_back(
1156 "xyz.openbmc_project.Dump.Create.CreateParameters.OriginatorId",
1157 req.session->clientIp);
1158 createDumpParamVec.emplace_back(
1159 "xyz.openbmc_project.Dump.Create.CreateParameters.OriginatorType",
1160 "xyz.openbmc_project.Common.OriginatedBy.OriginatorTypes.Client");
1161 }
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -06001162
Asmitha Karunanithia43be802020-05-07 05:05:36 -05001163 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001164 [asyncResp, payload(task::Payload(req)),
1165 dumpPath](const boost::system::error_code& ec,
1166 const sdbusplus::message_t& msg,
1167 const sdbusplus::message::object_path& objPath) mutable {
Ed Tanous002d39b2022-05-31 08:59:27 -07001168 if (ec)
1169 {
Ed Tanous62598e32023-07-17 17:06:25 -07001170 BMCWEB_LOG_ERROR("CreateDump resp_handler got error {}", ec);
Asmitha Karunanithi59075712021-10-22 01:17:41 -05001171 const sd_bus_error* dbusError = msg.get_error();
1172 if (dbusError == nullptr)
1173 {
1174 messages::internalError(asyncResp->res);
1175 return;
1176 }
1177
Ed Tanous62598e32023-07-17 17:06:25 -07001178 BMCWEB_LOG_ERROR("CreateDump DBus error: {} and error msg: {}",
1179 dbusError->name, dbusError->message);
Asmitha Karunanithi59075712021-10-22 01:17:41 -05001180 if (std::string_view(
1181 "xyz.openbmc_project.Common.Error.NotAllowed") ==
1182 dbusError->name)
1183 {
1184 messages::resourceInStandby(asyncResp->res);
1185 return;
1186 }
1187 if (std::string_view(
1188 "xyz.openbmc_project.Dump.Create.Error.Disabled") ==
1189 dbusError->name)
1190 {
1191 messages::serviceDisabled(asyncResp->res, dumpPath);
1192 return;
1193 }
1194 if (std::string_view(
1195 "xyz.openbmc_project.Common.Error.Unavailable") ==
1196 dbusError->name)
1197 {
1198 messages::resourceInUse(asyncResp->res);
1199 return;
1200 }
1201 // Other Dbus errors such as:
1202 // xyz.openbmc_project.Common.Error.InvalidArgument &
1203 // org.freedesktop.DBus.Error.InvalidArgs are all related to
1204 // the dbus call that is made here in the bmcweb
1205 // implementation and has nothing to do with the client's
1206 // input in the request. Hence, returning internal error
1207 // back to the client.
Ed Tanous002d39b2022-05-31 08:59:27 -07001208 messages::internalError(asyncResp->res);
1209 return;
1210 }
Ed Tanous62598e32023-07-17 17:06:25 -07001211 BMCWEB_LOG_DEBUG("Dump Created. Path: {}", objPath.str);
Asmitha Karunanithi8e317782020-12-10 03:35:05 -06001212 createDumpTaskCallback(std::move(payload), asyncResp, objPath);
Patrick Williams5a39f772023-10-20 11:20:21 -05001213 },
Ed Tanous18f8f602023-07-18 10:07:23 -07001214 "xyz.openbmc_project.Dump.Manager", getDumpPath(dumpType),
Asmitha Karunanithi8e317782020-12-10 03:35:05 -06001215 "xyz.openbmc_project.Dump.Create", "CreateDump", createDumpParamVec);
Asmitha Karunanithia43be802020-05-07 05:05:36 -05001216}
1217
zhanghch058d1b46d2021-04-01 11:18:24 +08001218inline void clearDump(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1219 const std::string& dumpType)
Asmitha Karunanithi80319af2020-05-07 05:30:21 -05001220{
Claire Weinan0d946212022-07-13 19:40:19 -07001221 crow::connections::systemBus->async_method_call(
1222 [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001223 if (ec)
1224 {
Ed Tanous62598e32023-07-17 17:06:25 -07001225 BMCWEB_LOG_ERROR("clearDump resp_handler got error {}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -07001226 messages::internalError(asyncResp->res);
1227 return;
1228 }
Patrick Williams5a39f772023-10-20 11:20:21 -05001229 },
Ed Tanous18f8f602023-07-18 10:07:23 -07001230 "xyz.openbmc_project.Dump.Manager", getDumpPath(dumpType),
Claire Weinan0d946212022-07-13 19:40:19 -07001231 "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
Asmitha Karunanithi80319af2020-05-07 05:30:21 -05001232}
1233
Ed Tanousb9d36b42022-02-26 21:42:46 -08001234inline static void
1235 parseCrashdumpParameters(const dbus::utility::DBusPropertiesMap& params,
1236 std::string& filename, std::string& timestamp,
1237 std::string& logfile)
Johnathan Mantey043a0532020-03-10 17:15:28 -07001238{
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001239 const std::string* filenamePtr = nullptr;
1240 const std::string* timestampPtr = nullptr;
1241 const std::string* logfilePtr = nullptr;
1242
1243 const bool success = sdbusplus::unpackPropertiesNoThrow(
1244 dbus_utils::UnpackErrorPrinter(), params, "Timestamp", timestampPtr,
1245 "Filename", filenamePtr, "Log", logfilePtr);
1246
1247 if (!success)
Johnathan Mantey043a0532020-03-10 17:15:28 -07001248 {
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001249 return;
1250 }
1251
1252 if (filenamePtr != nullptr)
1253 {
1254 filename = *filenamePtr;
1255 }
1256
1257 if (timestampPtr != nullptr)
1258 {
1259 timestamp = *timestampPtr;
1260 }
1261
1262 if (logfilePtr != nullptr)
1263 {
1264 logfile = *logfilePtr;
Johnathan Mantey043a0532020-03-10 17:15:28 -07001265 }
1266}
1267
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001268inline void requestRoutesSystemLogServiceCollection(App& app)
Ed Tanous1da66f72018-07-27 16:13:37 -07001269{
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001270 /**
1271 * Functions triggers appropriate requests on DBus
1272 */
Ed Tanous22d268c2022-05-19 09:39:07 -07001273 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/")
Ed Tanoused398212021-06-09 17:05:54 -07001274 .privileges(redfish::privileges::getLogServiceCollection)
Ed Tanous002d39b2022-05-31 08:59:27 -07001275 .methods(boost::beast::http::verb::get)(
1276 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07001277 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1278 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001279 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001280 {
1281 return;
1282 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08001283 if constexpr (bmcwebEnableMultiHost)
1284 {
1285 // Option currently returns no systems. TBD
1286 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1287 systemName);
1288 return;
1289 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001290 if (systemName != "system")
1291 {
1292 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1293 systemName);
1294 return;
1295 }
1296
Ed Tanous002d39b2022-05-31 08:59:27 -07001297 // Collections don't include the static data added by SubRoute
1298 // because it has a duplicate entry for members
1299 asyncResp->res.jsonValue["@odata.type"] =
1300 "#LogServiceCollection.LogServiceCollection";
1301 asyncResp->res.jsonValue["@odata.id"] =
1302 "/redfish/v1/Systems/system/LogServices";
1303 asyncResp->res.jsonValue["Name"] = "System Log Services Collection";
1304 asyncResp->res.jsonValue["Description"] =
1305 "Collection of LogServices for this Computer System";
1306 nlohmann::json& logServiceArray = asyncResp->res.jsonValue["Members"];
1307 logServiceArray = nlohmann::json::array();
1308 nlohmann::json::object_t eventLog;
1309 eventLog["@odata.id"] =
1310 "/redfish/v1/Systems/system/LogServices/EventLog";
Patrick Williamsb2ba3072023-05-12 10:27:39 -05001311 logServiceArray.emplace_back(std::move(eventLog));
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -05001312#ifdef BMCWEB_ENABLE_REDFISH_DUMP_LOG
Ed Tanous002d39b2022-05-31 08:59:27 -07001313 nlohmann::json::object_t dumpLog;
1314 dumpLog["@odata.id"] = "/redfish/v1/Systems/system/LogServices/Dump";
Patrick Williamsb2ba3072023-05-12 10:27:39 -05001315 logServiceArray.emplace_back(std::move(dumpLog));
raviteja-bc9bb6862020-02-03 11:53:32 -06001316#endif
1317
Jason M. Billsd53dd412019-02-12 17:16:22 -08001318#ifdef BMCWEB_ENABLE_REDFISH_CPU_LOG
Ed Tanous002d39b2022-05-31 08:59:27 -07001319 nlohmann::json::object_t crashdump;
1320 crashdump["@odata.id"] =
1321 "/redfish/v1/Systems/system/LogServices/Crashdump";
Patrick Williamsb2ba3072023-05-12 10:27:39 -05001322 logServiceArray.emplace_back(std::move(crashdump));
Jason M. Billsd53dd412019-02-12 17:16:22 -08001323#endif
Spencer Kub7028eb2021-10-26 15:27:35 +08001324
1325#ifdef BMCWEB_ENABLE_REDFISH_HOST_LOGGER
Ed Tanous002d39b2022-05-31 08:59:27 -07001326 nlohmann::json::object_t hostlogger;
1327 hostlogger["@odata.id"] =
1328 "/redfish/v1/Systems/system/LogServices/HostLogger";
Patrick Williamsb2ba3072023-05-12 10:27:39 -05001329 logServiceArray.emplace_back(std::move(hostlogger));
Spencer Kub7028eb2021-10-26 15:27:35 +08001330#endif
Ed Tanous002d39b2022-05-31 08:59:27 -07001331 asyncResp->res.jsonValue["Members@odata.count"] =
1332 logServiceArray.size();
ZhikuiRena3316fc2020-01-29 14:58:08 -08001333
George Liu7a1dbc42022-12-07 16:03:22 +08001334 constexpr std::array<std::string_view, 1> interfaces = {
1335 "xyz.openbmc_project.State.Boot.PostCode"};
1336 dbus::utility::getSubTreePaths(
1337 "/", 0, interfaces,
1338 [asyncResp](const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07001339 const dbus::utility::MapperGetSubTreePathsResponse&
1340 subtreePath) {
1341 if (ec)
1342 {
Ed Tanous62598e32023-07-17 17:06:25 -07001343 BMCWEB_LOG_ERROR("{}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -07001344 return;
1345 }
Ed Tanous45ca1b82022-03-25 13:07:27 -07001346
Ed Tanous002d39b2022-05-31 08:59:27 -07001347 for (const auto& pathStr : subtreePath)
1348 {
1349 if (pathStr.find("PostCode") != std::string::npos)
1350 {
1351 nlohmann::json& logServiceArrayLocal =
1352 asyncResp->res.jsonValue["Members"];
Ed Tanous613dabe2022-07-09 11:17:36 -07001353 nlohmann::json::object_t member;
1354 member["@odata.id"] =
1355 "/redfish/v1/Systems/system/LogServices/PostCodes";
1356
Patrick Williamsb2ba3072023-05-12 10:27:39 -05001357 logServiceArrayLocal.emplace_back(std::move(member));
Ed Tanous613dabe2022-07-09 11:17:36 -07001358
Ed Tanous002d39b2022-05-31 08:59:27 -07001359 asyncResp->res.jsonValue["Members@odata.count"] =
1360 logServiceArrayLocal.size();
1361 return;
1362 }
1363 }
Ed Tanous45ca1b82022-03-25 13:07:27 -07001364 });
Patrick Williams5a39f772023-10-20 11:20:21 -05001365 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001366}
1367
1368inline void requestRoutesEventLogService(App& app)
1369{
Ed Tanous22d268c2022-05-19 09:39:07 -07001370 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/EventLog/")
Ed Tanoused398212021-06-09 17:05:54 -07001371 .privileges(redfish::privileges::getLogService)
Ed Tanous002d39b2022-05-31 08:59:27 -07001372 .methods(boost::beast::http::verb::get)(
1373 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07001374 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1375 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001376 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001377 {
1378 return;
1379 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001380 if (systemName != "system")
1381 {
1382 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1383 systemName);
1384 return;
1385 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001386 asyncResp->res.jsonValue["@odata.id"] =
1387 "/redfish/v1/Systems/system/LogServices/EventLog";
1388 asyncResp->res.jsonValue["@odata.type"] =
Janet Adkinsb25644a2023-08-16 11:23:45 -05001389 "#LogService.v1_2_0.LogService";
Ed Tanous002d39b2022-05-31 08:59:27 -07001390 asyncResp->res.jsonValue["Name"] = "Event Log Service";
1391 asyncResp->res.jsonValue["Description"] = "System Event Log Service";
1392 asyncResp->res.jsonValue["Id"] = "EventLog";
1393 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
Tejas Patil7c8c4052021-06-04 17:43:14 +05301394
Ed Tanous002d39b2022-05-31 08:59:27 -07001395 std::pair<std::string, std::string> redfishDateTimeOffset =
Ed Tanous2b829372022-08-03 14:22:34 -07001396 redfish::time_utils::getDateTimeOffsetNow();
Tejas Patil7c8c4052021-06-04 17:43:14 +05301397
Ed Tanous002d39b2022-05-31 08:59:27 -07001398 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
1399 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
1400 redfishDateTimeOffset.second;
Tejas Patil7c8c4052021-06-04 17:43:14 +05301401
Ed Tanous002d39b2022-05-31 08:59:27 -07001402 asyncResp->res.jsonValue["Entries"]["@odata.id"] =
1403 "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
1404 asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"] = {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001405
Ed Tanous002d39b2022-05-31 08:59:27 -07001406 {"target",
1407 "/redfish/v1/Systems/system/LogServices/EventLog/Actions/LogService.ClearLog"}};
Patrick Williams5a39f772023-10-20 11:20:21 -05001408 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001409}
1410
1411inline void requestRoutesJournalEventLogClear(App& app)
1412{
Jason M. Bills4978b632022-02-22 14:17:43 -08001413 BMCWEB_ROUTE(
1414 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07001415 "/redfish/v1/Systems/<str>/LogServices/EventLog/Actions/LogService.ClearLog/")
Ed Tanous432a8902021-06-14 15:28:56 -07001416 .privileges({{"ConfigureComponents"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001417 .methods(boost::beast::http::verb::post)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07001418 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07001419 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1420 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001421 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001422 {
1423 return;
1424 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001425 if (systemName != "system")
1426 {
1427 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1428 systemName);
1429 return;
1430 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001431 // Clear the EventLog by deleting the log files
1432 std::vector<std::filesystem::path> redfishLogFiles;
1433 if (getRedfishLogFiles(redfishLogFiles))
1434 {
1435 for (const std::filesystem::path& file : redfishLogFiles)
1436 {
1437 std::error_code ec;
1438 std::filesystem::remove(file, ec);
1439 }
1440 }
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001441
Ed Tanous002d39b2022-05-31 08:59:27 -07001442 // Reload rsyslog so it knows to start new log files
1443 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001444 [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001445 if (ec)
1446 {
Ed Tanous62598e32023-07-17 17:06:25 -07001447 BMCWEB_LOG_ERROR("Failed to reload rsyslog: {}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -07001448 messages::internalError(asyncResp->res);
1449 return;
1450 }
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001451
Ed Tanous002d39b2022-05-31 08:59:27 -07001452 messages::success(asyncResp->res);
Patrick Williams5a39f772023-10-20 11:20:21 -05001453 },
Ed Tanous002d39b2022-05-31 08:59:27 -07001454 "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
1455 "org.freedesktop.systemd1.Manager", "ReloadUnit", "rsyslog.service",
1456 "replace");
Patrick Williams5a39f772023-10-20 11:20:21 -05001457 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001458}
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001459
Jason M. Billsac992cd2022-06-24 13:31:46 -07001460enum class LogParseError
1461{
1462 success,
1463 parseFailed,
1464 messageIdNotInRegistry,
1465};
1466
1467static LogParseError
1468 fillEventLogEntryJson(const std::string& logEntryID,
1469 const std::string& logEntry,
1470 nlohmann::json::object_t& logEntryJson)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001471{
Jason M. Bills95820182019-04-22 16:25:34 -07001472 // The redfish log format is "<Timestamp> <MessageId>,<MessageArgs>"
Jason M. Billscd225da2019-05-08 15:31:57 -07001473 // First get the Timestamp
Ed Tanousf23b7292020-10-15 09:41:17 -07001474 size_t space = logEntry.find_first_of(' ');
Jason M. Billscd225da2019-05-08 15:31:57 -07001475 if (space == std::string::npos)
Jason M. Bills95820182019-04-22 16:25:34 -07001476 {
Jason M. Billsac992cd2022-06-24 13:31:46 -07001477 return LogParseError::parseFailed;
Jason M. Bills95820182019-04-22 16:25:34 -07001478 }
Jason M. Billscd225da2019-05-08 15:31:57 -07001479 std::string timestamp = logEntry.substr(0, space);
1480 // Then get the log contents
Ed Tanousf23b7292020-10-15 09:41:17 -07001481 size_t entryStart = logEntry.find_first_not_of(' ', space);
Jason M. Billscd225da2019-05-08 15:31:57 -07001482 if (entryStart == std::string::npos)
1483 {
Jason M. Billsac992cd2022-06-24 13:31:46 -07001484 return LogParseError::parseFailed;
Jason M. Billscd225da2019-05-08 15:31:57 -07001485 }
1486 std::string_view entry(logEntry);
1487 entry.remove_prefix(entryStart);
1488 // Use split to separate the entry into its fields
1489 std::vector<std::string> logEntryFields;
Ed Tanous50ebd4a2023-01-19 19:03:17 -08001490 bmcweb::split(logEntryFields, entry, ',');
Jason M. Billscd225da2019-05-08 15:31:57 -07001491 // We need at least a MessageId to be valid
Ed Tanous1e6deaf2022-02-17 11:32:37 -08001492 auto logEntryIter = logEntryFields.begin();
1493 if (logEntryIter == logEntryFields.end())
Jason M. Billscd225da2019-05-08 15:31:57 -07001494 {
Jason M. Billsac992cd2022-06-24 13:31:46 -07001495 return LogParseError::parseFailed;
Jason M. Billscd225da2019-05-08 15:31:57 -07001496 }
Ed Tanous1e6deaf2022-02-17 11:32:37 -08001497 std::string& messageID = *logEntryIter;
Jason M. Bills4851d452019-03-28 11:27:48 -07001498 // Get the Message from the MessageRegistry
Ed Tanousfffb8c12022-02-07 23:53:03 -08001499 const registries::Message* message = registries::getMessage(messageID);
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001500
Ed Tanous1e6deaf2022-02-17 11:32:37 -08001501 logEntryIter++;
Sui Chen54417b02022-03-24 14:59:52 -07001502 if (message == nullptr)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001503 {
Ed Tanous62598e32023-07-17 17:06:25 -07001504 BMCWEB_LOG_WARNING("Log entry not found in registry: {}", logEntry);
Jason M. Billsac992cd2022-06-24 13:31:46 -07001505 return LogParseError::messageIdNotInRegistry;
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001506 }
1507
Ed Tanous1e6deaf2022-02-17 11:32:37 -08001508 std::vector<std::string_view> messageArgs(logEntryIter,
1509 logEntryFields.end());
Ed Tanousc05bba42023-06-28 08:33:29 -07001510 messageArgs.resize(message->numberOfArgs);
1511
Ed Tanous1e6deaf2022-02-17 11:32:37 -08001512 std::string msg = redfish::registries::fillMessageArgs(messageArgs,
1513 message->message);
1514 if (msg.empty())
Jason M. Bills4851d452019-03-28 11:27:48 -07001515 {
Ed Tanous1e6deaf2022-02-17 11:32:37 -08001516 return LogParseError::parseFailed;
Jason M. Bills4851d452019-03-28 11:27:48 -07001517 }
1518
Jason M. Bills95820182019-04-22 16:25:34 -07001519 // Get the Created time from the timestamp. The log timestamp is in RFC3339
1520 // format which matches the Redfish format except for the fractional seconds
1521 // between the '.' and the '+', so just remove them.
Ed Tanousf23b7292020-10-15 09:41:17 -07001522 std::size_t dot = timestamp.find_first_of('.');
1523 std::size_t plus = timestamp.find_first_of('+');
Jason M. Bills95820182019-04-22 16:25:34 -07001524 if (dot != std::string::npos && plus != std::string::npos)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001525 {
Jason M. Bills95820182019-04-22 16:25:34 -07001526 timestamp.erase(dot, plus - dot);
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001527 }
1528
1529 // Fill in the log entry with the gathered data
Vijay Lobo9c11a172021-10-07 16:53:16 -05001530 logEntryJson["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
Ed Tanousef4c65b2023-04-24 15:28:50 -07001531 logEntryJson["@odata.id"] = boost::urls::format(
1532 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/{}",
1533 logEntryID);
Jason M. Bills84afc482022-06-24 12:38:23 -07001534 logEntryJson["Name"] = "System Event Log Entry";
1535 logEntryJson["Id"] = logEntryID;
1536 logEntryJson["Message"] = std::move(msg);
1537 logEntryJson["MessageId"] = std::move(messageID);
1538 logEntryJson["MessageArgs"] = messageArgs;
1539 logEntryJson["EntryType"] = "Event";
1540 logEntryJson["Severity"] = message->messageSeverity;
1541 logEntryJson["Created"] = std::move(timestamp);
Jason M. Billsac992cd2022-06-24 13:31:46 -07001542 return LogParseError::success;
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001543}
1544
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001545inline void requestRoutesJournalEventLogEntryCollection(App& app)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001546{
Ed Tanous22d268c2022-05-19 09:39:07 -07001547 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/")
Gunnar Mills8b6a35f2021-07-30 14:52:53 -05001548 .privileges(redfish::privileges::getLogEntryCollection)
Ed Tanous002d39b2022-05-31 08:59:27 -07001549 .methods(boost::beast::http::verb::get)(
1550 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07001551 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1552 const std::string& systemName) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001553 query_param::QueryCapabilities capabilities = {
1554 .canDelegateTop = true,
1555 .canDelegateSkip = true,
1556 };
1557 query_param::Query delegatedQuery;
1558 if (!redfish::setUpRedfishRouteWithDelegation(
Carson Labrado3ba00072022-06-06 19:40:56 +00001559 app, req, asyncResp, delegatedQuery, capabilities))
Ed Tanous002d39b2022-05-31 08:59:27 -07001560 {
1561 return;
1562 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08001563 if constexpr (bmcwebEnableMultiHost)
1564 {
1565 // Option currently returns no systems. TBD
1566 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1567 systemName);
1568 return;
1569 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001570 if (systemName != "system")
1571 {
1572 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1573 systemName);
1574 return;
1575 }
1576
Jiaqing Zhao5143f7a2022-07-22 09:33:33 +08001577 size_t top = delegatedQuery.top.value_or(query_param::Query::maxTop);
Ed Tanous3648c8b2022-07-25 13:39:59 -07001578 size_t skip = delegatedQuery.skip.value_or(0);
1579
Ed Tanous002d39b2022-05-31 08:59:27 -07001580 // Collections don't include the static data added by SubRoute
1581 // because it has a duplicate entry for members
1582 asyncResp->res.jsonValue["@odata.type"] =
1583 "#LogEntryCollection.LogEntryCollection";
1584 asyncResp->res.jsonValue["@odata.id"] =
1585 "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
1586 asyncResp->res.jsonValue["Name"] = "System Event Log Entries";
1587 asyncResp->res.jsonValue["Description"] =
1588 "Collection of System Event Log Entries";
1589
1590 nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
1591 logEntryArray = nlohmann::json::array();
1592 // Go through the log files and create a unique ID for each
1593 // entry
1594 std::vector<std::filesystem::path> redfishLogFiles;
1595 getRedfishLogFiles(redfishLogFiles);
1596 uint64_t entryCount = 0;
1597 std::string logEntry;
1598
1599 // Oldest logs are in the last file, so start there and loop
1600 // backwards
1601 for (auto it = redfishLogFiles.rbegin(); it < redfishLogFiles.rend();
1602 it++)
1603 {
1604 std::ifstream logStream(*it);
1605 if (!logStream.is_open())
Jason M. Bills4978b632022-02-22 14:17:43 -08001606 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001607 continue;
Jason M. Bills4978b632022-02-22 14:17:43 -08001608 }
Jason M. Bills897967d2019-07-29 17:05:30 -07001609
Ed Tanous002d39b2022-05-31 08:59:27 -07001610 // Reset the unique ID on the first entry
1611 bool firstEntry = true;
1612 while (std::getline(logStream, logEntry))
Jason M. Bills4978b632022-02-22 14:17:43 -08001613 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001614 std::string idStr;
1615 if (!getUniqueEntryID(logEntry, idStr, firstEntry))
Jason M. Bills4978b632022-02-22 14:17:43 -08001616 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001617 continue;
1618 }
Jason M. Billsefde4ec2022-06-24 08:59:52 -07001619 firstEntry = false;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001620
Jason M. Billsde703c52022-06-23 14:19:04 -07001621 nlohmann::json::object_t bmcLogEntry;
Patrick Williams89492a12023-05-10 07:51:34 -05001622 LogParseError status = fillEventLogEntryJson(idStr, logEntry,
1623 bmcLogEntry);
Jason M. Billsac992cd2022-06-24 13:31:46 -07001624 if (status == LogParseError::messageIdNotInRegistry)
1625 {
1626 continue;
1627 }
1628 if (status != LogParseError::success)
Ed Tanous002d39b2022-05-31 08:59:27 -07001629 {
1630 messages::internalError(asyncResp->res);
1631 return;
Andrew Geisslercb92c032018-08-17 07:56:14 -07001632 }
Jason M. Billsde703c52022-06-23 14:19:04 -07001633
Jason M. Billsde703c52022-06-23 14:19:04 -07001634 entryCount++;
1635 // Handle paging using skip (number of entries to skip from the
1636 // start) and top (number of entries to display)
Ed Tanous3648c8b2022-07-25 13:39:59 -07001637 if (entryCount <= skip || entryCount > skip + top)
Jason M. Billsde703c52022-06-23 14:19:04 -07001638 {
1639 continue;
1640 }
1641
Patrick Williamsb2ba3072023-05-12 10:27:39 -05001642 logEntryArray.emplace_back(std::move(bmcLogEntry));
Jason M. Bills4978b632022-02-22 14:17:43 -08001643 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001644 }
1645 asyncResp->res.jsonValue["Members@odata.count"] = entryCount;
Ed Tanous3648c8b2022-07-25 13:39:59 -07001646 if (skip + top < entryCount)
Ed Tanous002d39b2022-05-31 08:59:27 -07001647 {
1648 asyncResp->res.jsonValue["Members@odata.nextLink"] =
1649 "/redfish/v1/Systems/system/LogServices/EventLog/Entries?$skip=" +
Ed Tanous3648c8b2022-07-25 13:39:59 -07001650 std::to_string(skip + top);
Ed Tanous002d39b2022-05-31 08:59:27 -07001651 }
Patrick Williams5a39f772023-10-20 11:20:21 -05001652 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001653}
Chicago Duan336e96c2019-07-15 14:22:08 +08001654
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001655inline void requestRoutesJournalEventLogEntry(App& app)
1656{
1657 BMCWEB_ROUTE(
Ed Tanous22d268c2022-05-19 09:39:07 -07001658 app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001659 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001660 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07001661 [&app](const crow::Request& req,
1662 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07001663 const std::string& systemName, const std::string& param) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001664 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001665 {
1666 return;
1667 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08001668 if constexpr (bmcwebEnableMultiHost)
1669 {
1670 // Option currently returns no systems. TBD
1671 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1672 systemName);
1673 return;
1674 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001675
1676 if (systemName != "system")
1677 {
1678 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1679 systemName);
1680 return;
1681 }
1682
Ed Tanous002d39b2022-05-31 08:59:27 -07001683 const std::string& targetID = param;
1684
1685 // Go through the log files and check the unique ID for each
1686 // entry to find the target entry
1687 std::vector<std::filesystem::path> redfishLogFiles;
1688 getRedfishLogFiles(redfishLogFiles);
1689 std::string logEntry;
1690
1691 // Oldest logs are in the last file, so start there and loop
1692 // backwards
1693 for (auto it = redfishLogFiles.rbegin(); it < redfishLogFiles.rend();
1694 it++)
1695 {
1696 std::ifstream logStream(*it);
1697 if (!logStream.is_open())
1698 {
1699 continue;
1700 }
1701
1702 // Reset the unique ID on the first entry
1703 bool firstEntry = true;
1704 while (std::getline(logStream, logEntry))
1705 {
1706 std::string idStr;
1707 if (!getUniqueEntryID(logEntry, idStr, firstEntry))
Ed Tanous45ca1b82022-03-25 13:07:27 -07001708 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001709 continue;
1710 }
Jason M. Billsefde4ec2022-06-24 08:59:52 -07001711 firstEntry = false;
Ed Tanous002d39b2022-05-31 08:59:27 -07001712
1713 if (idStr == targetID)
1714 {
Jason M. Billsde703c52022-06-23 14:19:04 -07001715 nlohmann::json::object_t bmcLogEntry;
Jason M. Billsac992cd2022-06-24 13:31:46 -07001716 LogParseError status =
1717 fillEventLogEntryJson(idStr, logEntry, bmcLogEntry);
1718 if (status != LogParseError::success)
Ed Tanous002d39b2022-05-31 08:59:27 -07001719 {
1720 messages::internalError(asyncResp->res);
1721 return;
1722 }
Jason M. Billsd405bb52022-06-24 10:52:05 -07001723 asyncResp->res.jsonValue.update(bmcLogEntry);
Ed Tanous45ca1b82022-03-25 13:07:27 -07001724 return;
1725 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001726 }
1727 }
1728 // Requested ID was not found
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08001729 messages::resourceNotFound(asyncResp->res, "LogEntry", targetID);
Patrick Williams5a39f772023-10-20 11:20:21 -05001730 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001731}
1732
1733inline void requestRoutesDBusEventLogEntryCollection(App& app)
1734{
Ed Tanous22d268c2022-05-19 09:39:07 -07001735 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07001736 .privileges(redfish::privileges::getLogEntryCollection)
Ed Tanous002d39b2022-05-31 08:59:27 -07001737 .methods(boost::beast::http::verb::get)(
1738 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07001739 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1740 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001741 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001742 {
1743 return;
1744 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08001745 if constexpr (bmcwebEnableMultiHost)
1746 {
1747 // Option currently returns no systems. TBD
1748 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1749 systemName);
1750 return;
1751 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001752 if (systemName != "system")
1753 {
1754 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1755 systemName);
1756 return;
1757 }
1758
Ed Tanous002d39b2022-05-31 08:59:27 -07001759 // Collections don't include the static data added by SubRoute
1760 // because it has a duplicate entry for members
1761 asyncResp->res.jsonValue["@odata.type"] =
1762 "#LogEntryCollection.LogEntryCollection";
1763 asyncResp->res.jsonValue["@odata.id"] =
1764 "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
1765 asyncResp->res.jsonValue["Name"] = "System Event Log Entries";
1766 asyncResp->res.jsonValue["Description"] =
1767 "Collection of System Event Log Entries";
1768
1769 // DBus implementation of EventLog/Entries
1770 // Make call to Logging Service to find all log entry objects
George Liu5eb468d2023-06-20 17:03:24 +08001771 sdbusplus::message::object_path path("/xyz/openbmc_project/logging");
1772 dbus::utility::getManagedObjects(
1773 "xyz.openbmc_project.Logging", path,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001774 [asyncResp](const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07001775 const dbus::utility::ManagedObjectType& resp) {
1776 if (ec)
Ed Tanous45ca1b82022-03-25 13:07:27 -07001777 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001778 // TODO Handle for specific error code
Ed Tanous62598e32023-07-17 17:06:25 -07001779 BMCWEB_LOG_ERROR(
1780 "getLogEntriesIfaceData resp_handler got error {}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -07001781 messages::internalError(asyncResp->res);
Ed Tanous45ca1b82022-03-25 13:07:27 -07001782 return;
1783 }
Ed Tanous3544d2a2023-08-06 18:12:20 -07001784 nlohmann::json::array_t entriesArray;
Ed Tanous002d39b2022-05-31 08:59:27 -07001785 for (const auto& objectPath : resp)
1786 {
1787 const uint32_t* id = nullptr;
1788 const uint64_t* timestamp = nullptr;
1789 const uint64_t* updateTimestamp = nullptr;
1790 const std::string* severity = nullptr;
1791 const std::string* message = nullptr;
1792 const std::string* filePath = nullptr;
Vijay Lobo9c11a172021-10-07 16:53:16 -05001793 const std::string* resolution = nullptr;
Ed Tanous002d39b2022-05-31 08:59:27 -07001794 bool resolved = false;
Abhishek Patel9017faf2021-09-14 22:48:55 -05001795 const std::string* notify = nullptr;
1796
Ed Tanous002d39b2022-05-31 08:59:27 -07001797 for (const auto& interfaceMap : objectPath.second)
1798 {
1799 if (interfaceMap.first ==
1800 "xyz.openbmc_project.Logging.Entry")
Xiaochao Ma75710de2021-01-21 17:56:02 +08001801 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001802 for (const auto& propertyMap : interfaceMap.second)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001803 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001804 if (propertyMap.first == "Id")
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001805 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001806 id = std::get_if<uint32_t>(&propertyMap.second);
1807 }
1808 else if (propertyMap.first == "Timestamp")
1809 {
1810 timestamp =
1811 std::get_if<uint64_t>(&propertyMap.second);
1812 }
1813 else if (propertyMap.first == "UpdateTimestamp")
1814 {
1815 updateTimestamp =
1816 std::get_if<uint64_t>(&propertyMap.second);
1817 }
1818 else if (propertyMap.first == "Severity")
1819 {
1820 severity = std::get_if<std::string>(
1821 &propertyMap.second);
1822 }
Vijay Lobo9c11a172021-10-07 16:53:16 -05001823 else if (propertyMap.first == "Resolution")
1824 {
1825 resolution = std::get_if<std::string>(
1826 &propertyMap.second);
1827 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001828 else if (propertyMap.first == "Message")
1829 {
1830 message = std::get_if<std::string>(
1831 &propertyMap.second);
1832 }
1833 else if (propertyMap.first == "Resolved")
1834 {
1835 const bool* resolveptr =
1836 std::get_if<bool>(&propertyMap.second);
1837 if (resolveptr == nullptr)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001838 {
1839 messages::internalError(asyncResp->res);
1840 return;
1841 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001842 resolved = *resolveptr;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001843 }
Abhishek Patel9017faf2021-09-14 22:48:55 -05001844 else if (propertyMap.first ==
1845 "ServiceProviderNotify")
1846 {
1847 notify = std::get_if<std::string>(
1848 &propertyMap.second);
1849 if (notify == nullptr)
1850 {
1851 messages::internalError(asyncResp->res);
1852 return;
1853 }
1854 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001855 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001856 if (id == nullptr || message == nullptr ||
Ed Tanous002d39b2022-05-31 08:59:27 -07001857 severity == nullptr)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001858 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001859 messages::internalError(asyncResp->res);
1860 return;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001861 }
1862 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001863 else if (interfaceMap.first ==
1864 "xyz.openbmc_project.Common.FilePath")
1865 {
1866 for (const auto& propertyMap : interfaceMap.second)
1867 {
1868 if (propertyMap.first == "Path")
1869 {
1870 filePath = std::get_if<std::string>(
1871 &propertyMap.second);
1872 }
1873 }
1874 }
1875 }
1876 // Object path without the
1877 // xyz.openbmc_project.Logging.Entry interface, ignore
1878 // and continue.
1879 if (id == nullptr || message == nullptr ||
1880 severity == nullptr || timestamp == nullptr ||
1881 updateTimestamp == nullptr)
1882 {
1883 continue;
1884 }
Ed Tanous3544d2a2023-08-06 18:12:20 -07001885 nlohmann::json& thisEntry = entriesArray.emplace_back();
Vijay Lobo9c11a172021-10-07 16:53:16 -05001886 thisEntry["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
Ed Tanousef4c65b2023-04-24 15:28:50 -07001887 thisEntry["@odata.id"] = boost::urls::format(
1888 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/{}",
1889 std::to_string(*id));
Ed Tanous002d39b2022-05-31 08:59:27 -07001890 thisEntry["Name"] = "System Event Log Entry";
1891 thisEntry["Id"] = std::to_string(*id);
1892 thisEntry["Message"] = *message;
1893 thisEntry["Resolved"] = resolved;
Vijay Lobo9c11a172021-10-07 16:53:16 -05001894 if ((resolution != nullptr) && (!(*resolution).empty()))
1895 {
1896 thisEntry["Resolution"] = *resolution;
1897 }
Abhishek Patel9017faf2021-09-14 22:48:55 -05001898 std::optional<bool> notifyAction =
1899 getProviderNotifyAction(*notify);
1900 if (notifyAction)
1901 {
1902 thisEntry["ServiceProviderNotified"] = *notifyAction;
1903 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001904 thisEntry["EntryType"] = "Event";
1905 thisEntry["Severity"] =
1906 translateSeverityDbusToRedfish(*severity);
1907 thisEntry["Created"] =
Ed Tanous2b829372022-08-03 14:22:34 -07001908 redfish::time_utils::getDateTimeUintMs(*timestamp);
Ed Tanous002d39b2022-05-31 08:59:27 -07001909 thisEntry["Modified"] =
Ed Tanous2b829372022-08-03 14:22:34 -07001910 redfish::time_utils::getDateTimeUintMs(*updateTimestamp);
Ed Tanous002d39b2022-05-31 08:59:27 -07001911 if (filePath != nullptr)
1912 {
1913 thisEntry["AdditionalDataURI"] =
1914 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
1915 std::to_string(*id) + "/attachment";
1916 }
1917 }
Ed Tanous3544d2a2023-08-06 18:12:20 -07001918 std::ranges::sort(entriesArray, [](const nlohmann::json& left,
1919 const nlohmann::json& right) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001920 return (left["Id"] <= right["Id"]);
Ed Tanous3544d2a2023-08-06 18:12:20 -07001921 });
Ed Tanous002d39b2022-05-31 08:59:27 -07001922 asyncResp->res.jsonValue["Members@odata.count"] =
1923 entriesArray.size();
Ed Tanous3544d2a2023-08-06 18:12:20 -07001924 asyncResp->res.jsonValue["Members"] = std::move(entriesArray);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001925 });
Patrick Williams5a39f772023-10-20 11:20:21 -05001926 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001927}
Xiaochao Ma75710de2021-01-21 17:56:02 +08001928
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001929inline void requestRoutesDBusEventLogEntry(App& app)
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001930{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001931 BMCWEB_ROUTE(
Ed Tanous22d268c2022-05-19 09:39:07 -07001932 app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001933 .privileges(redfish::privileges::getLogEntry)
Ed Tanous002d39b2022-05-31 08:59:27 -07001934 .methods(boost::beast::http::verb::get)(
1935 [&app](const crow::Request& req,
1936 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07001937 const std::string& systemName, const std::string& param) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001938 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001939 {
1940 return;
1941 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08001942 if constexpr (bmcwebEnableMultiHost)
1943 {
1944 // Option currently returns no systems. TBD
1945 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1946 systemName);
1947 return;
1948 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001949 if (systemName != "system")
1950 {
1951 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1952 systemName);
1953 return;
1954 }
1955
Ed Tanous002d39b2022-05-31 08:59:27 -07001956 std::string entryID = param;
1957 dbus::utility::escapePathForDbus(entryID);
1958
1959 // DBus implementation of EventLog/Entries
1960 // Make call to Logging Service to find all log entry objects
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001961 sdbusplus::asio::getAllProperties(
1962 *crow::connections::systemBus, "xyz.openbmc_project.Logging",
1963 "/xyz/openbmc_project/logging/entry/" + entryID, "",
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001964 [asyncResp, entryID](const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07001965 const dbus::utility::DBusPropertiesMap& resp) {
1966 if (ec.value() == EBADR)
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001967 {
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001968 messages::resourceNotFound(asyncResp->res, "EventLogEntry",
1969 entryID);
Ed Tanous45ca1b82022-03-25 13:07:27 -07001970 return;
1971 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001972 if (ec)
1973 {
Ed Tanous62598e32023-07-17 17:06:25 -07001974 BMCWEB_LOG_ERROR(
1975 "EventLogEntry (DBus) resp_handler got error {}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -07001976 messages::internalError(asyncResp->res);
1977 return;
1978 }
1979 const uint32_t* id = nullptr;
1980 const uint64_t* timestamp = nullptr;
1981 const uint64_t* updateTimestamp = nullptr;
1982 const std::string* severity = nullptr;
1983 const std::string* message = nullptr;
1984 const std::string* filePath = nullptr;
Vijay Lobo9c11a172021-10-07 16:53:16 -05001985 const std::string* resolution = nullptr;
Ed Tanous002d39b2022-05-31 08:59:27 -07001986 bool resolved = false;
Abhishek Patel9017faf2021-09-14 22:48:55 -05001987 const std::string* notify = nullptr;
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001988
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001989 const bool success = sdbusplus::unpackPropertiesNoThrow(
1990 dbus_utils::UnpackErrorPrinter(), resp, "Id", id, "Timestamp",
1991 timestamp, "UpdateTimestamp", updateTimestamp, "Severity",
Vijay Lobo9c11a172021-10-07 16:53:16 -05001992 severity, "Message", message, "Resolved", resolved,
Abhishek Patel9017faf2021-09-14 22:48:55 -05001993 "Resolution", resolution, "Path", filePath,
1994 "ServiceProviderNotify", notify);
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001995
1996 if (!success)
Ed Tanous002d39b2022-05-31 08:59:27 -07001997 {
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001998 messages::internalError(asyncResp->res);
1999 return;
Ed Tanous002d39b2022-05-31 08:59:27 -07002000 }
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02002001
Ed Tanous002d39b2022-05-31 08:59:27 -07002002 if (id == nullptr || message == nullptr || severity == nullptr ||
Abhishek Patel9017faf2021-09-14 22:48:55 -05002003 timestamp == nullptr || updateTimestamp == nullptr ||
2004 notify == nullptr)
Ed Tanous002d39b2022-05-31 08:59:27 -07002005 {
2006 messages::internalError(asyncResp->res);
2007 return;
2008 }
Abhishek Patel9017faf2021-09-14 22:48:55 -05002009
Ed Tanous002d39b2022-05-31 08:59:27 -07002010 asyncResp->res.jsonValue["@odata.type"] =
Vijay Lobo9c11a172021-10-07 16:53:16 -05002011 "#LogEntry.v1_9_0.LogEntry";
Ed Tanousef4c65b2023-04-24 15:28:50 -07002012 asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
2013 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/{}",
2014 std::to_string(*id));
Ed Tanous002d39b2022-05-31 08:59:27 -07002015 asyncResp->res.jsonValue["Name"] = "System Event Log Entry";
2016 asyncResp->res.jsonValue["Id"] = std::to_string(*id);
2017 asyncResp->res.jsonValue["Message"] = *message;
2018 asyncResp->res.jsonValue["Resolved"] = resolved;
Abhishek Patel9017faf2021-09-14 22:48:55 -05002019 std::optional<bool> notifyAction = getProviderNotifyAction(*notify);
2020 if (notifyAction)
2021 {
2022 asyncResp->res.jsonValue["ServiceProviderNotified"] =
2023 *notifyAction;
2024 }
Vijay Lobo9c11a172021-10-07 16:53:16 -05002025 if ((resolution != nullptr) && (!(*resolution).empty()))
2026 {
2027 asyncResp->res.jsonValue["Resolution"] = *resolution;
2028 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002029 asyncResp->res.jsonValue["EntryType"] = "Event";
2030 asyncResp->res.jsonValue["Severity"] =
2031 translateSeverityDbusToRedfish(*severity);
2032 asyncResp->res.jsonValue["Created"] =
Ed Tanous2b829372022-08-03 14:22:34 -07002033 redfish::time_utils::getDateTimeUintMs(*timestamp);
Ed Tanous002d39b2022-05-31 08:59:27 -07002034 asyncResp->res.jsonValue["Modified"] =
Ed Tanous2b829372022-08-03 14:22:34 -07002035 redfish::time_utils::getDateTimeUintMs(*updateTimestamp);
Ed Tanous002d39b2022-05-31 08:59:27 -07002036 if (filePath != nullptr)
2037 {
2038 asyncResp->res.jsonValue["AdditionalDataURI"] =
2039 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
2040 std::to_string(*id) + "/attachment";
2041 }
Ed Tanous45ca1b82022-03-25 13:07:27 -07002042 });
Patrick Williams5a39f772023-10-20 11:20:21 -05002043 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002044
2045 BMCWEB_ROUTE(
Ed Tanous22d268c2022-05-19 09:39:07 -07002046 app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002047 .privileges(redfish::privileges::patchLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002048 .methods(boost::beast::http::verb::patch)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002049 [&app](const crow::Request& req,
2050 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07002051 const std::string& systemName, const std::string& entryId) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002052 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002053 {
2054 return;
2055 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08002056 if constexpr (bmcwebEnableMultiHost)
2057 {
2058 // Option currently returns no systems. TBD
2059 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
2060 systemName);
2061 return;
2062 }
Ed Tanous22d268c2022-05-19 09:39:07 -07002063 if (systemName != "system")
2064 {
2065 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
2066 systemName);
2067 return;
2068 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002069 std::optional<bool> resolved;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002070
Ed Tanous002d39b2022-05-31 08:59:27 -07002071 if (!json_util::readJsonPatch(req, asyncResp->res, "Resolved",
2072 resolved))
2073 {
2074 return;
2075 }
Ed Tanous62598e32023-07-17 17:06:25 -07002076 BMCWEB_LOG_DEBUG("Set Resolved");
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06002077
George Liu9ae226f2023-06-21 17:56:46 +08002078 sdbusplus::asio::setProperty(
2079 *crow::connections::systemBus, "xyz.openbmc_project.Logging",
2080 "/xyz/openbmc_project/logging/entry/" + entryId,
2081 "xyz.openbmc_project.Logging.Entry", "Resolved", *resolved,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08002082 [asyncResp, entryId](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002083 if (ec)
2084 {
Ed Tanous62598e32023-07-17 17:06:25 -07002085 BMCWEB_LOG_DEBUG("DBUS response error {}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -07002086 messages::internalError(asyncResp->res);
2087 return;
2088 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002089 });
Patrick Williams5a39f772023-10-20 11:20:21 -05002090 });
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06002091
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002092 BMCWEB_ROUTE(
Ed Tanous22d268c2022-05-19 09:39:07 -07002093 app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002094 .privileges(redfish::privileges::deleteLogEntry)
2095
Ed Tanous002d39b2022-05-31 08:59:27 -07002096 .methods(boost::beast::http::verb::delete_)(
2097 [&app](const crow::Request& req,
2098 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07002099 const std::string& systemName, const std::string& param) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002100 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002101 {
2102 return;
2103 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08002104 if constexpr (bmcwebEnableMultiHost)
2105 {
2106 // Option currently returns no systems. TBD
2107 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
2108 systemName);
2109 return;
2110 }
Ed Tanous22d268c2022-05-19 09:39:07 -07002111 if (systemName != "system")
2112 {
2113 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
2114 systemName);
2115 return;
2116 }
Ed Tanous62598e32023-07-17 17:06:25 -07002117 BMCWEB_LOG_DEBUG("Do delete single event entries.");
Ed Tanous002d39b2022-05-31 08:59:27 -07002118
2119 std::string entryID = param;
2120
2121 dbus::utility::escapePathForDbus(entryID);
2122
2123 // Process response from Logging service.
Patrick Williams5a39f772023-10-20 11:20:21 -05002124 auto respHandler = [asyncResp,
2125 entryID](const boost::system::error_code& ec) {
Ed Tanous62598e32023-07-17 17:06:25 -07002126 BMCWEB_LOG_DEBUG("EventLogEntry (DBus) doDelete callback: Done");
Ed Tanous002d39b2022-05-31 08:59:27 -07002127 if (ec)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002128 {
Ed Tanous002d39b2022-05-31 08:59:27 -07002129 if (ec.value() == EBADR)
Ed Tanous45ca1b82022-03-25 13:07:27 -07002130 {
Ed Tanous002d39b2022-05-31 08:59:27 -07002131 messages::resourceNotFound(asyncResp->res, "LogEntry",
2132 entryID);
Ed Tanous45ca1b82022-03-25 13:07:27 -07002133 return;
2134 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002135 // TODO Handle for specific error code
Ed Tanous62598e32023-07-17 17:06:25 -07002136 BMCWEB_LOG_ERROR(
2137 "EventLogEntry (DBus) doDelete respHandler got error {}",
2138 ec);
Ed Tanous002d39b2022-05-31 08:59:27 -07002139 asyncResp->res.result(
2140 boost::beast::http::status::internal_server_error);
2141 return;
2142 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002143
Ed Tanous002d39b2022-05-31 08:59:27 -07002144 asyncResp->res.result(boost::beast::http::status::ok);
2145 };
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002146
Ed Tanous002d39b2022-05-31 08:59:27 -07002147 // Make call to Logging service to request Delete Log
2148 crow::connections::systemBus->async_method_call(
2149 respHandler, "xyz.openbmc_project.Logging",
2150 "/xyz/openbmc_project/logging/entry/" + entryID,
2151 "xyz.openbmc_project.Object.Delete", "Delete");
Patrick Williams5a39f772023-10-20 11:20:21 -05002152 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002153}
2154
Spencer Kub7028eb2021-10-26 15:27:35 +08002155constexpr const char* hostLoggerFolderPath = "/var/log/console";
2156
2157inline bool
2158 getHostLoggerFiles(const std::string& hostLoggerFilePath,
2159 std::vector<std::filesystem::path>& hostLoggerFiles)
2160{
2161 std::error_code ec;
2162 std::filesystem::directory_iterator logPath(hostLoggerFilePath, ec);
2163 if (ec)
2164 {
Carson Labradobf2dded2023-08-10 00:37:06 +00002165 BMCWEB_LOG_WARNING("{}", ec.message());
Spencer Kub7028eb2021-10-26 15:27:35 +08002166 return false;
2167 }
2168 for (const std::filesystem::directory_entry& it : logPath)
2169 {
2170 std::string filename = it.path().filename();
2171 // Prefix of each log files is "log". Find the file and save the
2172 // path
Ed Tanous11ba3972022-07-11 09:50:41 -07002173 if (filename.starts_with("log"))
Spencer Kub7028eb2021-10-26 15:27:35 +08002174 {
2175 hostLoggerFiles.emplace_back(it.path());
2176 }
2177 }
2178 // As the log files rotate, they are appended with a ".#" that is higher for
2179 // the older logs. Since we start from oldest logs, sort the name in
2180 // descending order.
2181 std::sort(hostLoggerFiles.rbegin(), hostLoggerFiles.rend(),
2182 AlphanumLess<std::string>());
2183
2184 return true;
2185}
2186
Ed Tanous02cad962022-06-30 16:50:15 -07002187inline bool getHostLoggerEntries(
2188 const std::vector<std::filesystem::path>& hostLoggerFiles, uint64_t skip,
2189 uint64_t top, std::vector<std::string>& logEntries, size_t& logCount)
Spencer Kub7028eb2021-10-26 15:27:35 +08002190{
2191 GzFileReader logFile;
2192
2193 // Go though all log files and expose host logs.
2194 for (const std::filesystem::path& it : hostLoggerFiles)
2195 {
2196 if (!logFile.gzGetLines(it.string(), skip, top, logEntries, logCount))
2197 {
Ed Tanous62598e32023-07-17 17:06:25 -07002198 BMCWEB_LOG_ERROR("fail to expose host logs");
Spencer Kub7028eb2021-10-26 15:27:35 +08002199 return false;
2200 }
2201 }
2202 // Get lastMessage from constructor by getter
2203 std::string lastMessage = logFile.getLastMessage();
2204 if (!lastMessage.empty())
2205 {
2206 logCount++;
2207 if (logCount > skip && logCount <= (skip + top))
2208 {
2209 logEntries.push_back(lastMessage);
2210 }
2211 }
2212 return true;
2213}
2214
2215inline void fillHostLoggerEntryJson(const std::string& logEntryID,
2216 const std::string& msg,
Jason M. Bills6d6574c2022-06-28 12:30:16 -07002217 nlohmann::json::object_t& logEntryJson)
Spencer Kub7028eb2021-10-26 15:27:35 +08002218{
2219 // Fill in the log entry with the gathered data.
Vijay Lobo9c11a172021-10-07 16:53:16 -05002220 logEntryJson["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
Ed Tanousef4c65b2023-04-24 15:28:50 -07002221 logEntryJson["@odata.id"] = boost::urls::format(
2222 "/redfish/v1/Systems/system/LogServices/HostLogger/Entries/{}",
2223 logEntryID);
Jason M. Bills6d6574c2022-06-28 12:30:16 -07002224 logEntryJson["Name"] = "Host Logger Entry";
2225 logEntryJson["Id"] = logEntryID;
2226 logEntryJson["Message"] = msg;
2227 logEntryJson["EntryType"] = "Oem";
2228 logEntryJson["Severity"] = "OK";
2229 logEntryJson["OemRecordFormat"] = "Host Logger Entry";
Spencer Kub7028eb2021-10-26 15:27:35 +08002230}
2231
2232inline void requestRoutesSystemHostLogger(App& app)
2233{
Ed Tanous22d268c2022-05-19 09:39:07 -07002234 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/HostLogger/")
Spencer Kub7028eb2021-10-26 15:27:35 +08002235 .privileges(redfish::privileges::getLogService)
Ed Tanous14766872022-03-15 10:44:42 -07002236 .methods(boost::beast::http::verb::get)(
2237 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07002238 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2239 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002240 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002241 {
2242 return;
2243 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08002244 if constexpr (bmcwebEnableMultiHost)
2245 {
2246 // Option currently returns no systems. TBD
2247 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
2248 systemName);
2249 return;
2250 }
Ed Tanous22d268c2022-05-19 09:39:07 -07002251 if (systemName != "system")
2252 {
2253 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
2254 systemName);
2255 return;
2256 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002257 asyncResp->res.jsonValue["@odata.id"] =
2258 "/redfish/v1/Systems/system/LogServices/HostLogger";
2259 asyncResp->res.jsonValue["@odata.type"] =
Janet Adkinsb25644a2023-08-16 11:23:45 -05002260 "#LogService.v1_2_0.LogService";
Ed Tanous002d39b2022-05-31 08:59:27 -07002261 asyncResp->res.jsonValue["Name"] = "Host Logger Service";
2262 asyncResp->res.jsonValue["Description"] = "Host Logger Service";
2263 asyncResp->res.jsonValue["Id"] = "HostLogger";
2264 asyncResp->res.jsonValue["Entries"]["@odata.id"] =
2265 "/redfish/v1/Systems/system/LogServices/HostLogger/Entries";
Patrick Williams5a39f772023-10-20 11:20:21 -05002266 });
Spencer Kub7028eb2021-10-26 15:27:35 +08002267}
2268
2269inline void requestRoutesSystemHostLoggerCollection(App& app)
2270{
2271 BMCWEB_ROUTE(app,
Ed Tanous22d268c2022-05-19 09:39:07 -07002272 "/redfish/v1/Systems/<str>/LogServices/HostLogger/Entries/")
Spencer Kub7028eb2021-10-26 15:27:35 +08002273 .privileges(redfish::privileges::getLogEntry)
Ed Tanous002d39b2022-05-31 08:59:27 -07002274 .methods(boost::beast::http::verb::get)(
2275 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07002276 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2277 const std::string& systemName) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002278 query_param::QueryCapabilities capabilities = {
2279 .canDelegateTop = true,
2280 .canDelegateSkip = true,
2281 };
2282 query_param::Query delegatedQuery;
2283 if (!redfish::setUpRedfishRouteWithDelegation(
Carson Labrado3ba00072022-06-06 19:40:56 +00002284 app, req, asyncResp, delegatedQuery, capabilities))
Ed Tanous002d39b2022-05-31 08:59:27 -07002285 {
2286 return;
2287 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08002288 if constexpr (bmcwebEnableMultiHost)
2289 {
2290 // Option currently returns no systems. TBD
2291 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
2292 systemName);
2293 return;
2294 }
Ed Tanous22d268c2022-05-19 09:39:07 -07002295 if (systemName != "system")
2296 {
2297 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
2298 systemName);
2299 return;
2300 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002301 asyncResp->res.jsonValue["@odata.id"] =
2302 "/redfish/v1/Systems/system/LogServices/HostLogger/Entries";
2303 asyncResp->res.jsonValue["@odata.type"] =
2304 "#LogEntryCollection.LogEntryCollection";
2305 asyncResp->res.jsonValue["Name"] = "HostLogger Entries";
2306 asyncResp->res.jsonValue["Description"] =
2307 "Collection of HostLogger Entries";
2308 nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
2309 logEntryArray = nlohmann::json::array();
2310 asyncResp->res.jsonValue["Members@odata.count"] = 0;
Spencer Kub7028eb2021-10-26 15:27:35 +08002311
Ed Tanous002d39b2022-05-31 08:59:27 -07002312 std::vector<std::filesystem::path> hostLoggerFiles;
2313 if (!getHostLoggerFiles(hostLoggerFolderPath, hostLoggerFiles))
2314 {
Carson Labradobf2dded2023-08-10 00:37:06 +00002315 BMCWEB_LOG_DEBUG("Failed to get host log file path");
Ed Tanous002d39b2022-05-31 08:59:27 -07002316 return;
2317 }
Ed Tanous3648c8b2022-07-25 13:39:59 -07002318 // If we weren't provided top and skip limits, use the defaults.
2319 size_t skip = delegatedQuery.skip.value_or(0);
Jiaqing Zhao5143f7a2022-07-22 09:33:33 +08002320 size_t top = delegatedQuery.top.value_or(query_param::Query::maxTop);
Ed Tanous002d39b2022-05-31 08:59:27 -07002321 size_t logCount = 0;
2322 // This vector only store the entries we want to expose that
2323 // control by skip and top.
2324 std::vector<std::string> logEntries;
Ed Tanous3648c8b2022-07-25 13:39:59 -07002325 if (!getHostLoggerEntries(hostLoggerFiles, skip, top, logEntries,
2326 logCount))
Ed Tanous002d39b2022-05-31 08:59:27 -07002327 {
2328 messages::internalError(asyncResp->res);
2329 return;
2330 }
2331 // If vector is empty, that means skip value larger than total
2332 // log count
2333 if (logEntries.empty())
2334 {
2335 asyncResp->res.jsonValue["Members@odata.count"] = logCount;
2336 return;
2337 }
2338 if (!logEntries.empty())
2339 {
2340 for (size_t i = 0; i < logEntries.size(); i++)
George Liu0fda0f12021-11-16 10:06:17 +08002341 {
Jason M. Bills6d6574c2022-06-28 12:30:16 -07002342 nlohmann::json::object_t hostLogEntry;
Ed Tanous3648c8b2022-07-25 13:39:59 -07002343 fillHostLoggerEntryJson(std::to_string(skip + i), logEntries[i],
2344 hostLogEntry);
Patrick Williamsb2ba3072023-05-12 10:27:39 -05002345 logEntryArray.emplace_back(std::move(hostLogEntry));
George Liu0fda0f12021-11-16 10:06:17 +08002346 }
2347
Ed Tanous002d39b2022-05-31 08:59:27 -07002348 asyncResp->res.jsonValue["Members@odata.count"] = logCount;
Ed Tanous3648c8b2022-07-25 13:39:59 -07002349 if (skip + top < logCount)
George Liu0fda0f12021-11-16 10:06:17 +08002350 {
Ed Tanous002d39b2022-05-31 08:59:27 -07002351 asyncResp->res.jsonValue["Members@odata.nextLink"] =
2352 "/redfish/v1/Systems/system/LogServices/HostLogger/Entries?$skip=" +
Ed Tanous3648c8b2022-07-25 13:39:59 -07002353 std::to_string(skip + top);
George Liu0fda0f12021-11-16 10:06:17 +08002354 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002355 }
Patrick Williams5a39f772023-10-20 11:20:21 -05002356 });
Spencer Kub7028eb2021-10-26 15:27:35 +08002357}
2358
2359inline void requestRoutesSystemHostLoggerLogEntry(App& app)
2360{
2361 BMCWEB_ROUTE(
Ed Tanous22d268c2022-05-19 09:39:07 -07002362 app, "/redfish/v1/Systems/<str>/LogServices/HostLogger/Entries/<str>/")
Spencer Kub7028eb2021-10-26 15:27:35 +08002363 .privileges(redfish::privileges::getLogEntry)
2364 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002365 [&app](const crow::Request& req,
2366 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07002367 const std::string& systemName, const std::string& param) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002368 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002369 {
2370 return;
2371 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08002372 if constexpr (bmcwebEnableMultiHost)
2373 {
2374 // Option currently returns no systems. TBD
2375 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
2376 systemName);
2377 return;
2378 }
Ed Tanous22d268c2022-05-19 09:39:07 -07002379 if (systemName != "system")
2380 {
2381 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
2382 systemName);
2383 return;
2384 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002385 const std::string& targetID = param;
Spencer Kub7028eb2021-10-26 15:27:35 +08002386
Ed Tanous002d39b2022-05-31 08:59:27 -07002387 uint64_t idInt = 0;
Ed Tanousca45aa32022-01-07 09:28:45 -08002388
Patrick Williams84396af2023-05-11 11:47:45 -05002389 auto [ptr, ec] = std::from_chars(&*targetID.begin(), &*targetID.end(),
2390 idInt);
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08002391 if (ec == std::errc::invalid_argument ||
2392 ec == std::errc::result_out_of_range)
Ed Tanous002d39b2022-05-31 08:59:27 -07002393 {
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08002394 messages::resourceNotFound(asyncResp->res, "LogEntry", param);
Ed Tanous002d39b2022-05-31 08:59:27 -07002395 return;
2396 }
Spencer Kub7028eb2021-10-26 15:27:35 +08002397
Ed Tanous002d39b2022-05-31 08:59:27 -07002398 std::vector<std::filesystem::path> hostLoggerFiles;
2399 if (!getHostLoggerFiles(hostLoggerFolderPath, hostLoggerFiles))
2400 {
Carson Labradobf2dded2023-08-10 00:37:06 +00002401 BMCWEB_LOG_DEBUG("Failed to get host log file path");
Ed Tanous002d39b2022-05-31 08:59:27 -07002402 return;
2403 }
Spencer Kub7028eb2021-10-26 15:27:35 +08002404
Ed Tanous002d39b2022-05-31 08:59:27 -07002405 size_t logCount = 0;
Ed Tanous3648c8b2022-07-25 13:39:59 -07002406 size_t top = 1;
Ed Tanous002d39b2022-05-31 08:59:27 -07002407 std::vector<std::string> logEntries;
2408 // We can get specific entry by skip and top. For example, if we
2409 // want to get nth entry, we can set skip = n-1 and top = 1 to
2410 // get that entry
2411 if (!getHostLoggerEntries(hostLoggerFiles, idInt, top, logEntries,
2412 logCount))
2413 {
2414 messages::internalError(asyncResp->res);
2415 return;
2416 }
Spencer Kub7028eb2021-10-26 15:27:35 +08002417
Ed Tanous002d39b2022-05-31 08:59:27 -07002418 if (!logEntries.empty())
2419 {
Jason M. Bills6d6574c2022-06-28 12:30:16 -07002420 nlohmann::json::object_t hostLogEntry;
2421 fillHostLoggerEntryJson(targetID, logEntries[0], hostLogEntry);
2422 asyncResp->res.jsonValue.update(hostLogEntry);
Ed Tanous002d39b2022-05-31 08:59:27 -07002423 return;
2424 }
Spencer Kub7028eb2021-10-26 15:27:35 +08002425
Ed Tanous002d39b2022-05-31 08:59:27 -07002426 // Requested ID was not found
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08002427 messages::resourceNotFound(asyncResp->res, "LogEntry", param);
Patrick Williams5a39f772023-10-20 11:20:21 -05002428 });
Spencer Kub7028eb2021-10-26 15:27:35 +08002429}
2430
Claire Weinandd72e872022-08-15 14:20:06 -07002431inline void handleBMCLogServicesCollectionGet(
Claire Weinanfdd26902022-03-01 14:18:25 -08002432 crow::App& app, const crow::Request& req,
2433 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2434{
2435 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2436 {
2437 return;
2438 }
2439 // Collections don't include the static data added by SubRoute
2440 // because it has a duplicate entry for members
2441 asyncResp->res.jsonValue["@odata.type"] =
2442 "#LogServiceCollection.LogServiceCollection";
2443 asyncResp->res.jsonValue["@odata.id"] =
2444 "/redfish/v1/Managers/bmc/LogServices";
2445 asyncResp->res.jsonValue["Name"] = "Open BMC Log Services Collection";
2446 asyncResp->res.jsonValue["Description"] =
2447 "Collection of LogServices for this Manager";
2448 nlohmann::json& logServiceArray = asyncResp->res.jsonValue["Members"];
2449 logServiceArray = nlohmann::json::array();
2450
2451#ifdef BMCWEB_ENABLE_REDFISH_BMC_JOURNAL
Ed Tanous613dabe2022-07-09 11:17:36 -07002452 nlohmann::json::object_t journal;
2453 journal["@odata.id"] = "/redfish/v1/Managers/bmc/LogServices/Journal";
Patrick Williamsb2ba3072023-05-12 10:27:39 -05002454 logServiceArray.emplace_back(std::move(journal));
Claire Weinanfdd26902022-03-01 14:18:25 -08002455#endif
2456
2457 asyncResp->res.jsonValue["Members@odata.count"] = logServiceArray.size();
2458
2459#ifdef BMCWEB_ENABLE_REDFISH_DUMP_LOG
George Liu15912152023-01-11 10:18:18 +08002460 constexpr std::array<std::string_view, 1> interfaces = {
George Liu7a1dbc42022-12-07 16:03:22 +08002461 "xyz.openbmc_project.Collection.DeleteAll"};
2462 dbus::utility::getSubTreePaths(
2463 "/xyz/openbmc_project/dump", 0, interfaces,
Claire Weinanfdd26902022-03-01 14:18:25 -08002464 [asyncResp](
George Liu7a1dbc42022-12-07 16:03:22 +08002465 const boost::system::error_code& ec,
Claire Weinanfdd26902022-03-01 14:18:25 -08002466 const dbus::utility::MapperGetSubTreePathsResponse& subTreePaths) {
2467 if (ec)
2468 {
Ed Tanous62598e32023-07-17 17:06:25 -07002469 BMCWEB_LOG_ERROR(
2470 "handleBMCLogServicesCollectionGet respHandler got error {}",
2471 ec);
Claire Weinanfdd26902022-03-01 14:18:25 -08002472 // Assume that getting an error simply means there are no dump
2473 // LogServices. Return without adding any error response.
2474 return;
2475 }
2476
2477 nlohmann::json& logServiceArrayLocal =
2478 asyncResp->res.jsonValue["Members"];
2479
2480 for (const std::string& path : subTreePaths)
2481 {
2482 if (path == "/xyz/openbmc_project/dump/bmc")
2483 {
Ed Tanous613dabe2022-07-09 11:17:36 -07002484 nlohmann::json::object_t member;
2485 member["@odata.id"] =
2486 "/redfish/v1/Managers/bmc/LogServices/Dump";
Patrick Williamsb2ba3072023-05-12 10:27:39 -05002487 logServiceArrayLocal.emplace_back(std::move(member));
Claire Weinanfdd26902022-03-01 14:18:25 -08002488 }
2489 else if (path == "/xyz/openbmc_project/dump/faultlog")
2490 {
Ed Tanous613dabe2022-07-09 11:17:36 -07002491 nlohmann::json::object_t member;
2492 member["@odata.id"] =
2493 "/redfish/v1/Managers/bmc/LogServices/FaultLog";
Patrick Williamsb2ba3072023-05-12 10:27:39 -05002494 logServiceArrayLocal.emplace_back(std::move(member));
Claire Weinanfdd26902022-03-01 14:18:25 -08002495 }
2496 }
2497
2498 asyncResp->res.jsonValue["Members@odata.count"] =
2499 logServiceArrayLocal.size();
Patrick Williams5a39f772023-10-20 11:20:21 -05002500 });
Claire Weinanfdd26902022-03-01 14:18:25 -08002501#endif
2502}
2503
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002504inline void requestRoutesBMCLogServiceCollection(App& app)
2505{
2506 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/")
Gunnar Millsad89dcf2021-07-30 14:40:11 -05002507 .privileges(redfish::privileges::getLogServiceCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002508 .methods(boost::beast::http::verb::get)(
Claire Weinandd72e872022-08-15 14:20:06 -07002509 std::bind_front(handleBMCLogServicesCollectionGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002510}
Ed Tanous1da66f72018-07-27 16:13:37 -07002511
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002512inline void requestRoutesBMCJournalLogService(App& app)
Ed Tanous1da66f72018-07-27 16:13:37 -07002513{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002514 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Journal/")
Ed Tanoused398212021-06-09 17:05:54 -07002515 .privileges(redfish::privileges::getLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002516 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002517 [&app](const crow::Request& req,
2518 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002519 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002520 {
2521 return;
2522 }
2523 asyncResp->res.jsonValue["@odata.type"] =
Janet Adkinsb25644a2023-08-16 11:23:45 -05002524 "#LogService.v1_2_0.LogService";
Ed Tanous002d39b2022-05-31 08:59:27 -07002525 asyncResp->res.jsonValue["@odata.id"] =
2526 "/redfish/v1/Managers/bmc/LogServices/Journal";
2527 asyncResp->res.jsonValue["Name"] = "Open BMC Journal Log Service";
2528 asyncResp->res.jsonValue["Description"] = "BMC Journal Log Service";
Ed Tanoused34a4a2023-02-08 15:43:27 -08002529 asyncResp->res.jsonValue["Id"] = "Journal";
Ed Tanous002d39b2022-05-31 08:59:27 -07002530 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
Tejas Patil7c8c4052021-06-04 17:43:14 +05302531
Ed Tanous002d39b2022-05-31 08:59:27 -07002532 std::pair<std::string, std::string> redfishDateTimeOffset =
Ed Tanous2b829372022-08-03 14:22:34 -07002533 redfish::time_utils::getDateTimeOffsetNow();
Ed Tanous002d39b2022-05-31 08:59:27 -07002534 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
2535 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
2536 redfishDateTimeOffset.second;
Tejas Patil7c8c4052021-06-04 17:43:14 +05302537
Ed Tanous002d39b2022-05-31 08:59:27 -07002538 asyncResp->res.jsonValue["Entries"]["@odata.id"] =
2539 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries";
Patrick Williams5a39f772023-10-20 11:20:21 -05002540 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002541}
Jason M. Billse1f26342018-07-18 12:12:00 -07002542
Jason M. Bills3a48b3a2022-06-24 10:10:15 -07002543static int
2544 fillBMCJournalLogEntryJson(const std::string& bmcJournalLogEntryID,
2545 sd_journal* journal,
2546 nlohmann::json::object_t& bmcJournalLogEntryJson)
Jason M. Billse1f26342018-07-18 12:12:00 -07002547{
2548 // Get the Log Entry contents
2549 int ret = 0;
Jason M. Billse1f26342018-07-18 12:12:00 -07002550
Jason M. Billsa8fe54f2020-11-20 15:57:55 -08002551 std::string message;
2552 std::string_view syslogID;
2553 ret = getJournalMetadata(journal, "SYSLOG_IDENTIFIER", syslogID);
2554 if (ret < 0)
2555 {
Carson Labradobf2dded2023-08-10 00:37:06 +00002556 BMCWEB_LOG_DEBUG("Failed to read SYSLOG_IDENTIFIER field: {}",
Ed Tanous62598e32023-07-17 17:06:25 -07002557 strerror(-ret));
Jason M. Billsa8fe54f2020-11-20 15:57:55 -08002558 }
2559 if (!syslogID.empty())
2560 {
2561 message += std::string(syslogID) + ": ";
2562 }
2563
Ed Tanous39e77502019-03-04 17:35:53 -08002564 std::string_view msg;
Jason M. Bills16428a12018-11-02 12:42:29 -07002565 ret = getJournalMetadata(journal, "MESSAGE", msg);
Jason M. Billse1f26342018-07-18 12:12:00 -07002566 if (ret < 0)
2567 {
Ed Tanous62598e32023-07-17 17:06:25 -07002568 BMCWEB_LOG_ERROR("Failed to read MESSAGE field: {}", strerror(-ret));
Jason M. Billse1f26342018-07-18 12:12:00 -07002569 return 1;
2570 }
Jason M. Billsa8fe54f2020-11-20 15:57:55 -08002571 message += std::string(msg);
Jason M. Billse1f26342018-07-18 12:12:00 -07002572
2573 // Get the severity from the PRIORITY field
Ed Tanous271584a2019-07-09 16:24:22 -07002574 long int severity = 8; // Default to an invalid priority
Jason M. Bills16428a12018-11-02 12:42:29 -07002575 ret = getJournalMetadata(journal, "PRIORITY", 10, severity);
Jason M. Billse1f26342018-07-18 12:12:00 -07002576 if (ret < 0)
2577 {
Carson Labradobf2dded2023-08-10 00:37:06 +00002578 BMCWEB_LOG_DEBUG("Failed to read PRIORITY field: {}", strerror(-ret));
Jason M. Billse1f26342018-07-18 12:12:00 -07002579 }
Jason M. Billse1f26342018-07-18 12:12:00 -07002580
2581 // Get the Created time from the timestamp
Jason M. Bills16428a12018-11-02 12:42:29 -07002582 std::string entryTimeStr;
2583 if (!getEntryTimestamp(journal, entryTimeStr))
Jason M. Billse1f26342018-07-18 12:12:00 -07002584 {
Jason M. Bills16428a12018-11-02 12:42:29 -07002585 return 1;
Jason M. Billse1f26342018-07-18 12:12:00 -07002586 }
Jason M. Billse1f26342018-07-18 12:12:00 -07002587
2588 // Fill in the log entry with the gathered data
Vijay Lobo9c11a172021-10-07 16:53:16 -05002589 bmcJournalLogEntryJson["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
Ed Tanousef4c65b2023-04-24 15:28:50 -07002590 bmcJournalLogEntryJson["@odata.id"] = boost::urls::format(
2591 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/{}",
Willy Tueddfc432022-09-26 16:46:38 +00002592 bmcJournalLogEntryID);
Jason M. Bills84afc482022-06-24 12:38:23 -07002593 bmcJournalLogEntryJson["Name"] = "BMC Journal Entry";
2594 bmcJournalLogEntryJson["Id"] = bmcJournalLogEntryID;
2595 bmcJournalLogEntryJson["Message"] = std::move(message);
2596 bmcJournalLogEntryJson["EntryType"] = "Oem";
Ed Tanousddf35642024-03-27 14:12:21 -07002597 log_entry::EventSeverity severityEnum = log_entry::EventSeverity::OK;
2598 if (severity <= 2)
2599 {
2600 severityEnum = log_entry::EventSeverity::Critical;
2601 }
2602 else if (severity <= 4)
2603 {
2604 severityEnum = log_entry::EventSeverity::Warning;
2605 }
2606
2607 bmcJournalLogEntryJson["Severity"] = severityEnum;
Jason M. Bills84afc482022-06-24 12:38:23 -07002608 bmcJournalLogEntryJson["OemRecordFormat"] = "BMC Journal Entry";
2609 bmcJournalLogEntryJson["Created"] = std::move(entryTimeStr);
Jason M. Billse1f26342018-07-18 12:12:00 -07002610 return 0;
2611}
2612
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002613inline void requestRoutesBMCJournalLogEntryCollection(App& app)
Jason M. Billse1f26342018-07-18 12:12:00 -07002614{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002615 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07002616 .privileges(redfish::privileges::getLogEntryCollection)
Ed Tanous002d39b2022-05-31 08:59:27 -07002617 .methods(boost::beast::http::verb::get)(
2618 [&app](const crow::Request& req,
2619 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2620 query_param::QueryCapabilities capabilities = {
2621 .canDelegateTop = true,
2622 .canDelegateSkip = true,
2623 };
2624 query_param::Query delegatedQuery;
2625 if (!redfish::setUpRedfishRouteWithDelegation(
Carson Labrado3ba00072022-06-06 19:40:56 +00002626 app, req, asyncResp, delegatedQuery, capabilities))
Ed Tanous002d39b2022-05-31 08:59:27 -07002627 {
2628 return;
2629 }
Ed Tanous3648c8b2022-07-25 13:39:59 -07002630
2631 size_t skip = delegatedQuery.skip.value_or(0);
Jiaqing Zhao5143f7a2022-07-22 09:33:33 +08002632 size_t top = delegatedQuery.top.value_or(query_param::Query::maxTop);
Ed Tanous3648c8b2022-07-25 13:39:59 -07002633
Ed Tanous002d39b2022-05-31 08:59:27 -07002634 // Collections don't include the static data added by SubRoute
2635 // because it has a duplicate entry for members
2636 asyncResp->res.jsonValue["@odata.type"] =
2637 "#LogEntryCollection.LogEntryCollection";
2638 asyncResp->res.jsonValue["@odata.id"] =
2639 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries";
2640 asyncResp->res.jsonValue["Name"] = "Open BMC Journal Entries";
2641 asyncResp->res.jsonValue["Description"] =
2642 "Collection of BMC Journal Entries";
2643 nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
2644 logEntryArray = nlohmann::json::array();
Jason M. Billse1f26342018-07-18 12:12:00 -07002645
Ed Tanous002d39b2022-05-31 08:59:27 -07002646 // Go through the journal and use the timestamp to create a
2647 // unique ID for each entry
2648 sd_journal* journalTmp = nullptr;
2649 int ret = sd_journal_open(&journalTmp, SD_JOURNAL_LOCAL_ONLY);
2650 if (ret < 0)
2651 {
Ed Tanous62598e32023-07-17 17:06:25 -07002652 BMCWEB_LOG_ERROR("failed to open journal: {}", strerror(-ret));
Ed Tanous002d39b2022-05-31 08:59:27 -07002653 messages::internalError(asyncResp->res);
2654 return;
2655 }
2656 std::unique_ptr<sd_journal, decltype(&sd_journal_close)> journal(
2657 journalTmp, sd_journal_close);
2658 journalTmp = nullptr;
2659 uint64_t entryCount = 0;
2660 // Reset the unique ID on the first entry
2661 bool firstEntry = true;
2662 SD_JOURNAL_FOREACH(journal.get())
2663 {
2664 entryCount++;
2665 // Handle paging using skip (number of entries to skip from
2666 // the start) and top (number of entries to display)
Ed Tanous3648c8b2022-07-25 13:39:59 -07002667 if (entryCount <= skip || entryCount > skip + top)
George Liu0fda0f12021-11-16 10:06:17 +08002668 {
Ed Tanous002d39b2022-05-31 08:59:27 -07002669 continue;
2670 }
2671
2672 std::string idStr;
2673 if (!getUniqueEntryID(journal.get(), idStr, firstEntry))
2674 {
2675 continue;
2676 }
Jason M. Billsefde4ec2022-06-24 08:59:52 -07002677 firstEntry = false;
Ed Tanous002d39b2022-05-31 08:59:27 -07002678
Jason M. Bills3a48b3a2022-06-24 10:10:15 -07002679 nlohmann::json::object_t bmcJournalLogEntry;
Ed Tanous002d39b2022-05-31 08:59:27 -07002680 if (fillBMCJournalLogEntryJson(idStr, journal.get(),
2681 bmcJournalLogEntry) != 0)
2682 {
George Liu0fda0f12021-11-16 10:06:17 +08002683 messages::internalError(asyncResp->res);
2684 return;
2685 }
Patrick Williamsb2ba3072023-05-12 10:27:39 -05002686 logEntryArray.emplace_back(std::move(bmcJournalLogEntry));
Ed Tanous002d39b2022-05-31 08:59:27 -07002687 }
2688 asyncResp->res.jsonValue["Members@odata.count"] = entryCount;
Ed Tanous3648c8b2022-07-25 13:39:59 -07002689 if (skip + top < entryCount)
Ed Tanous002d39b2022-05-31 08:59:27 -07002690 {
2691 asyncResp->res.jsonValue["Members@odata.nextLink"] =
2692 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries?$skip=" +
Ed Tanous3648c8b2022-07-25 13:39:59 -07002693 std::to_string(skip + top);
Ed Tanous002d39b2022-05-31 08:59:27 -07002694 }
Patrick Williams5a39f772023-10-20 11:20:21 -05002695 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002696}
Jason M. Billse1f26342018-07-18 12:12:00 -07002697
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002698inline void requestRoutesBMCJournalLogEntry(App& app)
Jason M. Billse1f26342018-07-18 12:12:00 -07002699{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002700 BMCWEB_ROUTE(app,
2701 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002702 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002703 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002704 [&app](const crow::Request& req,
2705 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2706 const std::string& entryID) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002707 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002708 {
2709 return;
2710 }
2711 // Convert the unique ID back to a timestamp to find the entry
Myung Bae75e8e212023-11-30 12:53:46 -08002712 sd_id128_t bootID{};
Ed Tanous002d39b2022-05-31 08:59:27 -07002713 uint64_t ts = 0;
2714 uint64_t index = 0;
Myung Bae75e8e212023-11-30 12:53:46 -08002715 if (!getTimestampFromID(asyncResp, entryID, bootID, ts, index))
Ed Tanous002d39b2022-05-31 08:59:27 -07002716 {
2717 return;
2718 }
Jason M. Billse1f26342018-07-18 12:12:00 -07002719
Ed Tanous002d39b2022-05-31 08:59:27 -07002720 sd_journal* journalTmp = nullptr;
2721 int ret = sd_journal_open(&journalTmp, SD_JOURNAL_LOCAL_ONLY);
2722 if (ret < 0)
2723 {
Ed Tanous62598e32023-07-17 17:06:25 -07002724 BMCWEB_LOG_ERROR("failed to open journal: {}", strerror(-ret));
Ed Tanous002d39b2022-05-31 08:59:27 -07002725 messages::internalError(asyncResp->res);
2726 return;
2727 }
2728 std::unique_ptr<sd_journal, decltype(&sd_journal_close)> journal(
2729 journalTmp, sd_journal_close);
2730 journalTmp = nullptr;
2731 // Go to the timestamp in the log and move to the entry at the
2732 // index tracking the unique ID
2733 std::string idStr;
2734 bool firstEntry = true;
Myung Bae75e8e212023-11-30 12:53:46 -08002735 ret = sd_journal_seek_monotonic_usec(journal.get(), bootID, ts);
Ed Tanous002d39b2022-05-31 08:59:27 -07002736 if (ret < 0)
2737 {
Ed Tanous62598e32023-07-17 17:06:25 -07002738 BMCWEB_LOG_ERROR("failed to seek to an entry in journal{}",
2739 strerror(-ret));
Ed Tanous002d39b2022-05-31 08:59:27 -07002740 messages::internalError(asyncResp->res);
2741 return;
2742 }
2743 for (uint64_t i = 0; i <= index; i++)
2744 {
2745 sd_journal_next(journal.get());
2746 if (!getUniqueEntryID(journal.get(), idStr, firstEntry))
2747 {
2748 messages::internalError(asyncResp->res);
2749 return;
2750 }
Jason M. Billsefde4ec2022-06-24 08:59:52 -07002751 firstEntry = false;
Ed Tanous002d39b2022-05-31 08:59:27 -07002752 }
2753 // Confirm that the entry ID matches what was requested
2754 if (idStr != entryID)
2755 {
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08002756 messages::resourceNotFound(asyncResp->res, "LogEntry", entryID);
Ed Tanous002d39b2022-05-31 08:59:27 -07002757 return;
2758 }
zhanghch058d1b46d2021-04-01 11:18:24 +08002759
Jason M. Bills3a48b3a2022-06-24 10:10:15 -07002760 nlohmann::json::object_t bmcJournalLogEntry;
Ed Tanous002d39b2022-05-31 08:59:27 -07002761 if (fillBMCJournalLogEntryJson(entryID, journal.get(),
Jason M. Bills3a48b3a2022-06-24 10:10:15 -07002762 bmcJournalLogEntry) != 0)
Ed Tanous002d39b2022-05-31 08:59:27 -07002763 {
2764 messages::internalError(asyncResp->res);
2765 return;
2766 }
Jason M. Billsd405bb52022-06-24 10:52:05 -07002767 asyncResp->res.jsonValue.update(bmcJournalLogEntry);
Patrick Williams5a39f772023-10-20 11:20:21 -05002768 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002769}
2770
Claire Weinanfdd26902022-03-01 14:18:25 -08002771inline void
2772 getDumpServiceInfo(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2773 const std::string& dumpType)
2774{
2775 std::string dumpPath;
2776 std::string overWritePolicy;
2777 bool collectDiagnosticDataSupported = false;
2778
2779 if (dumpType == "BMC")
2780 {
2781 dumpPath = "/redfish/v1/Managers/bmc/LogServices/Dump";
2782 overWritePolicy = "WrapsWhenFull";
2783 collectDiagnosticDataSupported = true;
2784 }
2785 else if (dumpType == "FaultLog")
2786 {
2787 dumpPath = "/redfish/v1/Managers/bmc/LogServices/FaultLog";
2788 overWritePolicy = "Unknown";
2789 collectDiagnosticDataSupported = false;
2790 }
2791 else if (dumpType == "System")
2792 {
2793 dumpPath = "/redfish/v1/Systems/system/LogServices/Dump";
2794 overWritePolicy = "WrapsWhenFull";
2795 collectDiagnosticDataSupported = true;
2796 }
2797 else
2798 {
Ed Tanous62598e32023-07-17 17:06:25 -07002799 BMCWEB_LOG_ERROR("getDumpServiceInfo() invalid dump type: {}",
2800 dumpType);
Claire Weinanfdd26902022-03-01 14:18:25 -08002801 messages::internalError(asyncResp->res);
2802 return;
2803 }
2804
2805 asyncResp->res.jsonValue["@odata.id"] = dumpPath;
2806 asyncResp->res.jsonValue["@odata.type"] = "#LogService.v1_2_0.LogService";
2807 asyncResp->res.jsonValue["Name"] = "Dump LogService";
2808 asyncResp->res.jsonValue["Description"] = dumpType + " Dump LogService";
2809 asyncResp->res.jsonValue["Id"] = std::filesystem::path(dumpPath).filename();
2810 asyncResp->res.jsonValue["OverWritePolicy"] = std::move(overWritePolicy);
2811
2812 std::pair<std::string, std::string> redfishDateTimeOffset =
Ed Tanous2b829372022-08-03 14:22:34 -07002813 redfish::time_utils::getDateTimeOffsetNow();
Claire Weinanfdd26902022-03-01 14:18:25 -08002814 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
2815 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
2816 redfishDateTimeOffset.second;
2817
2818 asyncResp->res.jsonValue["Entries"]["@odata.id"] = dumpPath + "/Entries";
Claire Weinanfdd26902022-03-01 14:18:25 -08002819
2820 if (collectDiagnosticDataSupported)
2821 {
2822 asyncResp->res.jsonValue["Actions"]["#LogService.CollectDiagnosticData"]
2823 ["target"] =
2824 dumpPath + "/Actions/LogService.CollectDiagnosticData";
2825 }
Claire Weinan0d946212022-07-13 19:40:19 -07002826
2827 constexpr std::array<std::string_view, 1> interfaces = {deleteAllInterface};
2828 dbus::utility::getSubTreePaths(
2829 "/xyz/openbmc_project/dump", 0, interfaces,
2830 [asyncResp, dumpType, dumpPath](
2831 const boost::system::error_code& ec,
2832 const dbus::utility::MapperGetSubTreePathsResponse& subTreePaths) {
2833 if (ec)
2834 {
Ed Tanous62598e32023-07-17 17:06:25 -07002835 BMCWEB_LOG_ERROR("getDumpServiceInfo respHandler got error {}", ec);
Claire Weinan0d946212022-07-13 19:40:19 -07002836 // Assume that getting an error simply means there are no dump
2837 // LogServices. Return without adding any error response.
2838 return;
2839 }
Ed Tanous18f8f602023-07-18 10:07:23 -07002840 std::string dbusDumpPath = getDumpPath(dumpType);
Claire Weinan0d946212022-07-13 19:40:19 -07002841 for (const std::string& path : subTreePaths)
2842 {
2843 if (path == dbusDumpPath)
2844 {
2845 asyncResp->res
2846 .jsonValue["Actions"]["#LogService.ClearLog"]["target"] =
2847 dumpPath + "/Actions/LogService.ClearLog";
2848 break;
2849 }
2850 }
Patrick Williams5a39f772023-10-20 11:20:21 -05002851 });
Claire Weinanfdd26902022-03-01 14:18:25 -08002852}
2853
2854inline void handleLogServicesDumpServiceGet(
2855 crow::App& app, const std::string& dumpType, const crow::Request& req,
2856 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2857{
2858 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2859 {
2860 return;
2861 }
2862 getDumpServiceInfo(asyncResp, dumpType);
2863}
2864
Ed Tanous22d268c2022-05-19 09:39:07 -07002865inline void handleLogServicesDumpServiceComputerSystemGet(
2866 crow::App& app, const crow::Request& req,
2867 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2868 const std::string& chassisId)
2869{
2870 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2871 {
2872 return;
2873 }
2874 if (chassisId != "system")
2875 {
2876 messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
2877 return;
2878 }
2879 getDumpServiceInfo(asyncResp, "System");
2880}
2881
Claire Weinanfdd26902022-03-01 14:18:25 -08002882inline void handleLogServicesDumpEntriesCollectionGet(
2883 crow::App& app, const std::string& dumpType, const crow::Request& req,
2884 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2885{
2886 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2887 {
2888 return;
2889 }
2890 getDumpEntryCollection(asyncResp, dumpType);
2891}
2892
Ed Tanous22d268c2022-05-19 09:39:07 -07002893inline void handleLogServicesDumpEntriesCollectionComputerSystemGet(
2894 crow::App& app, const crow::Request& req,
2895 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2896 const std::string& chassisId)
2897{
2898 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2899 {
2900 return;
2901 }
2902 if (chassisId != "system")
2903 {
2904 messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
2905 return;
2906 }
2907 getDumpEntryCollection(asyncResp, "System");
2908}
2909
Claire Weinanfdd26902022-03-01 14:18:25 -08002910inline void handleLogServicesDumpEntryGet(
2911 crow::App& app, const std::string& dumpType, const crow::Request& req,
2912 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2913 const std::string& dumpId)
2914{
2915 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2916 {
2917 return;
2918 }
2919 getDumpEntryById(asyncResp, dumpId, dumpType);
2920}
Carson Labrado168d1b12023-03-27 17:04:46 +00002921
Ed Tanous22d268c2022-05-19 09:39:07 -07002922inline void handleLogServicesDumpEntryComputerSystemGet(
2923 crow::App& app, const crow::Request& req,
2924 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2925 const std::string& chassisId, const std::string& dumpId)
2926{
2927 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2928 {
2929 return;
2930 }
2931 if (chassisId != "system")
2932 {
2933 messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
2934 return;
2935 }
2936 getDumpEntryById(asyncResp, dumpId, "System");
2937}
Claire Weinanfdd26902022-03-01 14:18:25 -08002938
2939inline void handleLogServicesDumpEntryDelete(
2940 crow::App& app, const std::string& dumpType, const crow::Request& req,
2941 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2942 const std::string& dumpId)
2943{
2944 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2945 {
2946 return;
2947 }
2948 deleteDumpEntry(asyncResp, dumpId, dumpType);
2949}
2950
Ed Tanous22d268c2022-05-19 09:39:07 -07002951inline void handleLogServicesDumpEntryComputerSystemDelete(
2952 crow::App& app, const crow::Request& req,
2953 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2954 const std::string& chassisId, const std::string& dumpId)
2955{
2956 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2957 {
2958 return;
2959 }
2960 if (chassisId != "system")
2961 {
2962 messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
2963 return;
2964 }
2965 deleteDumpEntry(asyncResp, dumpId, "System");
2966}
2967
Carson Labrado168d1b12023-03-27 17:04:46 +00002968inline void handleLogServicesDumpEntryDownloadGet(
2969 crow::App& app, const std::string& dumpType, const crow::Request& req,
2970 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2971 const std::string& dumpId)
2972{
2973 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2974 {
2975 return;
2976 }
2977 downloadDumpEntry(asyncResp, dumpId, dumpType);
2978}
2979
2980inline void handleDBusEventLogEntryDownloadGet(
2981 crow::App& app, const std::string& dumpType, const crow::Request& req,
2982 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2983 const std::string& systemName, const std::string& entryID)
2984{
2985 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2986 {
2987 return;
2988 }
2989 if (!http_helpers::isContentTypeAllowed(
2990 req.getHeaderValue("Accept"),
2991 http_helpers::ContentType::OctetStream, true))
2992 {
2993 asyncResp->res.result(boost::beast::http::status::bad_request);
2994 return;
2995 }
2996 downloadEventLogEntry(asyncResp, systemName, entryID, dumpType);
2997}
2998
Claire Weinanfdd26902022-03-01 14:18:25 -08002999inline void handleLogServicesDumpCollectDiagnosticDataPost(
3000 crow::App& app, const std::string& dumpType, const crow::Request& req,
3001 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
3002{
3003 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
3004 {
3005 return;
3006 }
3007 createDump(asyncResp, req, dumpType);
3008}
3009
Ed Tanous22d268c2022-05-19 09:39:07 -07003010inline void handleLogServicesDumpCollectDiagnosticDataComputerSystemPost(
3011 crow::App& app, const crow::Request& req,
3012 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous7f3e84a2022-12-28 16:22:54 -08003013 const std::string& systemName)
Ed Tanous22d268c2022-05-19 09:39:07 -07003014{
3015 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
3016 {
3017 return;
3018 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08003019
3020 if constexpr (bmcwebEnableMultiHost)
Ed Tanous22d268c2022-05-19 09:39:07 -07003021 {
Ed Tanous7f3e84a2022-12-28 16:22:54 -08003022 // Option currently returns no systems. TBD
3023 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3024 systemName);
3025 return;
3026 }
3027 if (systemName != "system")
3028 {
3029 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3030 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07003031 return;
3032 }
3033 createDump(asyncResp, req, "System");
3034}
3035
Claire Weinanfdd26902022-03-01 14:18:25 -08003036inline void handleLogServicesDumpClearLogPost(
3037 crow::App& app, const std::string& dumpType, const crow::Request& req,
3038 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
3039{
3040 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
3041 {
3042 return;
3043 }
3044 clearDump(asyncResp, dumpType);
3045}
3046
Ed Tanous22d268c2022-05-19 09:39:07 -07003047inline void handleLogServicesDumpClearLogComputerSystemPost(
3048 crow::App& app, const crow::Request& req,
3049 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous7f3e84a2022-12-28 16:22:54 -08003050 const std::string& systemName)
Ed Tanous22d268c2022-05-19 09:39:07 -07003051{
3052 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
3053 {
3054 return;
3055 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08003056 if constexpr (bmcwebEnableMultiHost)
Ed Tanous22d268c2022-05-19 09:39:07 -07003057 {
Ed Tanous7f3e84a2022-12-28 16:22:54 -08003058 // Option currently returns no systems. TBD
3059 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3060 systemName);
3061 return;
3062 }
3063 if (systemName != "system")
3064 {
3065 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3066 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07003067 return;
3068 }
3069 clearDump(asyncResp, "System");
3070}
3071
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003072inline void requestRoutesBMCDumpService(App& app)
3073{
3074 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Dump/")
Ed Tanoused398212021-06-09 17:05:54 -07003075 .privileges(redfish::privileges::getLogService)
Claire Weinanfdd26902022-03-01 14:18:25 -08003076 .methods(boost::beast::http::verb::get)(std::bind_front(
3077 handleLogServicesDumpServiceGet, std::ref(app), "BMC"));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003078}
3079
3080inline void requestRoutesBMCDumpEntryCollection(App& app)
3081{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003082 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07003083 .privileges(redfish::privileges::getLogEntryCollection)
Claire Weinanfdd26902022-03-01 14:18:25 -08003084 .methods(boost::beast::http::verb::get)(std::bind_front(
3085 handleLogServicesDumpEntriesCollectionGet, std::ref(app), "BMC"));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003086}
3087
3088inline void requestRoutesBMCDumpEntry(App& app)
3089{
3090 BMCWEB_ROUTE(app,
3091 "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07003092 .privileges(redfish::privileges::getLogEntry)
Claire Weinanfdd26902022-03-01 14:18:25 -08003093 .methods(boost::beast::http::verb::get)(std::bind_front(
3094 handleLogServicesDumpEntryGet, std::ref(app), "BMC"));
3095
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003096 BMCWEB_ROUTE(app,
3097 "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07003098 .privileges(redfish::privileges::deleteLogEntry)
Claire Weinanfdd26902022-03-01 14:18:25 -08003099 .methods(boost::beast::http::verb::delete_)(std::bind_front(
3100 handleLogServicesDumpEntryDelete, std::ref(app), "BMC"));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003101}
3102
Carson Labrado168d1b12023-03-27 17:04:46 +00003103inline void requestRoutesBMCDumpEntryDownload(App& app)
3104{
3105 BMCWEB_ROUTE(
3106 app,
Ravi Teja9e9d99d2023-11-08 05:33:59 -06003107 "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/<str>/attachment/")
Carson Labrado168d1b12023-03-27 17:04:46 +00003108 .privileges(redfish::privileges::getLogEntry)
3109 .methods(boost::beast::http::verb::get)(std::bind_front(
3110 handleLogServicesDumpEntryDownloadGet, std::ref(app), "BMC"));
3111}
3112
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003113inline void requestRoutesBMCDumpCreate(App& app)
3114{
George Liu0fda0f12021-11-16 10:06:17 +08003115 BMCWEB_ROUTE(
3116 app,
3117 "/redfish/v1/Managers/bmc/LogServices/Dump/Actions/LogService.CollectDiagnosticData/")
Ed Tanoused398212021-06-09 17:05:54 -07003118 .privileges(redfish::privileges::postLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003119 .methods(boost::beast::http::verb::post)(
Claire Weinanfdd26902022-03-01 14:18:25 -08003120 std::bind_front(handleLogServicesDumpCollectDiagnosticDataPost,
3121 std::ref(app), "BMC"));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003122}
3123
3124inline void requestRoutesBMCDumpClear(App& app)
3125{
George Liu0fda0f12021-11-16 10:06:17 +08003126 BMCWEB_ROUTE(
3127 app,
3128 "/redfish/v1/Managers/bmc/LogServices/Dump/Actions/LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07003129 .privileges(redfish::privileges::postLogService)
Claire Weinanfdd26902022-03-01 14:18:25 -08003130 .methods(boost::beast::http::verb::post)(std::bind_front(
3131 handleLogServicesDumpClearLogPost, std::ref(app), "BMC"));
3132}
3133
Carson Labrado168d1b12023-03-27 17:04:46 +00003134inline void requestRoutesDBusEventLogEntryDownload(App& app)
3135{
3136 BMCWEB_ROUTE(
3137 app,
Ravi Teja9e9d99d2023-11-08 05:33:59 -06003138 "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/<str>/attachment/")
Carson Labrado168d1b12023-03-27 17:04:46 +00003139 .privileges(redfish::privileges::getLogEntry)
3140 .methods(boost::beast::http::verb::get)(std::bind_front(
3141 handleDBusEventLogEntryDownloadGet, std::ref(app), "System"));
3142}
3143
Claire Weinanfdd26902022-03-01 14:18:25 -08003144inline void requestRoutesFaultLogDumpService(App& app)
3145{
3146 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/FaultLog/")
3147 .privileges(redfish::privileges::getLogService)
3148 .methods(boost::beast::http::verb::get)(std::bind_front(
3149 handleLogServicesDumpServiceGet, std::ref(app), "FaultLog"));
3150}
3151
3152inline void requestRoutesFaultLogDumpEntryCollection(App& app)
3153{
3154 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/FaultLog/Entries/")
3155 .privileges(redfish::privileges::getLogEntryCollection)
3156 .methods(boost::beast::http::verb::get)(
3157 std::bind_front(handleLogServicesDumpEntriesCollectionGet,
3158 std::ref(app), "FaultLog"));
3159}
3160
3161inline void requestRoutesFaultLogDumpEntry(App& app)
3162{
3163 BMCWEB_ROUTE(app,
3164 "/redfish/v1/Managers/bmc/LogServices/FaultLog/Entries/<str>/")
3165 .privileges(redfish::privileges::getLogEntry)
3166 .methods(boost::beast::http::verb::get)(std::bind_front(
3167 handleLogServicesDumpEntryGet, std::ref(app), "FaultLog"));
3168
3169 BMCWEB_ROUTE(app,
3170 "/redfish/v1/Managers/bmc/LogServices/FaultLog/Entries/<str>/")
3171 .privileges(redfish::privileges::deleteLogEntry)
3172 .methods(boost::beast::http::verb::delete_)(std::bind_front(
3173 handleLogServicesDumpEntryDelete, std::ref(app), "FaultLog"));
3174}
3175
3176inline void requestRoutesFaultLogDumpClear(App& app)
3177{
3178 BMCWEB_ROUTE(
3179 app,
3180 "/redfish/v1/Managers/bmc/LogServices/FaultLog/Actions/LogService.ClearLog/")
3181 .privileges(redfish::privileges::postLogService)
3182 .methods(boost::beast::http::verb::post)(std::bind_front(
3183 handleLogServicesDumpClearLogPost, std::ref(app), "FaultLog"));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003184}
3185
3186inline void requestRoutesSystemDumpService(App& app)
3187{
Ed Tanous22d268c2022-05-19 09:39:07 -07003188 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/Dump/")
Ed Tanoused398212021-06-09 17:05:54 -07003189 .privileges(redfish::privileges::getLogService)
Claire Weinan6ab9ad52022-08-12 18:20:17 -07003190 .methods(boost::beast::http::verb::get)(std::bind_front(
Ed Tanous22d268c2022-05-19 09:39:07 -07003191 handleLogServicesDumpServiceComputerSystemGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003192}
3193
3194inline void requestRoutesSystemDumpEntryCollection(App& app)
3195{
Ed Tanous22d268c2022-05-19 09:39:07 -07003196 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/Dump/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07003197 .privileges(redfish::privileges::getLogEntryCollection)
Ed Tanous22d268c2022-05-19 09:39:07 -07003198 .methods(boost::beast::http::verb::get)(std::bind_front(
3199 handleLogServicesDumpEntriesCollectionComputerSystemGet,
3200 std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003201}
3202
3203inline void requestRoutesSystemDumpEntry(App& app)
3204{
3205 BMCWEB_ROUTE(app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003206 "/redfish/v1/Systems/<str>/LogServices/Dump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07003207 .privileges(redfish::privileges::getLogEntry)
Claire Weinan6ab9ad52022-08-12 18:20:17 -07003208 .methods(boost::beast::http::verb::get)(std::bind_front(
Ed Tanous22d268c2022-05-19 09:39:07 -07003209 handleLogServicesDumpEntryComputerSystemGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003210
3211 BMCWEB_ROUTE(app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003212 "/redfish/v1/Systems/<str>/LogServices/Dump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07003213 .privileges(redfish::privileges::deleteLogEntry)
Claire Weinan6ab9ad52022-08-12 18:20:17 -07003214 .methods(boost::beast::http::verb::delete_)(std::bind_front(
Ed Tanous22d268c2022-05-19 09:39:07 -07003215 handleLogServicesDumpEntryComputerSystemDelete, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003216}
3217
3218inline void requestRoutesSystemDumpCreate(App& app)
3219{
George Liu0fda0f12021-11-16 10:06:17 +08003220 BMCWEB_ROUTE(
3221 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003222 "/redfish/v1/Systems/<str>/LogServices/Dump/Actions/LogService.CollectDiagnosticData/")
Ed Tanoused398212021-06-09 17:05:54 -07003223 .privileges(redfish::privileges::postLogService)
Ed Tanous22d268c2022-05-19 09:39:07 -07003224 .methods(boost::beast::http::verb::post)(std::bind_front(
3225 handleLogServicesDumpCollectDiagnosticDataComputerSystemPost,
3226 std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003227}
3228
3229inline void requestRoutesSystemDumpClear(App& app)
3230{
George Liu0fda0f12021-11-16 10:06:17 +08003231 BMCWEB_ROUTE(
3232 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003233 "/redfish/v1/Systems/<str>/LogServices/Dump/Actions/LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07003234 .privileges(redfish::privileges::postLogService)
Claire Weinan6ab9ad52022-08-12 18:20:17 -07003235 .methods(boost::beast::http::verb::post)(std::bind_front(
Ed Tanous22d268c2022-05-19 09:39:07 -07003236 handleLogServicesDumpClearLogComputerSystemPost, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003237}
3238
3239inline void requestRoutesCrashdumpService(App& app)
3240{
3241 // Note: Deviated from redfish privilege registry for GET & HEAD
3242 // method for security reasons.
3243 /**
3244 * Functions triggers appropriate requests on DBus
3245 */
Ed Tanous22d268c2022-05-19 09:39:07 -07003246 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/Crashdump/")
Ed Tanoused398212021-06-09 17:05:54 -07003247 // This is incorrect, should be:
3248 //.privileges(redfish::privileges::getLogService)
Ed Tanous432a8902021-06-14 15:28:56 -07003249 .privileges({{"ConfigureManager"}})
Ed Tanous002d39b2022-05-31 08:59:27 -07003250 .methods(boost::beast::http::verb::get)(
3251 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07003252 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3253 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003254 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003255 {
3256 return;
3257 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08003258 if constexpr (bmcwebEnableMultiHost)
3259 {
3260 // Option currently returns no systems. TBD
3261 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3262 systemName);
3263 return;
3264 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003265 if (systemName != "system")
3266 {
3267 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3268 systemName);
3269 return;
3270 }
3271
Ed Tanous002d39b2022-05-31 08:59:27 -07003272 // Copy over the static data to include the entries added by
3273 // SubRoute
3274 asyncResp->res.jsonValue["@odata.id"] =
3275 "/redfish/v1/Systems/system/LogServices/Crashdump";
3276 asyncResp->res.jsonValue["@odata.type"] =
3277 "#LogService.v1_2_0.LogService";
3278 asyncResp->res.jsonValue["Name"] = "Open BMC Oem Crashdump Service";
3279 asyncResp->res.jsonValue["Description"] = "Oem Crashdump Service";
V-Sanjana15b89722023-05-11 16:27:03 +05303280 asyncResp->res.jsonValue["Id"] = "Crashdump";
Ed Tanous002d39b2022-05-31 08:59:27 -07003281 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
3282 asyncResp->res.jsonValue["MaxNumberOfRecords"] = 3;
Tejas Patil7c8c4052021-06-04 17:43:14 +05303283
Ed Tanous002d39b2022-05-31 08:59:27 -07003284 std::pair<std::string, std::string> redfishDateTimeOffset =
Ed Tanous2b829372022-08-03 14:22:34 -07003285 redfish::time_utils::getDateTimeOffsetNow();
Ed Tanous002d39b2022-05-31 08:59:27 -07003286 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
3287 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
3288 redfishDateTimeOffset.second;
Tejas Patil7c8c4052021-06-04 17:43:14 +05303289
Ed Tanous002d39b2022-05-31 08:59:27 -07003290 asyncResp->res.jsonValue["Entries"]["@odata.id"] =
Ed Tanousef4c65b2023-04-24 15:28:50 -07003291 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries";
Ed Tanous002d39b2022-05-31 08:59:27 -07003292 asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"]["target"] =
3293 "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.ClearLog";
3294 asyncResp->res.jsonValue["Actions"]["#LogService.CollectDiagnosticData"]
3295 ["target"] =
3296 "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.CollectDiagnosticData";
Patrick Williams5a39f772023-10-20 11:20:21 -05003297 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003298}
3299
3300void inline requestRoutesCrashdumpClear(App& app)
3301{
George Liu0fda0f12021-11-16 10:06:17 +08003302 BMCWEB_ROUTE(
3303 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003304 "/redfish/v1/Systems/<str>/LogServices/Crashdump/Actions/LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07003305 // This is incorrect, should be:
3306 //.privileges(redfish::privileges::postLogService)
Ed Tanous432a8902021-06-14 15:28:56 -07003307 .privileges({{"ConfigureComponents"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003308 .methods(boost::beast::http::verb::post)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07003309 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07003310 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3311 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003312 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003313 {
3314 return;
3315 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08003316 if constexpr (bmcwebEnableMultiHost)
3317 {
3318 // Option currently returns no systems. TBD
3319 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3320 systemName);
3321 return;
3322 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003323 if (systemName != "system")
3324 {
3325 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3326 systemName);
3327 return;
3328 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003329 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08003330 [asyncResp](const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07003331 const std::string&) {
3332 if (ec)
3333 {
3334 messages::internalError(asyncResp->res);
3335 return;
3336 }
3337 messages::success(asyncResp->res);
Patrick Williams5a39f772023-10-20 11:20:21 -05003338 },
Ed Tanous002d39b2022-05-31 08:59:27 -07003339 crashdumpObject, crashdumpPath, deleteAllInterface, "DeleteAll");
Patrick Williams5a39f772023-10-20 11:20:21 -05003340 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003341}
Jason M. Bills5b61b5e2019-10-16 10:59:02 -07003342
zhanghch058d1b46d2021-04-01 11:18:24 +08003343static void
3344 logCrashdumpEntry(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3345 const std::string& logID, nlohmann::json& logEntryJson)
Jason M. Billse855dd22019-10-08 11:37:48 -07003346{
Johnathan Mantey043a0532020-03-10 17:15:28 -07003347 auto getStoredLogCallback =
Ed Tanousb9d36b42022-02-26 21:42:46 -08003348 [asyncResp, logID,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08003349 &logEntryJson](const boost::system::error_code& ec,
Ed Tanousb9d36b42022-02-26 21:42:46 -08003350 const dbus::utility::DBusPropertiesMap& params) {
Ed Tanous002d39b2022-05-31 08:59:27 -07003351 if (ec)
3352 {
Ed Tanous62598e32023-07-17 17:06:25 -07003353 BMCWEB_LOG_DEBUG("failed to get log ec: {}", ec.message());
Ed Tanous002d39b2022-05-31 08:59:27 -07003354 if (ec.value() ==
3355 boost::system::linux_error::bad_request_descriptor)
Jason M. Bills1ddcf012019-11-26 14:59:21 -08003356 {
Ed Tanous002d39b2022-05-31 08:59:27 -07003357 messages::resourceNotFound(asyncResp->res, "LogEntry", logID);
Jason M. Bills2b20ef62022-01-06 15:48:07 -08003358 }
3359 else
3360 {
Ed Tanous002d39b2022-05-31 08:59:27 -07003361 messages::internalError(asyncResp->res);
Jason M. Bills2b20ef62022-01-06 15:48:07 -08003362 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003363 return;
3364 }
3365
3366 std::string timestamp{};
3367 std::string filename{};
3368 std::string logfile{};
3369 parseCrashdumpParameters(params, filename, timestamp, logfile);
3370
3371 if (filename.empty() || timestamp.empty())
3372 {
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08003373 messages::resourceNotFound(asyncResp->res, "LogEntry", logID);
Ed Tanous002d39b2022-05-31 08:59:27 -07003374 return;
3375 }
3376
3377 std::string crashdumpURI =
3378 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/" +
3379 logID + "/" + filename;
Jason M. Bills84afc482022-06-24 12:38:23 -07003380 nlohmann::json::object_t logEntry;
Vijay Lobo9c11a172021-10-07 16:53:16 -05003381 logEntry["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
Ed Tanousef4c65b2023-04-24 15:28:50 -07003382 logEntry["@odata.id"] = boost::urls::format(
3383 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/{}",
3384 logID);
Jason M. Bills84afc482022-06-24 12:38:23 -07003385 logEntry["Name"] = "CPU Crashdump";
3386 logEntry["Id"] = logID;
3387 logEntry["EntryType"] = "Oem";
3388 logEntry["AdditionalDataURI"] = std::move(crashdumpURI);
3389 logEntry["DiagnosticDataType"] = "OEM";
3390 logEntry["OEMDiagnosticDataType"] = "PECICrashdump";
3391 logEntry["Created"] = std::move(timestamp);
Ed Tanous002d39b2022-05-31 08:59:27 -07003392
3393 // If logEntryJson references an array of LogEntry resources
3394 // ('Members' list), then push this as a new entry, otherwise set it
3395 // directly
3396 if (logEntryJson.is_array())
3397 {
3398 logEntryJson.push_back(logEntry);
3399 asyncResp->res.jsonValue["Members@odata.count"] =
3400 logEntryJson.size();
3401 }
3402 else
3403 {
Jason M. Billsd405bb52022-06-24 10:52:05 -07003404 logEntryJson.update(logEntry);
Ed Tanous002d39b2022-05-31 08:59:27 -07003405 }
3406 };
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02003407 sdbusplus::asio::getAllProperties(
3408 *crow::connections::systemBus, crashdumpObject,
3409 crashdumpPath + std::string("/") + logID, crashdumpInterface,
3410 std::move(getStoredLogCallback));
Jason M. Billse855dd22019-10-08 11:37:48 -07003411}
3412
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003413inline void requestRoutesCrashdumpEntryCollection(App& app)
Ed Tanous1da66f72018-07-27 16:13:37 -07003414{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003415 // Note: Deviated from redfish privilege registry for GET & HEAD
3416 // method for security reasons.
Ed Tanous1da66f72018-07-27 16:13:37 -07003417 /**
3418 * Functions triggers appropriate requests on DBus
3419 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003420 BMCWEB_ROUTE(app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003421 "/redfish/v1/Systems/<str>/LogServices/Crashdump/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07003422 // This is incorrect, should be.
3423 //.privileges(redfish::privileges::postLogEntryCollection)
Ed Tanous432a8902021-06-14 15:28:56 -07003424 .privileges({{"ConfigureComponents"}})
Ed Tanous002d39b2022-05-31 08:59:27 -07003425 .methods(boost::beast::http::verb::get)(
3426 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07003427 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3428 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003429 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003430 {
3431 return;
3432 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08003433 if constexpr (bmcwebEnableMultiHost)
3434 {
3435 // Option currently returns no systems. TBD
3436 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3437 systemName);
3438 return;
3439 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003440 if (systemName != "system")
3441 {
3442 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3443 systemName);
3444 return;
3445 }
3446
George Liu7a1dbc42022-12-07 16:03:22 +08003447 constexpr std::array<std::string_view, 1> interfaces = {
3448 crashdumpInterface};
3449 dbus::utility::getSubTreePaths(
3450 "/", 0, interfaces,
3451 [asyncResp](const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07003452 const std::vector<std::string>& resp) {
3453 if (ec)
Ed Tanous45ca1b82022-03-25 13:07:27 -07003454 {
Ed Tanous002d39b2022-05-31 08:59:27 -07003455 if (ec.value() !=
3456 boost::system::errc::no_such_file_or_directory)
3457 {
Ed Tanous62598e32023-07-17 17:06:25 -07003458 BMCWEB_LOG_DEBUG("failed to get entries ec: {}",
3459 ec.message());
Ed Tanous002d39b2022-05-31 08:59:27 -07003460 messages::internalError(asyncResp->res);
3461 return;
3462 }
Ed Tanous45ca1b82022-03-25 13:07:27 -07003463 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003464 asyncResp->res.jsonValue["@odata.type"] =
3465 "#LogEntryCollection.LogEntryCollection";
3466 asyncResp->res.jsonValue["@odata.id"] =
3467 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries";
3468 asyncResp->res.jsonValue["Name"] = "Open BMC Crashdump Entries";
3469 asyncResp->res.jsonValue["Description"] =
3470 "Collection of Crashdump Entries";
3471 asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
3472 asyncResp->res.jsonValue["Members@odata.count"] = 0;
Jason M. Bills2b20ef62022-01-06 15:48:07 -08003473
Ed Tanous002d39b2022-05-31 08:59:27 -07003474 for (const std::string& path : resp)
3475 {
3476 const sdbusplus::message::object_path objPath(path);
3477 // Get the log ID
3478 std::string logID = objPath.filename();
3479 if (logID.empty())
3480 {
3481 continue;
3482 }
3483 // Add the log entry to the array
3484 logCrashdumpEntry(asyncResp, logID,
3485 asyncResp->res.jsonValue["Members"]);
3486 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003487 });
Patrick Williams5a39f772023-10-20 11:20:21 -05003488 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003489}
Ed Tanous1da66f72018-07-27 16:13:37 -07003490
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003491inline void requestRoutesCrashdumpEntry(App& app)
Ed Tanous1da66f72018-07-27 16:13:37 -07003492{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003493 // Note: Deviated from redfish privilege registry for GET & HEAD
3494 // method for security reasons.
Ed Tanous1da66f72018-07-27 16:13:37 -07003495
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003496 BMCWEB_ROUTE(
Ed Tanous22d268c2022-05-19 09:39:07 -07003497 app, "/redfish/v1/Systems/<str>/LogServices/Crashdump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07003498 // this is incorrect, should be
3499 // .privileges(redfish::privileges::getLogEntry)
Ed Tanous432a8902021-06-14 15:28:56 -07003500 .privileges({{"ConfigureComponents"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003501 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07003502 [&app](const crow::Request& req,
3503 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07003504 const std::string& systemName, const std::string& param) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003505 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003506 {
3507 return;
3508 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08003509 if constexpr (bmcwebEnableMultiHost)
3510 {
3511 // Option currently returns no systems. TBD
3512 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3513 systemName);
3514 return;
3515 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003516 if (systemName != "system")
3517 {
3518 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3519 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07003520 return;
3521 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003522 const std::string& logID = param;
3523 logCrashdumpEntry(asyncResp, logID, asyncResp->res.jsonValue);
Patrick Williams5a39f772023-10-20 11:20:21 -05003524 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003525}
Ed Tanous1da66f72018-07-27 16:13:37 -07003526
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003527inline void requestRoutesCrashdumpFile(App& app)
3528{
3529 // Note: Deviated from redfish privilege registry for GET & HEAD
3530 // method for security reasons.
3531 BMCWEB_ROUTE(
3532 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003533 "/redfish/v1/Systems/<str>/LogServices/Crashdump/Entries/<str>/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07003534 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003535 .methods(boost::beast::http::verb::get)(
Nan Zhoua4ce1142022-08-02 18:45:25 +00003536 [](const crow::Request& req,
3537 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07003538 const std::string& systemName, const std::string& logID,
3539 const std::string& fileName) {
Shounak Mitra2a9beee2022-07-20 18:41:30 +00003540 // Do not call getRedfishRoute here since the crashdump file is not a
3541 // Redfish resource.
Ed Tanous22d268c2022-05-19 09:39:07 -07003542
Ed Tanous7f3e84a2022-12-28 16:22:54 -08003543 if constexpr (bmcwebEnableMultiHost)
3544 {
3545 // Option currently returns no systems. TBD
3546 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3547 systemName);
3548 return;
3549 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003550 if (systemName != "system")
3551 {
3552 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3553 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07003554 return;
3555 }
3556
Ed Tanous002d39b2022-05-31 08:59:27 -07003557 auto getStoredLogCallback =
Ed Tanous39662a32023-02-06 15:09:46 -08003558 [asyncResp, logID, fileName, url(boost::urls::url(req.url()))](
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08003559 const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07003560 const std::vector<
3561 std::pair<std::string, dbus::utility::DbusVariantType>>&
3562 resp) {
3563 if (ec)
3564 {
Ed Tanous62598e32023-07-17 17:06:25 -07003565 BMCWEB_LOG_DEBUG("failed to get log ec: {}", ec.message());
Ed Tanous002d39b2022-05-31 08:59:27 -07003566 messages::internalError(asyncResp->res);
3567 return;
3568 }
Jason M. Bills8e6c0992021-03-11 16:26:53 -08003569
Ed Tanous002d39b2022-05-31 08:59:27 -07003570 std::string dbusFilename{};
3571 std::string dbusTimestamp{};
3572 std::string dbusFilepath{};
Jason M. Bills8e6c0992021-03-11 16:26:53 -08003573
Ed Tanous002d39b2022-05-31 08:59:27 -07003574 parseCrashdumpParameters(resp, dbusFilename, dbusTimestamp,
3575 dbusFilepath);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003576
Ed Tanous002d39b2022-05-31 08:59:27 -07003577 if (dbusFilename.empty() || dbusTimestamp.empty() ||
3578 dbusFilepath.empty())
3579 {
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 // Verify the file name parameter is correct
3585 if (fileName != dbusFilename)
3586 {
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08003587 messages::resourceNotFound(asyncResp->res, "LogEntry", logID);
Ed Tanous002d39b2022-05-31 08:59:27 -07003588 return;
3589 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003590
Ed Tanous27b0cf92023-08-07 12:02:40 -07003591 if (!asyncResp->res.openFile(dbusFilepath))
Ed Tanous002d39b2022-05-31 08:59:27 -07003592 {
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08003593 messages::resourceNotFound(asyncResp->res, "LogEntry", logID);
Ed Tanous002d39b2022-05-31 08:59:27 -07003594 return;
3595 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003596
Ed Tanous002d39b2022-05-31 08:59:27 -07003597 // Configure this to be a file download when accessed
3598 // from a browser
Ed Tanousd9f6c622022-03-17 09:12:17 -07003599 asyncResp->res.addHeader(
3600 boost::beast::http::field::content_disposition, "attachment");
Ed Tanous002d39b2022-05-31 08:59:27 -07003601 };
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02003602 sdbusplus::asio::getAllProperties(
3603 *crow::connections::systemBus, crashdumpObject,
3604 crashdumpPath + std::string("/") + logID, crashdumpInterface,
3605 std::move(getStoredLogCallback));
Patrick Williams5a39f772023-10-20 11:20:21 -05003606 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003607}
3608
Jason M. Billsc5a4c822022-01-06 15:51:23 -08003609enum class OEMDiagnosticType
3610{
3611 onDemand,
3612 telemetry,
3613 invalid,
3614};
3615
Ed Tanous26ccae32023-02-16 10:28:44 -08003616inline OEMDiagnosticType getOEMDiagnosticType(std::string_view oemDiagStr)
Jason M. Billsc5a4c822022-01-06 15:51:23 -08003617{
3618 if (oemDiagStr == "OnDemand")
3619 {
3620 return OEMDiagnosticType::onDemand;
3621 }
3622 if (oemDiagStr == "Telemetry")
3623 {
3624 return OEMDiagnosticType::telemetry;
3625 }
3626
3627 return OEMDiagnosticType::invalid;
3628}
3629
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003630inline void requestRoutesCrashdumpCollect(App& app)
3631{
3632 // Note: Deviated from redfish privilege registry for GET & HEAD
3633 // method for security reasons.
George Liu0fda0f12021-11-16 10:06:17 +08003634 BMCWEB_ROUTE(
3635 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003636 "/redfish/v1/Systems/<str>/LogServices/Crashdump/Actions/LogService.CollectDiagnosticData/")
Ed Tanoused398212021-06-09 17:05:54 -07003637 // The below is incorrect; Should be ConfigureManager
3638 //.privileges(redfish::privileges::postLogService)
Ed Tanous432a8902021-06-14 15:28:56 -07003639 .privileges({{"ConfigureComponents"}})
Ed Tanous002d39b2022-05-31 08:59:27 -07003640 .methods(boost::beast::http::verb::post)(
3641 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07003642 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3643 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003644 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003645 {
3646 return;
3647 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003648
Ed Tanous7f3e84a2022-12-28 16:22:54 -08003649 if constexpr (bmcwebEnableMultiHost)
3650 {
3651 // Option currently returns no systems. TBD
3652 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3653 systemName);
3654 return;
3655 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003656 if (systemName != "system")
3657 {
3658 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3659 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07003660 return;
3661 }
3662
Ed Tanous002d39b2022-05-31 08:59:27 -07003663 std::string diagnosticDataType;
3664 std::string oemDiagnosticDataType;
3665 if (!redfish::json_util::readJsonAction(
3666 req, asyncResp->res, "DiagnosticDataType", diagnosticDataType,
3667 "OEMDiagnosticDataType", oemDiagnosticDataType))
3668 {
3669 return;
3670 }
3671
3672 if (diagnosticDataType != "OEM")
3673 {
Ed Tanous62598e32023-07-17 17:06:25 -07003674 BMCWEB_LOG_ERROR(
3675 "Only OEM DiagnosticDataType supported for Crashdump");
Ed Tanous002d39b2022-05-31 08:59:27 -07003676 messages::actionParameterValueFormatError(
3677 asyncResp->res, diagnosticDataType, "DiagnosticDataType",
3678 "CollectDiagnosticData");
3679 return;
3680 }
3681
3682 OEMDiagnosticType oemDiagType =
3683 getOEMDiagnosticType(oemDiagnosticDataType);
3684
3685 std::string iface;
3686 std::string method;
3687 std::string taskMatchStr;
3688 if (oemDiagType == OEMDiagnosticType::onDemand)
3689 {
3690 iface = crashdumpOnDemandInterface;
3691 method = "GenerateOnDemandLog";
3692 taskMatchStr = "type='signal',"
3693 "interface='org.freedesktop.DBus.Properties',"
3694 "member='PropertiesChanged',"
3695 "arg0namespace='com.intel.crashdump'";
3696 }
3697 else if (oemDiagType == OEMDiagnosticType::telemetry)
3698 {
3699 iface = crashdumpTelemetryInterface;
3700 method = "GenerateTelemetryLog";
3701 taskMatchStr = "type='signal',"
3702 "interface='org.freedesktop.DBus.Properties',"
3703 "member='PropertiesChanged',"
3704 "arg0namespace='com.intel.crashdump'";
3705 }
3706 else
3707 {
Ed Tanous62598e32023-07-17 17:06:25 -07003708 BMCWEB_LOG_ERROR("Unsupported OEMDiagnosticDataType: {}",
3709 oemDiagnosticDataType);
Ed Tanous002d39b2022-05-31 08:59:27 -07003710 messages::actionParameterValueFormatError(
3711 asyncResp->res, oemDiagnosticDataType, "OEMDiagnosticDataType",
3712 "CollectDiagnosticData");
3713 return;
3714 }
3715
3716 auto collectCrashdumpCallback =
3717 [asyncResp, payload(task::Payload(req)),
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08003718 taskMatchStr](const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07003719 const std::string&) mutable {
3720 if (ec)
Ed Tanous45ca1b82022-03-25 13:07:27 -07003721 {
Ed Tanous002d39b2022-05-31 08:59:27 -07003722 if (ec.value() == boost::system::errc::operation_not_supported)
3723 {
3724 messages::resourceInStandby(asyncResp->res);
3725 }
3726 else if (ec.value() ==
3727 boost::system::errc::device_or_resource_busy)
3728 {
3729 messages::serviceTemporarilyUnavailable(asyncResp->res,
3730 "60");
3731 }
3732 else
3733 {
3734 messages::internalError(asyncResp->res);
3735 }
Ed Tanous45ca1b82022-03-25 13:07:27 -07003736 return;
3737 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003738 std::shared_ptr<task::TaskData> task = task::TaskData::createTask(
Ed Tanous8b242752023-06-27 17:17:13 -07003739 [](const boost::system::error_code& ec2, sdbusplus::message_t&,
Ed Tanous002d39b2022-05-31 08:59:27 -07003740 const std::shared_ptr<task::TaskData>& taskData) {
Ed Tanous8b242752023-06-27 17:17:13 -07003741 if (!ec2)
Ed Tanous002d39b2022-05-31 08:59:27 -07003742 {
3743 taskData->messages.emplace_back(messages::taskCompletedOK(
3744 std::to_string(taskData->index)));
3745 taskData->state = "Completed";
3746 }
3747 return task::completed;
Patrick Williams5a39f772023-10-20 11:20:21 -05003748 },
Ed Tanous002d39b2022-05-31 08:59:27 -07003749 taskMatchStr);
Ed Tanous1da66f72018-07-27 16:13:37 -07003750
Ed Tanous002d39b2022-05-31 08:59:27 -07003751 task->startTimer(std::chrono::minutes(5));
3752 task->populateResp(asyncResp->res);
3753 task->payload.emplace(std::move(payload));
3754 };
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003755
Ed Tanous002d39b2022-05-31 08:59:27 -07003756 crow::connections::systemBus->async_method_call(
3757 std::move(collectCrashdumpCallback), crashdumpObject, crashdumpPath,
3758 iface, method);
Patrick Williams5a39f772023-10-20 11:20:21 -05003759 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003760}
Kenny L. Ku6eda7682020-06-19 09:48:36 -07003761
Andrew Geisslercb92c032018-08-17 07:56:14 -07003762/**
3763 * DBusLogServiceActionsClear class supports POST method for ClearLog action.
3764 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003765inline void requestRoutesDBusLogServiceActionsClear(App& app)
Andrew Geisslercb92c032018-08-17 07:56:14 -07003766{
Andrew Geisslercb92c032018-08-17 07:56:14 -07003767 /**
3768 * Function handles POST method request.
3769 * The Clear Log actions does not require any parameter.The action deletes
3770 * all entries found in the Entries collection for this Log Service.
3771 */
Andrew Geisslercb92c032018-08-17 07:56:14 -07003772
George Liu0fda0f12021-11-16 10:06:17 +08003773 BMCWEB_ROUTE(
3774 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003775 "/redfish/v1/Systems/<str>/LogServices/EventLog/Actions/LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07003776 .privileges(redfish::privileges::postLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003777 .methods(boost::beast::http::verb::post)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07003778 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07003779 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3780 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003781 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003782 {
3783 return;
3784 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08003785 if constexpr (bmcwebEnableMultiHost)
3786 {
3787 // Option currently returns no systems. TBD
3788 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3789 systemName);
3790 return;
3791 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003792 if (systemName != "system")
3793 {
3794 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3795 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07003796 return;
3797 }
Ed Tanous62598e32023-07-17 17:06:25 -07003798 BMCWEB_LOG_DEBUG("Do delete all entries.");
Andrew Geisslercb92c032018-08-17 07:56:14 -07003799
Ed Tanous002d39b2022-05-31 08:59:27 -07003800 // Process response from Logging service.
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08003801 auto respHandler = [asyncResp](const boost::system::error_code& ec) {
Ed Tanous62598e32023-07-17 17:06:25 -07003802 BMCWEB_LOG_DEBUG("doClearLog resp_handler callback: Done");
Ed Tanous002d39b2022-05-31 08:59:27 -07003803 if (ec)
3804 {
3805 // TODO Handle for specific error code
Ed Tanous62598e32023-07-17 17:06:25 -07003806 BMCWEB_LOG_ERROR("doClearLog resp_handler got error {}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -07003807 asyncResp->res.result(
3808 boost::beast::http::status::internal_server_error);
3809 return;
3810 }
Andrew Geisslercb92c032018-08-17 07:56:14 -07003811
Ed Tanous002d39b2022-05-31 08:59:27 -07003812 asyncResp->res.result(boost::beast::http::status::no_content);
3813 };
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003814
Ed Tanous002d39b2022-05-31 08:59:27 -07003815 // Make call to Logging service to request Clear Log
3816 crow::connections::systemBus->async_method_call(
3817 respHandler, "xyz.openbmc_project.Logging",
3818 "/xyz/openbmc_project/logging",
3819 "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
Patrick Williams5a39f772023-10-20 11:20:21 -05003820 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003821}
ZhikuiRena3316fc2020-01-29 14:58:08 -08003822
3823/****************************************************
3824 * Redfish PostCode interfaces
3825 * using DBUS interface: getPostCodesTS
3826 ******************************************************/
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003827inline void requestRoutesPostCodesLogService(App& app)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003828{
Ed Tanous22d268c2022-05-19 09:39:07 -07003829 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/PostCodes/")
Ed Tanoused398212021-06-09 17:05:54 -07003830 .privileges(redfish::privileges::getLogService)
Ed Tanous002d39b2022-05-31 08:59:27 -07003831 .methods(boost::beast::http::verb::get)(
3832 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07003833 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3834 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003835 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003836 {
3837 return;
3838 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08003839 if constexpr (bmcwebEnableMultiHost)
3840 {
3841 // Option currently returns no systems. TBD
3842 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3843 systemName);
3844 return;
3845 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003846 if (systemName != "system")
3847 {
3848 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3849 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07003850 return;
3851 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003852 asyncResp->res.jsonValue["@odata.id"] =
3853 "/redfish/v1/Systems/system/LogServices/PostCodes";
3854 asyncResp->res.jsonValue["@odata.type"] =
Janet Adkinsb25644a2023-08-16 11:23:45 -05003855 "#LogService.v1_2_0.LogService";
Ed Tanous002d39b2022-05-31 08:59:27 -07003856 asyncResp->res.jsonValue["Name"] = "POST Code Log Service";
3857 asyncResp->res.jsonValue["Description"] = "POST Code Log Service";
Ed Tanoused34a4a2023-02-08 15:43:27 -08003858 asyncResp->res.jsonValue["Id"] = "PostCodes";
Ed Tanous002d39b2022-05-31 08:59:27 -07003859 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
3860 asyncResp->res.jsonValue["Entries"]["@odata.id"] =
3861 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries";
Tejas Patil7c8c4052021-06-04 17:43:14 +05303862
Ed Tanous002d39b2022-05-31 08:59:27 -07003863 std::pair<std::string, std::string> redfishDateTimeOffset =
Ed Tanous2b829372022-08-03 14:22:34 -07003864 redfish::time_utils::getDateTimeOffsetNow();
Ed Tanous002d39b2022-05-31 08:59:27 -07003865 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
3866 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
3867 redfishDateTimeOffset.second;
Tejas Patil7c8c4052021-06-04 17:43:14 +05303868
Ed Tanous002d39b2022-05-31 08:59:27 -07003869 asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"] = {
3870 {"target",
3871 "/redfish/v1/Systems/system/LogServices/PostCodes/Actions/LogService.ClearLog"}};
Patrick Williams5a39f772023-10-20 11:20:21 -05003872 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003873}
ZhikuiRena3316fc2020-01-29 14:58:08 -08003874
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003875inline void requestRoutesPostCodesClear(App& app)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003876{
George Liu0fda0f12021-11-16 10:06:17 +08003877 BMCWEB_ROUTE(
3878 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003879 "/redfish/v1/Systems/<str>/LogServices/PostCodes/Actions/LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07003880 // The following privilege is incorrect; It should be ConfigureManager
3881 //.privileges(redfish::privileges::postLogService)
Ed Tanous432a8902021-06-14 15:28:56 -07003882 .privileges({{"ConfigureComponents"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003883 .methods(boost::beast::http::verb::post)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07003884 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07003885 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3886 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003887 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003888 {
3889 return;
3890 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08003891 if constexpr (bmcwebEnableMultiHost)
3892 {
3893 // Option currently returns no systems. TBD
3894 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3895 systemName);
3896 return;
3897 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003898 if (systemName != "system")
3899 {
3900 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3901 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07003902 return;
3903 }
Ed Tanous62598e32023-07-17 17:06:25 -07003904 BMCWEB_LOG_DEBUG("Do delete all postcodes entries.");
ZhikuiRena3316fc2020-01-29 14:58:08 -08003905
Ed Tanous002d39b2022-05-31 08:59:27 -07003906 // Make call to post-code service to request clear all
3907 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08003908 [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07003909 if (ec)
3910 {
3911 // TODO Handle for specific error code
Ed Tanous62598e32023-07-17 17:06:25 -07003912 BMCWEB_LOG_ERROR("doClearPostCodes resp_handler got error {}",
3913 ec);
Ed Tanous002d39b2022-05-31 08:59:27 -07003914 asyncResp->res.result(
3915 boost::beast::http::status::internal_server_error);
3916 messages::internalError(asyncResp->res);
3917 return;
3918 }
Tony Lee18fc70c2023-08-24 16:15:54 +08003919 messages::success(asyncResp->res);
Patrick Williams5a39f772023-10-20 11:20:21 -05003920 },
Ed Tanous002d39b2022-05-31 08:59:27 -07003921 "xyz.openbmc_project.State.Boot.PostCode0",
3922 "/xyz/openbmc_project/State/Boot/PostCode0",
3923 "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
Patrick Williams5a39f772023-10-20 11:20:21 -05003924 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003925}
ZhikuiRena3316fc2020-01-29 14:58:08 -08003926
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003927/**
3928 * @brief Parse post code ID and get the current value and index value
3929 * eg: postCodeID=B1-2, currentValue=1, index=2
3930 *
3931 * @param[in] postCodeID Post Code ID
3932 * @param[out] currentValue Current value
3933 * @param[out] index Index value
3934 *
3935 * @return bool true if the parsing is successful, false the parsing fails
3936 */
3937inline static bool parsePostCode(const std::string& postCodeID,
3938 uint64_t& currentValue, uint16_t& index)
3939{
3940 std::vector<std::string> split;
Ed Tanous50ebd4a2023-01-19 19:03:17 -08003941 bmcweb::split(split, postCodeID, '-');
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003942 if (split.size() != 2 || split[0].length() < 2 || split[0].front() != 'B')
3943 {
3944 return false;
3945 }
3946
Patrick Williams84396af2023-05-11 11:47:45 -05003947 auto start = std::next(split[0].begin());
3948 auto end = split[0].end();
3949 auto [ptrIndex, ecIndex] = std::from_chars(&*start, &*end, index);
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003950
Patrick Williams84396af2023-05-11 11:47:45 -05003951 if (ptrIndex != &*end || ecIndex != std::errc())
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003952 {
3953 return false;
3954 }
3955
Patrick Williams84396af2023-05-11 11:47:45 -05003956 start = split[1].begin();
3957 end = split[1].end();
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003958
Patrick Williams84396af2023-05-11 11:47:45 -05003959 auto [ptrValue, ecValue] = std::from_chars(&*start, &*end, currentValue);
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003960
Patrick Williams84396af2023-05-11 11:47:45 -05003961 return ptrValue == &*end && ecValue == std::errc();
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003962}
3963
3964static bool fillPostCodeEntry(
Ed Tanousac106bf2023-06-07 09:24:59 -07003965 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05303966 const boost::container::flat_map<
3967 uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>& postcode,
ZhikuiRena3316fc2020-01-29 14:58:08 -08003968 const uint16_t bootIndex, const uint64_t codeIndex = 0,
3969 const uint64_t skip = 0, const uint64_t top = 0)
3970{
3971 // Get the Message from the MessageRegistry
Ed Tanousfffb8c12022-02-07 23:53:03 -08003972 const registries::Message* message =
3973 registries::getMessage("OpenBMC.0.2.BIOSPOSTCode");
ZhikuiRena3316fc2020-01-29 14:58:08 -08003974
3975 uint64_t currentCodeIndex = 0;
ZhikuiRena3316fc2020-01-29 14:58:08 -08003976 uint64_t firstCodeTimeUs = 0;
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05303977 for (const std::pair<uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
3978 code : postcode)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003979 {
3980 currentCodeIndex++;
3981 std::string postcodeEntryID =
3982 "B" + std::to_string(bootIndex) + "-" +
3983 std::to_string(currentCodeIndex); // 1 based index in EntryID string
3984
3985 uint64_t usecSinceEpoch = code.first;
3986 uint64_t usTimeOffset = 0;
3987
3988 if (1 == currentCodeIndex)
3989 { // already incremented
3990 firstCodeTimeUs = code.first;
3991 }
3992 else
3993 {
3994 usTimeOffset = code.first - firstCodeTimeUs;
3995 }
3996
3997 // skip if no specific codeIndex is specified and currentCodeIndex does
3998 // not fall between top and skip
3999 if ((codeIndex == 0) &&
4000 (currentCodeIndex <= skip || currentCodeIndex > top))
4001 {
4002 continue;
4003 }
4004
Gunnar Mills4e0453b2020-07-08 14:00:30 -05004005 // skip if a specific codeIndex is specified and does not match the
ZhikuiRena3316fc2020-01-29 14:58:08 -08004006 // currentIndex
4007 if ((codeIndex > 0) && (currentCodeIndex != codeIndex))
4008 {
4009 // This is done for simplicity. 1st entry is needed to calculate
4010 // time offset. To improve efficiency, one can get to the entry
4011 // directly (possibly with flatmap's nth method)
4012 continue;
4013 }
4014
4015 // currentCodeIndex is within top and skip or equal to specified code
4016 // index
4017
4018 // Get the Created time from the timestamp
4019 std::string entryTimeStr;
Konstantin Aladyshev2a025612023-02-15 11:52:58 +03004020 entryTimeStr = redfish::time_utils::getDateTimeUintUs(usecSinceEpoch);
ZhikuiRena3316fc2020-01-29 14:58:08 -08004021
4022 // assemble messageArgs: BootIndex, TimeOffset(100us), PostCode(hex)
4023 std::ostringstream hexCode;
4024 hexCode << "0x" << std::setfill('0') << std::setw(2) << std::hex
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05304025 << std::get<0>(code.second);
ZhikuiRena3316fc2020-01-29 14:58:08 -08004026 std::ostringstream timeOffsetStr;
4027 // Set Fixed -Point Notation
4028 timeOffsetStr << std::fixed;
4029 // Set precision to 4 digits
4030 timeOffsetStr << std::setprecision(4);
4031 // Add double to stream
4032 timeOffsetStr << static_cast<double>(usTimeOffset) / 1000 / 1000;
ZhikuiRena3316fc2020-01-29 14:58:08 -08004033
Ed Tanous1e6deaf2022-02-17 11:32:37 -08004034 std::string bootIndexStr = std::to_string(bootIndex);
4035 std::string timeOffsetString = timeOffsetStr.str();
4036 std::string hexCodeStr = hexCode.str();
4037
4038 std::array<std::string_view, 3> messageArgs = {
4039 bootIndexStr, timeOffsetString, hexCodeStr};
4040
4041 std::string msg =
4042 redfish::registries::fillMessageArgs(messageArgs, message->message);
4043 if (msg.empty())
ZhikuiRena3316fc2020-01-29 14:58:08 -08004044 {
Ed Tanous1e6deaf2022-02-17 11:32:37 -08004045 messages::internalError(asyncResp->res);
4046 return false;
ZhikuiRena3316fc2020-01-29 14:58:08 -08004047 }
4048
Tim Leed4342a92020-04-27 11:47:58 +08004049 // Get Severity template from message registry
4050 std::string severity;
4051 if (message != nullptr)
4052 {
Ed Tanous5f2b84e2022-02-08 00:41:53 -08004053 severity = message->messageSeverity;
Tim Leed4342a92020-04-27 11:47:58 +08004054 }
4055
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08004056 // Format entry
4057 nlohmann::json::object_t bmcLogEntry;
Vijay Lobo9c11a172021-10-07 16:53:16 -05004058 bmcLogEntry["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
Ed Tanousef4c65b2023-04-24 15:28:50 -07004059 bmcLogEntry["@odata.id"] = boost::urls::format(
4060 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/{}",
4061 postcodeEntryID);
Jason M. Bills84afc482022-06-24 12:38:23 -07004062 bmcLogEntry["Name"] = "POST Code Log Entry";
4063 bmcLogEntry["Id"] = postcodeEntryID;
4064 bmcLogEntry["Message"] = std::move(msg);
4065 bmcLogEntry["MessageId"] = "OpenBMC.0.2.BIOSPOSTCode";
Ed Tanous1e6deaf2022-02-17 11:32:37 -08004066 bmcLogEntry["MessageArgs"] = messageArgs;
Jason M. Bills84afc482022-06-24 12:38:23 -07004067 bmcLogEntry["EntryType"] = "Event";
4068 bmcLogEntry["Severity"] = std::move(severity);
4069 bmcLogEntry["Created"] = entryTimeStr;
George Liu647b3cd2021-07-05 12:43:56 +08004070 if (!std::get<std::vector<uint8_t>>(code.second).empty())
4071 {
4072 bmcLogEntry["AdditionalDataURI"] =
4073 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/" +
4074 postcodeEntryID + "/attachment";
4075 }
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08004076
4077 // codeIndex is only specified when querying single entry, return only
4078 // that entry in this case
4079 if (codeIndex != 0)
4080 {
Ed Tanousac106bf2023-06-07 09:24:59 -07004081 asyncResp->res.jsonValue.update(bmcLogEntry);
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08004082 return true;
4083 }
4084
Ed Tanousac106bf2023-06-07 09:24:59 -07004085 nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
Patrick Williamsb2ba3072023-05-12 10:27:39 -05004086 logEntryArray.emplace_back(std::move(bmcLogEntry));
ZhikuiRena3316fc2020-01-29 14:58:08 -08004087 }
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08004088
4089 // Return value is always false when querying multiple entries
4090 return false;
ZhikuiRena3316fc2020-01-29 14:58:08 -08004091}
4092
Ed Tanousac106bf2023-06-07 09:24:59 -07004093static void
4094 getPostCodeForEntry(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
4095 const std::string& entryId)
ZhikuiRena3316fc2020-01-29 14:58:08 -08004096{
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08004097 uint16_t bootIndex = 0;
4098 uint64_t codeIndex = 0;
4099 if (!parsePostCode(entryId, codeIndex, bootIndex))
4100 {
4101 // Requested ID was not found
Ed Tanousac106bf2023-06-07 09:24:59 -07004102 messages::resourceNotFound(asyncResp->res, "LogEntry", entryId);
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08004103 return;
4104 }
4105
4106 if (bootIndex == 0 || codeIndex == 0)
4107 {
4108 // 0 is an invalid index
Ed Tanousac106bf2023-06-07 09:24:59 -07004109 messages::resourceNotFound(asyncResp->res, "LogEntry", entryId);
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08004110 return;
4111 }
4112
ZhikuiRena3316fc2020-01-29 14:58:08 -08004113 crow::connections::systemBus->async_method_call(
Ed Tanousac106bf2023-06-07 09:24:59 -07004114 [asyncResp, entryId, bootIndex,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08004115 codeIndex](const boost::system::error_code& ec,
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05304116 const boost::container::flat_map<
4117 uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
4118 postcode) {
Ed Tanous002d39b2022-05-31 08:59:27 -07004119 if (ec)
4120 {
Ed Tanous62598e32023-07-17 17:06:25 -07004121 BMCWEB_LOG_DEBUG("DBUS POST CODE PostCode response error");
Ed Tanousac106bf2023-06-07 09:24:59 -07004122 messages::internalError(asyncResp->res);
Ed Tanous002d39b2022-05-31 08:59:27 -07004123 return;
4124 }
ZhikuiRena3316fc2020-01-29 14:58:08 -08004125
Ed Tanous002d39b2022-05-31 08:59:27 -07004126 if (postcode.empty())
4127 {
Ed Tanousac106bf2023-06-07 09:24:59 -07004128 messages::resourceNotFound(asyncResp->res, "LogEntry", entryId);
Ed Tanous002d39b2022-05-31 08:59:27 -07004129 return;
4130 }
ZhikuiRena3316fc2020-01-29 14:58:08 -08004131
Ed Tanousac106bf2023-06-07 09:24:59 -07004132 if (!fillPostCodeEntry(asyncResp, postcode, bootIndex, codeIndex))
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08004133 {
Ed Tanousac106bf2023-06-07 09:24:59 -07004134 messages::resourceNotFound(asyncResp->res, "LogEntry", entryId);
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08004135 return;
4136 }
Patrick Williams5a39f772023-10-20 11:20:21 -05004137 },
Jonathan Doman15124762021-01-07 17:54:17 -08004138 "xyz.openbmc_project.State.Boot.PostCode0",
4139 "/xyz/openbmc_project/State/Boot/PostCode0",
ZhikuiRena3316fc2020-01-29 14:58:08 -08004140 "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodesWithTimeStamp",
4141 bootIndex);
4142}
4143
Ed Tanousac106bf2023-06-07 09:24:59 -07004144static void
4145 getPostCodeForBoot(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
4146 const uint16_t bootIndex, const uint16_t bootCount,
4147 const uint64_t entryCount, size_t skip, size_t top)
ZhikuiRena3316fc2020-01-29 14:58:08 -08004148{
4149 crow::connections::systemBus->async_method_call(
Ed Tanousac106bf2023-06-07 09:24:59 -07004150 [asyncResp, bootIndex, bootCount, entryCount, skip,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08004151 top](const boost::system::error_code& ec,
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05304152 const boost::container::flat_map<
4153 uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
4154 postcode) {
Ed Tanous002d39b2022-05-31 08:59:27 -07004155 if (ec)
4156 {
Ed Tanous62598e32023-07-17 17:06:25 -07004157 BMCWEB_LOG_DEBUG("DBUS POST CODE PostCode response error");
Ed Tanousac106bf2023-06-07 09:24:59 -07004158 messages::internalError(asyncResp->res);
Ed Tanous002d39b2022-05-31 08:59:27 -07004159 return;
4160 }
ZhikuiRena3316fc2020-01-29 14:58:08 -08004161
Ed Tanous002d39b2022-05-31 08:59:27 -07004162 uint64_t endCount = entryCount;
4163 if (!postcode.empty())
4164 {
4165 endCount = entryCount + postcode.size();
Ed Tanous3648c8b2022-07-25 13:39:59 -07004166 if (skip < endCount && (top + skip) > entryCount)
ZhikuiRena3316fc2020-01-29 14:58:08 -08004167 {
Patrick Williams89492a12023-05-10 07:51:34 -05004168 uint64_t thisBootSkip = std::max(static_cast<uint64_t>(skip),
4169 entryCount) -
4170 entryCount;
Ed Tanous002d39b2022-05-31 08:59:27 -07004171 uint64_t thisBootTop =
Ed Tanous3648c8b2022-07-25 13:39:59 -07004172 std::min(static_cast<uint64_t>(top + skip), endCount) -
4173 entryCount;
Ed Tanous002d39b2022-05-31 08:59:27 -07004174
Ed Tanousac106bf2023-06-07 09:24:59 -07004175 fillPostCodeEntry(asyncResp, postcode, bootIndex, 0,
4176 thisBootSkip, thisBootTop);
ZhikuiRena3316fc2020-01-29 14:58:08 -08004177 }
Ed Tanousac106bf2023-06-07 09:24:59 -07004178 asyncResp->res.jsonValue["Members@odata.count"] = endCount;
Ed Tanous002d39b2022-05-31 08:59:27 -07004179 }
4180
4181 // continue to previous bootIndex
4182 if (bootIndex < bootCount)
4183 {
Ed Tanousac106bf2023-06-07 09:24:59 -07004184 getPostCodeForBoot(asyncResp, static_cast<uint16_t>(bootIndex + 1),
Ed Tanous002d39b2022-05-31 08:59:27 -07004185 bootCount, endCount, skip, top);
4186 }
Jiaqing Zhao81584ab2022-07-28 00:33:45 +08004187 else if (skip + top < endCount)
Ed Tanous002d39b2022-05-31 08:59:27 -07004188 {
Ed Tanousac106bf2023-06-07 09:24:59 -07004189 asyncResp->res.jsonValue["Members@odata.nextLink"] =
Ed Tanous002d39b2022-05-31 08:59:27 -07004190 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries?$skip=" +
4191 std::to_string(skip + top);
4192 }
Patrick Williams5a39f772023-10-20 11:20:21 -05004193 },
Jonathan Doman15124762021-01-07 17:54:17 -08004194 "xyz.openbmc_project.State.Boot.PostCode0",
4195 "/xyz/openbmc_project/State/Boot/PostCode0",
ZhikuiRena3316fc2020-01-29 14:58:08 -08004196 "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodesWithTimeStamp",
4197 bootIndex);
4198}
4199
zhanghch058d1b46d2021-04-01 11:18:24 +08004200static void
Ed Tanousac106bf2023-06-07 09:24:59 -07004201 getCurrentBootNumber(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous3648c8b2022-07-25 13:39:59 -07004202 size_t skip, size_t top)
ZhikuiRena3316fc2020-01-29 14:58:08 -08004203{
4204 uint64_t entryCount = 0;
Jonathan Doman1e1e5982021-06-11 09:36:17 -07004205 sdbusplus::asio::getProperty<uint16_t>(
4206 *crow::connections::systemBus,
4207 "xyz.openbmc_project.State.Boot.PostCode0",
4208 "/xyz/openbmc_project/State/Boot/PostCode0",
4209 "xyz.openbmc_project.State.Boot.PostCode", "CurrentBootCycleCount",
Ed Tanousac106bf2023-06-07 09:24:59 -07004210 [asyncResp, entryCount, skip, top](const boost::system::error_code& ec,
4211 const uint16_t bootCount) {
Ed Tanous002d39b2022-05-31 08:59:27 -07004212 if (ec)
4213 {
Ed Tanous62598e32023-07-17 17:06:25 -07004214 BMCWEB_LOG_DEBUG("DBUS response error {}", ec);
Ed Tanousac106bf2023-06-07 09:24:59 -07004215 messages::internalError(asyncResp->res);
Ed Tanous002d39b2022-05-31 08:59:27 -07004216 return;
4217 }
Ed Tanousac106bf2023-06-07 09:24:59 -07004218 getPostCodeForBoot(asyncResp, 1, bootCount, entryCount, skip, top);
Patrick Williams5a39f772023-10-20 11:20:21 -05004219 });
ZhikuiRena3316fc2020-01-29 14:58:08 -08004220}
4221
John Edward Broadbent7e860f12021-04-08 15:57:16 -07004222inline void requestRoutesPostCodesEntryCollection(App& app)
ZhikuiRena3316fc2020-01-29 14:58:08 -08004223{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07004224 BMCWEB_ROUTE(app,
Ed Tanous22d268c2022-05-19 09:39:07 -07004225 "/redfish/v1/Systems/<str>/LogServices/PostCodes/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07004226 .privileges(redfish::privileges::getLogEntryCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07004227 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07004228 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07004229 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
4230 const std::string& systemName) {
Ed Tanous002d39b2022-05-31 08:59:27 -07004231 query_param::QueryCapabilities capabilities = {
4232 .canDelegateTop = true,
4233 .canDelegateSkip = true,
4234 };
4235 query_param::Query delegatedQuery;
4236 if (!redfish::setUpRedfishRouteWithDelegation(
Carson Labrado3ba00072022-06-06 19:40:56 +00004237 app, req, asyncResp, delegatedQuery, capabilities))
Ed Tanous002d39b2022-05-31 08:59:27 -07004238 {
4239 return;
4240 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08004241 if constexpr (bmcwebEnableMultiHost)
4242 {
4243 // Option currently returns no systems. TBD
4244 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
4245 systemName);
4246 return;
4247 }
Ed Tanous22d268c2022-05-19 09:39:07 -07004248
4249 if (systemName != "system")
4250 {
4251 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
4252 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07004253 return;
4254 }
Ed Tanous002d39b2022-05-31 08:59:27 -07004255 asyncResp->res.jsonValue["@odata.type"] =
4256 "#LogEntryCollection.LogEntryCollection";
4257 asyncResp->res.jsonValue["@odata.id"] =
4258 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries";
4259 asyncResp->res.jsonValue["Name"] = "BIOS POST Code Log Entries";
4260 asyncResp->res.jsonValue["Description"] =
4261 "Collection of POST Code Log Entries";
4262 asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
4263 asyncResp->res.jsonValue["Members@odata.count"] = 0;
Ed Tanous3648c8b2022-07-25 13:39:59 -07004264 size_t skip = delegatedQuery.skip.value_or(0);
Jiaqing Zhao5143f7a2022-07-22 09:33:33 +08004265 size_t top = delegatedQuery.top.value_or(query_param::Query::maxTop);
Ed Tanous3648c8b2022-07-25 13:39:59 -07004266 getCurrentBootNumber(asyncResp, skip, top);
Patrick Williams5a39f772023-10-20 11:20:21 -05004267 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07004268}
ZhikuiRena3316fc2020-01-29 14:58:08 -08004269
George Liu647b3cd2021-07-05 12:43:56 +08004270inline void requestRoutesPostCodesEntryAdditionalData(App& app)
4271{
George Liu0fda0f12021-11-16 10:06:17 +08004272 BMCWEB_ROUTE(
4273 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07004274 "/redfish/v1/Systems/<str>/LogServices/PostCodes/Entries/<str>/attachment/")
George Liu647b3cd2021-07-05 12:43:56 +08004275 .privileges(redfish::privileges::getLogEntry)
4276 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07004277 [&app](const crow::Request& req,
4278 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07004279 const std::string& systemName,
Ed Tanous45ca1b82022-03-25 13:07:27 -07004280 const std::string& postCodeID) {
Carson Labrado3ba00072022-06-06 19:40:56 +00004281 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07004282 {
4283 return;
4284 }
Matt Spinler72e21372023-04-19 12:53:33 -05004285 if (!http_helpers::isContentTypeAllowed(
Ed Tanous99351cd2022-08-07 16:42:51 -07004286 req.getHeaderValue("Accept"),
Ed Tanous4a0e1a02022-09-21 15:28:04 -07004287 http_helpers::ContentType::OctetStream, true))
Ed Tanous002d39b2022-05-31 08:59:27 -07004288 {
4289 asyncResp->res.result(boost::beast::http::status::bad_request);
4290 return;
4291 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08004292 if constexpr (bmcwebEnableMultiHost)
4293 {
4294 // Option currently returns no systems. TBD
4295 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
4296 systemName);
4297 return;
4298 }
Ed Tanous22d268c2022-05-19 09:39:07 -07004299 if (systemName != "system")
4300 {
4301 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
4302 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07004303 return;
4304 }
George Liu647b3cd2021-07-05 12:43:56 +08004305
Ed Tanous002d39b2022-05-31 08:59:27 -07004306 uint64_t currentValue = 0;
4307 uint16_t index = 0;
4308 if (!parsePostCode(postCodeID, currentValue, index))
4309 {
4310 messages::resourceNotFound(asyncResp->res, "LogEntry", postCodeID);
4311 return;
4312 }
George Liu647b3cd2021-07-05 12:43:56 +08004313
Ed Tanous002d39b2022-05-31 08:59:27 -07004314 crow::connections::systemBus->async_method_call(
4315 [asyncResp, postCodeID, currentValue](
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08004316 const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07004317 const std::vector<std::tuple<uint64_t, std::vector<uint8_t>>>&
4318 postcodes) {
4319 if (ec.value() == EBADR)
4320 {
4321 messages::resourceNotFound(asyncResp->res, "LogEntry",
4322 postCodeID);
4323 return;
4324 }
4325 if (ec)
4326 {
Ed Tanous62598e32023-07-17 17:06:25 -07004327 BMCWEB_LOG_DEBUG("DBUS response error {}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -07004328 messages::internalError(asyncResp->res);
4329 return;
4330 }
George Liu647b3cd2021-07-05 12:43:56 +08004331
Ed Tanous002d39b2022-05-31 08:59:27 -07004332 size_t value = static_cast<size_t>(currentValue) - 1;
4333 if (value == std::string::npos || postcodes.size() < currentValue)
4334 {
Ed Tanous62598e32023-07-17 17:06:25 -07004335 BMCWEB_LOG_WARNING("Wrong currentValue value");
Ed Tanous002d39b2022-05-31 08:59:27 -07004336 messages::resourceNotFound(asyncResp->res, "LogEntry",
4337 postCodeID);
4338 return;
4339 }
George Liu647b3cd2021-07-05 12:43:56 +08004340
Ed Tanous002d39b2022-05-31 08:59:27 -07004341 const auto& [tID, c] = postcodes[value];
4342 if (c.empty())
4343 {
Ed Tanous62598e32023-07-17 17:06:25 -07004344 BMCWEB_LOG_WARNING("No found post code data");
Ed Tanous002d39b2022-05-31 08:59:27 -07004345 messages::resourceNotFound(asyncResp->res, "LogEntry",
4346 postCodeID);
4347 return;
4348 }
4349 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
4350 const char* d = reinterpret_cast<const char*>(c.data());
4351 std::string_view strData(d, c.size());
George Liu647b3cd2021-07-05 12:43:56 +08004352
Ed Tanousd9f6c622022-03-17 09:12:17 -07004353 asyncResp->res.addHeader(boost::beast::http::field::content_type,
Ed Tanous002d39b2022-05-31 08:59:27 -07004354 "application/octet-stream");
Ed Tanousd9f6c622022-03-17 09:12:17 -07004355 asyncResp->res.addHeader(
4356 boost::beast::http::field::content_transfer_encoding, "Base64");
Ed Tanous27b0cf92023-08-07 12:02:40 -07004357 asyncResp->res.write(crow::utility::base64encode(strData));
Patrick Williams5a39f772023-10-20 11:20:21 -05004358 },
Ed Tanous002d39b2022-05-31 08:59:27 -07004359 "xyz.openbmc_project.State.Boot.PostCode0",
4360 "/xyz/openbmc_project/State/Boot/PostCode0",
4361 "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodes", index);
Patrick Williams5a39f772023-10-20 11:20:21 -05004362 });
George Liu647b3cd2021-07-05 12:43:56 +08004363}
4364
John Edward Broadbent7e860f12021-04-08 15:57:16 -07004365inline void requestRoutesPostCodesEntry(App& app)
ZhikuiRena3316fc2020-01-29 14:58:08 -08004366{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07004367 BMCWEB_ROUTE(
Ed Tanous22d268c2022-05-19 09:39:07 -07004368 app, "/redfish/v1/Systems/<str>/LogServices/PostCodes/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07004369 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07004370 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07004371 [&app](const crow::Request& req,
4372 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07004373 const std::string& systemName, const std::string& targetID) {
Carson Labrado3ba00072022-06-06 19:40:56 +00004374 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07004375 {
4376 return;
4377 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08004378 if constexpr (bmcwebEnableMultiHost)
4379 {
4380 // Option currently returns no systems. TBD
4381 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
4382 systemName);
4383 return;
4384 }
Ed Tanous22d268c2022-05-19 09:39:07 -07004385 if (systemName != "system")
4386 {
4387 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
4388 systemName);
4389 return;
4390 }
4391
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08004392 getPostCodeForEntry(asyncResp, targetID);
Patrick Williams5a39f772023-10-20 11:20:21 -05004393 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07004394}
ZhikuiRena3316fc2020-01-29 14:58:08 -08004395
Ed Tanous1da66f72018-07-27 16:13:37 -07004396} // namespace redfish