blob: 54284cfef271e8dd950d9228fced3a004b6d2bf7 [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
Jason M. Billse1f26342018-07-18 12:12:00 -070034#include <systemd/sd-journal.h>
Asmitha Karunanithi8e317782020-12-10 03:35:05 -060035#include <tinyxml2.h>
Adriana Kobylak400fd1f2021-01-29 09:01:30 -060036#include <unistd.h>
Jason M. Billse1f26342018-07-18 12:12:00 -070037
Ed Tanous9896eae2022-07-23 15:07:33 -070038#include <boost/algorithm/string/case_conv.hpp>
Ed Tanous11ba3972022-07-11 09:50:41 -070039#include <boost/algorithm/string/classification.hpp>
Adriana Kobylak400fd1f2021-01-29 09:01:30 -060040#include <boost/algorithm/string/replace.hpp>
Jason M. Bills4851d452019-03-28 11:27:48 -070041#include <boost/algorithm/string/split.hpp>
Ed Tanous07c8c202022-07-11 10:08:08 -070042#include <boost/beast/http/verb.hpp>
Ed Tanous1da66f72018-07-27 16:13:37 -070043#include <boost/container/flat_map.hpp>
Jason M. Bills1ddcf012019-11-26 14:59:21 -080044#include <boost/system/linux_error.hpp>
Ed Tanousef4c65b2023-04-24 15:28:50 -070045#include <boost/url/format.hpp>
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +020046#include <sdbusplus/asio/property.hpp>
47#include <sdbusplus/unpack_properties.hpp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050048
George Liu7a1dbc42022-12-07 16:03:22 +080049#include <array>
George Liu647b3cd2021-07-05 12:43:56 +080050#include <charconv>
James Feist4418c7f2019-04-15 11:09:15 -070051#include <filesystem>
Xiaochao Ma75710de2021-01-21 17:56:02 +080052#include <optional>
Ed Tanous3544d2a2023-08-06 18:12:20 -070053#include <ranges>
Ed Tanous26702d02021-11-03 15:02:33 -070054#include <span>
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
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700117inline static int getJournalMetadata(sd_journal* journal,
Ed Tanous26ccae32023-02-16 10:28:44 -0800118 std::string_view field,
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700119 std::string_view& contents)
Jason M. Bills16428a12018-11-02 12:42:29 -0700120{
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500121 const char* data = nullptr;
Jason M. Bills16428a12018-11-02 12:42:29 -0700122 size_t length = 0;
123 int ret = 0;
124 // Get the metadata from the requested field of the journal entry
Ed Tanous46ff87b2022-01-07 09:25:51 -0800125 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
126 const void** dataVoid = reinterpret_cast<const void**>(&data);
127
128 ret = sd_journal_get_data(journal, field.data(), dataVoid, &length);
Jason M. Bills16428a12018-11-02 12:42:29 -0700129 if (ret < 0)
130 {
131 return ret;
132 }
Ed Tanous39e77502019-03-04 17:35:53 -0800133 contents = std::string_view(data, length);
Jason M. Bills16428a12018-11-02 12:42:29 -0700134 // Only use the content after the "=" character.
Ed Tanous81ce6092020-12-17 16:54:55 +0000135 contents.remove_prefix(std::min(contents.find('=') + 1, contents.size()));
Jason M. Bills16428a12018-11-02 12:42:29 -0700136 return ret;
137}
138
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700139inline static int getJournalMetadata(sd_journal* journal,
Ed Tanous26ccae32023-02-16 10:28:44 -0800140 std::string_view field, const int& base,
141 long int& contents)
Jason M. Bills16428a12018-11-02 12:42:29 -0700142{
143 int ret = 0;
Ed Tanous39e77502019-03-04 17:35:53 -0800144 std::string_view metadata;
Jason M. Bills16428a12018-11-02 12:42:29 -0700145 // Get the metadata from the requested field of the journal entry
146 ret = getJournalMetadata(journal, field, metadata);
147 if (ret < 0)
148 {
149 return ret;
150 }
Ed Tanousb01bf292019-03-25 19:25:26 +0000151 contents = strtol(metadata.data(), nullptr, base);
Jason M. Bills16428a12018-11-02 12:42:29 -0700152 return ret;
153}
154
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700155inline static bool getEntryTimestamp(sd_journal* journal,
156 std::string& entryTimestamp)
ZhikuiRena3316fc2020-01-29 14:58:08 -0800157{
158 int ret = 0;
159 uint64_t timestamp = 0;
160 ret = sd_journal_get_realtime_usec(journal, &timestamp);
161 if (ret < 0)
162 {
Ed Tanous62598e32023-07-17 17:06:25 -0700163 BMCWEB_LOG_ERROR("Failed to read entry timestamp: {}", strerror(-ret));
ZhikuiRena3316fc2020-01-29 14:58:08 -0800164 return false;
165 }
Konstantin Aladysheve645c5e2023-02-17 13:09:53 +0300166 entryTimestamp = redfish::time_utils::getDateTimeUintUs(timestamp);
Asmitha Karunanithi9c620e22020-08-02 11:55:21 -0500167 return true;
ZhikuiRena3316fc2020-01-29 14:58:08 -0800168}
Ed Tanous50b8a432022-02-03 16:29:50 -0800169
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700170inline static bool getUniqueEntryID(sd_journal* journal, std::string& entryID,
171 const bool firstEntry = true)
Jason M. Bills16428a12018-11-02 12:42:29 -0700172{
173 int ret = 0;
174 static uint64_t prevTs = 0;
175 static int index = 0;
Jason M. Billse85d6b12019-07-29 17:01:15 -0700176 if (firstEntry)
177 {
178 prevTs = 0;
179 }
180
Jason M. Bills16428a12018-11-02 12:42:29 -0700181 // Get the entry timestamp
182 uint64_t curTs = 0;
183 ret = sd_journal_get_realtime_usec(journal, &curTs);
184 if (ret < 0)
185 {
Ed Tanous62598e32023-07-17 17:06:25 -0700186 BMCWEB_LOG_ERROR("Failed to read entry timestamp: {}", strerror(-ret));
Jason M. Bills16428a12018-11-02 12:42:29 -0700187 return false;
188 }
189 // If the timestamp isn't unique, increment the index
190 if (curTs == prevTs)
191 {
192 index++;
193 }
194 else
195 {
196 // Otherwise, reset it
197 index = 0;
198 }
199 // Save the timestamp
200 prevTs = curTs;
201
202 entryID = std::to_string(curTs);
203 if (index > 0)
204 {
205 entryID += "_" + std::to_string(index);
206 }
207 return true;
208}
209
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500210static bool getUniqueEntryID(const std::string& logEntry, std::string& entryID,
Jason M. Billse85d6b12019-07-29 17:01:15 -0700211 const bool firstEntry = true)
Jason M. Bills95820182019-04-22 16:25:34 -0700212{
Ed Tanous271584a2019-07-09 16:24:22 -0700213 static time_t prevTs = 0;
Jason M. Bills95820182019-04-22 16:25:34 -0700214 static int index = 0;
Jason M. Billse85d6b12019-07-29 17:01:15 -0700215 if (firstEntry)
216 {
217 prevTs = 0;
218 }
219
Jason M. Bills95820182019-04-22 16:25:34 -0700220 // Get the entry timestamp
Ed Tanous271584a2019-07-09 16:24:22 -0700221 std::time_t curTs = 0;
Jason M. Bills95820182019-04-22 16:25:34 -0700222 std::tm timeStruct = {};
223 std::istringstream entryStream(logEntry);
224 if (entryStream >> std::get_time(&timeStruct, "%Y-%m-%dT%H:%M:%S"))
225 {
226 curTs = std::mktime(&timeStruct);
227 }
228 // If the timestamp isn't unique, increment the index
229 if (curTs == prevTs)
230 {
231 index++;
232 }
233 else
234 {
235 // Otherwise, reset it
236 index = 0;
237 }
238 // Save the timestamp
239 prevTs = curTs;
240
241 entryID = std::to_string(curTs);
242 if (index > 0)
243 {
244 entryID += "_" + std::to_string(index);
245 }
246 return true;
247}
248
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700249inline static bool
zhanghch058d1b46d2021-04-01 11:18:24 +0800250 getTimestampFromID(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
251 const std::string& entryID, uint64_t& timestamp,
252 uint64_t& index)
Jason M. Bills16428a12018-11-02 12:42:29 -0700253{
254 if (entryID.empty())
255 {
256 return false;
257 }
258 // Convert the unique ID back to a timestamp to find the entry
Ed Tanous39e77502019-03-04 17:35:53 -0800259 std::string_view tsStr(entryID);
Jason M. Bills16428a12018-11-02 12:42:29 -0700260
Ed Tanous81ce6092020-12-17 16:54:55 +0000261 auto underscorePos = tsStr.find('_');
Ed Tanous71d5d8d2022-01-25 11:04:33 -0800262 if (underscorePos != std::string_view::npos)
Jason M. Bills16428a12018-11-02 12:42:29 -0700263 {
264 // Timestamp has an index
265 tsStr.remove_suffix(tsStr.size() - underscorePos);
Ed Tanous39e77502019-03-04 17:35:53 -0800266 std::string_view indexStr(entryID);
Jason M. Bills16428a12018-11-02 12:42:29 -0700267 indexStr.remove_prefix(underscorePos + 1);
Patrick Williams84396af2023-05-11 11:47:45 -0500268 auto [ptr, ec] = std::from_chars(indexStr.begin(), indexStr.end(),
269 index);
Ed Tanousc0bd5e42021-09-13 17:00:19 -0700270 if (ec != std::errc())
Jason M. Bills16428a12018-11-02 12:42:29 -0700271 {
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +0800272 messages::resourceNotFound(asyncResp->res, "LogEntry", entryID);
Jason M. Bills16428a12018-11-02 12:42:29 -0700273 return false;
274 }
275 }
276 // Timestamp has no index
Patrick Williams84396af2023-05-11 11:47:45 -0500277 auto [ptr, ec] = std::from_chars(tsStr.begin(), tsStr.end(), timestamp);
Ed Tanousc0bd5e42021-09-13 17:00:19 -0700278 if (ec != std::errc())
Jason M. Bills16428a12018-11-02 12:42:29 -0700279 {
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +0800280 messages::resourceNotFound(asyncResp->res, "LogEntry", entryID);
Jason M. Bills16428a12018-11-02 12:42:29 -0700281 return false;
282 }
283 return true;
284}
285
Jason M. Bills95820182019-04-22 16:25:34 -0700286static bool
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500287 getRedfishLogFiles(std::vector<std::filesystem::path>& redfishLogFiles)
Jason M. Bills95820182019-04-22 16:25:34 -0700288{
289 static const std::filesystem::path redfishLogDir = "/var/log";
290 static const std::string redfishLogFilename = "redfish";
291
292 // Loop through the directory looking for redfish log files
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500293 for (const std::filesystem::directory_entry& dirEnt :
Jason M. Bills95820182019-04-22 16:25:34 -0700294 std::filesystem::directory_iterator(redfishLogDir))
295 {
296 // If we find a redfish log file, save the path
297 std::string filename = dirEnt.path().filename();
Ed Tanous11ba3972022-07-11 09:50:41 -0700298 if (filename.starts_with(redfishLogFilename))
Jason M. Bills95820182019-04-22 16:25:34 -0700299 {
300 redfishLogFiles.emplace_back(redfishLogDir / filename);
301 }
302 }
303 // As the log files rotate, they are appended with a ".#" that is higher for
304 // the older logs. Since we don't expect more than 10 log files, we
305 // can just sort the list to get them in order from newest to oldest
Ed Tanous3544d2a2023-08-06 18:12:20 -0700306 std::ranges::sort(redfishLogFiles);
Jason M. Bills95820182019-04-22 16:25:34 -0700307
308 return !redfishLogFiles.empty();
309}
310
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -0600311inline log_entry::OriginatorTypes
312 mapDbusOriginatorTypeToRedfish(const std::string& originatorType)
313{
314 if (originatorType ==
315 "xyz.openbmc_project.Common.OriginatedBy.OriginatorTypes.Client")
316 {
317 return log_entry::OriginatorTypes::Client;
318 }
319 if (originatorType ==
320 "xyz.openbmc_project.Common.OriginatedBy.OriginatorTypes.Internal")
321 {
322 return log_entry::OriginatorTypes::Internal;
323 }
324 if (originatorType ==
325 "xyz.openbmc_project.Common.OriginatedBy.OriginatorTypes.SupportingService")
326 {
327 return log_entry::OriginatorTypes::SupportingService;
328 }
329 return log_entry::OriginatorTypes::Invalid;
330}
331
Claire Weinanaefe3782022-07-15 19:17:19 -0700332inline void parseDumpEntryFromDbusObject(
Jiaqing Zhao2d613eb2022-08-15 16:03:00 +0800333 const dbus::utility::ManagedObjectType::value_type& object,
Claire Weinanc6fecda2022-07-15 10:43:25 -0700334 std::string& dumpStatus, uint64_t& size, uint64_t& timestampUs,
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -0600335 std::string& originatorId, log_entry::OriginatorTypes& originatorType,
Claire Weinanaefe3782022-07-15 19:17:19 -0700336 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
337{
338 for (const auto& interfaceMap : object.second)
339 {
340 if (interfaceMap.first == "xyz.openbmc_project.Common.Progress")
341 {
342 for (const auto& propertyMap : interfaceMap.second)
343 {
344 if (propertyMap.first == "Status")
345 {
346 const auto* status =
347 std::get_if<std::string>(&propertyMap.second);
348 if (status == nullptr)
349 {
350 messages::internalError(asyncResp->res);
351 break;
352 }
353 dumpStatus = *status;
354 }
355 }
356 }
357 else if (interfaceMap.first == "xyz.openbmc_project.Dump.Entry")
358 {
359 for (const auto& propertyMap : interfaceMap.second)
360 {
361 if (propertyMap.first == "Size")
362 {
363 const auto* sizePtr =
364 std::get_if<uint64_t>(&propertyMap.second);
365 if (sizePtr == nullptr)
366 {
367 messages::internalError(asyncResp->res);
368 break;
369 }
370 size = *sizePtr;
371 break;
372 }
373 }
374 }
375 else if (interfaceMap.first == "xyz.openbmc_project.Time.EpochTime")
376 {
377 for (const auto& propertyMap : interfaceMap.second)
378 {
379 if (propertyMap.first == "Elapsed")
380 {
381 const uint64_t* usecsTimeStamp =
382 std::get_if<uint64_t>(&propertyMap.second);
383 if (usecsTimeStamp == nullptr)
384 {
385 messages::internalError(asyncResp->res);
386 break;
387 }
Claire Weinanc6fecda2022-07-15 10:43:25 -0700388 timestampUs = *usecsTimeStamp;
Claire Weinanaefe3782022-07-15 19:17:19 -0700389 break;
390 }
391 }
392 }
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -0600393 else if (interfaceMap.first ==
394 "xyz.openbmc_project.Common.OriginatedBy")
395 {
396 for (const auto& propertyMap : interfaceMap.second)
397 {
398 if (propertyMap.first == "OriginatorId")
399 {
400 const std::string* id =
401 std::get_if<std::string>(&propertyMap.second);
402 if (id == nullptr)
403 {
404 messages::internalError(asyncResp->res);
405 break;
406 }
407 originatorId = *id;
408 }
409
410 if (propertyMap.first == "OriginatorType")
411 {
412 const std::string* type =
413 std::get_if<std::string>(&propertyMap.second);
414 if (type == nullptr)
415 {
416 messages::internalError(asyncResp->res);
417 break;
418 }
419
420 originatorType = mapDbusOriginatorTypeToRedfish(*type);
421 if (originatorType == log_entry::OriginatorTypes::Invalid)
422 {
423 messages::internalError(asyncResp->res);
424 break;
425 }
426 }
427 }
428 }
Claire Weinanaefe3782022-07-15 19:17:19 -0700429 }
430}
431
Nan Zhou21ab4042022-06-26 23:07:40 +0000432static std::string getDumpEntriesPath(const std::string& dumpType)
Claire Weinanfdd26902022-03-01 14:18:25 -0800433{
434 std::string entriesPath;
435
436 if (dumpType == "BMC")
437 {
438 entriesPath = "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/";
439 }
440 else if (dumpType == "FaultLog")
441 {
442 entriesPath = "/redfish/v1/Managers/bmc/LogServices/FaultLog/Entries/";
443 }
444 else if (dumpType == "System")
445 {
446 entriesPath = "/redfish/v1/Systems/system/LogServices/Dump/Entries/";
447 }
448 else
449 {
Ed Tanous62598e32023-07-17 17:06:25 -0700450 BMCWEB_LOG_ERROR("getDumpEntriesPath() invalid dump type: {}",
451 dumpType);
Claire Weinanfdd26902022-03-01 14:18:25 -0800452 }
453
454 // Returns empty string on error
455 return entriesPath;
456}
457
zhanghch058d1b46d2021-04-01 11:18:24 +0800458inline void
459 getDumpEntryCollection(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
460 const std::string& dumpType)
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500461{
Claire Weinanfdd26902022-03-01 14:18:25 -0800462 std::string entriesPath = getDumpEntriesPath(dumpType);
463 if (entriesPath.empty())
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500464 {
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500465 messages::internalError(asyncResp->res);
466 return;
467 }
468
George Liu5eb468d2023-06-20 17:03:24 +0800469 sdbusplus::message::object_path path("/xyz/openbmc_project/dump");
470 dbus::utility::getManagedObjects(
471 "xyz.openbmc_project.Dump.Manager", path,
Claire Weinanfdd26902022-03-01 14:18:25 -0800472 [asyncResp, entriesPath,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800473 dumpType](const boost::system::error_code& ec,
George Liu5eb468d2023-06-20 17:03:24 +0800474 const dbus::utility::ManagedObjectType& objects) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700475 if (ec)
476 {
Ed Tanous62598e32023-07-17 17:06:25 -0700477 BMCWEB_LOG_ERROR("DumpEntry resp_handler got error {}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -0700478 messages::internalError(asyncResp->res);
479 return;
480 }
481
Claire Weinanfdd26902022-03-01 14:18:25 -0800482 // Remove ending slash
483 std::string odataIdStr = entriesPath;
484 if (!odataIdStr.empty())
485 {
486 odataIdStr.pop_back();
487 }
488
489 asyncResp->res.jsonValue["@odata.type"] =
490 "#LogEntryCollection.LogEntryCollection";
491 asyncResp->res.jsonValue["@odata.id"] = std::move(odataIdStr);
492 asyncResp->res.jsonValue["Name"] = dumpType + " Dump Entries";
Patrick Williams89492a12023-05-10 07:51:34 -0500493 asyncResp->res.jsonValue["Description"] = "Collection of " + dumpType +
494 " Dump Entries";
Claire Weinanfdd26902022-03-01 14:18:25 -0800495
Ed Tanous3544d2a2023-08-06 18:12:20 -0700496 nlohmann::json::array_t entriesArray;
Ed Tanous002d39b2022-05-31 08:59:27 -0700497 std::string dumpEntryPath =
498 "/xyz/openbmc_project/dump/" +
499 std::string(boost::algorithm::to_lower_copy(dumpType)) + "/entry/";
500
George Liu5eb468d2023-06-20 17:03:24 +0800501 dbus::utility::ManagedObjectType resp(objects);
Ed Tanous3544d2a2023-08-06 18:12:20 -0700502 std::ranges::sort(resp, [](const auto& l, const auto& r) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700503 return AlphanumLess<std::string>()(l.first.filename(),
504 r.first.filename());
505 });
506
507 for (auto& object : resp)
508 {
509 if (object.first.str.find(dumpEntryPath) == std::string::npos)
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500510 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700511 continue;
512 }
Claire Weinanc6fecda2022-07-15 10:43:25 -0700513 uint64_t timestampUs = 0;
Ed Tanous002d39b2022-05-31 08:59:27 -0700514 uint64_t size = 0;
515 std::string dumpStatus;
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -0600516 std::string originatorId;
517 log_entry::OriginatorTypes originatorType =
518 log_entry::OriginatorTypes::Internal;
Jason M. Bills433b68b2022-06-28 12:24:26 -0700519 nlohmann::json::object_t thisEntry;
Ed Tanous002d39b2022-05-31 08:59:27 -0700520
521 std::string entryID = object.first.filename();
522 if (entryID.empty())
523 {
524 continue;
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500525 }
526
Claire Weinanc6fecda2022-07-15 10:43:25 -0700527 parseDumpEntryFromDbusObject(object, dumpStatus, size, timestampUs,
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -0600528 originatorId, originatorType,
Claire Weinanaefe3782022-07-15 19:17:19 -0700529 asyncResp);
Ed Tanous002d39b2022-05-31 08:59:27 -0700530
531 if (dumpStatus !=
532 "xyz.openbmc_project.Common.Progress.OperationStatus.Completed" &&
533 !dumpStatus.empty())
534 {
535 // Dump status is not Complete, no need to enumerate
536 continue;
537 }
538
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -0600539 thisEntry["@odata.type"] = "#LogEntry.v1_11_0.LogEntry";
Claire Weinanfdd26902022-03-01 14:18:25 -0800540 thisEntry["@odata.id"] = entriesPath + entryID;
Ed Tanous002d39b2022-05-31 08:59:27 -0700541 thisEntry["Id"] = entryID;
542 thisEntry["EntryType"] = "Event";
Ed Tanous002d39b2022-05-31 08:59:27 -0700543 thisEntry["Name"] = dumpType + " Dump Entry";
Claire Weinanbbd80db2022-10-26 16:55:52 -0700544 thisEntry["Created"] =
545 redfish::time_utils::getDateTimeUintUs(timestampUs);
Ed Tanous002d39b2022-05-31 08:59:27 -0700546
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -0600547 if (!originatorId.empty())
548 {
549 thisEntry["Originator"] = originatorId;
550 thisEntry["OriginatorType"] = originatorType;
551 }
552
Ed Tanous002d39b2022-05-31 08:59:27 -0700553 if (dumpType == "BMC")
554 {
555 thisEntry["DiagnosticDataType"] = "Manager";
Patrick Williams89492a12023-05-10 07:51:34 -0500556 thisEntry["AdditionalDataURI"] = entriesPath + entryID +
557 "/attachment";
Claire Weinanfdd26902022-03-01 14:18:25 -0800558 thisEntry["AdditionalDataSizeBytes"] = size;
Ed Tanous002d39b2022-05-31 08:59:27 -0700559 }
560 else if (dumpType == "System")
561 {
562 thisEntry["DiagnosticDataType"] = "OEM";
563 thisEntry["OEMDiagnosticDataType"] = "System";
Patrick Williams89492a12023-05-10 07:51:34 -0500564 thisEntry["AdditionalDataURI"] = entriesPath + entryID +
565 "/attachment";
Claire Weinanfdd26902022-03-01 14:18:25 -0800566 thisEntry["AdditionalDataSizeBytes"] = size;
Ed Tanous002d39b2022-05-31 08:59:27 -0700567 }
Patrick Williamsb2ba3072023-05-12 10:27:39 -0500568 entriesArray.emplace_back(std::move(thisEntry));
Ed Tanous002d39b2022-05-31 08:59:27 -0700569 }
570 asyncResp->res.jsonValue["Members@odata.count"] = entriesArray.size();
Ed Tanous3544d2a2023-08-06 18:12:20 -0700571 asyncResp->res.jsonValue["Members"] = std::move(entriesArray);
Patrick Williams5a39f772023-10-20 11:20:21 -0500572 });
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500573}
574
zhanghch058d1b46d2021-04-01 11:18:24 +0800575inline void
Claire Weinanc7a6d662022-06-13 16:36:39 -0700576 getDumpEntryById(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
zhanghch058d1b46d2021-04-01 11:18:24 +0800577 const std::string& entryID, const std::string& dumpType)
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500578{
Claire Weinanfdd26902022-03-01 14:18:25 -0800579 std::string entriesPath = getDumpEntriesPath(dumpType);
580 if (entriesPath.empty())
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500581 {
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500582 messages::internalError(asyncResp->res);
583 return;
584 }
585
George Liu5eb468d2023-06-20 17:03:24 +0800586 sdbusplus::message::object_path path("/xyz/openbmc_project/dump");
587 dbus::utility::getManagedObjects(
588 "xyz.openbmc_project.Dump.Manager", path,
Claire Weinanfdd26902022-03-01 14:18:25 -0800589 [asyncResp, entryID, dumpType,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800590 entriesPath](const boost::system::error_code& ec,
Ed Tanous02cad962022-06-30 16:50:15 -0700591 const dbus::utility::ManagedObjectType& resp) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700592 if (ec)
593 {
Ed Tanous62598e32023-07-17 17:06:25 -0700594 BMCWEB_LOG_ERROR("DumpEntry resp_handler got error {}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -0700595 messages::internalError(asyncResp->res);
596 return;
597 }
598
599 bool foundDumpEntry = false;
600 std::string dumpEntryPath =
601 "/xyz/openbmc_project/dump/" +
602 std::string(boost::algorithm::to_lower_copy(dumpType)) + "/entry/";
603
604 for (const auto& objectPath : resp)
605 {
606 if (objectPath.first.str != dumpEntryPath + entryID)
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500607 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700608 continue;
609 }
610
611 foundDumpEntry = true;
Claire Weinanc6fecda2022-07-15 10:43:25 -0700612 uint64_t timestampUs = 0;
Ed Tanous002d39b2022-05-31 08:59:27 -0700613 uint64_t size = 0;
614 std::string dumpStatus;
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -0600615 std::string originatorId;
616 log_entry::OriginatorTypes originatorType =
617 log_entry::OriginatorTypes::Internal;
Ed Tanous002d39b2022-05-31 08:59:27 -0700618
Claire Weinanaefe3782022-07-15 19:17:19 -0700619 parseDumpEntryFromDbusObject(objectPath, dumpStatus, size,
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -0600620 timestampUs, originatorId,
621 originatorType, asyncResp);
Ed Tanous002d39b2022-05-31 08:59:27 -0700622
623 if (dumpStatus !=
624 "xyz.openbmc_project.Common.Progress.OperationStatus.Completed" &&
625 !dumpStatus.empty())
626 {
627 // Dump status is not Complete
628 // return not found until status is changed to Completed
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +0200629 messages::resourceNotFound(asyncResp->res, dumpType + " dump",
630 entryID);
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500631 return;
632 }
633
Ed Tanous002d39b2022-05-31 08:59:27 -0700634 asyncResp->res.jsonValue["@odata.type"] =
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -0600635 "#LogEntry.v1_11_0.LogEntry";
Claire Weinanfdd26902022-03-01 14:18:25 -0800636 asyncResp->res.jsonValue["@odata.id"] = entriesPath + entryID;
Ed Tanous002d39b2022-05-31 08:59:27 -0700637 asyncResp->res.jsonValue["Id"] = entryID;
638 asyncResp->res.jsonValue["EntryType"] = "Event";
Ed Tanous002d39b2022-05-31 08:59:27 -0700639 asyncResp->res.jsonValue["Name"] = dumpType + " Dump Entry";
Claire Weinanbbd80db2022-10-26 16:55:52 -0700640 asyncResp->res.jsonValue["Created"] =
641 redfish::time_utils::getDateTimeUintUs(timestampUs);
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500642
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -0600643 if (!originatorId.empty())
644 {
645 asyncResp->res.jsonValue["Originator"] = originatorId;
646 asyncResp->res.jsonValue["OriginatorType"] = originatorType;
647 }
648
Ed Tanous002d39b2022-05-31 08:59:27 -0700649 if (dumpType == "BMC")
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500650 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700651 asyncResp->res.jsonValue["DiagnosticDataType"] = "Manager";
652 asyncResp->res.jsonValue["AdditionalDataURI"] =
Claire Weinanfdd26902022-03-01 14:18:25 -0800653 entriesPath + entryID + "/attachment";
654 asyncResp->res.jsonValue["AdditionalDataSizeBytes"] = size;
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500655 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700656 else if (dumpType == "System")
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500657 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700658 asyncResp->res.jsonValue["DiagnosticDataType"] = "OEM";
659 asyncResp->res.jsonValue["OEMDiagnosticDataType"] = "System";
660 asyncResp->res.jsonValue["AdditionalDataURI"] =
Claire Weinanfdd26902022-03-01 14:18:25 -0800661 entriesPath + entryID + "/attachment";
662 asyncResp->res.jsonValue["AdditionalDataSizeBytes"] = size;
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500663 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700664 }
665 if (!foundDumpEntry)
666 {
Ed Tanous62598e32023-07-17 17:06:25 -0700667 BMCWEB_LOG_WARNING("Can't find Dump Entry {}", entryID);
Myung Baeb90d14f2023-05-31 14:40:39 -0500668 messages::resourceNotFound(asyncResp->res, dumpType + " dump",
669 entryID);
Ed Tanous002d39b2022-05-31 08:59:27 -0700670 return;
671 }
Patrick Williams5a39f772023-10-20 11:20:21 -0500672 });
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500673}
674
zhanghch058d1b46d2021-04-01 11:18:24 +0800675inline void deleteDumpEntry(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Stanley Chu98782562020-11-04 16:10:24 +0800676 const std::string& entryID,
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500677 const std::string& dumpType)
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500678{
Patrick Williams5a39f772023-10-20 11:20:21 -0500679 auto respHandler = [asyncResp,
680 entryID](const boost::system::error_code& ec) {
Ed Tanous62598e32023-07-17 17:06:25 -0700681 BMCWEB_LOG_DEBUG("Dump Entry doDelete callback: Done");
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500682 if (ec)
683 {
George Liu3de8d8b2021-03-22 17:49:39 +0800684 if (ec.value() == EBADR)
685 {
686 messages::resourceNotFound(asyncResp->res, "LogEntry", entryID);
687 return;
688 }
Ed Tanous62598e32023-07-17 17:06:25 -0700689 BMCWEB_LOG_ERROR(
690 "Dump (DBus) doDelete respHandler got error {} entryID={}", ec,
691 entryID);
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500692 messages::internalError(asyncResp->res);
693 return;
694 }
695 };
696 crow::connections::systemBus->async_method_call(
697 respHandler, "xyz.openbmc_project.Dump.Manager",
Asmitha Karunanithib47452b2020-09-25 02:02:19 -0500698 "/xyz/openbmc_project/dump/" +
699 std::string(boost::algorithm::to_lower_copy(dumpType)) + "/entry/" +
700 entryID,
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -0500701 "xyz.openbmc_project.Object.Delete", "Delete");
702}
703
Carson Labrado168d1b12023-03-27 17:04:46 +0000704inline void
705 downloadEntryCallback(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
706 const std::string& entryID,
707 const std::string& downloadEntryType,
708 const boost::system::error_code& ec,
709 const sdbusplus::message::unix_fd& unixfd)
710{
711 if (ec.value() == EBADR)
712 {
713 messages::resourceNotFound(asyncResp->res, "EntryAttachment", entryID);
714 return;
715 }
716 if (ec)
717 {
718 BMCWEB_LOG_ERROR("DBUS response error: {}", ec);
719 messages::internalError(asyncResp->res);
720 return;
721 }
722
723 // Make sure we know how to process the retrieved entry attachment
724 if ((downloadEntryType != "BMC") && (downloadEntryType != "System"))
725 {
726 BMCWEB_LOG_ERROR("downloadEntryCallback() invalid entry type: {}",
727 downloadEntryType);
728 messages::internalError(asyncResp->res);
729 }
730
731 int fd = -1;
732 fd = dup(unixfd);
733 if (fd < 0)
734 {
735 BMCWEB_LOG_ERROR("Failed to open file");
736 messages::internalError(asyncResp->res);
737 return;
738 }
739
740 long long int size = lseek(fd, 0, SEEK_END);
741 if (size <= 0)
742 {
743 BMCWEB_LOG_ERROR("Failed to get size of file, lseek() returned {}",
744 size);
745 messages::internalError(asyncResp->res);
746 close(fd);
747 return;
748 }
749
750 // Arbitrary max size of 20MB to accommodate BMC dumps
751 constexpr int maxFileSize = 20 * 1024 * 1024;
752 if (size > maxFileSize)
753 {
754 BMCWEB_LOG_ERROR("File size {} exceeds maximum allowed size of {}",
755 size, maxFileSize);
756 messages::internalError(asyncResp->res);
757 close(fd);
758 return;
759 }
760 long long int rc = lseek(fd, 0, SEEK_SET);
761 if (rc < 0)
762 {
763 BMCWEB_LOG_ERROR("Failed to reset file offset to 0");
764 messages::internalError(asyncResp->res);
765 close(fd);
766 return;
767 }
768
Ed Tanous27b0cf92023-08-07 12:02:40 -0700769 std::string body;
770 body.resize(static_cast<size_t>(size), '\0');
771 rc = read(fd, body.data(), body.size());
Carson Labrado168d1b12023-03-27 17:04:46 +0000772 if ((rc == -1) || (rc != size))
773 {
774 BMCWEB_LOG_ERROR("Failed to read in file");
775 messages::internalError(asyncResp->res);
776 close(fd);
777 return;
778 }
779 close(fd);
Carson Labrado168d1b12023-03-27 17:04:46 +0000780 if (downloadEntryType == "System")
781 {
Ed Tanous27b0cf92023-08-07 12:02:40 -0700782 // Base64 encode response.
783 asyncResp->res.write(crow::utility::base64encode(body));
Carson Labrado168d1b12023-03-27 17:04:46 +0000784 asyncResp->res.addHeader(
785 boost::beast::http::field::content_transfer_encoding, "Base64");
786 }
Ed Tanous27b0cf92023-08-07 12:02:40 -0700787 else
788 {
789 asyncResp->res.write(std::move(body));
790 }
Carson Labrado168d1b12023-03-27 17:04:46 +0000791
792 asyncResp->res.addHeader(boost::beast::http::field::content_type,
793 "application/octet-stream");
794}
795
796inline void
797 downloadDumpEntry(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
798 const std::string& entryID, const std::string& dumpType)
799{
800 if (dumpType != "BMC")
801 {
802 BMCWEB_LOG_WARNING("Can't find Dump Entry {}", entryID);
803 messages::resourceNotFound(asyncResp->res, dumpType + " dump", entryID);
804 return;
805 }
806
807 std::string dumpEntryPath =
808 sdbusplus::message::object_path("/xyz/openbmc_project/dump/") /
809 std::string(boost::algorithm::to_lower_copy(dumpType)) / "entry" /
810 entryID;
811
812 auto downloadDumpEntryHandler =
813 [asyncResp, entryID,
814 dumpType](const boost::system::error_code& ec,
815 const sdbusplus::message::unix_fd& unixfd) {
816 downloadEntryCallback(asyncResp, entryID, dumpType, ec, unixfd);
817 };
818
819 crow::connections::systemBus->async_method_call(
820 std::move(downloadDumpEntryHandler), "xyz.openbmc_project.Dump.Manager",
821 dumpEntryPath, "xyz.openbmc_project.Dump.Entry", "GetFileHandle");
822}
823
824inline void
825 downloadEventLogEntry(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
826 const std::string& systemName,
827 const std::string& entryID,
828 const std::string& dumpType)
829{
830 if constexpr (bmcwebEnableMultiHost)
831 {
832 // Option currently returns no systems. TBD
833 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
834 systemName);
835 return;
836 }
837 if (systemName != "system")
838 {
839 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
840 systemName);
841 return;
842 }
843
844 std::string entryPath =
845 sdbusplus::message::object_path("/xyz/openbmc_project/logging/entry") /
846 entryID;
847
848 auto downloadEventLogEntryHandler =
849 [asyncResp, entryID,
850 dumpType](const boost::system::error_code& ec,
851 const sdbusplus::message::unix_fd& unixfd) {
852 downloadEntryCallback(asyncResp, entryID, dumpType, ec, unixfd);
853 };
854
855 crow::connections::systemBus->async_method_call(
856 std::move(downloadEventLogEntryHandler), "xyz.openbmc_project.Logging",
857 entryPath, "xyz.openbmc_project.Logging.Entry", "GetEntry");
858}
859
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600860inline DumpCreationProgress
861 mapDbusStatusToDumpProgress(const std::string& status)
Asmitha Karunanithia43be802020-05-07 05:05:36 -0500862{
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600863 if (status ==
864 "xyz.openbmc_project.Common.Progress.OperationStatus.Failed" ||
865 status == "xyz.openbmc_project.Common.Progress.OperationStatus.Aborted")
866 {
867 return DumpCreationProgress::DUMP_CREATE_FAILED;
868 }
869 if (status ==
870 "xyz.openbmc_project.Common.Progress.OperationStatus.Completed")
871 {
872 return DumpCreationProgress::DUMP_CREATE_SUCCESS;
873 }
874 return DumpCreationProgress::DUMP_CREATE_INPROGRESS;
875}
876
877inline DumpCreationProgress
878 getDumpCompletionStatus(const dbus::utility::DBusPropertiesMap& values)
879{
880 for (const auto& [key, val] : values)
881 {
882 if (key == "Status")
Ed Tanous002d39b2022-05-31 08:59:27 -0700883 {
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600884 const std::string* value = std::get_if<std::string>(&val);
885 if (value == nullptr)
886 {
Ed Tanous62598e32023-07-17 17:06:25 -0700887 BMCWEB_LOG_ERROR("Status property value is null");
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600888 return DumpCreationProgress::DUMP_CREATE_FAILED;
889 }
890 return mapDbusStatusToDumpProgress(*value);
891 }
892 }
893 return DumpCreationProgress::DUMP_CREATE_INPROGRESS;
894}
895
896inline std::string getDumpEntryPath(const std::string& dumpPath)
897{
898 if (dumpPath == "/xyz/openbmc_project/dump/bmc/entry")
899 {
900 return "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/";
901 }
902 if (dumpPath == "/xyz/openbmc_project/dump/system/entry")
903 {
904 return "/redfish/v1/Systems/system/LogServices/Dump/Entries/";
905 }
906 return "";
907}
908
909inline void createDumpTaskCallback(
910 task::Payload&& payload,
911 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
912 const sdbusplus::message::object_path& createdObjPath)
913{
914 const std::string dumpPath = createdObjPath.parent_path().str;
915 const std::string dumpId = createdObjPath.filename();
916
917 std::string dumpEntryPath = getDumpEntryPath(dumpPath);
918
919 if (dumpEntryPath.empty())
920 {
Ed Tanous62598e32023-07-17 17:06:25 -0700921 BMCWEB_LOG_ERROR("Invalid dump type received");
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600922 messages::internalError(asyncResp->res);
923 return;
924 }
925
926 crow::connections::systemBus->async_method_call(
927 [asyncResp, payload, createdObjPath,
928 dumpEntryPath{std::move(dumpEntryPath)},
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800929 dumpId](const boost::system::error_code& ec,
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600930 const std::string& introspectXml) {
931 if (ec)
932 {
Ed Tanous62598e32023-07-17 17:06:25 -0700933 BMCWEB_LOG_ERROR("Introspect call failed with error: {}",
934 ec.message());
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600935 messages::internalError(asyncResp->res);
936 return;
Ed Tanous002d39b2022-05-31 08:59:27 -0700937 }
938
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600939 // Check if the created dump object has implemented Progress
940 // interface to track dump completion. If yes, fetch the "Status"
941 // property of the interface, modify the task state accordingly.
942 // Else, return task completed.
943 tinyxml2::XMLDocument doc;
Ed Tanous002d39b2022-05-31 08:59:27 -0700944
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600945 doc.Parse(introspectXml.data(), introspectXml.size());
946 tinyxml2::XMLNode* pRoot = doc.FirstChildElement("node");
947 if (pRoot == nullptr)
Ed Tanous002d39b2022-05-31 08:59:27 -0700948 {
Ed Tanous62598e32023-07-17 17:06:25 -0700949 BMCWEB_LOG_ERROR("XML document failed to parse");
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600950 messages::internalError(asyncResp->res);
951 return;
952 }
953 tinyxml2::XMLElement* interfaceNode =
954 pRoot->FirstChildElement("interface");
955
956 bool isProgressIntfPresent = false;
957 while (interfaceNode != nullptr)
958 {
959 const char* thisInterfaceName = interfaceNode->Attribute("name");
960 if (thisInterfaceName != nullptr)
961 {
962 if (thisInterfaceName ==
963 std::string_view("xyz.openbmc_project.Common.Progress"))
964 {
965 interfaceNode =
966 interfaceNode->NextSiblingElement("interface");
967 continue;
968 }
969 isProgressIntfPresent = true;
970 break;
971 }
972 interfaceNode = interfaceNode->NextSiblingElement("interface");
973 }
974
975 std::shared_ptr<task::TaskData> task = task::TaskData::createTask(
976 [createdObjPath, dumpEntryPath, dumpId, isProgressIntfPresent](
Ed Tanous8b242752023-06-27 17:17:13 -0700977 const boost::system::error_code& ec2, sdbusplus::message_t& msg,
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600978 const std::shared_ptr<task::TaskData>& taskData) {
Ed Tanous8b242752023-06-27 17:17:13 -0700979 if (ec2)
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600980 {
Ed Tanous62598e32023-07-17 17:06:25 -0700981 BMCWEB_LOG_ERROR("{}: Error in creating dump",
982 createdObjPath.str);
Asmitha Karunanithi8e317782020-12-10 03:35:05 -0600983 taskData->messages.emplace_back(messages::internalError());
984 taskData->state = "Cancelled";
985 return task::completed;
986 }
987
988 if (isProgressIntfPresent)
989 {
990 dbus::utility::DBusPropertiesMap values;
991 std::string prop;
992 msg.read(prop, values);
993
994 DumpCreationProgress dumpStatus =
995 getDumpCompletionStatus(values);
996 if (dumpStatus == DumpCreationProgress::DUMP_CREATE_FAILED)
997 {
Ed Tanous62598e32023-07-17 17:06:25 -0700998 BMCWEB_LOG_ERROR("{}: Error in creating dump",
999 createdObjPath.str);
Asmitha Karunanithi8e317782020-12-10 03:35:05 -06001000 taskData->state = "Cancelled";
1001 return task::completed;
1002 }
1003
1004 if (dumpStatus == DumpCreationProgress::DUMP_CREATE_INPROGRESS)
1005 {
Ed Tanous62598e32023-07-17 17:06:25 -07001006 BMCWEB_LOG_DEBUG("{}: Dump creation task is in progress",
1007 createdObjPath.str);
Asmitha Karunanithi8e317782020-12-10 03:35:05 -06001008 return !task::completed;
1009 }
1010 }
1011
Ed Tanous002d39b2022-05-31 08:59:27 -07001012 nlohmann::json retMessage = messages::success();
1013 taskData->messages.emplace_back(retMessage);
1014
Ed Tanousc51a58e2023-03-27 14:43:19 -07001015 boost::urls::url url = boost::urls::format(
1016 "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/{}", dumpId);
1017
1018 std::string headerLoc = "Location: ";
1019 headerLoc += url.buffer();
1020
Ed Tanous002d39b2022-05-31 08:59:27 -07001021 taskData->payload->httpHeaders.emplace_back(std::move(headerLoc));
1022
Ed Tanous62598e32023-07-17 17:06:25 -07001023 BMCWEB_LOG_DEBUG("{}: Dump creation task completed",
1024 createdObjPath.str);
Ed Tanous002d39b2022-05-31 08:59:27 -07001025 taskData->state = "Completed";
1026 return task::completed;
Patrick Williams5a39f772023-10-20 11:20:21 -05001027 },
Asmitha Karunanithi8e317782020-12-10 03:35:05 -06001028 "type='signal',interface='org.freedesktop.DBus.Properties',"
1029 "member='PropertiesChanged',path='" +
1030 createdObjPath.str + "'");
Asmitha Karunanithia43be802020-05-07 05:05:36 -05001031
Asmitha Karunanithi8e317782020-12-10 03:35:05 -06001032 // The task timer is set to max time limit within which the
1033 // requested dump will be collected.
1034 task->startTimer(std::chrono::minutes(6));
1035 task->populateResp(asyncResp->res);
1036 task->payload.emplace(payload);
Patrick Williams5a39f772023-10-20 11:20:21 -05001037 },
Asmitha Karunanithi8e317782020-12-10 03:35:05 -06001038 "xyz.openbmc_project.Dump.Manager", createdObjPath,
1039 "org.freedesktop.DBus.Introspectable", "Introspect");
Asmitha Karunanithia43be802020-05-07 05:05:36 -05001040}
1041
zhanghch058d1b46d2021-04-01 11:18:24 +08001042inline void createDump(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1043 const crow::Request& req, const std::string& dumpType)
Asmitha Karunanithia43be802020-05-07 05:05:36 -05001044{
Claire Weinanfdd26902022-03-01 14:18:25 -08001045 std::string dumpPath = getDumpEntriesPath(dumpType);
1046 if (dumpPath.empty())
Asmitha Karunanithia43be802020-05-07 05:05:36 -05001047 {
Asmitha Karunanithia43be802020-05-07 05:05:36 -05001048 messages::internalError(asyncResp->res);
1049 return;
1050 }
1051
1052 std::optional<std::string> diagnosticDataType;
1053 std::optional<std::string> oemDiagnosticDataType;
1054
Willy Tu15ed6782021-12-14 11:03:16 -08001055 if (!redfish::json_util::readJsonAction(
Asmitha Karunanithia43be802020-05-07 05:05:36 -05001056 req, asyncResp->res, "DiagnosticDataType", diagnosticDataType,
1057 "OEMDiagnosticDataType", oemDiagnosticDataType))
1058 {
1059 return;
1060 }
1061
1062 if (dumpType == "System")
1063 {
1064 if (!oemDiagnosticDataType || !diagnosticDataType)
1065 {
Ed Tanous62598e32023-07-17 17:06:25 -07001066 BMCWEB_LOG_ERROR(
1067 "CreateDump action parameter 'DiagnosticDataType'/'OEMDiagnosticDataType' value not found!");
Asmitha Karunanithia43be802020-05-07 05:05:36 -05001068 messages::actionParameterMissing(
1069 asyncResp->res, "CollectDiagnosticData",
1070 "DiagnosticDataType & OEMDiagnosticDataType");
1071 return;
1072 }
Ed Tanous3174e4d2020-10-07 11:41:22 -07001073 if ((*oemDiagnosticDataType != "System") ||
1074 (*diagnosticDataType != "OEM"))
Asmitha Karunanithia43be802020-05-07 05:05:36 -05001075 {
Ed Tanous62598e32023-07-17 17:06:25 -07001076 BMCWEB_LOG_ERROR("Wrong parameter values passed");
Ed Tanousace85d62021-10-26 12:45:59 -07001077 messages::internalError(asyncResp->res);
Asmitha Karunanithia43be802020-05-07 05:05:36 -05001078 return;
1079 }
Asmitha Karunanithi59075712021-10-22 01:17:41 -05001080 dumpPath = "/redfish/v1/Systems/system/LogServices/Dump/";
Asmitha Karunanithia43be802020-05-07 05:05:36 -05001081 }
1082 else if (dumpType == "BMC")
1083 {
1084 if (!diagnosticDataType)
1085 {
Ed Tanous62598e32023-07-17 17:06:25 -07001086 BMCWEB_LOG_ERROR(
1087 "CreateDump action parameter 'DiagnosticDataType' not found!");
Asmitha Karunanithia43be802020-05-07 05:05:36 -05001088 messages::actionParameterMissing(
1089 asyncResp->res, "CollectDiagnosticData", "DiagnosticDataType");
1090 return;
1091 }
Ed Tanous3174e4d2020-10-07 11:41:22 -07001092 if (*diagnosticDataType != "Manager")
Asmitha Karunanithia43be802020-05-07 05:05:36 -05001093 {
Ed Tanous62598e32023-07-17 17:06:25 -07001094 BMCWEB_LOG_ERROR(
1095 "Wrong parameter value passed for 'DiagnosticDataType'");
Ed Tanousace85d62021-10-26 12:45:59 -07001096 messages::internalError(asyncResp->res);
Asmitha Karunanithia43be802020-05-07 05:05:36 -05001097 return;
1098 }
Asmitha Karunanithi59075712021-10-22 01:17:41 -05001099 dumpPath = "/redfish/v1/Managers/bmc/LogServices/Dump/";
1100 }
1101 else
1102 {
Ed Tanous62598e32023-07-17 17:06:25 -07001103 BMCWEB_LOG_ERROR("CreateDump failed. Unknown dump type");
Asmitha Karunanithi59075712021-10-22 01:17:41 -05001104 messages::internalError(asyncResp->res);
1105 return;
Asmitha Karunanithia43be802020-05-07 05:05:36 -05001106 }
1107
Asmitha Karunanithi8e317782020-12-10 03:35:05 -06001108 std::vector<std::pair<std::string, std::variant<std::string, uint64_t>>>
1109 createDumpParamVec;
1110
Carson Labradof574a8e2023-03-22 02:26:00 +00001111 if (req.session != nullptr)
1112 {
1113 createDumpParamVec.emplace_back(
1114 "xyz.openbmc_project.Dump.Create.CreateParameters.OriginatorId",
1115 req.session->clientIp);
1116 createDumpParamVec.emplace_back(
1117 "xyz.openbmc_project.Dump.Create.CreateParameters.OriginatorType",
1118 "xyz.openbmc_project.Common.OriginatedBy.OriginatorTypes.Client");
1119 }
Asmitha Karunanithi68dd0752022-11-15 11:33:46 -06001120
Asmitha Karunanithia43be802020-05-07 05:05:36 -05001121 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001122 [asyncResp, payload(task::Payload(req)),
1123 dumpPath](const boost::system::error_code& ec,
1124 const sdbusplus::message_t& msg,
1125 const sdbusplus::message::object_path& objPath) mutable {
Ed Tanous002d39b2022-05-31 08:59:27 -07001126 if (ec)
1127 {
Ed Tanous62598e32023-07-17 17:06:25 -07001128 BMCWEB_LOG_ERROR("CreateDump resp_handler got error {}", ec);
Asmitha Karunanithi59075712021-10-22 01:17:41 -05001129 const sd_bus_error* dbusError = msg.get_error();
1130 if (dbusError == nullptr)
1131 {
1132 messages::internalError(asyncResp->res);
1133 return;
1134 }
1135
Ed Tanous62598e32023-07-17 17:06:25 -07001136 BMCWEB_LOG_ERROR("CreateDump DBus error: {} and error msg: {}",
1137 dbusError->name, dbusError->message);
Asmitha Karunanithi59075712021-10-22 01:17:41 -05001138 if (std::string_view(
1139 "xyz.openbmc_project.Common.Error.NotAllowed") ==
1140 dbusError->name)
1141 {
1142 messages::resourceInStandby(asyncResp->res);
1143 return;
1144 }
1145 if (std::string_view(
1146 "xyz.openbmc_project.Dump.Create.Error.Disabled") ==
1147 dbusError->name)
1148 {
1149 messages::serviceDisabled(asyncResp->res, dumpPath);
1150 return;
1151 }
1152 if (std::string_view(
1153 "xyz.openbmc_project.Common.Error.Unavailable") ==
1154 dbusError->name)
1155 {
1156 messages::resourceInUse(asyncResp->res);
1157 return;
1158 }
1159 // Other Dbus errors such as:
1160 // xyz.openbmc_project.Common.Error.InvalidArgument &
1161 // org.freedesktop.DBus.Error.InvalidArgs are all related to
1162 // the dbus call that is made here in the bmcweb
1163 // implementation and has nothing to do with the client's
1164 // input in the request. Hence, returning internal error
1165 // back to the client.
Ed Tanous002d39b2022-05-31 08:59:27 -07001166 messages::internalError(asyncResp->res);
1167 return;
1168 }
Ed Tanous62598e32023-07-17 17:06:25 -07001169 BMCWEB_LOG_DEBUG("Dump Created. Path: {}", objPath.str);
Asmitha Karunanithi8e317782020-12-10 03:35:05 -06001170 createDumpTaskCallback(std::move(payload), asyncResp, objPath);
Patrick Williams5a39f772023-10-20 11:20:21 -05001171 },
Asmitha Karunanithib47452b2020-09-25 02:02:19 -05001172 "xyz.openbmc_project.Dump.Manager",
1173 "/xyz/openbmc_project/dump/" +
1174 std::string(boost::algorithm::to_lower_copy(dumpType)),
Asmitha Karunanithi8e317782020-12-10 03:35:05 -06001175 "xyz.openbmc_project.Dump.Create", "CreateDump", createDumpParamVec);
Asmitha Karunanithia43be802020-05-07 05:05:36 -05001176}
1177
zhanghch058d1b46d2021-04-01 11:18:24 +08001178inline void clearDump(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1179 const std::string& dumpType)
Asmitha Karunanithi80319af2020-05-07 05:30:21 -05001180{
Asmitha Karunanithib47452b2020-09-25 02:02:19 -05001181 std::string dumpTypeLowerCopy =
1182 std::string(boost::algorithm::to_lower_copy(dumpType));
zhanghch058d1b46d2021-04-01 11:18:24 +08001183
Claire Weinan0d946212022-07-13 19:40:19 -07001184 crow::connections::systemBus->async_method_call(
1185 [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001186 if (ec)
1187 {
Ed Tanous62598e32023-07-17 17:06:25 -07001188 BMCWEB_LOG_ERROR("clearDump resp_handler got error {}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -07001189 messages::internalError(asyncResp->res);
1190 return;
1191 }
Patrick Williams5a39f772023-10-20 11:20:21 -05001192 },
Claire Weinan0d946212022-07-13 19:40:19 -07001193 "xyz.openbmc_project.Dump.Manager",
1194 "/xyz/openbmc_project/dump/" + dumpTypeLowerCopy,
1195 "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
Asmitha Karunanithi80319af2020-05-07 05:30:21 -05001196}
1197
Ed Tanousb9d36b42022-02-26 21:42:46 -08001198inline static void
1199 parseCrashdumpParameters(const dbus::utility::DBusPropertiesMap& params,
1200 std::string& filename, std::string& timestamp,
1201 std::string& logfile)
Johnathan Mantey043a0532020-03-10 17:15:28 -07001202{
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001203 const std::string* filenamePtr = nullptr;
1204 const std::string* timestampPtr = nullptr;
1205 const std::string* logfilePtr = nullptr;
1206
1207 const bool success = sdbusplus::unpackPropertiesNoThrow(
1208 dbus_utils::UnpackErrorPrinter(), params, "Timestamp", timestampPtr,
1209 "Filename", filenamePtr, "Log", logfilePtr);
1210
1211 if (!success)
Johnathan Mantey043a0532020-03-10 17:15:28 -07001212 {
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001213 return;
1214 }
1215
1216 if (filenamePtr != nullptr)
1217 {
1218 filename = *filenamePtr;
1219 }
1220
1221 if (timestampPtr != nullptr)
1222 {
1223 timestamp = *timestampPtr;
1224 }
1225
1226 if (logfilePtr != nullptr)
1227 {
1228 logfile = *logfilePtr;
Johnathan Mantey043a0532020-03-10 17:15:28 -07001229 }
1230}
1231
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001232inline void requestRoutesSystemLogServiceCollection(App& app)
Ed Tanous1da66f72018-07-27 16:13:37 -07001233{
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001234 /**
1235 * Functions triggers appropriate requests on DBus
1236 */
Ed Tanous22d268c2022-05-19 09:39:07 -07001237 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/")
Ed Tanoused398212021-06-09 17:05:54 -07001238 .privileges(redfish::privileges::getLogServiceCollection)
Ed Tanous002d39b2022-05-31 08:59:27 -07001239 .methods(boost::beast::http::verb::get)(
1240 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07001241 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1242 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001243 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001244 {
1245 return;
1246 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08001247 if constexpr (bmcwebEnableMultiHost)
1248 {
1249 // Option currently returns no systems. TBD
1250 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1251 systemName);
1252 return;
1253 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001254 if (systemName != "system")
1255 {
1256 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1257 systemName);
1258 return;
1259 }
1260
Ed Tanous002d39b2022-05-31 08:59:27 -07001261 // Collections don't include the static data added by SubRoute
1262 // because it has a duplicate entry for members
1263 asyncResp->res.jsonValue["@odata.type"] =
1264 "#LogServiceCollection.LogServiceCollection";
1265 asyncResp->res.jsonValue["@odata.id"] =
1266 "/redfish/v1/Systems/system/LogServices";
1267 asyncResp->res.jsonValue["Name"] = "System Log Services Collection";
1268 asyncResp->res.jsonValue["Description"] =
1269 "Collection of LogServices for this Computer System";
1270 nlohmann::json& logServiceArray = asyncResp->res.jsonValue["Members"];
1271 logServiceArray = nlohmann::json::array();
1272 nlohmann::json::object_t eventLog;
1273 eventLog["@odata.id"] =
1274 "/redfish/v1/Systems/system/LogServices/EventLog";
Patrick Williamsb2ba3072023-05-12 10:27:39 -05001275 logServiceArray.emplace_back(std::move(eventLog));
Asmitha Karunanithi5cb1dd22020-05-07 04:35:02 -05001276#ifdef BMCWEB_ENABLE_REDFISH_DUMP_LOG
Ed Tanous002d39b2022-05-31 08:59:27 -07001277 nlohmann::json::object_t dumpLog;
1278 dumpLog["@odata.id"] = "/redfish/v1/Systems/system/LogServices/Dump";
Patrick Williamsb2ba3072023-05-12 10:27:39 -05001279 logServiceArray.emplace_back(std::move(dumpLog));
raviteja-bc9bb6862020-02-03 11:53:32 -06001280#endif
1281
Jason M. Billsd53dd412019-02-12 17:16:22 -08001282#ifdef BMCWEB_ENABLE_REDFISH_CPU_LOG
Ed Tanous002d39b2022-05-31 08:59:27 -07001283 nlohmann::json::object_t crashdump;
1284 crashdump["@odata.id"] =
1285 "/redfish/v1/Systems/system/LogServices/Crashdump";
Patrick Williamsb2ba3072023-05-12 10:27:39 -05001286 logServiceArray.emplace_back(std::move(crashdump));
Jason M. Billsd53dd412019-02-12 17:16:22 -08001287#endif
Spencer Kub7028eb2021-10-26 15:27:35 +08001288
1289#ifdef BMCWEB_ENABLE_REDFISH_HOST_LOGGER
Ed Tanous002d39b2022-05-31 08:59:27 -07001290 nlohmann::json::object_t hostlogger;
1291 hostlogger["@odata.id"] =
1292 "/redfish/v1/Systems/system/LogServices/HostLogger";
Patrick Williamsb2ba3072023-05-12 10:27:39 -05001293 logServiceArray.emplace_back(std::move(hostlogger));
Spencer Kub7028eb2021-10-26 15:27:35 +08001294#endif
Ed Tanous002d39b2022-05-31 08:59:27 -07001295 asyncResp->res.jsonValue["Members@odata.count"] =
1296 logServiceArray.size();
ZhikuiRena3316fc2020-01-29 14:58:08 -08001297
George Liu7a1dbc42022-12-07 16:03:22 +08001298 constexpr std::array<std::string_view, 1> interfaces = {
1299 "xyz.openbmc_project.State.Boot.PostCode"};
1300 dbus::utility::getSubTreePaths(
1301 "/", 0, interfaces,
1302 [asyncResp](const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07001303 const dbus::utility::MapperGetSubTreePathsResponse&
1304 subtreePath) {
1305 if (ec)
1306 {
Ed Tanous62598e32023-07-17 17:06:25 -07001307 BMCWEB_LOG_ERROR("{}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -07001308 return;
1309 }
Ed Tanous45ca1b82022-03-25 13:07:27 -07001310
Ed Tanous002d39b2022-05-31 08:59:27 -07001311 for (const auto& pathStr : subtreePath)
1312 {
1313 if (pathStr.find("PostCode") != std::string::npos)
1314 {
1315 nlohmann::json& logServiceArrayLocal =
1316 asyncResp->res.jsonValue["Members"];
Ed Tanous613dabe2022-07-09 11:17:36 -07001317 nlohmann::json::object_t member;
1318 member["@odata.id"] =
1319 "/redfish/v1/Systems/system/LogServices/PostCodes";
1320
Patrick Williamsb2ba3072023-05-12 10:27:39 -05001321 logServiceArrayLocal.emplace_back(std::move(member));
Ed Tanous613dabe2022-07-09 11:17:36 -07001322
Ed Tanous002d39b2022-05-31 08:59:27 -07001323 asyncResp->res.jsonValue["Members@odata.count"] =
1324 logServiceArrayLocal.size();
1325 return;
1326 }
1327 }
Ed Tanous45ca1b82022-03-25 13:07:27 -07001328 });
Patrick Williams5a39f772023-10-20 11:20:21 -05001329 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001330}
1331
1332inline void requestRoutesEventLogService(App& app)
1333{
Ed Tanous22d268c2022-05-19 09:39:07 -07001334 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/EventLog/")
Ed Tanoused398212021-06-09 17:05:54 -07001335 .privileges(redfish::privileges::getLogService)
Ed Tanous002d39b2022-05-31 08:59:27 -07001336 .methods(boost::beast::http::verb::get)(
1337 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07001338 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1339 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001340 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001341 {
1342 return;
1343 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001344 if (systemName != "system")
1345 {
1346 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1347 systemName);
1348 return;
1349 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001350 asyncResp->res.jsonValue["@odata.id"] =
1351 "/redfish/v1/Systems/system/LogServices/EventLog";
1352 asyncResp->res.jsonValue["@odata.type"] =
Janet Adkinsb25644a2023-08-16 11:23:45 -05001353 "#LogService.v1_2_0.LogService";
Ed Tanous002d39b2022-05-31 08:59:27 -07001354 asyncResp->res.jsonValue["Name"] = "Event Log Service";
1355 asyncResp->res.jsonValue["Description"] = "System Event Log Service";
1356 asyncResp->res.jsonValue["Id"] = "EventLog";
1357 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
Tejas Patil7c8c4052021-06-04 17:43:14 +05301358
Ed Tanous002d39b2022-05-31 08:59:27 -07001359 std::pair<std::string, std::string> redfishDateTimeOffset =
Ed Tanous2b829372022-08-03 14:22:34 -07001360 redfish::time_utils::getDateTimeOffsetNow();
Tejas Patil7c8c4052021-06-04 17:43:14 +05301361
Ed Tanous002d39b2022-05-31 08:59:27 -07001362 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
1363 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
1364 redfishDateTimeOffset.second;
Tejas Patil7c8c4052021-06-04 17:43:14 +05301365
Ed Tanous002d39b2022-05-31 08:59:27 -07001366 asyncResp->res.jsonValue["Entries"]["@odata.id"] =
1367 "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
1368 asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"] = {
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001369
Ed Tanous002d39b2022-05-31 08:59:27 -07001370 {"target",
1371 "/redfish/v1/Systems/system/LogServices/EventLog/Actions/LogService.ClearLog"}};
Patrick Williams5a39f772023-10-20 11:20:21 -05001372 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001373}
1374
1375inline void requestRoutesJournalEventLogClear(App& app)
1376{
Jason M. Bills4978b632022-02-22 14:17:43 -08001377 BMCWEB_ROUTE(
1378 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07001379 "/redfish/v1/Systems/<str>/LogServices/EventLog/Actions/LogService.ClearLog/")
Ed Tanous432a8902021-06-14 15:28:56 -07001380 .privileges({{"ConfigureComponents"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001381 .methods(boost::beast::http::verb::post)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07001382 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07001383 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1384 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001385 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001386 {
1387 return;
1388 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001389 if (systemName != "system")
1390 {
1391 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1392 systemName);
1393 return;
1394 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001395 // Clear the EventLog by deleting the log files
1396 std::vector<std::filesystem::path> redfishLogFiles;
1397 if (getRedfishLogFiles(redfishLogFiles))
1398 {
1399 for (const std::filesystem::path& file : redfishLogFiles)
1400 {
1401 std::error_code ec;
1402 std::filesystem::remove(file, ec);
1403 }
1404 }
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001405
Ed Tanous002d39b2022-05-31 08:59:27 -07001406 // Reload rsyslog so it knows to start new log files
1407 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001408 [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001409 if (ec)
1410 {
Ed Tanous62598e32023-07-17 17:06:25 -07001411 BMCWEB_LOG_ERROR("Failed to reload rsyslog: {}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -07001412 messages::internalError(asyncResp->res);
1413 return;
1414 }
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001415
Ed Tanous002d39b2022-05-31 08:59:27 -07001416 messages::success(asyncResp->res);
Patrick Williams5a39f772023-10-20 11:20:21 -05001417 },
Ed Tanous002d39b2022-05-31 08:59:27 -07001418 "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
1419 "org.freedesktop.systemd1.Manager", "ReloadUnit", "rsyslog.service",
1420 "replace");
Patrick Williams5a39f772023-10-20 11:20:21 -05001421 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001422}
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001423
Jason M. Billsac992cd2022-06-24 13:31:46 -07001424enum class LogParseError
1425{
1426 success,
1427 parseFailed,
1428 messageIdNotInRegistry,
1429};
1430
1431static LogParseError
1432 fillEventLogEntryJson(const std::string& logEntryID,
1433 const std::string& logEntry,
1434 nlohmann::json::object_t& logEntryJson)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001435{
Jason M. Bills95820182019-04-22 16:25:34 -07001436 // The redfish log format is "<Timestamp> <MessageId>,<MessageArgs>"
Jason M. Billscd225da2019-05-08 15:31:57 -07001437 // First get the Timestamp
Ed Tanousf23b7292020-10-15 09:41:17 -07001438 size_t space = logEntry.find_first_of(' ');
Jason M. Billscd225da2019-05-08 15:31:57 -07001439 if (space == std::string::npos)
Jason M. Bills95820182019-04-22 16:25:34 -07001440 {
Jason M. Billsac992cd2022-06-24 13:31:46 -07001441 return LogParseError::parseFailed;
Jason M. Bills95820182019-04-22 16:25:34 -07001442 }
Jason M. Billscd225da2019-05-08 15:31:57 -07001443 std::string timestamp = logEntry.substr(0, space);
1444 // Then get the log contents
Ed Tanousf23b7292020-10-15 09:41:17 -07001445 size_t entryStart = logEntry.find_first_not_of(' ', space);
Jason M. Billscd225da2019-05-08 15:31:57 -07001446 if (entryStart == std::string::npos)
1447 {
Jason M. Billsac992cd2022-06-24 13:31:46 -07001448 return LogParseError::parseFailed;
Jason M. Billscd225da2019-05-08 15:31:57 -07001449 }
1450 std::string_view entry(logEntry);
1451 entry.remove_prefix(entryStart);
1452 // Use split to separate the entry into its fields
1453 std::vector<std::string> logEntryFields;
Ed Tanous50ebd4a2023-01-19 19:03:17 -08001454 bmcweb::split(logEntryFields, entry, ',');
Jason M. Billscd225da2019-05-08 15:31:57 -07001455 // We need at least a MessageId to be valid
Ed Tanous1e6deaf2022-02-17 11:32:37 -08001456 auto logEntryIter = logEntryFields.begin();
1457 if (logEntryIter == logEntryFields.end())
Jason M. Billscd225da2019-05-08 15:31:57 -07001458 {
Jason M. Billsac992cd2022-06-24 13:31:46 -07001459 return LogParseError::parseFailed;
Jason M. Billscd225da2019-05-08 15:31:57 -07001460 }
Ed Tanous1e6deaf2022-02-17 11:32:37 -08001461 std::string& messageID = *logEntryIter;
Jason M. Bills4851d452019-03-28 11:27:48 -07001462 // Get the Message from the MessageRegistry
Ed Tanousfffb8c12022-02-07 23:53:03 -08001463 const registries::Message* message = registries::getMessage(messageID);
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001464
Ed Tanous1e6deaf2022-02-17 11:32:37 -08001465 logEntryIter++;
Sui Chen54417b02022-03-24 14:59:52 -07001466 if (message == nullptr)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001467 {
Ed Tanous62598e32023-07-17 17:06:25 -07001468 BMCWEB_LOG_WARNING("Log entry not found in registry: {}", logEntry);
Jason M. Billsac992cd2022-06-24 13:31:46 -07001469 return LogParseError::messageIdNotInRegistry;
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001470 }
1471
Ed Tanous1e6deaf2022-02-17 11:32:37 -08001472 std::vector<std::string_view> messageArgs(logEntryIter,
1473 logEntryFields.end());
Ed Tanousc05bba42023-06-28 08:33:29 -07001474 messageArgs.resize(message->numberOfArgs);
1475
Ed Tanous1e6deaf2022-02-17 11:32:37 -08001476 std::string msg = redfish::registries::fillMessageArgs(messageArgs,
1477 message->message);
1478 if (msg.empty())
Jason M. Bills4851d452019-03-28 11:27:48 -07001479 {
Ed Tanous1e6deaf2022-02-17 11:32:37 -08001480 return LogParseError::parseFailed;
Jason M. Bills4851d452019-03-28 11:27:48 -07001481 }
1482
Jason M. Bills95820182019-04-22 16:25:34 -07001483 // Get the Created time from the timestamp. The log timestamp is in RFC3339
1484 // format which matches the Redfish format except for the fractional seconds
1485 // between the '.' and the '+', so just remove them.
Ed Tanousf23b7292020-10-15 09:41:17 -07001486 std::size_t dot = timestamp.find_first_of('.');
1487 std::size_t plus = timestamp.find_first_of('+');
Jason M. Bills95820182019-04-22 16:25:34 -07001488 if (dot != std::string::npos && plus != std::string::npos)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001489 {
Jason M. Bills95820182019-04-22 16:25:34 -07001490 timestamp.erase(dot, plus - dot);
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001491 }
1492
1493 // Fill in the log entry with the gathered data
Vijay Lobo9c11a172021-10-07 16:53:16 -05001494 logEntryJson["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
Ed Tanousef4c65b2023-04-24 15:28:50 -07001495 logEntryJson["@odata.id"] = boost::urls::format(
1496 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/{}",
1497 logEntryID);
Jason M. Bills84afc482022-06-24 12:38:23 -07001498 logEntryJson["Name"] = "System Event Log Entry";
1499 logEntryJson["Id"] = logEntryID;
1500 logEntryJson["Message"] = std::move(msg);
1501 logEntryJson["MessageId"] = std::move(messageID);
1502 logEntryJson["MessageArgs"] = messageArgs;
1503 logEntryJson["EntryType"] = "Event";
1504 logEntryJson["Severity"] = message->messageSeverity;
1505 logEntryJson["Created"] = std::move(timestamp);
Jason M. Billsac992cd2022-06-24 13:31:46 -07001506 return LogParseError::success;
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001507}
1508
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001509inline void requestRoutesJournalEventLogEntryCollection(App& app)
Jason M. Billsc4bf6372018-11-05 13:48:27 -08001510{
Ed Tanous22d268c2022-05-19 09:39:07 -07001511 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/")
Gunnar Mills8b6a35f2021-07-30 14:52:53 -05001512 .privileges(redfish::privileges::getLogEntryCollection)
Ed Tanous002d39b2022-05-31 08:59:27 -07001513 .methods(boost::beast::http::verb::get)(
1514 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07001515 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1516 const std::string& systemName) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001517 query_param::QueryCapabilities capabilities = {
1518 .canDelegateTop = true,
1519 .canDelegateSkip = true,
1520 };
1521 query_param::Query delegatedQuery;
1522 if (!redfish::setUpRedfishRouteWithDelegation(
Carson Labrado3ba00072022-06-06 19:40:56 +00001523 app, req, asyncResp, delegatedQuery, capabilities))
Ed Tanous002d39b2022-05-31 08:59:27 -07001524 {
1525 return;
1526 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08001527 if constexpr (bmcwebEnableMultiHost)
1528 {
1529 // Option currently returns no systems. TBD
1530 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1531 systemName);
1532 return;
1533 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001534 if (systemName != "system")
1535 {
1536 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1537 systemName);
1538 return;
1539 }
1540
Jiaqing Zhao5143f7a2022-07-22 09:33:33 +08001541 size_t top = delegatedQuery.top.value_or(query_param::Query::maxTop);
Ed Tanous3648c8b2022-07-25 13:39:59 -07001542 size_t skip = delegatedQuery.skip.value_or(0);
1543
Ed Tanous002d39b2022-05-31 08:59:27 -07001544 // Collections don't include the static data added by SubRoute
1545 // because it has a duplicate entry for members
1546 asyncResp->res.jsonValue["@odata.type"] =
1547 "#LogEntryCollection.LogEntryCollection";
1548 asyncResp->res.jsonValue["@odata.id"] =
1549 "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
1550 asyncResp->res.jsonValue["Name"] = "System Event Log Entries";
1551 asyncResp->res.jsonValue["Description"] =
1552 "Collection of System Event Log Entries";
1553
1554 nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
1555 logEntryArray = nlohmann::json::array();
1556 // Go through the log files and create a unique ID for each
1557 // entry
1558 std::vector<std::filesystem::path> redfishLogFiles;
1559 getRedfishLogFiles(redfishLogFiles);
1560 uint64_t entryCount = 0;
1561 std::string logEntry;
1562
1563 // Oldest logs are in the last file, so start there and loop
1564 // backwards
1565 for (auto it = redfishLogFiles.rbegin(); it < redfishLogFiles.rend();
1566 it++)
1567 {
1568 std::ifstream logStream(*it);
1569 if (!logStream.is_open())
Jason M. Bills4978b632022-02-22 14:17:43 -08001570 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001571 continue;
Jason M. Bills4978b632022-02-22 14:17:43 -08001572 }
Jason M. Bills897967d2019-07-29 17:05:30 -07001573
Ed Tanous002d39b2022-05-31 08:59:27 -07001574 // Reset the unique ID on the first entry
1575 bool firstEntry = true;
1576 while (std::getline(logStream, logEntry))
Jason M. Bills4978b632022-02-22 14:17:43 -08001577 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001578 std::string idStr;
1579 if (!getUniqueEntryID(logEntry, idStr, firstEntry))
Jason M. Bills4978b632022-02-22 14:17:43 -08001580 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001581 continue;
1582 }
Jason M. Billsefde4ec2022-06-24 08:59:52 -07001583 firstEntry = false;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001584
Jason M. Billsde703c52022-06-23 14:19:04 -07001585 nlohmann::json::object_t bmcLogEntry;
Patrick Williams89492a12023-05-10 07:51:34 -05001586 LogParseError status = fillEventLogEntryJson(idStr, logEntry,
1587 bmcLogEntry);
Jason M. Billsac992cd2022-06-24 13:31:46 -07001588 if (status == LogParseError::messageIdNotInRegistry)
1589 {
1590 continue;
1591 }
1592 if (status != LogParseError::success)
Ed Tanous002d39b2022-05-31 08:59:27 -07001593 {
1594 messages::internalError(asyncResp->res);
1595 return;
Andrew Geisslercb92c032018-08-17 07:56:14 -07001596 }
Jason M. Billsde703c52022-06-23 14:19:04 -07001597
Jason M. Billsde703c52022-06-23 14:19:04 -07001598 entryCount++;
1599 // Handle paging using skip (number of entries to skip from the
1600 // start) and top (number of entries to display)
Ed Tanous3648c8b2022-07-25 13:39:59 -07001601 if (entryCount <= skip || entryCount > skip + top)
Jason M. Billsde703c52022-06-23 14:19:04 -07001602 {
1603 continue;
1604 }
1605
Patrick Williamsb2ba3072023-05-12 10:27:39 -05001606 logEntryArray.emplace_back(std::move(bmcLogEntry));
Jason M. Bills4978b632022-02-22 14:17:43 -08001607 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001608 }
1609 asyncResp->res.jsonValue["Members@odata.count"] = entryCount;
Ed Tanous3648c8b2022-07-25 13:39:59 -07001610 if (skip + top < entryCount)
Ed Tanous002d39b2022-05-31 08:59:27 -07001611 {
1612 asyncResp->res.jsonValue["Members@odata.nextLink"] =
1613 "/redfish/v1/Systems/system/LogServices/EventLog/Entries?$skip=" +
Ed Tanous3648c8b2022-07-25 13:39:59 -07001614 std::to_string(skip + top);
Ed Tanous002d39b2022-05-31 08:59:27 -07001615 }
Patrick Williams5a39f772023-10-20 11:20:21 -05001616 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001617}
Chicago Duan336e96c2019-07-15 14:22:08 +08001618
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001619inline void requestRoutesJournalEventLogEntry(App& app)
1620{
1621 BMCWEB_ROUTE(
Ed Tanous22d268c2022-05-19 09:39:07 -07001622 app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001623 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001624 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07001625 [&app](const crow::Request& req,
1626 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07001627 const std::string& systemName, const std::string& param) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001628 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001629 {
1630 return;
1631 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08001632 if constexpr (bmcwebEnableMultiHost)
1633 {
1634 // Option currently returns no systems. TBD
1635 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1636 systemName);
1637 return;
1638 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001639
1640 if (systemName != "system")
1641 {
1642 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1643 systemName);
1644 return;
1645 }
1646
Ed Tanous002d39b2022-05-31 08:59:27 -07001647 const std::string& targetID = param;
1648
1649 // Go through the log files and check the unique ID for each
1650 // entry to find the target entry
1651 std::vector<std::filesystem::path> redfishLogFiles;
1652 getRedfishLogFiles(redfishLogFiles);
1653 std::string logEntry;
1654
1655 // Oldest logs are in the last file, so start there and loop
1656 // backwards
1657 for (auto it = redfishLogFiles.rbegin(); it < redfishLogFiles.rend();
1658 it++)
1659 {
1660 std::ifstream logStream(*it);
1661 if (!logStream.is_open())
1662 {
1663 continue;
1664 }
1665
1666 // Reset the unique ID on the first entry
1667 bool firstEntry = true;
1668 while (std::getline(logStream, logEntry))
1669 {
1670 std::string idStr;
1671 if (!getUniqueEntryID(logEntry, idStr, firstEntry))
Ed Tanous45ca1b82022-03-25 13:07:27 -07001672 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001673 continue;
1674 }
Jason M. Billsefde4ec2022-06-24 08:59:52 -07001675 firstEntry = false;
Ed Tanous002d39b2022-05-31 08:59:27 -07001676
1677 if (idStr == targetID)
1678 {
Jason M. Billsde703c52022-06-23 14:19:04 -07001679 nlohmann::json::object_t bmcLogEntry;
Jason M. Billsac992cd2022-06-24 13:31:46 -07001680 LogParseError status =
1681 fillEventLogEntryJson(idStr, logEntry, bmcLogEntry);
1682 if (status != LogParseError::success)
Ed Tanous002d39b2022-05-31 08:59:27 -07001683 {
1684 messages::internalError(asyncResp->res);
1685 return;
1686 }
Jason M. Billsd405bb52022-06-24 10:52:05 -07001687 asyncResp->res.jsonValue.update(bmcLogEntry);
Ed Tanous45ca1b82022-03-25 13:07:27 -07001688 return;
1689 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001690 }
1691 }
1692 // Requested ID was not found
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08001693 messages::resourceNotFound(asyncResp->res, "LogEntry", targetID);
Patrick Williams5a39f772023-10-20 11:20:21 -05001694 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001695}
1696
1697inline void requestRoutesDBusEventLogEntryCollection(App& app)
1698{
Ed Tanous22d268c2022-05-19 09:39:07 -07001699 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07001700 .privileges(redfish::privileges::getLogEntryCollection)
Ed Tanous002d39b2022-05-31 08:59:27 -07001701 .methods(boost::beast::http::verb::get)(
1702 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07001703 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1704 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001705 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001706 {
1707 return;
1708 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08001709 if constexpr (bmcwebEnableMultiHost)
1710 {
1711 // Option currently returns no systems. TBD
1712 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1713 systemName);
1714 return;
1715 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001716 if (systemName != "system")
1717 {
1718 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1719 systemName);
1720 return;
1721 }
1722
Ed Tanous002d39b2022-05-31 08:59:27 -07001723 // Collections don't include the static data added by SubRoute
1724 // because it has a duplicate entry for members
1725 asyncResp->res.jsonValue["@odata.type"] =
1726 "#LogEntryCollection.LogEntryCollection";
1727 asyncResp->res.jsonValue["@odata.id"] =
1728 "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
1729 asyncResp->res.jsonValue["Name"] = "System Event Log Entries";
1730 asyncResp->res.jsonValue["Description"] =
1731 "Collection of System Event Log Entries";
1732
1733 // DBus implementation of EventLog/Entries
1734 // Make call to Logging Service to find all log entry objects
George Liu5eb468d2023-06-20 17:03:24 +08001735 sdbusplus::message::object_path path("/xyz/openbmc_project/logging");
1736 dbus::utility::getManagedObjects(
1737 "xyz.openbmc_project.Logging", path,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001738 [asyncResp](const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07001739 const dbus::utility::ManagedObjectType& resp) {
1740 if (ec)
Ed Tanous45ca1b82022-03-25 13:07:27 -07001741 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001742 // TODO Handle for specific error code
Ed Tanous62598e32023-07-17 17:06:25 -07001743 BMCWEB_LOG_ERROR(
1744 "getLogEntriesIfaceData resp_handler got error {}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -07001745 messages::internalError(asyncResp->res);
Ed Tanous45ca1b82022-03-25 13:07:27 -07001746 return;
1747 }
Ed Tanous3544d2a2023-08-06 18:12:20 -07001748 nlohmann::json::array_t entriesArray;
Ed Tanous002d39b2022-05-31 08:59:27 -07001749 for (const auto& objectPath : resp)
1750 {
1751 const uint32_t* id = nullptr;
1752 const uint64_t* timestamp = nullptr;
1753 const uint64_t* updateTimestamp = nullptr;
1754 const std::string* severity = nullptr;
1755 const std::string* message = nullptr;
1756 const std::string* filePath = nullptr;
Vijay Lobo9c11a172021-10-07 16:53:16 -05001757 const std::string* resolution = nullptr;
Ed Tanous002d39b2022-05-31 08:59:27 -07001758 bool resolved = false;
Abhishek Patel9017faf2021-09-14 22:48:55 -05001759 const std::string* notify = nullptr;
1760
Ed Tanous002d39b2022-05-31 08:59:27 -07001761 for (const auto& interfaceMap : objectPath.second)
1762 {
1763 if (interfaceMap.first ==
1764 "xyz.openbmc_project.Logging.Entry")
Xiaochao Ma75710de2021-01-21 17:56:02 +08001765 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001766 for (const auto& propertyMap : interfaceMap.second)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001767 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001768 if (propertyMap.first == "Id")
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001769 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001770 id = std::get_if<uint32_t>(&propertyMap.second);
1771 }
1772 else if (propertyMap.first == "Timestamp")
1773 {
1774 timestamp =
1775 std::get_if<uint64_t>(&propertyMap.second);
1776 }
1777 else if (propertyMap.first == "UpdateTimestamp")
1778 {
1779 updateTimestamp =
1780 std::get_if<uint64_t>(&propertyMap.second);
1781 }
1782 else if (propertyMap.first == "Severity")
1783 {
1784 severity = std::get_if<std::string>(
1785 &propertyMap.second);
1786 }
Vijay Lobo9c11a172021-10-07 16:53:16 -05001787 else if (propertyMap.first == "Resolution")
1788 {
1789 resolution = std::get_if<std::string>(
1790 &propertyMap.second);
1791 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001792 else if (propertyMap.first == "Message")
1793 {
1794 message = std::get_if<std::string>(
1795 &propertyMap.second);
1796 }
1797 else if (propertyMap.first == "Resolved")
1798 {
1799 const bool* resolveptr =
1800 std::get_if<bool>(&propertyMap.second);
1801 if (resolveptr == nullptr)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001802 {
1803 messages::internalError(asyncResp->res);
1804 return;
1805 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001806 resolved = *resolveptr;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001807 }
Abhishek Patel9017faf2021-09-14 22:48:55 -05001808 else if (propertyMap.first ==
1809 "ServiceProviderNotify")
1810 {
1811 notify = std::get_if<std::string>(
1812 &propertyMap.second);
1813 if (notify == nullptr)
1814 {
1815 messages::internalError(asyncResp->res);
1816 return;
1817 }
1818 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001819 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001820 if (id == nullptr || message == nullptr ||
Ed Tanous002d39b2022-05-31 08:59:27 -07001821 severity == nullptr)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001822 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001823 messages::internalError(asyncResp->res);
1824 return;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001825 }
1826 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001827 else if (interfaceMap.first ==
1828 "xyz.openbmc_project.Common.FilePath")
1829 {
1830 for (const auto& propertyMap : interfaceMap.second)
1831 {
1832 if (propertyMap.first == "Path")
1833 {
1834 filePath = std::get_if<std::string>(
1835 &propertyMap.second);
1836 }
1837 }
1838 }
1839 }
1840 // Object path without the
1841 // xyz.openbmc_project.Logging.Entry interface, ignore
1842 // and continue.
1843 if (id == nullptr || message == nullptr ||
1844 severity == nullptr || timestamp == nullptr ||
1845 updateTimestamp == nullptr)
1846 {
1847 continue;
1848 }
Ed Tanous3544d2a2023-08-06 18:12:20 -07001849 nlohmann::json& thisEntry = entriesArray.emplace_back();
Vijay Lobo9c11a172021-10-07 16:53:16 -05001850 thisEntry["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
Ed Tanousef4c65b2023-04-24 15:28:50 -07001851 thisEntry["@odata.id"] = boost::urls::format(
1852 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/{}",
1853 std::to_string(*id));
Ed Tanous002d39b2022-05-31 08:59:27 -07001854 thisEntry["Name"] = "System Event Log Entry";
1855 thisEntry["Id"] = std::to_string(*id);
1856 thisEntry["Message"] = *message;
1857 thisEntry["Resolved"] = resolved;
Vijay Lobo9c11a172021-10-07 16:53:16 -05001858 if ((resolution != nullptr) && (!(*resolution).empty()))
1859 {
1860 thisEntry["Resolution"] = *resolution;
1861 }
Abhishek Patel9017faf2021-09-14 22:48:55 -05001862 std::optional<bool> notifyAction =
1863 getProviderNotifyAction(*notify);
1864 if (notifyAction)
1865 {
1866 thisEntry["ServiceProviderNotified"] = *notifyAction;
1867 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001868 thisEntry["EntryType"] = "Event";
1869 thisEntry["Severity"] =
1870 translateSeverityDbusToRedfish(*severity);
1871 thisEntry["Created"] =
Ed Tanous2b829372022-08-03 14:22:34 -07001872 redfish::time_utils::getDateTimeUintMs(*timestamp);
Ed Tanous002d39b2022-05-31 08:59:27 -07001873 thisEntry["Modified"] =
Ed Tanous2b829372022-08-03 14:22:34 -07001874 redfish::time_utils::getDateTimeUintMs(*updateTimestamp);
Ed Tanous002d39b2022-05-31 08:59:27 -07001875 if (filePath != nullptr)
1876 {
1877 thisEntry["AdditionalDataURI"] =
1878 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
1879 std::to_string(*id) + "/attachment";
1880 }
1881 }
Ed Tanous3544d2a2023-08-06 18:12:20 -07001882 std::ranges::sort(entriesArray, [](const nlohmann::json& left,
1883 const nlohmann::json& right) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001884 return (left["Id"] <= right["Id"]);
Ed Tanous3544d2a2023-08-06 18:12:20 -07001885 });
Ed Tanous002d39b2022-05-31 08:59:27 -07001886 asyncResp->res.jsonValue["Members@odata.count"] =
1887 entriesArray.size();
Ed Tanous3544d2a2023-08-06 18:12:20 -07001888 asyncResp->res.jsonValue["Members"] = std::move(entriesArray);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001889 });
Patrick Williams5a39f772023-10-20 11:20:21 -05001890 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001891}
Xiaochao Ma75710de2021-01-21 17:56:02 +08001892
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001893inline void requestRoutesDBusEventLogEntry(App& app)
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001894{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07001895 BMCWEB_ROUTE(
Ed Tanous22d268c2022-05-19 09:39:07 -07001896 app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001897 .privileges(redfish::privileges::getLogEntry)
Ed Tanous002d39b2022-05-31 08:59:27 -07001898 .methods(boost::beast::http::verb::get)(
1899 [&app](const crow::Request& req,
1900 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07001901 const std::string& systemName, const std::string& param) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001902 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001903 {
1904 return;
1905 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08001906 if constexpr (bmcwebEnableMultiHost)
1907 {
1908 // Option currently returns no systems. TBD
1909 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1910 systemName);
1911 return;
1912 }
Ed Tanous22d268c2022-05-19 09:39:07 -07001913 if (systemName != "system")
1914 {
1915 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1916 systemName);
1917 return;
1918 }
1919
Ed Tanous002d39b2022-05-31 08:59:27 -07001920 std::string entryID = param;
1921 dbus::utility::escapePathForDbus(entryID);
1922
1923 // DBus implementation of EventLog/Entries
1924 // Make call to Logging Service to find all log entry objects
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001925 sdbusplus::asio::getAllProperties(
1926 *crow::connections::systemBus, "xyz.openbmc_project.Logging",
1927 "/xyz/openbmc_project/logging/entry/" + entryID, "",
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001928 [asyncResp, entryID](const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07001929 const dbus::utility::DBusPropertiesMap& resp) {
1930 if (ec.value() == EBADR)
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001931 {
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001932 messages::resourceNotFound(asyncResp->res, "EventLogEntry",
1933 entryID);
Ed Tanous45ca1b82022-03-25 13:07:27 -07001934 return;
1935 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001936 if (ec)
1937 {
Ed Tanous62598e32023-07-17 17:06:25 -07001938 BMCWEB_LOG_ERROR(
1939 "EventLogEntry (DBus) resp_handler got error {}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -07001940 messages::internalError(asyncResp->res);
1941 return;
1942 }
1943 const uint32_t* id = nullptr;
1944 const uint64_t* timestamp = nullptr;
1945 const uint64_t* updateTimestamp = nullptr;
1946 const std::string* severity = nullptr;
1947 const std::string* message = nullptr;
1948 const std::string* filePath = nullptr;
Vijay Lobo9c11a172021-10-07 16:53:16 -05001949 const std::string* resolution = nullptr;
Ed Tanous002d39b2022-05-31 08:59:27 -07001950 bool resolved = false;
Abhishek Patel9017faf2021-09-14 22:48:55 -05001951 const std::string* notify = nullptr;
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06001952
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001953 const bool success = sdbusplus::unpackPropertiesNoThrow(
1954 dbus_utils::UnpackErrorPrinter(), resp, "Id", id, "Timestamp",
1955 timestamp, "UpdateTimestamp", updateTimestamp, "Severity",
Vijay Lobo9c11a172021-10-07 16:53:16 -05001956 severity, "Message", message, "Resolved", resolved,
Abhishek Patel9017faf2021-09-14 22:48:55 -05001957 "Resolution", resolution, "Path", filePath,
1958 "ServiceProviderNotify", notify);
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001959
1960 if (!success)
Ed Tanous002d39b2022-05-31 08:59:27 -07001961 {
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001962 messages::internalError(asyncResp->res);
1963 return;
Ed Tanous002d39b2022-05-31 08:59:27 -07001964 }
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02001965
Ed Tanous002d39b2022-05-31 08:59:27 -07001966 if (id == nullptr || message == nullptr || severity == nullptr ||
Abhishek Patel9017faf2021-09-14 22:48:55 -05001967 timestamp == nullptr || updateTimestamp == nullptr ||
1968 notify == nullptr)
Ed Tanous002d39b2022-05-31 08:59:27 -07001969 {
1970 messages::internalError(asyncResp->res);
1971 return;
1972 }
Abhishek Patel9017faf2021-09-14 22:48:55 -05001973
Ed Tanous002d39b2022-05-31 08:59:27 -07001974 asyncResp->res.jsonValue["@odata.type"] =
Vijay Lobo9c11a172021-10-07 16:53:16 -05001975 "#LogEntry.v1_9_0.LogEntry";
Ed Tanousef4c65b2023-04-24 15:28:50 -07001976 asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
1977 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/{}",
1978 std::to_string(*id));
Ed Tanous002d39b2022-05-31 08:59:27 -07001979 asyncResp->res.jsonValue["Name"] = "System Event Log Entry";
1980 asyncResp->res.jsonValue["Id"] = std::to_string(*id);
1981 asyncResp->res.jsonValue["Message"] = *message;
1982 asyncResp->res.jsonValue["Resolved"] = resolved;
Abhishek Patel9017faf2021-09-14 22:48:55 -05001983 std::optional<bool> notifyAction = getProviderNotifyAction(*notify);
1984 if (notifyAction)
1985 {
1986 asyncResp->res.jsonValue["ServiceProviderNotified"] =
1987 *notifyAction;
1988 }
Vijay Lobo9c11a172021-10-07 16:53:16 -05001989 if ((resolution != nullptr) && (!(*resolution).empty()))
1990 {
1991 asyncResp->res.jsonValue["Resolution"] = *resolution;
1992 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001993 asyncResp->res.jsonValue["EntryType"] = "Event";
1994 asyncResp->res.jsonValue["Severity"] =
1995 translateSeverityDbusToRedfish(*severity);
1996 asyncResp->res.jsonValue["Created"] =
Ed Tanous2b829372022-08-03 14:22:34 -07001997 redfish::time_utils::getDateTimeUintMs(*timestamp);
Ed Tanous002d39b2022-05-31 08:59:27 -07001998 asyncResp->res.jsonValue["Modified"] =
Ed Tanous2b829372022-08-03 14:22:34 -07001999 redfish::time_utils::getDateTimeUintMs(*updateTimestamp);
Ed Tanous002d39b2022-05-31 08:59:27 -07002000 if (filePath != nullptr)
2001 {
2002 asyncResp->res.jsonValue["AdditionalDataURI"] =
2003 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
2004 std::to_string(*id) + "/attachment";
2005 }
Ed Tanous45ca1b82022-03-25 13:07:27 -07002006 });
Patrick Williams5a39f772023-10-20 11:20:21 -05002007 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002008
2009 BMCWEB_ROUTE(
Ed Tanous22d268c2022-05-19 09:39:07 -07002010 app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002011 .privileges(redfish::privileges::patchLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002012 .methods(boost::beast::http::verb::patch)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002013 [&app](const crow::Request& req,
2014 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07002015 const std::string& systemName, const std::string& entryId) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002016 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002017 {
2018 return;
2019 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08002020 if constexpr (bmcwebEnableMultiHost)
2021 {
2022 // Option currently returns no systems. TBD
2023 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
2024 systemName);
2025 return;
2026 }
Ed Tanous22d268c2022-05-19 09:39:07 -07002027 if (systemName != "system")
2028 {
2029 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
2030 systemName);
2031 return;
2032 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002033 std::optional<bool> resolved;
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002034
Ed Tanous002d39b2022-05-31 08:59:27 -07002035 if (!json_util::readJsonPatch(req, asyncResp->res, "Resolved",
2036 resolved))
2037 {
2038 return;
2039 }
Ed Tanous62598e32023-07-17 17:06:25 -07002040 BMCWEB_LOG_DEBUG("Set Resolved");
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06002041
George Liu9ae226f2023-06-21 17:56:46 +08002042 sdbusplus::asio::setProperty(
2043 *crow::connections::systemBus, "xyz.openbmc_project.Logging",
2044 "/xyz/openbmc_project/logging/entry/" + entryId,
2045 "xyz.openbmc_project.Logging.Entry", "Resolved", *resolved,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08002046 [asyncResp, entryId](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002047 if (ec)
2048 {
Ed Tanous62598e32023-07-17 17:06:25 -07002049 BMCWEB_LOG_DEBUG("DBUS response error {}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -07002050 messages::internalError(asyncResp->res);
2051 return;
2052 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002053 });
Patrick Williams5a39f772023-10-20 11:20:21 -05002054 });
Adriana Kobylak400fd1f2021-01-29 09:01:30 -06002055
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002056 BMCWEB_ROUTE(
Ed Tanous22d268c2022-05-19 09:39:07 -07002057 app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002058 .privileges(redfish::privileges::deleteLogEntry)
2059
Ed Tanous002d39b2022-05-31 08:59:27 -07002060 .methods(boost::beast::http::verb::delete_)(
2061 [&app](const crow::Request& req,
2062 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07002063 const std::string& systemName, const std::string& param) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002064 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002065 {
2066 return;
2067 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08002068 if constexpr (bmcwebEnableMultiHost)
2069 {
2070 // Option currently returns no systems. TBD
2071 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
2072 systemName);
2073 return;
2074 }
Ed Tanous22d268c2022-05-19 09:39:07 -07002075 if (systemName != "system")
2076 {
2077 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
2078 systemName);
2079 return;
2080 }
Ed Tanous62598e32023-07-17 17:06:25 -07002081 BMCWEB_LOG_DEBUG("Do delete single event entries.");
Ed Tanous002d39b2022-05-31 08:59:27 -07002082
2083 std::string entryID = param;
2084
2085 dbus::utility::escapePathForDbus(entryID);
2086
2087 // Process response from Logging service.
Patrick Williams5a39f772023-10-20 11:20:21 -05002088 auto respHandler = [asyncResp,
2089 entryID](const boost::system::error_code& ec) {
Ed Tanous62598e32023-07-17 17:06:25 -07002090 BMCWEB_LOG_DEBUG("EventLogEntry (DBus) doDelete callback: Done");
Ed Tanous002d39b2022-05-31 08:59:27 -07002091 if (ec)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002092 {
Ed Tanous002d39b2022-05-31 08:59:27 -07002093 if (ec.value() == EBADR)
Ed Tanous45ca1b82022-03-25 13:07:27 -07002094 {
Ed Tanous002d39b2022-05-31 08:59:27 -07002095 messages::resourceNotFound(asyncResp->res, "LogEntry",
2096 entryID);
Ed Tanous45ca1b82022-03-25 13:07:27 -07002097 return;
2098 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002099 // TODO Handle for specific error code
Ed Tanous62598e32023-07-17 17:06:25 -07002100 BMCWEB_LOG_ERROR(
2101 "EventLogEntry (DBus) doDelete respHandler got error {}",
2102 ec);
Ed Tanous002d39b2022-05-31 08:59:27 -07002103 asyncResp->res.result(
2104 boost::beast::http::status::internal_server_error);
2105 return;
2106 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002107
Ed Tanous002d39b2022-05-31 08:59:27 -07002108 asyncResp->res.result(boost::beast::http::status::ok);
2109 };
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002110
Ed Tanous002d39b2022-05-31 08:59:27 -07002111 // Make call to Logging service to request Delete Log
2112 crow::connections::systemBus->async_method_call(
2113 respHandler, "xyz.openbmc_project.Logging",
2114 "/xyz/openbmc_project/logging/entry/" + entryID,
2115 "xyz.openbmc_project.Object.Delete", "Delete");
Patrick Williams5a39f772023-10-20 11:20:21 -05002116 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002117}
2118
Spencer Kub7028eb2021-10-26 15:27:35 +08002119constexpr const char* hostLoggerFolderPath = "/var/log/console";
2120
2121inline bool
2122 getHostLoggerFiles(const std::string& hostLoggerFilePath,
2123 std::vector<std::filesystem::path>& hostLoggerFiles)
2124{
2125 std::error_code ec;
2126 std::filesystem::directory_iterator logPath(hostLoggerFilePath, ec);
2127 if (ec)
2128 {
Carson Labradobf2dded2023-08-10 00:37:06 +00002129 BMCWEB_LOG_WARNING("{}", ec.message());
Spencer Kub7028eb2021-10-26 15:27:35 +08002130 return false;
2131 }
2132 for (const std::filesystem::directory_entry& it : logPath)
2133 {
2134 std::string filename = it.path().filename();
2135 // Prefix of each log files is "log". Find the file and save the
2136 // path
Ed Tanous11ba3972022-07-11 09:50:41 -07002137 if (filename.starts_with("log"))
Spencer Kub7028eb2021-10-26 15:27:35 +08002138 {
2139 hostLoggerFiles.emplace_back(it.path());
2140 }
2141 }
2142 // As the log files rotate, they are appended with a ".#" that is higher for
2143 // the older logs. Since we start from oldest logs, sort the name in
2144 // descending order.
2145 std::sort(hostLoggerFiles.rbegin(), hostLoggerFiles.rend(),
2146 AlphanumLess<std::string>());
2147
2148 return true;
2149}
2150
Ed Tanous02cad962022-06-30 16:50:15 -07002151inline bool getHostLoggerEntries(
2152 const std::vector<std::filesystem::path>& hostLoggerFiles, uint64_t skip,
2153 uint64_t top, std::vector<std::string>& logEntries, size_t& logCount)
Spencer Kub7028eb2021-10-26 15:27:35 +08002154{
2155 GzFileReader logFile;
2156
2157 // Go though all log files and expose host logs.
2158 for (const std::filesystem::path& it : hostLoggerFiles)
2159 {
2160 if (!logFile.gzGetLines(it.string(), skip, top, logEntries, logCount))
2161 {
Ed Tanous62598e32023-07-17 17:06:25 -07002162 BMCWEB_LOG_ERROR("fail to expose host logs");
Spencer Kub7028eb2021-10-26 15:27:35 +08002163 return false;
2164 }
2165 }
2166 // Get lastMessage from constructor by getter
2167 std::string lastMessage = logFile.getLastMessage();
2168 if (!lastMessage.empty())
2169 {
2170 logCount++;
2171 if (logCount > skip && logCount <= (skip + top))
2172 {
2173 logEntries.push_back(lastMessage);
2174 }
2175 }
2176 return true;
2177}
2178
2179inline void fillHostLoggerEntryJson(const std::string& logEntryID,
2180 const std::string& msg,
Jason M. Bills6d6574c2022-06-28 12:30:16 -07002181 nlohmann::json::object_t& logEntryJson)
Spencer Kub7028eb2021-10-26 15:27:35 +08002182{
2183 // Fill in the log entry with the gathered data.
Vijay Lobo9c11a172021-10-07 16:53:16 -05002184 logEntryJson["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
Ed Tanousef4c65b2023-04-24 15:28:50 -07002185 logEntryJson["@odata.id"] = boost::urls::format(
2186 "/redfish/v1/Systems/system/LogServices/HostLogger/Entries/{}",
2187 logEntryID);
Jason M. Bills6d6574c2022-06-28 12:30:16 -07002188 logEntryJson["Name"] = "Host Logger Entry";
2189 logEntryJson["Id"] = logEntryID;
2190 logEntryJson["Message"] = msg;
2191 logEntryJson["EntryType"] = "Oem";
2192 logEntryJson["Severity"] = "OK";
2193 logEntryJson["OemRecordFormat"] = "Host Logger Entry";
Spencer Kub7028eb2021-10-26 15:27:35 +08002194}
2195
2196inline void requestRoutesSystemHostLogger(App& app)
2197{
Ed Tanous22d268c2022-05-19 09:39:07 -07002198 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/HostLogger/")
Spencer Kub7028eb2021-10-26 15:27:35 +08002199 .privileges(redfish::privileges::getLogService)
Ed Tanous14766872022-03-15 10:44:42 -07002200 .methods(boost::beast::http::verb::get)(
2201 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07002202 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2203 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002204 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002205 {
2206 return;
2207 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08002208 if constexpr (bmcwebEnableMultiHost)
2209 {
2210 // Option currently returns no systems. TBD
2211 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
2212 systemName);
2213 return;
2214 }
Ed Tanous22d268c2022-05-19 09:39:07 -07002215 if (systemName != "system")
2216 {
2217 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
2218 systemName);
2219 return;
2220 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002221 asyncResp->res.jsonValue["@odata.id"] =
2222 "/redfish/v1/Systems/system/LogServices/HostLogger";
2223 asyncResp->res.jsonValue["@odata.type"] =
Janet Adkinsb25644a2023-08-16 11:23:45 -05002224 "#LogService.v1_2_0.LogService";
Ed Tanous002d39b2022-05-31 08:59:27 -07002225 asyncResp->res.jsonValue["Name"] = "Host Logger Service";
2226 asyncResp->res.jsonValue["Description"] = "Host Logger Service";
2227 asyncResp->res.jsonValue["Id"] = "HostLogger";
2228 asyncResp->res.jsonValue["Entries"]["@odata.id"] =
2229 "/redfish/v1/Systems/system/LogServices/HostLogger/Entries";
Patrick Williams5a39f772023-10-20 11:20:21 -05002230 });
Spencer Kub7028eb2021-10-26 15:27:35 +08002231}
2232
2233inline void requestRoutesSystemHostLoggerCollection(App& app)
2234{
2235 BMCWEB_ROUTE(app,
Ed Tanous22d268c2022-05-19 09:39:07 -07002236 "/redfish/v1/Systems/<str>/LogServices/HostLogger/Entries/")
Spencer Kub7028eb2021-10-26 15:27:35 +08002237 .privileges(redfish::privileges::getLogEntry)
Ed Tanous002d39b2022-05-31 08:59:27 -07002238 .methods(boost::beast::http::verb::get)(
2239 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07002240 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2241 const std::string& systemName) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002242 query_param::QueryCapabilities capabilities = {
2243 .canDelegateTop = true,
2244 .canDelegateSkip = true,
2245 };
2246 query_param::Query delegatedQuery;
2247 if (!redfish::setUpRedfishRouteWithDelegation(
Carson Labrado3ba00072022-06-06 19:40:56 +00002248 app, req, asyncResp, delegatedQuery, capabilities))
Ed Tanous002d39b2022-05-31 08:59:27 -07002249 {
2250 return;
2251 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08002252 if constexpr (bmcwebEnableMultiHost)
2253 {
2254 // Option currently returns no systems. TBD
2255 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
2256 systemName);
2257 return;
2258 }
Ed Tanous22d268c2022-05-19 09:39:07 -07002259 if (systemName != "system")
2260 {
2261 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
2262 systemName);
2263 return;
2264 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002265 asyncResp->res.jsonValue["@odata.id"] =
2266 "/redfish/v1/Systems/system/LogServices/HostLogger/Entries";
2267 asyncResp->res.jsonValue["@odata.type"] =
2268 "#LogEntryCollection.LogEntryCollection";
2269 asyncResp->res.jsonValue["Name"] = "HostLogger Entries";
2270 asyncResp->res.jsonValue["Description"] =
2271 "Collection of HostLogger Entries";
2272 nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
2273 logEntryArray = nlohmann::json::array();
2274 asyncResp->res.jsonValue["Members@odata.count"] = 0;
Spencer Kub7028eb2021-10-26 15:27:35 +08002275
Ed Tanous002d39b2022-05-31 08:59:27 -07002276 std::vector<std::filesystem::path> hostLoggerFiles;
2277 if (!getHostLoggerFiles(hostLoggerFolderPath, hostLoggerFiles))
2278 {
Carson Labradobf2dded2023-08-10 00:37:06 +00002279 BMCWEB_LOG_DEBUG("Failed to get host log file path");
Ed Tanous002d39b2022-05-31 08:59:27 -07002280 return;
2281 }
Ed Tanous3648c8b2022-07-25 13:39:59 -07002282 // If we weren't provided top and skip limits, use the defaults.
2283 size_t skip = delegatedQuery.skip.value_or(0);
Jiaqing Zhao5143f7a2022-07-22 09:33:33 +08002284 size_t top = delegatedQuery.top.value_or(query_param::Query::maxTop);
Ed Tanous002d39b2022-05-31 08:59:27 -07002285 size_t logCount = 0;
2286 // This vector only store the entries we want to expose that
2287 // control by skip and top.
2288 std::vector<std::string> logEntries;
Ed Tanous3648c8b2022-07-25 13:39:59 -07002289 if (!getHostLoggerEntries(hostLoggerFiles, skip, top, logEntries,
2290 logCount))
Ed Tanous002d39b2022-05-31 08:59:27 -07002291 {
2292 messages::internalError(asyncResp->res);
2293 return;
2294 }
2295 // If vector is empty, that means skip value larger than total
2296 // log count
2297 if (logEntries.empty())
2298 {
2299 asyncResp->res.jsonValue["Members@odata.count"] = logCount;
2300 return;
2301 }
2302 if (!logEntries.empty())
2303 {
2304 for (size_t i = 0; i < logEntries.size(); i++)
George Liu0fda0f12021-11-16 10:06:17 +08002305 {
Jason M. Bills6d6574c2022-06-28 12:30:16 -07002306 nlohmann::json::object_t hostLogEntry;
Ed Tanous3648c8b2022-07-25 13:39:59 -07002307 fillHostLoggerEntryJson(std::to_string(skip + i), logEntries[i],
2308 hostLogEntry);
Patrick Williamsb2ba3072023-05-12 10:27:39 -05002309 logEntryArray.emplace_back(std::move(hostLogEntry));
George Liu0fda0f12021-11-16 10:06:17 +08002310 }
2311
Ed Tanous002d39b2022-05-31 08:59:27 -07002312 asyncResp->res.jsonValue["Members@odata.count"] = logCount;
Ed Tanous3648c8b2022-07-25 13:39:59 -07002313 if (skip + top < logCount)
George Liu0fda0f12021-11-16 10:06:17 +08002314 {
Ed Tanous002d39b2022-05-31 08:59:27 -07002315 asyncResp->res.jsonValue["Members@odata.nextLink"] =
2316 "/redfish/v1/Systems/system/LogServices/HostLogger/Entries?$skip=" +
Ed Tanous3648c8b2022-07-25 13:39:59 -07002317 std::to_string(skip + top);
George Liu0fda0f12021-11-16 10:06:17 +08002318 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002319 }
Patrick Williams5a39f772023-10-20 11:20:21 -05002320 });
Spencer Kub7028eb2021-10-26 15:27:35 +08002321}
2322
2323inline void requestRoutesSystemHostLoggerLogEntry(App& app)
2324{
2325 BMCWEB_ROUTE(
Ed Tanous22d268c2022-05-19 09:39:07 -07002326 app, "/redfish/v1/Systems/<str>/LogServices/HostLogger/Entries/<str>/")
Spencer Kub7028eb2021-10-26 15:27:35 +08002327 .privileges(redfish::privileges::getLogEntry)
2328 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002329 [&app](const crow::Request& req,
2330 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07002331 const std::string& systemName, const std::string& param) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002332 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002333 {
2334 return;
2335 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08002336 if constexpr (bmcwebEnableMultiHost)
2337 {
2338 // Option currently returns no systems. TBD
2339 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
2340 systemName);
2341 return;
2342 }
Ed Tanous22d268c2022-05-19 09:39:07 -07002343 if (systemName != "system")
2344 {
2345 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
2346 systemName);
2347 return;
2348 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002349 const std::string& targetID = param;
Spencer Kub7028eb2021-10-26 15:27:35 +08002350
Ed Tanous002d39b2022-05-31 08:59:27 -07002351 uint64_t idInt = 0;
Ed Tanousca45aa32022-01-07 09:28:45 -08002352
Patrick Williams84396af2023-05-11 11:47:45 -05002353 auto [ptr, ec] = std::from_chars(&*targetID.begin(), &*targetID.end(),
2354 idInt);
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08002355 if (ec == std::errc::invalid_argument ||
2356 ec == std::errc::result_out_of_range)
Ed Tanous002d39b2022-05-31 08:59:27 -07002357 {
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08002358 messages::resourceNotFound(asyncResp->res, "LogEntry", param);
Ed Tanous002d39b2022-05-31 08:59:27 -07002359 return;
2360 }
Spencer Kub7028eb2021-10-26 15:27:35 +08002361
Ed Tanous002d39b2022-05-31 08:59:27 -07002362 std::vector<std::filesystem::path> hostLoggerFiles;
2363 if (!getHostLoggerFiles(hostLoggerFolderPath, hostLoggerFiles))
2364 {
Carson Labradobf2dded2023-08-10 00:37:06 +00002365 BMCWEB_LOG_DEBUG("Failed to get host log file path");
Ed Tanous002d39b2022-05-31 08:59:27 -07002366 return;
2367 }
Spencer Kub7028eb2021-10-26 15:27:35 +08002368
Ed Tanous002d39b2022-05-31 08:59:27 -07002369 size_t logCount = 0;
Ed Tanous3648c8b2022-07-25 13:39:59 -07002370 size_t top = 1;
Ed Tanous002d39b2022-05-31 08:59:27 -07002371 std::vector<std::string> logEntries;
2372 // We can get specific entry by skip and top. For example, if we
2373 // want to get nth entry, we can set skip = n-1 and top = 1 to
2374 // get that entry
2375 if (!getHostLoggerEntries(hostLoggerFiles, idInt, top, logEntries,
2376 logCount))
2377 {
2378 messages::internalError(asyncResp->res);
2379 return;
2380 }
Spencer Kub7028eb2021-10-26 15:27:35 +08002381
Ed Tanous002d39b2022-05-31 08:59:27 -07002382 if (!logEntries.empty())
2383 {
Jason M. Bills6d6574c2022-06-28 12:30:16 -07002384 nlohmann::json::object_t hostLogEntry;
2385 fillHostLoggerEntryJson(targetID, logEntries[0], hostLogEntry);
2386 asyncResp->res.jsonValue.update(hostLogEntry);
Ed Tanous002d39b2022-05-31 08:59:27 -07002387 return;
2388 }
Spencer Kub7028eb2021-10-26 15:27:35 +08002389
Ed Tanous002d39b2022-05-31 08:59:27 -07002390 // Requested ID was not found
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08002391 messages::resourceNotFound(asyncResp->res, "LogEntry", param);
Patrick Williams5a39f772023-10-20 11:20:21 -05002392 });
Spencer Kub7028eb2021-10-26 15:27:35 +08002393}
2394
Claire Weinandd72e872022-08-15 14:20:06 -07002395inline void handleBMCLogServicesCollectionGet(
Claire Weinanfdd26902022-03-01 14:18:25 -08002396 crow::App& app, const crow::Request& req,
2397 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2398{
2399 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2400 {
2401 return;
2402 }
2403 // Collections don't include the static data added by SubRoute
2404 // because it has a duplicate entry for members
2405 asyncResp->res.jsonValue["@odata.type"] =
2406 "#LogServiceCollection.LogServiceCollection";
2407 asyncResp->res.jsonValue["@odata.id"] =
2408 "/redfish/v1/Managers/bmc/LogServices";
2409 asyncResp->res.jsonValue["Name"] = "Open BMC Log Services Collection";
2410 asyncResp->res.jsonValue["Description"] =
2411 "Collection of LogServices for this Manager";
2412 nlohmann::json& logServiceArray = asyncResp->res.jsonValue["Members"];
2413 logServiceArray = nlohmann::json::array();
2414
2415#ifdef BMCWEB_ENABLE_REDFISH_BMC_JOURNAL
Ed Tanous613dabe2022-07-09 11:17:36 -07002416 nlohmann::json::object_t journal;
2417 journal["@odata.id"] = "/redfish/v1/Managers/bmc/LogServices/Journal";
Patrick Williamsb2ba3072023-05-12 10:27:39 -05002418 logServiceArray.emplace_back(std::move(journal));
Claire Weinanfdd26902022-03-01 14:18:25 -08002419#endif
2420
2421 asyncResp->res.jsonValue["Members@odata.count"] = logServiceArray.size();
2422
2423#ifdef BMCWEB_ENABLE_REDFISH_DUMP_LOG
George Liu15912152023-01-11 10:18:18 +08002424 constexpr std::array<std::string_view, 1> interfaces = {
George Liu7a1dbc42022-12-07 16:03:22 +08002425 "xyz.openbmc_project.Collection.DeleteAll"};
2426 dbus::utility::getSubTreePaths(
2427 "/xyz/openbmc_project/dump", 0, interfaces,
Claire Weinanfdd26902022-03-01 14:18:25 -08002428 [asyncResp](
George Liu7a1dbc42022-12-07 16:03:22 +08002429 const boost::system::error_code& ec,
Claire Weinanfdd26902022-03-01 14:18:25 -08002430 const dbus::utility::MapperGetSubTreePathsResponse& subTreePaths) {
2431 if (ec)
2432 {
Ed Tanous62598e32023-07-17 17:06:25 -07002433 BMCWEB_LOG_ERROR(
2434 "handleBMCLogServicesCollectionGet respHandler got error {}",
2435 ec);
Claire Weinanfdd26902022-03-01 14:18:25 -08002436 // Assume that getting an error simply means there are no dump
2437 // LogServices. Return without adding any error response.
2438 return;
2439 }
2440
2441 nlohmann::json& logServiceArrayLocal =
2442 asyncResp->res.jsonValue["Members"];
2443
2444 for (const std::string& path : subTreePaths)
2445 {
2446 if (path == "/xyz/openbmc_project/dump/bmc")
2447 {
Ed Tanous613dabe2022-07-09 11:17:36 -07002448 nlohmann::json::object_t member;
2449 member["@odata.id"] =
2450 "/redfish/v1/Managers/bmc/LogServices/Dump";
Patrick Williamsb2ba3072023-05-12 10:27:39 -05002451 logServiceArrayLocal.emplace_back(std::move(member));
Claire Weinanfdd26902022-03-01 14:18:25 -08002452 }
2453 else if (path == "/xyz/openbmc_project/dump/faultlog")
2454 {
Ed Tanous613dabe2022-07-09 11:17:36 -07002455 nlohmann::json::object_t member;
2456 member["@odata.id"] =
2457 "/redfish/v1/Managers/bmc/LogServices/FaultLog";
Patrick Williamsb2ba3072023-05-12 10:27:39 -05002458 logServiceArrayLocal.emplace_back(std::move(member));
Claire Weinanfdd26902022-03-01 14:18:25 -08002459 }
2460 }
2461
2462 asyncResp->res.jsonValue["Members@odata.count"] =
2463 logServiceArrayLocal.size();
Patrick Williams5a39f772023-10-20 11:20:21 -05002464 });
Claire Weinanfdd26902022-03-01 14:18:25 -08002465#endif
2466}
2467
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002468inline void requestRoutesBMCLogServiceCollection(App& app)
2469{
2470 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/")
Gunnar Millsad89dcf2021-07-30 14:40:11 -05002471 .privileges(redfish::privileges::getLogServiceCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002472 .methods(boost::beast::http::verb::get)(
Claire Weinandd72e872022-08-15 14:20:06 -07002473 std::bind_front(handleBMCLogServicesCollectionGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002474}
Ed Tanous1da66f72018-07-27 16:13:37 -07002475
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002476inline void requestRoutesBMCJournalLogService(App& app)
Ed Tanous1da66f72018-07-27 16:13:37 -07002477{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002478 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Journal/")
Ed Tanoused398212021-06-09 17:05:54 -07002479 .privileges(redfish::privileges::getLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002480 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002481 [&app](const crow::Request& req,
2482 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002483 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002484 {
2485 return;
2486 }
2487 asyncResp->res.jsonValue["@odata.type"] =
Janet Adkinsb25644a2023-08-16 11:23:45 -05002488 "#LogService.v1_2_0.LogService";
Ed Tanous002d39b2022-05-31 08:59:27 -07002489 asyncResp->res.jsonValue["@odata.id"] =
2490 "/redfish/v1/Managers/bmc/LogServices/Journal";
2491 asyncResp->res.jsonValue["Name"] = "Open BMC Journal Log Service";
2492 asyncResp->res.jsonValue["Description"] = "BMC Journal Log Service";
Ed Tanoused34a4a2023-02-08 15:43:27 -08002493 asyncResp->res.jsonValue["Id"] = "Journal";
Ed Tanous002d39b2022-05-31 08:59:27 -07002494 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
Tejas Patil7c8c4052021-06-04 17:43:14 +05302495
Ed Tanous002d39b2022-05-31 08:59:27 -07002496 std::pair<std::string, std::string> redfishDateTimeOffset =
Ed Tanous2b829372022-08-03 14:22:34 -07002497 redfish::time_utils::getDateTimeOffsetNow();
Ed Tanous002d39b2022-05-31 08:59:27 -07002498 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
2499 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
2500 redfishDateTimeOffset.second;
Tejas Patil7c8c4052021-06-04 17:43:14 +05302501
Ed Tanous002d39b2022-05-31 08:59:27 -07002502 asyncResp->res.jsonValue["Entries"]["@odata.id"] =
2503 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries";
Patrick Williams5a39f772023-10-20 11:20:21 -05002504 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002505}
Jason M. Billse1f26342018-07-18 12:12:00 -07002506
Jason M. Bills3a48b3a2022-06-24 10:10:15 -07002507static int
2508 fillBMCJournalLogEntryJson(const std::string& bmcJournalLogEntryID,
2509 sd_journal* journal,
2510 nlohmann::json::object_t& bmcJournalLogEntryJson)
Jason M. Billse1f26342018-07-18 12:12:00 -07002511{
2512 // Get the Log Entry contents
2513 int ret = 0;
Jason M. Billse1f26342018-07-18 12:12:00 -07002514
Jason M. Billsa8fe54f2020-11-20 15:57:55 -08002515 std::string message;
2516 std::string_view syslogID;
2517 ret = getJournalMetadata(journal, "SYSLOG_IDENTIFIER", syslogID);
2518 if (ret < 0)
2519 {
Carson Labradobf2dded2023-08-10 00:37:06 +00002520 BMCWEB_LOG_DEBUG("Failed to read SYSLOG_IDENTIFIER field: {}",
Ed Tanous62598e32023-07-17 17:06:25 -07002521 strerror(-ret));
Jason M. Billsa8fe54f2020-11-20 15:57:55 -08002522 }
2523 if (!syslogID.empty())
2524 {
2525 message += std::string(syslogID) + ": ";
2526 }
2527
Ed Tanous39e77502019-03-04 17:35:53 -08002528 std::string_view msg;
Jason M. Bills16428a12018-11-02 12:42:29 -07002529 ret = getJournalMetadata(journal, "MESSAGE", msg);
Jason M. Billse1f26342018-07-18 12:12:00 -07002530 if (ret < 0)
2531 {
Ed Tanous62598e32023-07-17 17:06:25 -07002532 BMCWEB_LOG_ERROR("Failed to read MESSAGE field: {}", strerror(-ret));
Jason M. Billse1f26342018-07-18 12:12:00 -07002533 return 1;
2534 }
Jason M. Billsa8fe54f2020-11-20 15:57:55 -08002535 message += std::string(msg);
Jason M. Billse1f26342018-07-18 12:12:00 -07002536
2537 // Get the severity from the PRIORITY field
Ed Tanous271584a2019-07-09 16:24:22 -07002538 long int severity = 8; // Default to an invalid priority
Jason M. Bills16428a12018-11-02 12:42:29 -07002539 ret = getJournalMetadata(journal, "PRIORITY", 10, severity);
Jason M. Billse1f26342018-07-18 12:12:00 -07002540 if (ret < 0)
2541 {
Carson Labradobf2dded2023-08-10 00:37:06 +00002542 BMCWEB_LOG_DEBUG("Failed to read PRIORITY field: {}", strerror(-ret));
Jason M. Billse1f26342018-07-18 12:12:00 -07002543 }
Jason M. Billse1f26342018-07-18 12:12:00 -07002544
2545 // Get the Created time from the timestamp
Jason M. Bills16428a12018-11-02 12:42:29 -07002546 std::string entryTimeStr;
2547 if (!getEntryTimestamp(journal, entryTimeStr))
Jason M. Billse1f26342018-07-18 12:12:00 -07002548 {
Jason M. Bills16428a12018-11-02 12:42:29 -07002549 return 1;
Jason M. Billse1f26342018-07-18 12:12:00 -07002550 }
Jason M. Billse1f26342018-07-18 12:12:00 -07002551
2552 // Fill in the log entry with the gathered data
Vijay Lobo9c11a172021-10-07 16:53:16 -05002553 bmcJournalLogEntryJson["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
Ed Tanousef4c65b2023-04-24 15:28:50 -07002554 bmcJournalLogEntryJson["@odata.id"] = boost::urls::format(
2555 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/{}",
Willy Tueddfc432022-09-26 16:46:38 +00002556 bmcJournalLogEntryID);
Jason M. Bills84afc482022-06-24 12:38:23 -07002557 bmcJournalLogEntryJson["Name"] = "BMC Journal Entry";
2558 bmcJournalLogEntryJson["Id"] = bmcJournalLogEntryID;
2559 bmcJournalLogEntryJson["Message"] = std::move(message);
2560 bmcJournalLogEntryJson["EntryType"] = "Oem";
2561 bmcJournalLogEntryJson["Severity"] = severity <= 2 ? "Critical"
2562 : severity <= 4 ? "Warning"
2563 : "OK";
2564 bmcJournalLogEntryJson["OemRecordFormat"] = "BMC Journal Entry";
2565 bmcJournalLogEntryJson["Created"] = std::move(entryTimeStr);
Jason M. Billse1f26342018-07-18 12:12:00 -07002566 return 0;
2567}
2568
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002569inline void requestRoutesBMCJournalLogEntryCollection(App& app)
Jason M. Billse1f26342018-07-18 12:12:00 -07002570{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002571 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07002572 .privileges(redfish::privileges::getLogEntryCollection)
Ed Tanous002d39b2022-05-31 08:59:27 -07002573 .methods(boost::beast::http::verb::get)(
2574 [&app](const crow::Request& req,
2575 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2576 query_param::QueryCapabilities capabilities = {
2577 .canDelegateTop = true,
2578 .canDelegateSkip = true,
2579 };
2580 query_param::Query delegatedQuery;
2581 if (!redfish::setUpRedfishRouteWithDelegation(
Carson Labrado3ba00072022-06-06 19:40:56 +00002582 app, req, asyncResp, delegatedQuery, capabilities))
Ed Tanous002d39b2022-05-31 08:59:27 -07002583 {
2584 return;
2585 }
Ed Tanous3648c8b2022-07-25 13:39:59 -07002586
2587 size_t skip = delegatedQuery.skip.value_or(0);
Jiaqing Zhao5143f7a2022-07-22 09:33:33 +08002588 size_t top = delegatedQuery.top.value_or(query_param::Query::maxTop);
Ed Tanous3648c8b2022-07-25 13:39:59 -07002589
Ed Tanous002d39b2022-05-31 08:59:27 -07002590 // Collections don't include the static data added by SubRoute
2591 // because it has a duplicate entry for members
2592 asyncResp->res.jsonValue["@odata.type"] =
2593 "#LogEntryCollection.LogEntryCollection";
2594 asyncResp->res.jsonValue["@odata.id"] =
2595 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries";
2596 asyncResp->res.jsonValue["Name"] = "Open BMC Journal Entries";
2597 asyncResp->res.jsonValue["Description"] =
2598 "Collection of BMC Journal Entries";
2599 nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
2600 logEntryArray = nlohmann::json::array();
Jason M. Billse1f26342018-07-18 12:12:00 -07002601
Ed Tanous002d39b2022-05-31 08:59:27 -07002602 // Go through the journal and use the timestamp to create a
2603 // unique ID for each entry
2604 sd_journal* journalTmp = nullptr;
2605 int ret = sd_journal_open(&journalTmp, SD_JOURNAL_LOCAL_ONLY);
2606 if (ret < 0)
2607 {
Ed Tanous62598e32023-07-17 17:06:25 -07002608 BMCWEB_LOG_ERROR("failed to open journal: {}", strerror(-ret));
Ed Tanous002d39b2022-05-31 08:59:27 -07002609 messages::internalError(asyncResp->res);
2610 return;
2611 }
2612 std::unique_ptr<sd_journal, decltype(&sd_journal_close)> journal(
2613 journalTmp, sd_journal_close);
2614 journalTmp = nullptr;
2615 uint64_t entryCount = 0;
2616 // Reset the unique ID on the first entry
2617 bool firstEntry = true;
2618 SD_JOURNAL_FOREACH(journal.get())
2619 {
2620 entryCount++;
2621 // Handle paging using skip (number of entries to skip from
2622 // the start) and top (number of entries to display)
Ed Tanous3648c8b2022-07-25 13:39:59 -07002623 if (entryCount <= skip || entryCount > skip + top)
George Liu0fda0f12021-11-16 10:06:17 +08002624 {
Ed Tanous002d39b2022-05-31 08:59:27 -07002625 continue;
2626 }
2627
2628 std::string idStr;
2629 if (!getUniqueEntryID(journal.get(), idStr, firstEntry))
2630 {
2631 continue;
2632 }
Jason M. Billsefde4ec2022-06-24 08:59:52 -07002633 firstEntry = false;
Ed Tanous002d39b2022-05-31 08:59:27 -07002634
Jason M. Bills3a48b3a2022-06-24 10:10:15 -07002635 nlohmann::json::object_t bmcJournalLogEntry;
Ed Tanous002d39b2022-05-31 08:59:27 -07002636 if (fillBMCJournalLogEntryJson(idStr, journal.get(),
2637 bmcJournalLogEntry) != 0)
2638 {
George Liu0fda0f12021-11-16 10:06:17 +08002639 messages::internalError(asyncResp->res);
2640 return;
2641 }
Patrick Williamsb2ba3072023-05-12 10:27:39 -05002642 logEntryArray.emplace_back(std::move(bmcJournalLogEntry));
Ed Tanous002d39b2022-05-31 08:59:27 -07002643 }
2644 asyncResp->res.jsonValue["Members@odata.count"] = entryCount;
Ed Tanous3648c8b2022-07-25 13:39:59 -07002645 if (skip + top < entryCount)
Ed Tanous002d39b2022-05-31 08:59:27 -07002646 {
2647 asyncResp->res.jsonValue["Members@odata.nextLink"] =
2648 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries?$skip=" +
Ed Tanous3648c8b2022-07-25 13:39:59 -07002649 std::to_string(skip + top);
Ed Tanous002d39b2022-05-31 08:59:27 -07002650 }
Patrick Williams5a39f772023-10-20 11:20:21 -05002651 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002652}
Jason M. Billse1f26342018-07-18 12:12:00 -07002653
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002654inline void requestRoutesBMCJournalLogEntry(App& app)
Jason M. Billse1f26342018-07-18 12:12:00 -07002655{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002656 BMCWEB_ROUTE(app,
2657 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002658 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002659 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002660 [&app](const crow::Request& req,
2661 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2662 const std::string& entryID) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002663 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002664 {
2665 return;
2666 }
2667 // Convert the unique ID back to a timestamp to find the entry
2668 uint64_t ts = 0;
2669 uint64_t index = 0;
2670 if (!getTimestampFromID(asyncResp, entryID, ts, index))
2671 {
2672 return;
2673 }
Jason M. Billse1f26342018-07-18 12:12:00 -07002674
Ed Tanous002d39b2022-05-31 08:59:27 -07002675 sd_journal* journalTmp = nullptr;
2676 int ret = sd_journal_open(&journalTmp, SD_JOURNAL_LOCAL_ONLY);
2677 if (ret < 0)
2678 {
Ed Tanous62598e32023-07-17 17:06:25 -07002679 BMCWEB_LOG_ERROR("failed to open journal: {}", strerror(-ret));
Ed Tanous002d39b2022-05-31 08:59:27 -07002680 messages::internalError(asyncResp->res);
2681 return;
2682 }
2683 std::unique_ptr<sd_journal, decltype(&sd_journal_close)> journal(
2684 journalTmp, sd_journal_close);
2685 journalTmp = nullptr;
2686 // Go to the timestamp in the log and move to the entry at the
2687 // index tracking the unique ID
2688 std::string idStr;
2689 bool firstEntry = true;
2690 ret = sd_journal_seek_realtime_usec(journal.get(), ts);
2691 if (ret < 0)
2692 {
Ed Tanous62598e32023-07-17 17:06:25 -07002693 BMCWEB_LOG_ERROR("failed to seek to an entry in journal{}",
2694 strerror(-ret));
Ed Tanous002d39b2022-05-31 08:59:27 -07002695 messages::internalError(asyncResp->res);
2696 return;
2697 }
2698 for (uint64_t i = 0; i <= index; i++)
2699 {
2700 sd_journal_next(journal.get());
2701 if (!getUniqueEntryID(journal.get(), idStr, firstEntry))
2702 {
2703 messages::internalError(asyncResp->res);
2704 return;
2705 }
Jason M. Billsefde4ec2022-06-24 08:59:52 -07002706 firstEntry = false;
Ed Tanous002d39b2022-05-31 08:59:27 -07002707 }
2708 // Confirm that the entry ID matches what was requested
2709 if (idStr != entryID)
2710 {
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08002711 messages::resourceNotFound(asyncResp->res, "LogEntry", entryID);
Ed Tanous002d39b2022-05-31 08:59:27 -07002712 return;
2713 }
zhanghch058d1b46d2021-04-01 11:18:24 +08002714
Jason M. Bills3a48b3a2022-06-24 10:10:15 -07002715 nlohmann::json::object_t bmcJournalLogEntry;
Ed Tanous002d39b2022-05-31 08:59:27 -07002716 if (fillBMCJournalLogEntryJson(entryID, journal.get(),
Jason M. Bills3a48b3a2022-06-24 10:10:15 -07002717 bmcJournalLogEntry) != 0)
Ed Tanous002d39b2022-05-31 08:59:27 -07002718 {
2719 messages::internalError(asyncResp->res);
2720 return;
2721 }
Jason M. Billsd405bb52022-06-24 10:52:05 -07002722 asyncResp->res.jsonValue.update(bmcJournalLogEntry);
Patrick Williams5a39f772023-10-20 11:20:21 -05002723 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07002724}
2725
Claire Weinanfdd26902022-03-01 14:18:25 -08002726inline void
2727 getDumpServiceInfo(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2728 const std::string& dumpType)
2729{
2730 std::string dumpPath;
2731 std::string overWritePolicy;
2732 bool collectDiagnosticDataSupported = false;
2733
2734 if (dumpType == "BMC")
2735 {
2736 dumpPath = "/redfish/v1/Managers/bmc/LogServices/Dump";
2737 overWritePolicy = "WrapsWhenFull";
2738 collectDiagnosticDataSupported = true;
2739 }
2740 else if (dumpType == "FaultLog")
2741 {
2742 dumpPath = "/redfish/v1/Managers/bmc/LogServices/FaultLog";
2743 overWritePolicy = "Unknown";
2744 collectDiagnosticDataSupported = false;
2745 }
2746 else if (dumpType == "System")
2747 {
2748 dumpPath = "/redfish/v1/Systems/system/LogServices/Dump";
2749 overWritePolicy = "WrapsWhenFull";
2750 collectDiagnosticDataSupported = true;
2751 }
2752 else
2753 {
Ed Tanous62598e32023-07-17 17:06:25 -07002754 BMCWEB_LOG_ERROR("getDumpServiceInfo() invalid dump type: {}",
2755 dumpType);
Claire Weinanfdd26902022-03-01 14:18:25 -08002756 messages::internalError(asyncResp->res);
2757 return;
2758 }
2759
2760 asyncResp->res.jsonValue["@odata.id"] = dumpPath;
2761 asyncResp->res.jsonValue["@odata.type"] = "#LogService.v1_2_0.LogService";
2762 asyncResp->res.jsonValue["Name"] = "Dump LogService";
2763 asyncResp->res.jsonValue["Description"] = dumpType + " Dump LogService";
2764 asyncResp->res.jsonValue["Id"] = std::filesystem::path(dumpPath).filename();
2765 asyncResp->res.jsonValue["OverWritePolicy"] = std::move(overWritePolicy);
2766
2767 std::pair<std::string, std::string> redfishDateTimeOffset =
Ed Tanous2b829372022-08-03 14:22:34 -07002768 redfish::time_utils::getDateTimeOffsetNow();
Claire Weinanfdd26902022-03-01 14:18:25 -08002769 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
2770 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
2771 redfishDateTimeOffset.second;
2772
2773 asyncResp->res.jsonValue["Entries"]["@odata.id"] = dumpPath + "/Entries";
Claire Weinanfdd26902022-03-01 14:18:25 -08002774
2775 if (collectDiagnosticDataSupported)
2776 {
2777 asyncResp->res.jsonValue["Actions"]["#LogService.CollectDiagnosticData"]
2778 ["target"] =
2779 dumpPath + "/Actions/LogService.CollectDiagnosticData";
2780 }
Claire Weinan0d946212022-07-13 19:40:19 -07002781
2782 constexpr std::array<std::string_view, 1> interfaces = {deleteAllInterface};
2783 dbus::utility::getSubTreePaths(
2784 "/xyz/openbmc_project/dump", 0, interfaces,
2785 [asyncResp, dumpType, dumpPath](
2786 const boost::system::error_code& ec,
2787 const dbus::utility::MapperGetSubTreePathsResponse& subTreePaths) {
2788 if (ec)
2789 {
Ed Tanous62598e32023-07-17 17:06:25 -07002790 BMCWEB_LOG_ERROR("getDumpServiceInfo respHandler got error {}", ec);
Claire Weinan0d946212022-07-13 19:40:19 -07002791 // Assume that getting an error simply means there are no dump
2792 // LogServices. Return without adding any error response.
2793 return;
2794 }
2795
2796 const std::string dbusDumpPath =
2797 "/xyz/openbmc_project/dump/" +
2798 boost::algorithm::to_lower_copy(dumpType);
2799
2800 for (const std::string& path : subTreePaths)
2801 {
2802 if (path == dbusDumpPath)
2803 {
2804 asyncResp->res
2805 .jsonValue["Actions"]["#LogService.ClearLog"]["target"] =
2806 dumpPath + "/Actions/LogService.ClearLog";
2807 break;
2808 }
2809 }
Patrick Williams5a39f772023-10-20 11:20:21 -05002810 });
Claire Weinanfdd26902022-03-01 14:18:25 -08002811}
2812
2813inline void handleLogServicesDumpServiceGet(
2814 crow::App& app, const std::string& dumpType, const crow::Request& req,
2815 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2816{
2817 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2818 {
2819 return;
2820 }
2821 getDumpServiceInfo(asyncResp, dumpType);
2822}
2823
Ed Tanous22d268c2022-05-19 09:39:07 -07002824inline void handleLogServicesDumpServiceComputerSystemGet(
2825 crow::App& app, const crow::Request& req,
2826 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2827 const std::string& chassisId)
2828{
2829 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2830 {
2831 return;
2832 }
2833 if (chassisId != "system")
2834 {
2835 messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
2836 return;
2837 }
2838 getDumpServiceInfo(asyncResp, "System");
2839}
2840
Claire Weinanfdd26902022-03-01 14:18:25 -08002841inline void handleLogServicesDumpEntriesCollectionGet(
2842 crow::App& app, const std::string& dumpType, const crow::Request& req,
2843 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2844{
2845 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2846 {
2847 return;
2848 }
2849 getDumpEntryCollection(asyncResp, dumpType);
2850}
2851
Ed Tanous22d268c2022-05-19 09:39:07 -07002852inline void handleLogServicesDumpEntriesCollectionComputerSystemGet(
2853 crow::App& app, const crow::Request& req,
2854 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2855 const std::string& chassisId)
2856{
2857 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2858 {
2859 return;
2860 }
2861 if (chassisId != "system")
2862 {
2863 messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
2864 return;
2865 }
2866 getDumpEntryCollection(asyncResp, "System");
2867}
2868
Claire Weinanfdd26902022-03-01 14:18:25 -08002869inline void handleLogServicesDumpEntryGet(
2870 crow::App& app, const std::string& dumpType, const crow::Request& req,
2871 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2872 const std::string& dumpId)
2873{
2874 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2875 {
2876 return;
2877 }
2878 getDumpEntryById(asyncResp, dumpId, dumpType);
2879}
Carson Labrado168d1b12023-03-27 17:04:46 +00002880
Ed Tanous22d268c2022-05-19 09:39:07 -07002881inline void handleLogServicesDumpEntryComputerSystemGet(
2882 crow::App& app, const crow::Request& req,
2883 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2884 const std::string& chassisId, const std::string& dumpId)
2885{
2886 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2887 {
2888 return;
2889 }
2890 if (chassisId != "system")
2891 {
2892 messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
2893 return;
2894 }
2895 getDumpEntryById(asyncResp, dumpId, "System");
2896}
Claire Weinanfdd26902022-03-01 14:18:25 -08002897
2898inline void handleLogServicesDumpEntryDelete(
2899 crow::App& app, const std::string& dumpType, const crow::Request& req,
2900 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2901 const std::string& dumpId)
2902{
2903 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2904 {
2905 return;
2906 }
2907 deleteDumpEntry(asyncResp, dumpId, dumpType);
2908}
2909
Ed Tanous22d268c2022-05-19 09:39:07 -07002910inline void handleLogServicesDumpEntryComputerSystemDelete(
2911 crow::App& app, const crow::Request& req,
2912 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2913 const std::string& chassisId, const std::string& dumpId)
2914{
2915 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2916 {
2917 return;
2918 }
2919 if (chassisId != "system")
2920 {
2921 messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
2922 return;
2923 }
2924 deleteDumpEntry(asyncResp, dumpId, "System");
2925}
2926
Carson Labrado168d1b12023-03-27 17:04:46 +00002927inline void handleLogServicesDumpEntryDownloadGet(
2928 crow::App& app, const std::string& dumpType, const crow::Request& req,
2929 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2930 const std::string& dumpId)
2931{
2932 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2933 {
2934 return;
2935 }
2936 downloadDumpEntry(asyncResp, dumpId, dumpType);
2937}
2938
2939inline void handleDBusEventLogEntryDownloadGet(
2940 crow::App& app, const std::string& dumpType, const crow::Request& req,
2941 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2942 const std::string& systemName, const std::string& entryID)
2943{
2944 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2945 {
2946 return;
2947 }
2948 if (!http_helpers::isContentTypeAllowed(
2949 req.getHeaderValue("Accept"),
2950 http_helpers::ContentType::OctetStream, true))
2951 {
2952 asyncResp->res.result(boost::beast::http::status::bad_request);
2953 return;
2954 }
2955 downloadEventLogEntry(asyncResp, systemName, entryID, dumpType);
2956}
2957
Claire Weinanfdd26902022-03-01 14:18:25 -08002958inline void handleLogServicesDumpCollectDiagnosticDataPost(
2959 crow::App& app, const std::string& dumpType, const crow::Request& req,
2960 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2961{
2962 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2963 {
2964 return;
2965 }
2966 createDump(asyncResp, req, dumpType);
2967}
2968
Ed Tanous22d268c2022-05-19 09:39:07 -07002969inline void handleLogServicesDumpCollectDiagnosticDataComputerSystemPost(
2970 crow::App& app, const crow::Request& req,
2971 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous7f3e84a2022-12-28 16:22:54 -08002972 const std::string& systemName)
Ed Tanous22d268c2022-05-19 09:39:07 -07002973{
2974 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2975 {
2976 return;
2977 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08002978
2979 if constexpr (bmcwebEnableMultiHost)
Ed Tanous22d268c2022-05-19 09:39:07 -07002980 {
Ed Tanous7f3e84a2022-12-28 16:22:54 -08002981 // Option currently returns no systems. TBD
2982 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
2983 systemName);
2984 return;
2985 }
2986 if (systemName != "system")
2987 {
2988 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
2989 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07002990 return;
2991 }
2992 createDump(asyncResp, req, "System");
2993}
2994
Claire Weinanfdd26902022-03-01 14:18:25 -08002995inline void handleLogServicesDumpClearLogPost(
2996 crow::App& app, const std::string& dumpType, const crow::Request& req,
2997 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2998{
2999 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
3000 {
3001 return;
3002 }
3003 clearDump(asyncResp, dumpType);
3004}
3005
Ed Tanous22d268c2022-05-19 09:39:07 -07003006inline void handleLogServicesDumpClearLogComputerSystemPost(
3007 crow::App& app, const crow::Request& req,
3008 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous7f3e84a2022-12-28 16:22:54 -08003009 const std::string& systemName)
Ed Tanous22d268c2022-05-19 09:39:07 -07003010{
3011 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
3012 {
3013 return;
3014 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08003015 if constexpr (bmcwebEnableMultiHost)
Ed Tanous22d268c2022-05-19 09:39:07 -07003016 {
Ed Tanous7f3e84a2022-12-28 16:22:54 -08003017 // Option currently returns no systems. TBD
3018 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3019 systemName);
3020 return;
3021 }
3022 if (systemName != "system")
3023 {
3024 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3025 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07003026 return;
3027 }
3028 clearDump(asyncResp, "System");
3029}
3030
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003031inline void requestRoutesBMCDumpService(App& app)
3032{
3033 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Dump/")
Ed Tanoused398212021-06-09 17:05:54 -07003034 .privileges(redfish::privileges::getLogService)
Claire Weinanfdd26902022-03-01 14:18:25 -08003035 .methods(boost::beast::http::verb::get)(std::bind_front(
3036 handleLogServicesDumpServiceGet, std::ref(app), "BMC"));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003037}
3038
3039inline void requestRoutesBMCDumpEntryCollection(App& app)
3040{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003041 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07003042 .privileges(redfish::privileges::getLogEntryCollection)
Claire Weinanfdd26902022-03-01 14:18:25 -08003043 .methods(boost::beast::http::verb::get)(std::bind_front(
3044 handleLogServicesDumpEntriesCollectionGet, std::ref(app), "BMC"));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003045}
3046
3047inline void requestRoutesBMCDumpEntry(App& app)
3048{
3049 BMCWEB_ROUTE(app,
3050 "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07003051 .privileges(redfish::privileges::getLogEntry)
Claire Weinanfdd26902022-03-01 14:18:25 -08003052 .methods(boost::beast::http::verb::get)(std::bind_front(
3053 handleLogServicesDumpEntryGet, std::ref(app), "BMC"));
3054
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003055 BMCWEB_ROUTE(app,
3056 "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07003057 .privileges(redfish::privileges::deleteLogEntry)
Claire Weinanfdd26902022-03-01 14:18:25 -08003058 .methods(boost::beast::http::verb::delete_)(std::bind_front(
3059 handleLogServicesDumpEntryDelete, std::ref(app), "BMC"));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003060}
3061
Carson Labrado168d1b12023-03-27 17:04:46 +00003062inline void requestRoutesBMCDumpEntryDownload(App& app)
3063{
3064 BMCWEB_ROUTE(
3065 app,
3066 "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/<str>/attachment")
3067 .privileges(redfish::privileges::getLogEntry)
3068 .methods(boost::beast::http::verb::get)(std::bind_front(
3069 handleLogServicesDumpEntryDownloadGet, std::ref(app), "BMC"));
3070}
3071
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003072inline void requestRoutesBMCDumpCreate(App& app)
3073{
George Liu0fda0f12021-11-16 10:06:17 +08003074 BMCWEB_ROUTE(
3075 app,
3076 "/redfish/v1/Managers/bmc/LogServices/Dump/Actions/LogService.CollectDiagnosticData/")
Ed Tanoused398212021-06-09 17:05:54 -07003077 .privileges(redfish::privileges::postLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003078 .methods(boost::beast::http::verb::post)(
Claire Weinanfdd26902022-03-01 14:18:25 -08003079 std::bind_front(handleLogServicesDumpCollectDiagnosticDataPost,
3080 std::ref(app), "BMC"));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003081}
3082
3083inline void requestRoutesBMCDumpClear(App& app)
3084{
George Liu0fda0f12021-11-16 10:06:17 +08003085 BMCWEB_ROUTE(
3086 app,
3087 "/redfish/v1/Managers/bmc/LogServices/Dump/Actions/LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07003088 .privileges(redfish::privileges::postLogService)
Claire Weinanfdd26902022-03-01 14:18:25 -08003089 .methods(boost::beast::http::verb::post)(std::bind_front(
3090 handleLogServicesDumpClearLogPost, std::ref(app), "BMC"));
3091}
3092
Carson Labrado168d1b12023-03-27 17:04:46 +00003093inline void requestRoutesDBusEventLogEntryDownload(App& app)
3094{
3095 BMCWEB_ROUTE(
3096 app,
3097 "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/<str>/attachment")
3098 .privileges(redfish::privileges::getLogEntry)
3099 .methods(boost::beast::http::verb::get)(std::bind_front(
3100 handleDBusEventLogEntryDownloadGet, std::ref(app), "System"));
3101}
3102
Claire Weinanfdd26902022-03-01 14:18:25 -08003103inline void requestRoutesFaultLogDumpService(App& app)
3104{
3105 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/FaultLog/")
3106 .privileges(redfish::privileges::getLogService)
3107 .methods(boost::beast::http::verb::get)(std::bind_front(
3108 handleLogServicesDumpServiceGet, std::ref(app), "FaultLog"));
3109}
3110
3111inline void requestRoutesFaultLogDumpEntryCollection(App& app)
3112{
3113 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/FaultLog/Entries/")
3114 .privileges(redfish::privileges::getLogEntryCollection)
3115 .methods(boost::beast::http::verb::get)(
3116 std::bind_front(handleLogServicesDumpEntriesCollectionGet,
3117 std::ref(app), "FaultLog"));
3118}
3119
3120inline void requestRoutesFaultLogDumpEntry(App& app)
3121{
3122 BMCWEB_ROUTE(app,
3123 "/redfish/v1/Managers/bmc/LogServices/FaultLog/Entries/<str>/")
3124 .privileges(redfish::privileges::getLogEntry)
3125 .methods(boost::beast::http::verb::get)(std::bind_front(
3126 handleLogServicesDumpEntryGet, std::ref(app), "FaultLog"));
3127
3128 BMCWEB_ROUTE(app,
3129 "/redfish/v1/Managers/bmc/LogServices/FaultLog/Entries/<str>/")
3130 .privileges(redfish::privileges::deleteLogEntry)
3131 .methods(boost::beast::http::verb::delete_)(std::bind_front(
3132 handleLogServicesDumpEntryDelete, std::ref(app), "FaultLog"));
3133}
3134
3135inline void requestRoutesFaultLogDumpClear(App& app)
3136{
3137 BMCWEB_ROUTE(
3138 app,
3139 "/redfish/v1/Managers/bmc/LogServices/FaultLog/Actions/LogService.ClearLog/")
3140 .privileges(redfish::privileges::postLogService)
3141 .methods(boost::beast::http::verb::post)(std::bind_front(
3142 handleLogServicesDumpClearLogPost, std::ref(app), "FaultLog"));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003143}
3144
3145inline void requestRoutesSystemDumpService(App& app)
3146{
Ed Tanous22d268c2022-05-19 09:39:07 -07003147 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/Dump/")
Ed Tanoused398212021-06-09 17:05:54 -07003148 .privileges(redfish::privileges::getLogService)
Claire Weinan6ab9ad52022-08-12 18:20:17 -07003149 .methods(boost::beast::http::verb::get)(std::bind_front(
Ed Tanous22d268c2022-05-19 09:39:07 -07003150 handleLogServicesDumpServiceComputerSystemGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003151}
3152
3153inline void requestRoutesSystemDumpEntryCollection(App& app)
3154{
Ed Tanous22d268c2022-05-19 09:39:07 -07003155 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/Dump/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07003156 .privileges(redfish::privileges::getLogEntryCollection)
Ed Tanous22d268c2022-05-19 09:39:07 -07003157 .methods(boost::beast::http::verb::get)(std::bind_front(
3158 handleLogServicesDumpEntriesCollectionComputerSystemGet,
3159 std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003160}
3161
3162inline void requestRoutesSystemDumpEntry(App& app)
3163{
3164 BMCWEB_ROUTE(app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003165 "/redfish/v1/Systems/<str>/LogServices/Dump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07003166 .privileges(redfish::privileges::getLogEntry)
Claire Weinan6ab9ad52022-08-12 18:20:17 -07003167 .methods(boost::beast::http::verb::get)(std::bind_front(
Ed Tanous22d268c2022-05-19 09:39:07 -07003168 handleLogServicesDumpEntryComputerSystemGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003169
3170 BMCWEB_ROUTE(app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003171 "/redfish/v1/Systems/<str>/LogServices/Dump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07003172 .privileges(redfish::privileges::deleteLogEntry)
Claire Weinan6ab9ad52022-08-12 18:20:17 -07003173 .methods(boost::beast::http::verb::delete_)(std::bind_front(
Ed Tanous22d268c2022-05-19 09:39:07 -07003174 handleLogServicesDumpEntryComputerSystemDelete, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003175}
3176
3177inline void requestRoutesSystemDumpCreate(App& app)
3178{
George Liu0fda0f12021-11-16 10:06:17 +08003179 BMCWEB_ROUTE(
3180 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003181 "/redfish/v1/Systems/<str>/LogServices/Dump/Actions/LogService.CollectDiagnosticData/")
Ed Tanoused398212021-06-09 17:05:54 -07003182 .privileges(redfish::privileges::postLogService)
Ed Tanous22d268c2022-05-19 09:39:07 -07003183 .methods(boost::beast::http::verb::post)(std::bind_front(
3184 handleLogServicesDumpCollectDiagnosticDataComputerSystemPost,
3185 std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003186}
3187
3188inline void requestRoutesSystemDumpClear(App& app)
3189{
George Liu0fda0f12021-11-16 10:06:17 +08003190 BMCWEB_ROUTE(
3191 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003192 "/redfish/v1/Systems/<str>/LogServices/Dump/Actions/LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07003193 .privileges(redfish::privileges::postLogService)
Claire Weinan6ab9ad52022-08-12 18:20:17 -07003194 .methods(boost::beast::http::verb::post)(std::bind_front(
Ed Tanous22d268c2022-05-19 09:39:07 -07003195 handleLogServicesDumpClearLogComputerSystemPost, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003196}
3197
3198inline void requestRoutesCrashdumpService(App& app)
3199{
3200 // Note: Deviated from redfish privilege registry for GET & HEAD
3201 // method for security reasons.
3202 /**
3203 * Functions triggers appropriate requests on DBus
3204 */
Ed Tanous22d268c2022-05-19 09:39:07 -07003205 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/Crashdump/")
Ed Tanoused398212021-06-09 17:05:54 -07003206 // This is incorrect, should be:
3207 //.privileges(redfish::privileges::getLogService)
Ed Tanous432a8902021-06-14 15:28:56 -07003208 .privileges({{"ConfigureManager"}})
Ed Tanous002d39b2022-05-31 08:59:27 -07003209 .methods(boost::beast::http::verb::get)(
3210 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07003211 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3212 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003213 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003214 {
3215 return;
3216 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08003217 if constexpr (bmcwebEnableMultiHost)
3218 {
3219 // Option currently returns no systems. TBD
3220 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3221 systemName);
3222 return;
3223 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003224 if (systemName != "system")
3225 {
3226 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3227 systemName);
3228 return;
3229 }
3230
Ed Tanous002d39b2022-05-31 08:59:27 -07003231 // Copy over the static data to include the entries added by
3232 // SubRoute
3233 asyncResp->res.jsonValue["@odata.id"] =
3234 "/redfish/v1/Systems/system/LogServices/Crashdump";
3235 asyncResp->res.jsonValue["@odata.type"] =
3236 "#LogService.v1_2_0.LogService";
3237 asyncResp->res.jsonValue["Name"] = "Open BMC Oem Crashdump Service";
3238 asyncResp->res.jsonValue["Description"] = "Oem Crashdump Service";
V-Sanjana15b89722023-05-11 16:27:03 +05303239 asyncResp->res.jsonValue["Id"] = "Crashdump";
Ed Tanous002d39b2022-05-31 08:59:27 -07003240 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
3241 asyncResp->res.jsonValue["MaxNumberOfRecords"] = 3;
Tejas Patil7c8c4052021-06-04 17:43:14 +05303242
Ed Tanous002d39b2022-05-31 08:59:27 -07003243 std::pair<std::string, std::string> redfishDateTimeOffset =
Ed Tanous2b829372022-08-03 14:22:34 -07003244 redfish::time_utils::getDateTimeOffsetNow();
Ed Tanous002d39b2022-05-31 08:59:27 -07003245 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
3246 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
3247 redfishDateTimeOffset.second;
Tejas Patil7c8c4052021-06-04 17:43:14 +05303248
Ed Tanous002d39b2022-05-31 08:59:27 -07003249 asyncResp->res.jsonValue["Entries"]["@odata.id"] =
Ed Tanousef4c65b2023-04-24 15:28:50 -07003250 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries";
Ed Tanous002d39b2022-05-31 08:59:27 -07003251 asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"]["target"] =
3252 "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.ClearLog";
3253 asyncResp->res.jsonValue["Actions"]["#LogService.CollectDiagnosticData"]
3254 ["target"] =
3255 "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.CollectDiagnosticData";
Patrick Williams5a39f772023-10-20 11:20:21 -05003256 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003257}
3258
3259void inline requestRoutesCrashdumpClear(App& app)
3260{
George Liu0fda0f12021-11-16 10:06:17 +08003261 BMCWEB_ROUTE(
3262 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003263 "/redfish/v1/Systems/<str>/LogServices/Crashdump/Actions/LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07003264 // This is incorrect, should be:
3265 //.privileges(redfish::privileges::postLogService)
Ed Tanous432a8902021-06-14 15:28:56 -07003266 .privileges({{"ConfigureComponents"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003267 .methods(boost::beast::http::verb::post)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07003268 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07003269 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3270 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003271 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003272 {
3273 return;
3274 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08003275 if constexpr (bmcwebEnableMultiHost)
3276 {
3277 // Option currently returns no systems. TBD
3278 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3279 systemName);
3280 return;
3281 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003282 if (systemName != "system")
3283 {
3284 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3285 systemName);
3286 return;
3287 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003288 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08003289 [asyncResp](const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07003290 const std::string&) {
3291 if (ec)
3292 {
3293 messages::internalError(asyncResp->res);
3294 return;
3295 }
3296 messages::success(asyncResp->res);
Patrick Williams5a39f772023-10-20 11:20:21 -05003297 },
Ed Tanous002d39b2022-05-31 08:59:27 -07003298 crashdumpObject, crashdumpPath, deleteAllInterface, "DeleteAll");
Patrick Williams5a39f772023-10-20 11:20:21 -05003299 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003300}
Jason M. Bills5b61b5e2019-10-16 10:59:02 -07003301
zhanghch058d1b46d2021-04-01 11:18:24 +08003302static void
3303 logCrashdumpEntry(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3304 const std::string& logID, nlohmann::json& logEntryJson)
Jason M. Billse855dd22019-10-08 11:37:48 -07003305{
Johnathan Mantey043a0532020-03-10 17:15:28 -07003306 auto getStoredLogCallback =
Ed Tanousb9d36b42022-02-26 21:42:46 -08003307 [asyncResp, logID,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08003308 &logEntryJson](const boost::system::error_code& ec,
Ed Tanousb9d36b42022-02-26 21:42:46 -08003309 const dbus::utility::DBusPropertiesMap& params) {
Ed Tanous002d39b2022-05-31 08:59:27 -07003310 if (ec)
3311 {
Ed Tanous62598e32023-07-17 17:06:25 -07003312 BMCWEB_LOG_DEBUG("failed to get log ec: {}", ec.message());
Ed Tanous002d39b2022-05-31 08:59:27 -07003313 if (ec.value() ==
3314 boost::system::linux_error::bad_request_descriptor)
Jason M. Bills1ddcf012019-11-26 14:59:21 -08003315 {
Ed Tanous002d39b2022-05-31 08:59:27 -07003316 messages::resourceNotFound(asyncResp->res, "LogEntry", logID);
Jason M. Bills2b20ef62022-01-06 15:48:07 -08003317 }
3318 else
3319 {
Ed Tanous002d39b2022-05-31 08:59:27 -07003320 messages::internalError(asyncResp->res);
Jason M. Bills2b20ef62022-01-06 15:48:07 -08003321 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003322 return;
3323 }
3324
3325 std::string timestamp{};
3326 std::string filename{};
3327 std::string logfile{};
3328 parseCrashdumpParameters(params, filename, timestamp, logfile);
3329
3330 if (filename.empty() || timestamp.empty())
3331 {
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08003332 messages::resourceNotFound(asyncResp->res, "LogEntry", logID);
Ed Tanous002d39b2022-05-31 08:59:27 -07003333 return;
3334 }
3335
3336 std::string crashdumpURI =
3337 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/" +
3338 logID + "/" + filename;
Jason M. Bills84afc482022-06-24 12:38:23 -07003339 nlohmann::json::object_t logEntry;
Vijay Lobo9c11a172021-10-07 16:53:16 -05003340 logEntry["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
Ed Tanousef4c65b2023-04-24 15:28:50 -07003341 logEntry["@odata.id"] = boost::urls::format(
3342 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/{}",
3343 logID);
Jason M. Bills84afc482022-06-24 12:38:23 -07003344 logEntry["Name"] = "CPU Crashdump";
3345 logEntry["Id"] = logID;
3346 logEntry["EntryType"] = "Oem";
3347 logEntry["AdditionalDataURI"] = std::move(crashdumpURI);
3348 logEntry["DiagnosticDataType"] = "OEM";
3349 logEntry["OEMDiagnosticDataType"] = "PECICrashdump";
3350 logEntry["Created"] = std::move(timestamp);
Ed Tanous002d39b2022-05-31 08:59:27 -07003351
3352 // If logEntryJson references an array of LogEntry resources
3353 // ('Members' list), then push this as a new entry, otherwise set it
3354 // directly
3355 if (logEntryJson.is_array())
3356 {
3357 logEntryJson.push_back(logEntry);
3358 asyncResp->res.jsonValue["Members@odata.count"] =
3359 logEntryJson.size();
3360 }
3361 else
3362 {
Jason M. Billsd405bb52022-06-24 10:52:05 -07003363 logEntryJson.update(logEntry);
Ed Tanous002d39b2022-05-31 08:59:27 -07003364 }
3365 };
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02003366 sdbusplus::asio::getAllProperties(
3367 *crow::connections::systemBus, crashdumpObject,
3368 crashdumpPath + std::string("/") + logID, crashdumpInterface,
3369 std::move(getStoredLogCallback));
Jason M. Billse855dd22019-10-08 11:37:48 -07003370}
3371
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003372inline void requestRoutesCrashdumpEntryCollection(App& app)
Ed Tanous1da66f72018-07-27 16:13:37 -07003373{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003374 // Note: Deviated from redfish privilege registry for GET & HEAD
3375 // method for security reasons.
Ed Tanous1da66f72018-07-27 16:13:37 -07003376 /**
3377 * Functions triggers appropriate requests on DBus
3378 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003379 BMCWEB_ROUTE(app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003380 "/redfish/v1/Systems/<str>/LogServices/Crashdump/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07003381 // This is incorrect, should be.
3382 //.privileges(redfish::privileges::postLogEntryCollection)
Ed Tanous432a8902021-06-14 15:28:56 -07003383 .privileges({{"ConfigureComponents"}})
Ed Tanous002d39b2022-05-31 08:59:27 -07003384 .methods(boost::beast::http::verb::get)(
3385 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07003386 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3387 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003388 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003389 {
3390 return;
3391 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08003392 if constexpr (bmcwebEnableMultiHost)
3393 {
3394 // Option currently returns no systems. TBD
3395 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3396 systemName);
3397 return;
3398 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003399 if (systemName != "system")
3400 {
3401 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3402 systemName);
3403 return;
3404 }
3405
George Liu7a1dbc42022-12-07 16:03:22 +08003406 constexpr std::array<std::string_view, 1> interfaces = {
3407 crashdumpInterface};
3408 dbus::utility::getSubTreePaths(
3409 "/", 0, interfaces,
3410 [asyncResp](const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07003411 const std::vector<std::string>& resp) {
3412 if (ec)
Ed Tanous45ca1b82022-03-25 13:07:27 -07003413 {
Ed Tanous002d39b2022-05-31 08:59:27 -07003414 if (ec.value() !=
3415 boost::system::errc::no_such_file_or_directory)
3416 {
Ed Tanous62598e32023-07-17 17:06:25 -07003417 BMCWEB_LOG_DEBUG("failed to get entries ec: {}",
3418 ec.message());
Ed Tanous002d39b2022-05-31 08:59:27 -07003419 messages::internalError(asyncResp->res);
3420 return;
3421 }
Ed Tanous45ca1b82022-03-25 13:07:27 -07003422 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003423 asyncResp->res.jsonValue["@odata.type"] =
3424 "#LogEntryCollection.LogEntryCollection";
3425 asyncResp->res.jsonValue["@odata.id"] =
3426 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries";
3427 asyncResp->res.jsonValue["Name"] = "Open BMC Crashdump Entries";
3428 asyncResp->res.jsonValue["Description"] =
3429 "Collection of Crashdump Entries";
3430 asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
3431 asyncResp->res.jsonValue["Members@odata.count"] = 0;
Jason M. Bills2b20ef62022-01-06 15:48:07 -08003432
Ed Tanous002d39b2022-05-31 08:59:27 -07003433 for (const std::string& path : resp)
3434 {
3435 const sdbusplus::message::object_path objPath(path);
3436 // Get the log ID
3437 std::string logID = objPath.filename();
3438 if (logID.empty())
3439 {
3440 continue;
3441 }
3442 // Add the log entry to the array
3443 logCrashdumpEntry(asyncResp, logID,
3444 asyncResp->res.jsonValue["Members"]);
3445 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003446 });
Patrick Williams5a39f772023-10-20 11:20:21 -05003447 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003448}
Ed Tanous1da66f72018-07-27 16:13:37 -07003449
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003450inline void requestRoutesCrashdumpEntry(App& app)
Ed Tanous1da66f72018-07-27 16:13:37 -07003451{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003452 // Note: Deviated from redfish privilege registry for GET & HEAD
3453 // method for security reasons.
Ed Tanous1da66f72018-07-27 16:13:37 -07003454
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003455 BMCWEB_ROUTE(
Ed Tanous22d268c2022-05-19 09:39:07 -07003456 app, "/redfish/v1/Systems/<str>/LogServices/Crashdump/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07003457 // this is incorrect, should be
3458 // .privileges(redfish::privileges::getLogEntry)
Ed Tanous432a8902021-06-14 15:28:56 -07003459 .privileges({{"ConfigureComponents"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003460 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07003461 [&app](const crow::Request& req,
3462 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07003463 const std::string& systemName, const std::string& param) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003464 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003465 {
3466 return;
3467 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08003468 if constexpr (bmcwebEnableMultiHost)
3469 {
3470 // Option currently returns no systems. TBD
3471 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3472 systemName);
3473 return;
3474 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003475 if (systemName != "system")
3476 {
3477 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3478 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07003479 return;
3480 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003481 const std::string& logID = param;
3482 logCrashdumpEntry(asyncResp, logID, asyncResp->res.jsonValue);
Patrick Williams5a39f772023-10-20 11:20:21 -05003483 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003484}
Ed Tanous1da66f72018-07-27 16:13:37 -07003485
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003486inline void requestRoutesCrashdumpFile(App& app)
3487{
3488 // Note: Deviated from redfish privilege registry for GET & HEAD
3489 // method for security reasons.
3490 BMCWEB_ROUTE(
3491 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003492 "/redfish/v1/Systems/<str>/LogServices/Crashdump/Entries/<str>/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07003493 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003494 .methods(boost::beast::http::verb::get)(
Nan Zhoua4ce1142022-08-02 18:45:25 +00003495 [](const crow::Request& req,
3496 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07003497 const std::string& systemName, const std::string& logID,
3498 const std::string& fileName) {
Shounak Mitra2a9beee2022-07-20 18:41:30 +00003499 // Do not call getRedfishRoute here since the crashdump file is not a
3500 // Redfish resource.
Ed Tanous22d268c2022-05-19 09:39:07 -07003501
Ed Tanous7f3e84a2022-12-28 16:22:54 -08003502 if constexpr (bmcwebEnableMultiHost)
3503 {
3504 // Option currently returns no systems. TBD
3505 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3506 systemName);
3507 return;
3508 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003509 if (systemName != "system")
3510 {
3511 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3512 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07003513 return;
3514 }
3515
Ed Tanous002d39b2022-05-31 08:59:27 -07003516 auto getStoredLogCallback =
Ed Tanous39662a32023-02-06 15:09:46 -08003517 [asyncResp, logID, fileName, url(boost::urls::url(req.url()))](
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08003518 const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07003519 const std::vector<
3520 std::pair<std::string, dbus::utility::DbusVariantType>>&
3521 resp) {
3522 if (ec)
3523 {
Ed Tanous62598e32023-07-17 17:06:25 -07003524 BMCWEB_LOG_DEBUG("failed to get log ec: {}", ec.message());
Ed Tanous002d39b2022-05-31 08:59:27 -07003525 messages::internalError(asyncResp->res);
3526 return;
3527 }
Jason M. Bills8e6c0992021-03-11 16:26:53 -08003528
Ed Tanous002d39b2022-05-31 08:59:27 -07003529 std::string dbusFilename{};
3530 std::string dbusTimestamp{};
3531 std::string dbusFilepath{};
Jason M. Bills8e6c0992021-03-11 16:26:53 -08003532
Ed Tanous002d39b2022-05-31 08:59:27 -07003533 parseCrashdumpParameters(resp, dbusFilename, dbusTimestamp,
3534 dbusFilepath);
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003535
Ed Tanous002d39b2022-05-31 08:59:27 -07003536 if (dbusFilename.empty() || dbusTimestamp.empty() ||
3537 dbusFilepath.empty())
3538 {
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08003539 messages::resourceNotFound(asyncResp->res, "LogEntry", logID);
Ed Tanous002d39b2022-05-31 08:59:27 -07003540 return;
3541 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003542
Ed Tanous002d39b2022-05-31 08:59:27 -07003543 // Verify the file name parameter is correct
3544 if (fileName != dbusFilename)
3545 {
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08003546 messages::resourceNotFound(asyncResp->res, "LogEntry", logID);
Ed Tanous002d39b2022-05-31 08:59:27 -07003547 return;
3548 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003549
Ed Tanous27b0cf92023-08-07 12:02:40 -07003550 if (!asyncResp->res.openFile(dbusFilepath))
Ed Tanous002d39b2022-05-31 08:59:27 -07003551 {
Jiaqing Zhao9db4ba22022-10-09 17:24:40 +08003552 messages::resourceNotFound(asyncResp->res, "LogEntry", logID);
Ed Tanous002d39b2022-05-31 08:59:27 -07003553 return;
3554 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003555
Ed Tanous002d39b2022-05-31 08:59:27 -07003556 // Configure this to be a file download when accessed
3557 // from a browser
Ed Tanousd9f6c622022-03-17 09:12:17 -07003558 asyncResp->res.addHeader(
3559 boost::beast::http::field::content_disposition, "attachment");
Ed Tanous002d39b2022-05-31 08:59:27 -07003560 };
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +02003561 sdbusplus::asio::getAllProperties(
3562 *crow::connections::systemBus, crashdumpObject,
3563 crashdumpPath + std::string("/") + logID, crashdumpInterface,
3564 std::move(getStoredLogCallback));
Patrick Williams5a39f772023-10-20 11:20:21 -05003565 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003566}
3567
Jason M. Billsc5a4c822022-01-06 15:51:23 -08003568enum class OEMDiagnosticType
3569{
3570 onDemand,
3571 telemetry,
3572 invalid,
3573};
3574
Ed Tanous26ccae32023-02-16 10:28:44 -08003575inline OEMDiagnosticType getOEMDiagnosticType(std::string_view oemDiagStr)
Jason M. Billsc5a4c822022-01-06 15:51:23 -08003576{
3577 if (oemDiagStr == "OnDemand")
3578 {
3579 return OEMDiagnosticType::onDemand;
3580 }
3581 if (oemDiagStr == "Telemetry")
3582 {
3583 return OEMDiagnosticType::telemetry;
3584 }
3585
3586 return OEMDiagnosticType::invalid;
3587}
3588
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003589inline void requestRoutesCrashdumpCollect(App& app)
3590{
3591 // Note: Deviated from redfish privilege registry for GET & HEAD
3592 // method for security reasons.
George Liu0fda0f12021-11-16 10:06:17 +08003593 BMCWEB_ROUTE(
3594 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003595 "/redfish/v1/Systems/<str>/LogServices/Crashdump/Actions/LogService.CollectDiagnosticData/")
Ed Tanoused398212021-06-09 17:05:54 -07003596 // The below is incorrect; Should be ConfigureManager
3597 //.privileges(redfish::privileges::postLogService)
Ed Tanous432a8902021-06-14 15:28:56 -07003598 .privileges({{"ConfigureComponents"}})
Ed Tanous002d39b2022-05-31 08:59:27 -07003599 .methods(boost::beast::http::verb::post)(
3600 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07003601 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3602 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003603 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003604 {
3605 return;
3606 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003607
Ed Tanous7f3e84a2022-12-28 16:22:54 -08003608 if constexpr (bmcwebEnableMultiHost)
3609 {
3610 // Option currently returns no systems. TBD
3611 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3612 systemName);
3613 return;
3614 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003615 if (systemName != "system")
3616 {
3617 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3618 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07003619 return;
3620 }
3621
Ed Tanous002d39b2022-05-31 08:59:27 -07003622 std::string diagnosticDataType;
3623 std::string oemDiagnosticDataType;
3624 if (!redfish::json_util::readJsonAction(
3625 req, asyncResp->res, "DiagnosticDataType", diagnosticDataType,
3626 "OEMDiagnosticDataType", oemDiagnosticDataType))
3627 {
3628 return;
3629 }
3630
3631 if (diagnosticDataType != "OEM")
3632 {
Ed Tanous62598e32023-07-17 17:06:25 -07003633 BMCWEB_LOG_ERROR(
3634 "Only OEM DiagnosticDataType supported for Crashdump");
Ed Tanous002d39b2022-05-31 08:59:27 -07003635 messages::actionParameterValueFormatError(
3636 asyncResp->res, diagnosticDataType, "DiagnosticDataType",
3637 "CollectDiagnosticData");
3638 return;
3639 }
3640
3641 OEMDiagnosticType oemDiagType =
3642 getOEMDiagnosticType(oemDiagnosticDataType);
3643
3644 std::string iface;
3645 std::string method;
3646 std::string taskMatchStr;
3647 if (oemDiagType == OEMDiagnosticType::onDemand)
3648 {
3649 iface = crashdumpOnDemandInterface;
3650 method = "GenerateOnDemandLog";
3651 taskMatchStr = "type='signal',"
3652 "interface='org.freedesktop.DBus.Properties',"
3653 "member='PropertiesChanged',"
3654 "arg0namespace='com.intel.crashdump'";
3655 }
3656 else if (oemDiagType == OEMDiagnosticType::telemetry)
3657 {
3658 iface = crashdumpTelemetryInterface;
3659 method = "GenerateTelemetryLog";
3660 taskMatchStr = "type='signal',"
3661 "interface='org.freedesktop.DBus.Properties',"
3662 "member='PropertiesChanged',"
3663 "arg0namespace='com.intel.crashdump'";
3664 }
3665 else
3666 {
Ed Tanous62598e32023-07-17 17:06:25 -07003667 BMCWEB_LOG_ERROR("Unsupported OEMDiagnosticDataType: {}",
3668 oemDiagnosticDataType);
Ed Tanous002d39b2022-05-31 08:59:27 -07003669 messages::actionParameterValueFormatError(
3670 asyncResp->res, oemDiagnosticDataType, "OEMDiagnosticDataType",
3671 "CollectDiagnosticData");
3672 return;
3673 }
3674
3675 auto collectCrashdumpCallback =
3676 [asyncResp, payload(task::Payload(req)),
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08003677 taskMatchStr](const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07003678 const std::string&) mutable {
3679 if (ec)
Ed Tanous45ca1b82022-03-25 13:07:27 -07003680 {
Ed Tanous002d39b2022-05-31 08:59:27 -07003681 if (ec.value() == boost::system::errc::operation_not_supported)
3682 {
3683 messages::resourceInStandby(asyncResp->res);
3684 }
3685 else if (ec.value() ==
3686 boost::system::errc::device_or_resource_busy)
3687 {
3688 messages::serviceTemporarilyUnavailable(asyncResp->res,
3689 "60");
3690 }
3691 else
3692 {
3693 messages::internalError(asyncResp->res);
3694 }
Ed Tanous45ca1b82022-03-25 13:07:27 -07003695 return;
3696 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003697 std::shared_ptr<task::TaskData> task = task::TaskData::createTask(
Ed Tanous8b242752023-06-27 17:17:13 -07003698 [](const boost::system::error_code& ec2, sdbusplus::message_t&,
Ed Tanous002d39b2022-05-31 08:59:27 -07003699 const std::shared_ptr<task::TaskData>& taskData) {
Ed Tanous8b242752023-06-27 17:17:13 -07003700 if (!ec2)
Ed Tanous002d39b2022-05-31 08:59:27 -07003701 {
3702 taskData->messages.emplace_back(messages::taskCompletedOK(
3703 std::to_string(taskData->index)));
3704 taskData->state = "Completed";
3705 }
3706 return task::completed;
Patrick Williams5a39f772023-10-20 11:20:21 -05003707 },
Ed Tanous002d39b2022-05-31 08:59:27 -07003708 taskMatchStr);
Ed Tanous1da66f72018-07-27 16:13:37 -07003709
Ed Tanous002d39b2022-05-31 08:59:27 -07003710 task->startTimer(std::chrono::minutes(5));
3711 task->populateResp(asyncResp->res);
3712 task->payload.emplace(std::move(payload));
3713 };
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003714
Ed Tanous002d39b2022-05-31 08:59:27 -07003715 crow::connections::systemBus->async_method_call(
3716 std::move(collectCrashdumpCallback), crashdumpObject, crashdumpPath,
3717 iface, method);
Patrick Williams5a39f772023-10-20 11:20:21 -05003718 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003719}
Kenny L. Ku6eda7682020-06-19 09:48:36 -07003720
Andrew Geisslercb92c032018-08-17 07:56:14 -07003721/**
3722 * DBusLogServiceActionsClear class supports POST method for ClearLog action.
3723 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003724inline void requestRoutesDBusLogServiceActionsClear(App& app)
Andrew Geisslercb92c032018-08-17 07:56:14 -07003725{
Andrew Geisslercb92c032018-08-17 07:56:14 -07003726 /**
3727 * Function handles POST method request.
3728 * The Clear Log actions does not require any parameter.The action deletes
3729 * all entries found in the Entries collection for this Log Service.
3730 */
Andrew Geisslercb92c032018-08-17 07:56:14 -07003731
George Liu0fda0f12021-11-16 10:06:17 +08003732 BMCWEB_ROUTE(
3733 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003734 "/redfish/v1/Systems/<str>/LogServices/EventLog/Actions/LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07003735 .privileges(redfish::privileges::postLogService)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003736 .methods(boost::beast::http::verb::post)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07003737 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07003738 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3739 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003740 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003741 {
3742 return;
3743 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08003744 if constexpr (bmcwebEnableMultiHost)
3745 {
3746 // Option currently returns no systems. TBD
3747 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3748 systemName);
3749 return;
3750 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003751 if (systemName != "system")
3752 {
3753 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3754 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07003755 return;
3756 }
Ed Tanous62598e32023-07-17 17:06:25 -07003757 BMCWEB_LOG_DEBUG("Do delete all entries.");
Andrew Geisslercb92c032018-08-17 07:56:14 -07003758
Ed Tanous002d39b2022-05-31 08:59:27 -07003759 // Process response from Logging service.
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08003760 auto respHandler = [asyncResp](const boost::system::error_code& ec) {
Ed Tanous62598e32023-07-17 17:06:25 -07003761 BMCWEB_LOG_DEBUG("doClearLog resp_handler callback: Done");
Ed Tanous002d39b2022-05-31 08:59:27 -07003762 if (ec)
3763 {
3764 // TODO Handle for specific error code
Ed Tanous62598e32023-07-17 17:06:25 -07003765 BMCWEB_LOG_ERROR("doClearLog resp_handler got error {}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -07003766 asyncResp->res.result(
3767 boost::beast::http::status::internal_server_error);
3768 return;
3769 }
Andrew Geisslercb92c032018-08-17 07:56:14 -07003770
Ed Tanous002d39b2022-05-31 08:59:27 -07003771 asyncResp->res.result(boost::beast::http::status::no_content);
3772 };
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003773
Ed Tanous002d39b2022-05-31 08:59:27 -07003774 // Make call to Logging service to request Clear Log
3775 crow::connections::systemBus->async_method_call(
3776 respHandler, "xyz.openbmc_project.Logging",
3777 "/xyz/openbmc_project/logging",
3778 "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
Patrick Williams5a39f772023-10-20 11:20:21 -05003779 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003780}
ZhikuiRena3316fc2020-01-29 14:58:08 -08003781
3782/****************************************************
3783 * Redfish PostCode interfaces
3784 * using DBUS interface: getPostCodesTS
3785 ******************************************************/
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003786inline void requestRoutesPostCodesLogService(App& app)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003787{
Ed Tanous22d268c2022-05-19 09:39:07 -07003788 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/PostCodes/")
Ed Tanoused398212021-06-09 17:05:54 -07003789 .privileges(redfish::privileges::getLogService)
Ed Tanous002d39b2022-05-31 08:59:27 -07003790 .methods(boost::beast::http::verb::get)(
3791 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07003792 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3793 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003794 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003795 {
3796 return;
3797 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08003798 if constexpr (bmcwebEnableMultiHost)
3799 {
3800 // Option currently returns no systems. TBD
3801 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3802 systemName);
3803 return;
3804 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003805 if (systemName != "system")
3806 {
3807 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3808 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07003809 return;
3810 }
Ed Tanous002d39b2022-05-31 08:59:27 -07003811 asyncResp->res.jsonValue["@odata.id"] =
3812 "/redfish/v1/Systems/system/LogServices/PostCodes";
3813 asyncResp->res.jsonValue["@odata.type"] =
Janet Adkinsb25644a2023-08-16 11:23:45 -05003814 "#LogService.v1_2_0.LogService";
Ed Tanous002d39b2022-05-31 08:59:27 -07003815 asyncResp->res.jsonValue["Name"] = "POST Code Log Service";
3816 asyncResp->res.jsonValue["Description"] = "POST Code Log Service";
Ed Tanoused34a4a2023-02-08 15:43:27 -08003817 asyncResp->res.jsonValue["Id"] = "PostCodes";
Ed Tanous002d39b2022-05-31 08:59:27 -07003818 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
3819 asyncResp->res.jsonValue["Entries"]["@odata.id"] =
3820 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries";
Tejas Patil7c8c4052021-06-04 17:43:14 +05303821
Ed Tanous002d39b2022-05-31 08:59:27 -07003822 std::pair<std::string, std::string> redfishDateTimeOffset =
Ed Tanous2b829372022-08-03 14:22:34 -07003823 redfish::time_utils::getDateTimeOffsetNow();
Ed Tanous002d39b2022-05-31 08:59:27 -07003824 asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
3825 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
3826 redfishDateTimeOffset.second;
Tejas Patil7c8c4052021-06-04 17:43:14 +05303827
Ed Tanous002d39b2022-05-31 08:59:27 -07003828 asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"] = {
3829 {"target",
3830 "/redfish/v1/Systems/system/LogServices/PostCodes/Actions/LogService.ClearLog"}};
Patrick Williams5a39f772023-10-20 11:20:21 -05003831 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003832}
ZhikuiRena3316fc2020-01-29 14:58:08 -08003833
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003834inline void requestRoutesPostCodesClear(App& app)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003835{
George Liu0fda0f12021-11-16 10:06:17 +08003836 BMCWEB_ROUTE(
3837 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07003838 "/redfish/v1/Systems/<str>/LogServices/PostCodes/Actions/LogService.ClearLog/")
Ed Tanoused398212021-06-09 17:05:54 -07003839 // The following privilege is incorrect; It should be ConfigureManager
3840 //.privileges(redfish::privileges::postLogService)
Ed Tanous432a8902021-06-14 15:28:56 -07003841 .privileges({{"ConfigureComponents"}})
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003842 .methods(boost::beast::http::verb::post)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07003843 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07003844 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3845 const std::string& systemName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00003846 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07003847 {
3848 return;
3849 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08003850 if constexpr (bmcwebEnableMultiHost)
3851 {
3852 // Option currently returns no systems. TBD
3853 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3854 systemName);
3855 return;
3856 }
Ed Tanous22d268c2022-05-19 09:39:07 -07003857 if (systemName != "system")
3858 {
3859 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3860 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07003861 return;
3862 }
Ed Tanous62598e32023-07-17 17:06:25 -07003863 BMCWEB_LOG_DEBUG("Do delete all postcodes entries.");
ZhikuiRena3316fc2020-01-29 14:58:08 -08003864
Ed Tanous002d39b2022-05-31 08:59:27 -07003865 // Make call to post-code service to request clear all
3866 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08003867 [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07003868 if (ec)
3869 {
3870 // TODO Handle for specific error code
Ed Tanous62598e32023-07-17 17:06:25 -07003871 BMCWEB_LOG_ERROR("doClearPostCodes resp_handler got error {}",
3872 ec);
Ed Tanous002d39b2022-05-31 08:59:27 -07003873 asyncResp->res.result(
3874 boost::beast::http::status::internal_server_error);
3875 messages::internalError(asyncResp->res);
3876 return;
3877 }
Tony Lee18fc70c2023-08-24 16:15:54 +08003878 messages::success(asyncResp->res);
Patrick Williams5a39f772023-10-20 11:20:21 -05003879 },
Ed Tanous002d39b2022-05-31 08:59:27 -07003880 "xyz.openbmc_project.State.Boot.PostCode0",
3881 "/xyz/openbmc_project/State/Boot/PostCode0",
3882 "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
Patrick Williams5a39f772023-10-20 11:20:21 -05003883 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07003884}
ZhikuiRena3316fc2020-01-29 14:58:08 -08003885
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003886/**
3887 * @brief Parse post code ID and get the current value and index value
3888 * eg: postCodeID=B1-2, currentValue=1, index=2
3889 *
3890 * @param[in] postCodeID Post Code ID
3891 * @param[out] currentValue Current value
3892 * @param[out] index Index value
3893 *
3894 * @return bool true if the parsing is successful, false the parsing fails
3895 */
3896inline static bool parsePostCode(const std::string& postCodeID,
3897 uint64_t& currentValue, uint16_t& index)
3898{
3899 std::vector<std::string> split;
Ed Tanous50ebd4a2023-01-19 19:03:17 -08003900 bmcweb::split(split, postCodeID, '-');
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003901 if (split.size() != 2 || split[0].length() < 2 || split[0].front() != 'B')
3902 {
3903 return false;
3904 }
3905
Patrick Williams84396af2023-05-11 11:47:45 -05003906 auto start = std::next(split[0].begin());
3907 auto end = split[0].end();
3908 auto [ptrIndex, ecIndex] = std::from_chars(&*start, &*end, index);
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003909
Patrick Williams84396af2023-05-11 11:47:45 -05003910 if (ptrIndex != &*end || ecIndex != std::errc())
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003911 {
3912 return false;
3913 }
3914
Patrick Williams84396af2023-05-11 11:47:45 -05003915 start = split[1].begin();
3916 end = split[1].end();
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003917
Patrick Williams84396af2023-05-11 11:47:45 -05003918 auto [ptrValue, ecValue] = std::from_chars(&*start, &*end, currentValue);
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003919
Patrick Williams84396af2023-05-11 11:47:45 -05003920 return ptrValue == &*end && ecValue == std::errc();
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08003921}
3922
3923static bool fillPostCodeEntry(
Ed Tanousac106bf2023-06-07 09:24:59 -07003924 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05303925 const boost::container::flat_map<
3926 uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>& postcode,
ZhikuiRena3316fc2020-01-29 14:58:08 -08003927 const uint16_t bootIndex, const uint64_t codeIndex = 0,
3928 const uint64_t skip = 0, const uint64_t top = 0)
3929{
3930 // Get the Message from the MessageRegistry
Ed Tanousfffb8c12022-02-07 23:53:03 -08003931 const registries::Message* message =
3932 registries::getMessage("OpenBMC.0.2.BIOSPOSTCode");
ZhikuiRena3316fc2020-01-29 14:58:08 -08003933
3934 uint64_t currentCodeIndex = 0;
ZhikuiRena3316fc2020-01-29 14:58:08 -08003935 uint64_t firstCodeTimeUs = 0;
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05303936 for (const std::pair<uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
3937 code : postcode)
ZhikuiRena3316fc2020-01-29 14:58:08 -08003938 {
3939 currentCodeIndex++;
3940 std::string postcodeEntryID =
3941 "B" + std::to_string(bootIndex) + "-" +
3942 std::to_string(currentCodeIndex); // 1 based index in EntryID string
3943
3944 uint64_t usecSinceEpoch = code.first;
3945 uint64_t usTimeOffset = 0;
3946
3947 if (1 == currentCodeIndex)
3948 { // already incremented
3949 firstCodeTimeUs = code.first;
3950 }
3951 else
3952 {
3953 usTimeOffset = code.first - firstCodeTimeUs;
3954 }
3955
3956 // skip if no specific codeIndex is specified and currentCodeIndex does
3957 // not fall between top and skip
3958 if ((codeIndex == 0) &&
3959 (currentCodeIndex <= skip || currentCodeIndex > top))
3960 {
3961 continue;
3962 }
3963
Gunnar Mills4e0453b2020-07-08 14:00:30 -05003964 // skip if a specific codeIndex is specified and does not match the
ZhikuiRena3316fc2020-01-29 14:58:08 -08003965 // currentIndex
3966 if ((codeIndex > 0) && (currentCodeIndex != codeIndex))
3967 {
3968 // This is done for simplicity. 1st entry is needed to calculate
3969 // time offset. To improve efficiency, one can get to the entry
3970 // directly (possibly with flatmap's nth method)
3971 continue;
3972 }
3973
3974 // currentCodeIndex is within top and skip or equal to specified code
3975 // index
3976
3977 // Get the Created time from the timestamp
3978 std::string entryTimeStr;
Konstantin Aladyshev2a025612023-02-15 11:52:58 +03003979 entryTimeStr = redfish::time_utils::getDateTimeUintUs(usecSinceEpoch);
ZhikuiRena3316fc2020-01-29 14:58:08 -08003980
3981 // assemble messageArgs: BootIndex, TimeOffset(100us), PostCode(hex)
3982 std::ostringstream hexCode;
3983 hexCode << "0x" << std::setfill('0') << std::setw(2) << std::hex
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05303984 << std::get<0>(code.second);
ZhikuiRena3316fc2020-01-29 14:58:08 -08003985 std::ostringstream timeOffsetStr;
3986 // Set Fixed -Point Notation
3987 timeOffsetStr << std::fixed;
3988 // Set precision to 4 digits
3989 timeOffsetStr << std::setprecision(4);
3990 // Add double to stream
3991 timeOffsetStr << static_cast<double>(usTimeOffset) / 1000 / 1000;
ZhikuiRena3316fc2020-01-29 14:58:08 -08003992
Ed Tanous1e6deaf2022-02-17 11:32:37 -08003993 std::string bootIndexStr = std::to_string(bootIndex);
3994 std::string timeOffsetString = timeOffsetStr.str();
3995 std::string hexCodeStr = hexCode.str();
3996
3997 std::array<std::string_view, 3> messageArgs = {
3998 bootIndexStr, timeOffsetString, hexCodeStr};
3999
4000 std::string msg =
4001 redfish::registries::fillMessageArgs(messageArgs, message->message);
4002 if (msg.empty())
ZhikuiRena3316fc2020-01-29 14:58:08 -08004003 {
Ed Tanous1e6deaf2022-02-17 11:32:37 -08004004 messages::internalError(asyncResp->res);
4005 return false;
ZhikuiRena3316fc2020-01-29 14:58:08 -08004006 }
4007
Tim Leed4342a92020-04-27 11:47:58 +08004008 // Get Severity template from message registry
4009 std::string severity;
4010 if (message != nullptr)
4011 {
Ed Tanous5f2b84e2022-02-08 00:41:53 -08004012 severity = message->messageSeverity;
Tim Leed4342a92020-04-27 11:47:58 +08004013 }
4014
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08004015 // Format entry
4016 nlohmann::json::object_t bmcLogEntry;
Vijay Lobo9c11a172021-10-07 16:53:16 -05004017 bmcLogEntry["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
Ed Tanousef4c65b2023-04-24 15:28:50 -07004018 bmcLogEntry["@odata.id"] = boost::urls::format(
4019 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/{}",
4020 postcodeEntryID);
Jason M. Bills84afc482022-06-24 12:38:23 -07004021 bmcLogEntry["Name"] = "POST Code Log Entry";
4022 bmcLogEntry["Id"] = postcodeEntryID;
4023 bmcLogEntry["Message"] = std::move(msg);
4024 bmcLogEntry["MessageId"] = "OpenBMC.0.2.BIOSPOSTCode";
Ed Tanous1e6deaf2022-02-17 11:32:37 -08004025 bmcLogEntry["MessageArgs"] = messageArgs;
Jason M. Bills84afc482022-06-24 12:38:23 -07004026 bmcLogEntry["EntryType"] = "Event";
4027 bmcLogEntry["Severity"] = std::move(severity);
4028 bmcLogEntry["Created"] = entryTimeStr;
George Liu647b3cd2021-07-05 12:43:56 +08004029 if (!std::get<std::vector<uint8_t>>(code.second).empty())
4030 {
4031 bmcLogEntry["AdditionalDataURI"] =
4032 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/" +
4033 postcodeEntryID + "/attachment";
4034 }
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08004035
4036 // codeIndex is only specified when querying single entry, return only
4037 // that entry in this case
4038 if (codeIndex != 0)
4039 {
Ed Tanousac106bf2023-06-07 09:24:59 -07004040 asyncResp->res.jsonValue.update(bmcLogEntry);
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08004041 return true;
4042 }
4043
Ed Tanousac106bf2023-06-07 09:24:59 -07004044 nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
Patrick Williamsb2ba3072023-05-12 10:27:39 -05004045 logEntryArray.emplace_back(std::move(bmcLogEntry));
ZhikuiRena3316fc2020-01-29 14:58:08 -08004046 }
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08004047
4048 // Return value is always false when querying multiple entries
4049 return false;
ZhikuiRena3316fc2020-01-29 14:58:08 -08004050}
4051
Ed Tanousac106bf2023-06-07 09:24:59 -07004052static void
4053 getPostCodeForEntry(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
4054 const std::string& entryId)
ZhikuiRena3316fc2020-01-29 14:58:08 -08004055{
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08004056 uint16_t bootIndex = 0;
4057 uint64_t codeIndex = 0;
4058 if (!parsePostCode(entryId, codeIndex, bootIndex))
4059 {
4060 // Requested ID was not found
Ed Tanousac106bf2023-06-07 09:24:59 -07004061 messages::resourceNotFound(asyncResp->res, "LogEntry", entryId);
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08004062 return;
4063 }
4064
4065 if (bootIndex == 0 || codeIndex == 0)
4066 {
4067 // 0 is an invalid index
Ed Tanousac106bf2023-06-07 09:24:59 -07004068 messages::resourceNotFound(asyncResp->res, "LogEntry", entryId);
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08004069 return;
4070 }
4071
ZhikuiRena3316fc2020-01-29 14:58:08 -08004072 crow::connections::systemBus->async_method_call(
Ed Tanousac106bf2023-06-07 09:24:59 -07004073 [asyncResp, entryId, bootIndex,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08004074 codeIndex](const boost::system::error_code& ec,
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05304075 const boost::container::flat_map<
4076 uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
4077 postcode) {
Ed Tanous002d39b2022-05-31 08:59:27 -07004078 if (ec)
4079 {
Ed Tanous62598e32023-07-17 17:06:25 -07004080 BMCWEB_LOG_DEBUG("DBUS POST CODE PostCode response error");
Ed Tanousac106bf2023-06-07 09:24:59 -07004081 messages::internalError(asyncResp->res);
Ed Tanous002d39b2022-05-31 08:59:27 -07004082 return;
4083 }
ZhikuiRena3316fc2020-01-29 14:58:08 -08004084
Ed Tanous002d39b2022-05-31 08:59:27 -07004085 if (postcode.empty())
4086 {
Ed Tanousac106bf2023-06-07 09:24:59 -07004087 messages::resourceNotFound(asyncResp->res, "LogEntry", entryId);
Ed Tanous002d39b2022-05-31 08:59:27 -07004088 return;
4089 }
ZhikuiRena3316fc2020-01-29 14:58:08 -08004090
Ed Tanousac106bf2023-06-07 09:24:59 -07004091 if (!fillPostCodeEntry(asyncResp, postcode, bootIndex, codeIndex))
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08004092 {
Ed Tanousac106bf2023-06-07 09:24:59 -07004093 messages::resourceNotFound(asyncResp->res, "LogEntry", entryId);
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08004094 return;
4095 }
Patrick Williams5a39f772023-10-20 11:20:21 -05004096 },
Jonathan Doman15124762021-01-07 17:54:17 -08004097 "xyz.openbmc_project.State.Boot.PostCode0",
4098 "/xyz/openbmc_project/State/Boot/PostCode0",
ZhikuiRena3316fc2020-01-29 14:58:08 -08004099 "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodesWithTimeStamp",
4100 bootIndex);
4101}
4102
Ed Tanousac106bf2023-06-07 09:24:59 -07004103static void
4104 getPostCodeForBoot(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
4105 const uint16_t bootIndex, const uint16_t bootCount,
4106 const uint64_t entryCount, size_t skip, size_t top)
ZhikuiRena3316fc2020-01-29 14:58:08 -08004107{
4108 crow::connections::systemBus->async_method_call(
Ed Tanousac106bf2023-06-07 09:24:59 -07004109 [asyncResp, bootIndex, bootCount, entryCount, skip,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08004110 top](const boost::system::error_code& ec,
Manojkiran Eda6c9a2792021-02-27 14:25:04 +05304111 const boost::container::flat_map<
4112 uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
4113 postcode) {
Ed Tanous002d39b2022-05-31 08:59:27 -07004114 if (ec)
4115 {
Ed Tanous62598e32023-07-17 17:06:25 -07004116 BMCWEB_LOG_DEBUG("DBUS POST CODE PostCode response error");
Ed Tanousac106bf2023-06-07 09:24:59 -07004117 messages::internalError(asyncResp->res);
Ed Tanous002d39b2022-05-31 08:59:27 -07004118 return;
4119 }
ZhikuiRena3316fc2020-01-29 14:58:08 -08004120
Ed Tanous002d39b2022-05-31 08:59:27 -07004121 uint64_t endCount = entryCount;
4122 if (!postcode.empty())
4123 {
4124 endCount = entryCount + postcode.size();
Ed Tanous3648c8b2022-07-25 13:39:59 -07004125 if (skip < endCount && (top + skip) > entryCount)
ZhikuiRena3316fc2020-01-29 14:58:08 -08004126 {
Patrick Williams89492a12023-05-10 07:51:34 -05004127 uint64_t thisBootSkip = std::max(static_cast<uint64_t>(skip),
4128 entryCount) -
4129 entryCount;
Ed Tanous002d39b2022-05-31 08:59:27 -07004130 uint64_t thisBootTop =
Ed Tanous3648c8b2022-07-25 13:39:59 -07004131 std::min(static_cast<uint64_t>(top + skip), endCount) -
4132 entryCount;
Ed Tanous002d39b2022-05-31 08:59:27 -07004133
Ed Tanousac106bf2023-06-07 09:24:59 -07004134 fillPostCodeEntry(asyncResp, postcode, bootIndex, 0,
4135 thisBootSkip, thisBootTop);
ZhikuiRena3316fc2020-01-29 14:58:08 -08004136 }
Ed Tanousac106bf2023-06-07 09:24:59 -07004137 asyncResp->res.jsonValue["Members@odata.count"] = endCount;
Ed Tanous002d39b2022-05-31 08:59:27 -07004138 }
4139
4140 // continue to previous bootIndex
4141 if (bootIndex < bootCount)
4142 {
Ed Tanousac106bf2023-06-07 09:24:59 -07004143 getPostCodeForBoot(asyncResp, static_cast<uint16_t>(bootIndex + 1),
Ed Tanous002d39b2022-05-31 08:59:27 -07004144 bootCount, endCount, skip, top);
4145 }
Jiaqing Zhao81584ab2022-07-28 00:33:45 +08004146 else if (skip + top < endCount)
Ed Tanous002d39b2022-05-31 08:59:27 -07004147 {
Ed Tanousac106bf2023-06-07 09:24:59 -07004148 asyncResp->res.jsonValue["Members@odata.nextLink"] =
Ed Tanous002d39b2022-05-31 08:59:27 -07004149 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries?$skip=" +
4150 std::to_string(skip + top);
4151 }
Patrick Williams5a39f772023-10-20 11:20:21 -05004152 },
Jonathan Doman15124762021-01-07 17:54:17 -08004153 "xyz.openbmc_project.State.Boot.PostCode0",
4154 "/xyz/openbmc_project/State/Boot/PostCode0",
ZhikuiRena3316fc2020-01-29 14:58:08 -08004155 "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodesWithTimeStamp",
4156 bootIndex);
4157}
4158
zhanghch058d1b46d2021-04-01 11:18:24 +08004159static void
Ed Tanousac106bf2023-06-07 09:24:59 -07004160 getCurrentBootNumber(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous3648c8b2022-07-25 13:39:59 -07004161 size_t skip, size_t top)
ZhikuiRena3316fc2020-01-29 14:58:08 -08004162{
4163 uint64_t entryCount = 0;
Jonathan Doman1e1e5982021-06-11 09:36:17 -07004164 sdbusplus::asio::getProperty<uint16_t>(
4165 *crow::connections::systemBus,
4166 "xyz.openbmc_project.State.Boot.PostCode0",
4167 "/xyz/openbmc_project/State/Boot/PostCode0",
4168 "xyz.openbmc_project.State.Boot.PostCode", "CurrentBootCycleCount",
Ed Tanousac106bf2023-06-07 09:24:59 -07004169 [asyncResp, entryCount, skip, top](const boost::system::error_code& ec,
4170 const uint16_t bootCount) {
Ed Tanous002d39b2022-05-31 08:59:27 -07004171 if (ec)
4172 {
Ed Tanous62598e32023-07-17 17:06:25 -07004173 BMCWEB_LOG_DEBUG("DBUS response error {}", ec);
Ed Tanousac106bf2023-06-07 09:24:59 -07004174 messages::internalError(asyncResp->res);
Ed Tanous002d39b2022-05-31 08:59:27 -07004175 return;
4176 }
Ed Tanousac106bf2023-06-07 09:24:59 -07004177 getPostCodeForBoot(asyncResp, 1, bootCount, entryCount, skip, top);
Patrick Williams5a39f772023-10-20 11:20:21 -05004178 });
ZhikuiRena3316fc2020-01-29 14:58:08 -08004179}
4180
John Edward Broadbent7e860f12021-04-08 15:57:16 -07004181inline void requestRoutesPostCodesEntryCollection(App& app)
ZhikuiRena3316fc2020-01-29 14:58:08 -08004182{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07004183 BMCWEB_ROUTE(app,
Ed Tanous22d268c2022-05-19 09:39:07 -07004184 "/redfish/v1/Systems/<str>/LogServices/PostCodes/Entries/")
Ed Tanoused398212021-06-09 17:05:54 -07004185 .privileges(redfish::privileges::getLogEntryCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07004186 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07004187 [&app](const crow::Request& req,
Ed Tanous22d268c2022-05-19 09:39:07 -07004188 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
4189 const std::string& systemName) {
Ed Tanous002d39b2022-05-31 08:59:27 -07004190 query_param::QueryCapabilities capabilities = {
4191 .canDelegateTop = true,
4192 .canDelegateSkip = true,
4193 };
4194 query_param::Query delegatedQuery;
4195 if (!redfish::setUpRedfishRouteWithDelegation(
Carson Labrado3ba00072022-06-06 19:40:56 +00004196 app, req, asyncResp, delegatedQuery, capabilities))
Ed Tanous002d39b2022-05-31 08:59:27 -07004197 {
4198 return;
4199 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08004200 if constexpr (bmcwebEnableMultiHost)
4201 {
4202 // Option currently returns no systems. TBD
4203 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
4204 systemName);
4205 return;
4206 }
Ed Tanous22d268c2022-05-19 09:39:07 -07004207
4208 if (systemName != "system")
4209 {
4210 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
4211 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07004212 return;
4213 }
Ed Tanous002d39b2022-05-31 08:59:27 -07004214 asyncResp->res.jsonValue["@odata.type"] =
4215 "#LogEntryCollection.LogEntryCollection";
4216 asyncResp->res.jsonValue["@odata.id"] =
4217 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries";
4218 asyncResp->res.jsonValue["Name"] = "BIOS POST Code Log Entries";
4219 asyncResp->res.jsonValue["Description"] =
4220 "Collection of POST Code Log Entries";
4221 asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
4222 asyncResp->res.jsonValue["Members@odata.count"] = 0;
Ed Tanous3648c8b2022-07-25 13:39:59 -07004223 size_t skip = delegatedQuery.skip.value_or(0);
Jiaqing Zhao5143f7a2022-07-22 09:33:33 +08004224 size_t top = delegatedQuery.top.value_or(query_param::Query::maxTop);
Ed Tanous3648c8b2022-07-25 13:39:59 -07004225 getCurrentBootNumber(asyncResp, skip, top);
Patrick Williams5a39f772023-10-20 11:20:21 -05004226 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07004227}
ZhikuiRena3316fc2020-01-29 14:58:08 -08004228
George Liu647b3cd2021-07-05 12:43:56 +08004229inline void requestRoutesPostCodesEntryAdditionalData(App& app)
4230{
George Liu0fda0f12021-11-16 10:06:17 +08004231 BMCWEB_ROUTE(
4232 app,
Ed Tanous22d268c2022-05-19 09:39:07 -07004233 "/redfish/v1/Systems/<str>/LogServices/PostCodes/Entries/<str>/attachment/")
George Liu647b3cd2021-07-05 12:43:56 +08004234 .privileges(redfish::privileges::getLogEntry)
4235 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07004236 [&app](const crow::Request& req,
4237 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07004238 const std::string& systemName,
Ed Tanous45ca1b82022-03-25 13:07:27 -07004239 const std::string& postCodeID) {
Carson Labrado3ba00072022-06-06 19:40:56 +00004240 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07004241 {
4242 return;
4243 }
Matt Spinler72e21372023-04-19 12:53:33 -05004244 if (!http_helpers::isContentTypeAllowed(
Ed Tanous99351cd2022-08-07 16:42:51 -07004245 req.getHeaderValue("Accept"),
Ed Tanous4a0e1a02022-09-21 15:28:04 -07004246 http_helpers::ContentType::OctetStream, true))
Ed Tanous002d39b2022-05-31 08:59:27 -07004247 {
4248 asyncResp->res.result(boost::beast::http::status::bad_request);
4249 return;
4250 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08004251 if constexpr (bmcwebEnableMultiHost)
4252 {
4253 // Option currently returns no systems. TBD
4254 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
4255 systemName);
4256 return;
4257 }
Ed Tanous22d268c2022-05-19 09:39:07 -07004258 if (systemName != "system")
4259 {
4260 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
4261 systemName);
Ed Tanous22d268c2022-05-19 09:39:07 -07004262 return;
4263 }
George Liu647b3cd2021-07-05 12:43:56 +08004264
Ed Tanous002d39b2022-05-31 08:59:27 -07004265 uint64_t currentValue = 0;
4266 uint16_t index = 0;
4267 if (!parsePostCode(postCodeID, currentValue, index))
4268 {
4269 messages::resourceNotFound(asyncResp->res, "LogEntry", postCodeID);
4270 return;
4271 }
George Liu647b3cd2021-07-05 12:43:56 +08004272
Ed Tanous002d39b2022-05-31 08:59:27 -07004273 crow::connections::systemBus->async_method_call(
4274 [asyncResp, postCodeID, currentValue](
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08004275 const boost::system::error_code& ec,
Ed Tanous002d39b2022-05-31 08:59:27 -07004276 const std::vector<std::tuple<uint64_t, std::vector<uint8_t>>>&
4277 postcodes) {
4278 if (ec.value() == EBADR)
4279 {
4280 messages::resourceNotFound(asyncResp->res, "LogEntry",
4281 postCodeID);
4282 return;
4283 }
4284 if (ec)
4285 {
Ed Tanous62598e32023-07-17 17:06:25 -07004286 BMCWEB_LOG_DEBUG("DBUS response error {}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -07004287 messages::internalError(asyncResp->res);
4288 return;
4289 }
George Liu647b3cd2021-07-05 12:43:56 +08004290
Ed Tanous002d39b2022-05-31 08:59:27 -07004291 size_t value = static_cast<size_t>(currentValue) - 1;
4292 if (value == std::string::npos || postcodes.size() < currentValue)
4293 {
Ed Tanous62598e32023-07-17 17:06:25 -07004294 BMCWEB_LOG_WARNING("Wrong currentValue value");
Ed Tanous002d39b2022-05-31 08:59:27 -07004295 messages::resourceNotFound(asyncResp->res, "LogEntry",
4296 postCodeID);
4297 return;
4298 }
George Liu647b3cd2021-07-05 12:43:56 +08004299
Ed Tanous002d39b2022-05-31 08:59:27 -07004300 const auto& [tID, c] = postcodes[value];
4301 if (c.empty())
4302 {
Ed Tanous62598e32023-07-17 17:06:25 -07004303 BMCWEB_LOG_WARNING("No found post code data");
Ed Tanous002d39b2022-05-31 08:59:27 -07004304 messages::resourceNotFound(asyncResp->res, "LogEntry",
4305 postCodeID);
4306 return;
4307 }
4308 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
4309 const char* d = reinterpret_cast<const char*>(c.data());
4310 std::string_view strData(d, c.size());
George Liu647b3cd2021-07-05 12:43:56 +08004311
Ed Tanousd9f6c622022-03-17 09:12:17 -07004312 asyncResp->res.addHeader(boost::beast::http::field::content_type,
Ed Tanous002d39b2022-05-31 08:59:27 -07004313 "application/octet-stream");
Ed Tanousd9f6c622022-03-17 09:12:17 -07004314 asyncResp->res.addHeader(
4315 boost::beast::http::field::content_transfer_encoding, "Base64");
Ed Tanous27b0cf92023-08-07 12:02:40 -07004316 asyncResp->res.write(crow::utility::base64encode(strData));
Patrick Williams5a39f772023-10-20 11:20:21 -05004317 },
Ed Tanous002d39b2022-05-31 08:59:27 -07004318 "xyz.openbmc_project.State.Boot.PostCode0",
4319 "/xyz/openbmc_project/State/Boot/PostCode0",
4320 "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodes", index);
Patrick Williams5a39f772023-10-20 11:20:21 -05004321 });
George Liu647b3cd2021-07-05 12:43:56 +08004322}
4323
John Edward Broadbent7e860f12021-04-08 15:57:16 -07004324inline void requestRoutesPostCodesEntry(App& app)
ZhikuiRena3316fc2020-01-29 14:58:08 -08004325{
John Edward Broadbent7e860f12021-04-08 15:57:16 -07004326 BMCWEB_ROUTE(
Ed Tanous22d268c2022-05-19 09:39:07 -07004327 app, "/redfish/v1/Systems/<str>/LogServices/PostCodes/Entries/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07004328 .privileges(redfish::privileges::getLogEntry)
John Edward Broadbent7e860f12021-04-08 15:57:16 -07004329 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07004330 [&app](const crow::Request& req,
4331 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous22d268c2022-05-19 09:39:07 -07004332 const std::string& systemName, const std::string& targetID) {
Carson Labrado3ba00072022-06-06 19:40:56 +00004333 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07004334 {
4335 return;
4336 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -08004337 if constexpr (bmcwebEnableMultiHost)
4338 {
4339 // Option currently returns no systems. TBD
4340 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
4341 systemName);
4342 return;
4343 }
Ed Tanous22d268c2022-05-19 09:39:07 -07004344 if (systemName != "system")
4345 {
4346 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
4347 systemName);
4348 return;
4349 }
4350
Jiaqing Zhao6f284d22022-10-10 15:56:45 +08004351 getPostCodeForEntry(asyncResp, targetID);
Patrick Williams5a39f772023-10-20 11:20:21 -05004352 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -07004353}
ZhikuiRena3316fc2020-01-29 14:58:08 -08004354
Ed Tanous1da66f72018-07-27 16:13:37 -07004355} // namespace redfish